aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/mycomputer.py
blob: bbfd539c86990dfeaf255ec2f787abe79249c88e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- python -*-
from plugins.pacman   import Pacman
from plugins.uname    import Uname
from plugins.users    import Users
from plugins.xdisplay import XDisplay
from plugins.xkb      import XKB

from common import *

class MyComputer(Entry):
    def __init__(self, *args, locks = None, **kwargs):
        self.have_linux_libre, self.have_pacman = True, None
        self.linux_installed, self.linux_latest = None, None
        self.last_uname_read = ''
        self.last_users_read = ''
        self.xdisplay = None
        self.you = pwd.getpwuid(os.getuid()).pw_name
        self.display = 0
        self.show_detailed = False
        self.keys = []
        if locks is None:
            locks = ['num', 'cap']
        self.num_lock = 'num' in locks or 'number' in locks or 'numerical' in locks or 'numpad' in locks
        self.cap_lock = 'cap' in locks or 'caps' in locks
        self.scr_lock = 'scr' in locks or 'scroll' in locks or 'scrl' in locks
        if self.num_lock:
            self.keys.append(Xlib.XK.XK_Num_Lock)
        if self.cap_lock:
            self.keys.append(Xlib.XK.XK_Caps_Lock)
        if self.scr_lock:
            self.keys.append(Xlib.XK.XK_Scroll_Lock)
        Entry.__init__(self, *args, **kwargs)
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            if self.display == 3:
                col -= 5
                if col % 4 == 3:
                    return
                col //= 4
                key = self.keys[col]
                G.display.xtest_fake_input(Xlib.X.KeyPress, G.display.keysym_to_keycode(key))
                G.display.xtest_fake_input(Xlib.X.KeyRelease, G.display.keysym_to_keycode(key))
                G.display.flush()
        elif button == MIDDLE_BUTTON:
            self.show_detailed = not self.show_detailed
            self.invalidate()
        elif button == RIGHT_BUTTON:
            if self.display == 0:
                xasync(lambda : subprocess.Popen(['mate-system-monitor']).wait())
            else:
                self.invalidate()
        elif button == SCROLL_UP:
            self.display = min(self.display + 1, 2) ## TODO the keyboard support is not too good
            self.invalidate()
        elif button == SCROLL_DOWN:
            self.display = max(self.display - 1, 0)
            self.invalidate()
    
    def function_uname(self):
        uname = Uname()
        nodename = uname.nodename
        kernel_release = uname.kernel_release
        operating_system = uname.operating_system
        
        lts = '-lts' if kernel_release.lower().endswith('-lts') else ''
        if self.have_pacman is None:
            self.have_pacman = True
            try:
                self.linux_installed = Pacman('linux-libre' + lts, True)
            except:
                self.have_linux_libre = False
                try:
                    self.linux_installed = Pacman('linux' + lts, True)
                except:
                    self.have_pacman = False
            if self.have_pacman:
                try:
                    self.linux_latest = Pacman(('linux-libre' if self.have_linux_libre else 'linux') + lts, False)
                except:
                    self.have_pacman = None
        elif self.have_pacman:
            try:
                self.linux_installed = Pacman(('linux-libre' if self.have_linux_libre else 'linux') + lts, True)
                self.linux_latest = Pacman(('linux-libre' if self.have_linux_libre else 'linux') + lts, False)
            except:
                self.have_pacman = None
        
        if (self.have_pacman is not None) and self.have_pacman:
            linux_running = kernel_release.split('-')
            linux_running, kernel_release = linux_running[:2], linux_running[2:]
            linux_running = '-'.join(linux_running)
            kernel_release = '-' + '-'.join(kernel_release)
            self.linux_installed = self.linux_installed.version
            self.linux_latest = self.linux_latest.version
            if self.linux_installed == self.linux_latest:
                if linux_running == self.linux_installed:
                    linux_running = '\033[32m%s\033[39m' % linux_running
            else:
                if linux_running == self.linux_installed:
                    linux_running = '\033[33m%s\033[39m' % linux_running
                else:
                    linux_running = '\033[31m%s\033[39m' % linux_running
            kernel_release = linux_running + kernel_release
        
        rc = '%s %s %s'
        rc %= (nodename, kernel_release, operating_system)
        self.last_uname_read = rc
        return rc
    
    def function_users(self): ## TODO use inotify
        users = Users('devfs').users
        def colour_user(user):
            if user == 'root':          return '\033[31m%s\033[39m'
            elif not user == self.you:  return '\033[33m%s\033[39m'
            else:                       return '%s'
        users = ['%s{%i}' % (colour_user(u) % u, len(users[u])) for u in users.keys()]
        users = 'Usr: %s' % (' '.join(users))
        self.last_users_read = users
        return users
    
    def function_xdisplay(self):
        if self.xdisplay is None:
            x = XDisplay()
            if x.connection is None:
                self.xdisplay = 'No DISPLAY environment variable set'
                self.xdisplay_detailed = self.xdisplay
            else:
                self.xdisplay = 'X: %s' % x.connection
                self.xdisplay_detailed  = 'Host: %s' % ('(localhost)' if x.host is None else x.host)
                self.xdisplay_detailed += SEPARATOR
                self.xdisplay_detailed += 'Display: %s' % (x.display)
                if x.screen is not None:
                    self.xdisplay_detailed += SEPARATOR
                    self.xdisplay_detailed += 'Screen: %s' % (x.screen)
                if x.vt is not None:
                    self.xdisplay += '%sVt: %i' % (SEPARATOR, x.vt)
                    self.xdisplay_detailed += '%sVt: %i' % (SEPARATOR, x.vt)
        return self.xdisplay_detailed if self.show_detailed else self.xdisplay
    
    def function_xkb(self): ## TODO add update listener
        xkb = XKB().get_locks()
        kbd = 'Kbd:'
        if self.num_lock:
            kbd += ' \033[3%imnum\033[0m' % (1 if (xkb & XKB.NUM)    == 0 else 2)
        if self.cap_lock:
            kbd += ' \033[3%imcap\033[0m' % (1 if (xkb & XKB.CAPS)   == 0 else 2)
        if self.scr_lock:
            kbd += ' \033[3%imscr\033[0m' % (1 if (xkb & XKB.SCROLL) == 0 else 2)
        return kbd
    
    def function(self):
        if self.display == 0:
            return self.function_uname()
        elif self.display == 1:
            return self.function_users()
        elif self.display == 2:
            return self.function_xdisplay()
        elif self.display == 3:
            return self.function_xkb()