-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwmctrl.py
More file actions
90 lines (75 loc) · 2.69 KB
/
Copy pathwmctrl.py
File metadata and controls
90 lines (75 loc) · 2.69 KB
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
# Slightly modified wmctrl from pip3 to work with python 3.
# I did find a change request that hadn't been accepted which
# was originally put in several years ago.
import os
from subprocess import getoutput
try:
from collections import namedtuple
except ImportError:
from namedtuple import namedtuple
BaseWindow = namedtuple('Window', 'id desktop pid x y w h wm_class host wm_name wm_window_role')
class Window(BaseWindow):
@classmethod
def list(cls):
out = getoutput('wmctrl -l -G -p -x')
windows = []
for line in out.splitlines():
parts = line.split(None, len(Window._fields)-2)
parts = list(map(str.strip, parts))
parts[1:7] = list(map(int, parts[1:7]))
parts.append(_wm_window_role(parts[0]))
windows.append(cls(*parts))
return windows
@classmethod
def by_name(cls, name):
return [win for win in cls.list() if win.wm_name == name]
@classmethod
def by_name_endswith(cls, name):
return [win for win in cls.list() if win.wm_name.endswith(name)]
@classmethod
def by_name_startswith(cls, name):
return [win for win in cls.list() if win.wm_name.startswith(name)]
@classmethod
def by_role(cls, role):
return [win for win in cls.list() if win.wm_window_role == role]
@classmethod
def by_class(cls, wm_class):
return [win for win in cls.list() if win.wm_class == wm_class]
@classmethod
def by_id(cls, id):
return [win for win in cls.list() if int(win.id, 16) == id]
@classmethod
def get_active(cls):
out = getoutput("xprop -root _NET_ACTIVE_WINDOW")
parts = out.split()
try:
id = int(parts[-1], 16)
except ValueError:
return None
lst = cls.by_id(id)
if not lst:
return None
assert len(lst) == 1
return lst[0]
def activate(self):
os.system('wmctrl -id -a %s' % self.id)
def resize_and_move(self, x, y, w, h):
mvarg = '0,%d,%d,%d,%d' % (x, y, w, h)
os.system('wmctrl -i -r %s -e %s' % (self.id, mvarg))
def set_geometry(self, geometry):
dim, pos = geometry.split('+', 1)
w, h = map(int, dim.split('x'))
x, y = map(int, pos.split('+'))
self.resize_and_move(x, y, w, h)
def set_properties(self,properties):
proparg = ",".join(properties)
os.system('wmctrl -i -r %s -b %s' % (self.id,proparg))
def _wm_window_role(winid):
out = getoutput('xprop -id %s WM_WINDOW_ROLE' % winid)
try:
_, value = out.split(' = ')
except ValueError:
# probably xprop returned an error
return ''
else:
return value.strip('"')