Debian Codegraph @

ewmh/ewmh.py

raw
1 """
2 This module intends to provide an implementation of Extended Window Manager
3 Hints, based on the Xlib modules for python.
4
5 See the freedesktop.org `specification
6 <http://standards.freedesktop.org/wm-spec/wm-spec-latest.html>`_ for more
7 information.
8 """
9
10 from Xlib import display, X, protocol
11 import time
12
13
14 class EWMH:
15 """
16 This class provides the ability to get and set properties defined by the
17 EWMH spec.
18
19 Each property can be accessed in two ways. For example, to get the active
20 window::
21
22 win = ewmh.getActiveWindow()
23 # or: win = ewmh.getProperty('_NET_ACTIVE_WINDOW')
24
25 Similarly, to set the active window::
26
27 ewmh.setActiveWindow(myWindow)
28 # or: ewmh.setProperty('_NET_ACTIVE_WINDOW', myWindow)
29
30 When a property is written, don't forget to really send the notification by
31 flushing requests::
32
33 ewmh.display.flush()
34
35 :param _display: the display to use. If not given, Xlib.display.Display()
36 is used.
37 :param root: the root window to use. If not given,
38 self.display.screen().root is used.
39 """
40
41 NET_WM_WINDOW_TYPES = (
42 '_NET_WM_WINDOW_TYPE_DESKTOP', '_NET_WM_WINDOW_TYPE_DOCK',
43 '_NET_WM_WINDOW_TYPE_TOOLBAR', '_NET_WM_WINDOW_TYPE_MENU',
44 '_NET_WM_WINDOW_TYPE_UTILITY', '_NET_WM_WINDOW_TYPE_SPLASH',
45 '_NET_WM_WINDOW_TYPE_DIALOG', '_NET_WM_WINDOW_TYPE_DROPDOWN_MENU',
46 '_NET_WM_WINDOW_TYPE_POPUP_MENU', '_NET_WM_WINDOW_TYPE_NOTIFICATION',
47 '_NET_WM_WINDOW_TYPE_COMBO', '_NET_WM_WINDOW_TYPE_DND',
48 '_NET_WM_WINDOW_TYPE_NORMAL')
49 """List of strings representing all known window types."""
50
51 NET_WM_ACTIONS = (
52 '_NET_WM_ACTION_MOVE', '_NET_WM_ACTION_RESIZE',
53 '_NET_WM_ACTION_MINIMIZE', '_NET_WM_ACTION_SHADE',
54 '_NET_WM_ACTION_STICK', '_NET_WM_ACTION_MAXIMIZE_HORZ',
55 '_NET_WM_ACTION_MAXIMIZE_VERT', '_NET_WM_ACTION_FULLSCREEN',
56 '_NET_WM_ACTION_CHANGE_DESKTOP', '_NET_WM_ACTION_CLOSE',
57 '_NET_WM_ACTION_ABOVE', '_NET_WM_ACTION_BELOW')
58 """List of strings representing all known window actions."""
59
60 NET_WM_STATES = (
61 '_NET_WM_STATE_MODAL', '_NET_WM_STATE_STICKY',
62 '_NET_WM_STATE_MAXIMIZED_VERT', '_NET_WM_STATE_MAXIMIZED_HORZ',
63 '_NET_WM_STATE_SHADED', '_NET_WM_STATE_SKIP_TASKBAR',
64 '_NET_WM_STATE_SKIP_PAGER', '_NET_WM_STATE_HIDDEN',
65 '_NET_WM_STATE_FULLSCREEN', '_NET_WM_STATE_ABOVE',
66 '_NET_WM_STATE_BELOW', '_NET_WM_STATE_DEMANDS_ATTENTION')
67 """List of strings representing all known window states."""
68
69 def __init__(self, _display=None, root=None):
70 self.display = _display or display.Display()
71 self.root = root or self.display.screen().root
72 self.__getAttrs = {
73 '_NET_CLIENT_LIST': self.getClientList,
74 '_NET_CLIENT_LIST_STACKING': self.getClientListStacking,
75 '_NET_NUMBER_OF_DESKTOPS': self.getNumberOfDesktops,
76 '_NET_DESKTOP_GEOMETRY': self.getDesktopGeometry,
77 '_NET_DESKTOP_VIEWPORT': self.getDesktopViewPort,
78 '_NET_CURRENT_DESKTOP': self.getCurrentDesktop,
79 '_NET_ACTIVE_WINDOW': self.getActiveWindow,
80 '_NET_WORKAREA': self.getWorkArea,
81 '_NET_SHOWING_DESKTOP': self.getShowingDesktop,
82 '_NET_WM_NAME': self.getWmName,
83 '_NET_WM_VISIBLE_NAME': self.getWmVisibleName,
84 '_NET_WM_DESKTOP': self.getWmDesktop,
85 '_NET_WM_WINDOW_TYPE': self.getWmWindowType,
86 '_NET_WM_STATE': self.getWmState,
87 '_NET_WM_ALLOWED_ACTIONS': self.getWmAllowedActions,
88 '_NET_WM_PID': self.getWmPid,
89 }
90 self.__setAttrs = {
91 '_NET_NUMBER_OF_DESKTOPS': self.setNumberOfDesktops,
92 '_NET_DESKTOP_GEOMETRY': self.setDesktopGeometry,
93 '_NET_DESKTOP_VIEWPORT': self.setDesktopViewport,
94 '_NET_CURRENT_DESKTOP': self.setCurrentDesktop,
95 '_NET_ACTIVE_WINDOW': self.setActiveWindow,
96 '_NET_SHOWING_DESKTOP': self.setShowingDesktop,
97 '_NET_CLOSE_WINDOW': self.setCloseWindow,
98 '_NET_MOVERESIZE_WINDOW': self.setMoveResizeWindow,
99 '_NET_WM_NAME': self.setWmName,
100 '_NET_WM_VISIBLE_NAME': self.setWmVisibleName,
101 '_NET_WM_DESKTOP': self.setWmDesktop,
102 '_NET_WM_STATE': self.setWmState,
103 }
104
105 # ------------------------ setters properties ------------------------
106
107 def setNumberOfDesktops(self, nb):
108 """
109 Set the number of desktops (property _NET_NUMBER_OF_DESKTOPS).
110
111 :param nb: the number of desired desktops"""
112 self._setProperty('_NET_NUMBER_OF_DESKTOPS', [nb])
113
114 def setDesktopGeometry(self, w, h):
115 """
116 Set the desktop geometry (property _NET_DESKTOP_GEOMETRY)
117
118 :param w: desktop width
119 :param h: desktop height"""
120 self._setProperty('_NET_DESKTOP_GEOMETRY', [w, h])
121
122 def setDesktopViewport(self, w, h):
123 """
124 Set the viewport size of the current desktop
125 (property _NET_DESKTOP_VIEWPORT)
126
127 :param w: desktop width
128 :param h: desktop height"""
129 self._setProperty('_NET_DESKTOP_VIEWPORT', [w, h])
130
131 def setCurrentDesktop(self, i):
132 """
133 Set the current desktop (property _NET_CURRENT_DESKTOP).
134
135 :param i: the desired desktop number"""
136 self._setProperty('_NET_CURRENT_DESKTOP', [i, X.CurrentTime])
137
138 def setActiveWindow(self, win):
139 """
140 Set the given window active (property _NET_ACTIVE_WINDOW)
141
142 :param win: the window object"""
143 self._setProperty('_NET_ACTIVE_WINDOW', [1, X.CurrentTime, win.id],
144 win)
145
146 def setShowingDesktop(self, show):
147 """
148 Set/unset the mode Showing desktop (property _NET_SHOWING_DESKTOP)
149
150 :param show: 1 to set the desktop mode, else 0"""
151 self._setProperty('_NET_SHOWING_DESKTOP', [show])
152
153 def setCloseWindow(self, win):
154 """
155 Close the given window (property _NET_CLOSE_WINDOW)
156
157 :param win: the window object"""
158 self._setProperty('_NET_CLOSE_WINDOW',
159 [int(time.mktime(time.localtime())), 1], win)
160
161 def setWmName(self, win, name):
162 """
163 Set the property _NET_WM_NAME
164
165 :param win: the window object
166 :param name: desired name"""
167 self._setProperty('_NET_WM_NAME', name, win)
168
169 def setWmVisibleName(self, win, name):
170 """
171 Set the property _NET_WM_VISIBLE_NAME
172
173 :param win: the window object
174 :param name: desired visible name"""
175 self._setProperty('_NET_WM_VISIBLE_NAME', name, win)
176
177 def setWmDesktop(self, win, i):
178 """
179 Move the window to the desired desktop by changing the property
180 _NET_WM_DESKTOP.
181
182 :param win: the window object
183 :param i: desired desktop number
184 """
185 self._setProperty('_NET_WM_DESKTOP', [i, 1], win)
186
187 def setMoveResizeWindow(self, win, gravity=0, x=None, y=None, w=None,
188 h=None):
189 """
190 Set the property _NET_MOVERESIZE_WINDOW to move or resize the given
191 window. Flags are automatically calculated if x, y, w or h are defined.
192
193 :param win: the window object
194 :param gravity: gravity (one of the Xlib.X.*Gravity constant or 0)
195 :param x: int or None
196 :param y: int or None
197 :param w: int or None
198 :param h: int or None
199 """
200 # indicate source (application)
201 gravity_flags = gravity | 0b0000100000000000
202 if x is None:
203 x = 0
204 else:
205 gravity_flags = gravity_flags | 0b0000010000000000
206 if y is None:
207 y = 0
208 else:
209 gravity_flags = gravity_flags | 0b0000001000000000
210 if w is None:
211 w = 0
212 else:
213 gravity_flags = gravity_flags | 0b0000000100000000
214 if h is None:
215 h = 0
216 else:
217 gravity_flags = gravity_flags | 0b0000000010000000
218 self._setProperty('_NET_MOVERESIZE_WINDOW',
219 [gravity_flags, x, y, w, h], win)
220
221 def setWmState(self, win, action, state, state2=0):
222 """
223 Set/unset one or two state(s) for the given window (property
224 _NET_WM_STATE).
225
226 :param win: the window object
227 :param action: 0 to remove, 1 to add or 2 to toggle state(s)
228 :param state: a state
229 :type state: int or str (see :attr:`NET_WM_STATES`)
230 :param state2: a state or 0
231 :type state2: int or str (see :attr:`NET_WM_STATES`)
232 """
233 if type(state) != int:
234 state = self.display.get_atom(state, 1)
235 if type(state2) != int:
236 state2 = self.display.get_atom(state2, 1)
237 self._setProperty('_NET_WM_STATE', [action, state, state2, 1], win)
238
239 # ------------------------ getters properties ------------------------
240
241 def getClientList(self):
242 """
243 Get the list of windows maintained by the window manager for the
244 property _NET_CLIENT_LIST.
245
246 :return: list of Window objects
247 """
248 return [self._createWindow(w)
249 for w in self._getProperty('_NET_CLIENT_LIST')]
250
251 def getClientListStacking(self):
252 """
253 Get the list of windows maintained by the window manager for the
254 property _NET_CLIENT_LIST_STACKING.
255
256 :return: list of Window objects"""
257 return [self._createWindow(w)
258 for w in self._getProperty('_NET_CLIENT_LIST_STACKING')]
259
260 def getNumberOfDesktops(self):
261 """
262 Get the number of desktops (property _NET_NUMBER_OF_DESKTOPS).
263
264 :return: int"""
265 return self._getProperty('_NET_NUMBER_OF_DESKTOPS')[0]
266
267 def getDesktopGeometry(self):
268 """
269 Get the desktop geometry (property _NET_DESKTOP_GEOMETRY) as an array
270 of two integers [width, height].
271
272 :return: [int, int]"""
273 return self._getProperty('_NET_DESKTOP_GEOMETRY')
274
275 def getDesktopViewPort(self):
276 """
277 Get the current viewports of each desktop as a list of [x, y]
278 representing the top left corner (property _NET_DESKTOP_VIEWPORT).
279
280 :return: list of [int, int]
281 """
282 return self._getProperty('_NET_DESKTOP_VIEWPORT')
283
284 def getCurrentDesktop(self):
285 """
286 Get the current desktop number (property _NET_CURRENT_DESKTOP)
287
288 :return: int
289 """
290 return self._getProperty('_NET_CURRENT_DESKTOP')[0]
291
292 def getActiveWindow(self):
293 """
294 Get the current active (toplevel) window or None (property
295 _NET_ACTIVE_WINDOW)
296
297 :return: Window object or None
298 """
299 active_window = self._getProperty('_NET_ACTIVE_WINDOW')
300 if active_window is None:
301 return None
302 return self._createWindow(active_window[0])
303
304 def getWorkArea(self):
305 """
306 Get the work area for each desktop (property _NET_WORKAREA) as a list
307 of [x, y, width, height]
308
309 :return: a list of [int, int, int, int]
310 """
311 return self._getProperty('_NET_WORKAREA')
312
313 def getShowingDesktop(self):
314 """
315 Get the value of "showing the desktop" mode of the window manager
316 (property _NET_SHOWING_DESKTOP). 1 means the mode is activated, and 0
317 means deactivated.
318
319 :return: int
320 """
321 return self._getProperty('_NET_SHOWING_DESKTOP')[0]
322
323 def getWmName(self, win):
324 """
325 Get the property _NET_WM_NAME for the given window as a string.
326
327 :param win: the window object
328 :return: str
329 """
330 return self._getProperty('_NET_WM_NAME', win)
331
332 def getWmVisibleName(self, win):
333 """
334 Get the property _NET_WM_VISIBLE_NAME for the given window as a string.
335
336 :param win: the window object
337 :return: str
338 """
339 return self._getProperty('_NET_WM_VISIBLE_NAME', win)
340
341 def getWmDesktop(self, win):
342 """
343 Get the current desktop number of the given window (property
344 _NET_WM_DESKTOP).
345
346 :param win: the window object
347 :return: int
348 """
349 return self._getProperty('_NET_WM_DESKTOP', win)[0]
350
351 def getWmWindowType(self, win, str=False):
352 """
353 Get the list of window types of the given window (property
354 _NET_WM_WINDOW_TYPE).
355
356 :param win: the window object
357 :param str: True to get a list of string types instead of int
358 :return: list of (int|str)
359 """
360 types = self._getProperty('_NET_WM_WINDOW_TYPE', win) or []
361 if not str:
362 return types
363 return [self._getAtomName(t) for t in types]
364
365 def getWmState(self, win, str=False):
366 """
367 Get the list of states of the given window (property _NET_WM_STATE).
368
369 :param win: the window object
370 :param str: True to get a list of string states instead of int
371 :return: list of (int|str)
372 """
373 states = self._getProperty('_NET_WM_STATE', win) or []
374 if not str:
375 return states
376 return [self._getAtomName(s) for s in states]
377
378 def getWmAllowedActions(self, win, str=False):
379 """
380 Get the list of allowed actions for the given window (property
381 _NET_WM_ALLOWED_ACTIONS).
382
383 :param win: the window object
384 :param str: True to get a list of string allowed actions instead of int
385 :return: list of (int|str)
386 """
387 wAllowedActions = (
388 self._getProperty('_NET_WM_ALLOWED_ACTIONS', win) or [])
389 if not str:
390 return wAllowedActions
391 return [self._getAtomName(a) for a in wAllowedActions]
392
393 def getWmPid(self, win):
394 """
395 Get the pid of the application associated to the given window (property
396 _NET_WM_PID)
397
398 :param win: the window object
399 """
400 return self._getProperty('_NET_WM_PID', win)[0]
401
402 def _getProperty(self, _type, win=None):
403 if not win:
404 win = self.root
405 atom = win.get_full_property(self.display.get_atom(_type),
406 X.AnyPropertyType)
407 if atom:
408 return atom.value
409
410 def _setProperty(self, _type, data, win=None, mask=None):
411 """
412 Send a ClientMessage event to the root window
413 """
414 if not win:
415 win = self.root
416 if type(data) is str:
417 dataSize = 8
418 else:
419 data = (data+[0]*(5-len(data)))[:5]
420 dataSize = 32
421
422 ev = protocol.event.ClientMessage(
423 window=win,
424 client_type=self.display.get_atom(_type), data=(dataSize, data))
425
426 if not mask:
427 mask = (X.SubstructureRedirectMask | X.SubstructureNotifyMask)
428 self.root.send_event(ev, event_mask=mask)
429
430 def _getAtomName(self, atom):
431 try:
432 return self.display.get_atom_name(atom)
433 except:
434 return 'UNKNOWN'
435
436 def _createWindow(self, wId):
437 if not wId:
438 return None
439 return self.display.create_resource_object('window', wId)
440
441 def getReadableProperties(self):
442 """
443 Get all the readable properties' names
444 """
445 return self.__getAttrs.keys()
446
447 def getProperty(self, prop, *args, **kwargs):
448 """
449 Get the value of a property. See the corresponding method for the
450 required arguments. For example, for the property _NET_WM_STATE, look
451 for :meth:`getWmState`
452 """
453 f = self.__getAttrs.get(prop)
454 if not f:
455 raise KeyError('Unknown readable property: %s' % prop)
456 return f(self, *args, **kwargs)
457
458 def getWritableProperties(self):
459 """Get all the writable properties names"""
460 return self.__setAttrs.keys()
461
462 def setProperty(self, prop, *args, **kwargs):
463 """
464 Set the value of a property by sending an event on the root window.
465 See the corresponding method for the required arguments. For example,
466 for the property _NET_WM_STATE, look for :meth:`setWmState`
467 """
468 f = self.__setAttrs.get(prop)
469 if not f:
470 raise KeyError('Unknown writable property: %s' % prop)
471 f(self, *args, **kwargs)