wl-togglescreens.py 2.09 KB
Newer Older
onny's avatar
onny committed
1
2
#!/usr/bin/python

onny's avatar
onny committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess
import re

class Screen:

    def __init__(self, name, state=None):
        self.name = name
        self._state = state

    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, value):
onny's avatar
onny committed
18
19
20
21
22
        if self._state != None:
            if value:
                subprocess.run(['wlr-randr', '--output', self.name, '--on'])
            else:
                subprocess.run(['wlr-randr', '--output', self.name, '--off'])
onny's avatar
onny committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
        self._state = value

class wlScreens:
    
    def __init__(self):
        self.screens = []
        result = subprocess.run(['wlr-randr'], stdout=subprocess.PIPE)
        result = result.stdout.decode('utf-8')
        re_search_pat = r'([A-Z]+-\d) '
        match = re.findall(re_search_pat, result)
        for screenName in match:
            screen = Screen(screenName)
            self.screens.append(screen)
        re_search_pat = r'Enabled: ([a-z]+)'
        match = re.findall(re_search_pat, result)
        for num, state in enumerate(match):
            screen = self.screens[num]
            if (state == "yes"):
                screen.state = True
            else:
                screen.state = False

    def toggleModes(self):
onny's avatar
onny committed
46
47
48
49
50
51
        if (len(self.screens) == 1):
            if (self.screens[0].state == True):
                self.screens[0].state = False
            else:
                self.screens[0].state = True
        elif (len(self.screens) == 2):
onny's avatar
onny committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
            if (self.screens[0].state == True and self.screens[1].state == True):
                self.screens[0].state = False
            elif (self.screens[0].state == False and self.screens[1].state == True):
                self.screens[1].state = False
                self.screens[0].state = True
            elif (self.screens[0].state == True and self.screens[1].state == False):
                self.screens[1].state = True

    def __len__(self):
        return len(self.screens)

    def __str__(self):
        return str(self.screens)

    def __getitem__(self, key):
        return self.screens[key]

screens = wlScreens()
screens.toggleModes()