/
mozzarella.py
318 lines (284 loc) · 11.8 KB
/
mozzarella.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import xcb
from xcb import xproto
from xpybutil import event, ewmh, icccm, mousebind, keybind, cursor, conn as Xconn
import os, traceback, sys, time
import grid
from window import Window
from screen import Screen
from workspaces import Workspace
from window_store import WindowStore
import config
from config import bind_launcher_keys, spawn
import atom
import logger
#This variable handles exiting from the mainloop()
#Don't remove it
exit = False
cw = xproto.ConfigWindow
class OtherWindowManagerRunning(Exception):
pass
class Manager():
def __init__ (self, conn) :
self.conn = conn
setup = self.conn.get_setup()
self.current_workspace = None
self.screen = Screen(self.conn, setup.roots[0], self)
self.root = self.screen.root
self.win_store = WindowStore(self.conn, self, [])
self.swap_win = None
self.swaps = 0
self.default_cursor= cursor.create_font_cursor(cursor.FontCursor.LeftPtr)
self.move_cursor = cursor.create_font_cursor(cursor.FontCursor.Fleur)
self.workspaces = []
self.current_workspace_num = 1
self.current_name = 'Desktop'
self.current_win = None
self.logger = logger.DzenLogger(self)
#Proclaim to all just what we support
atoms = ["_NET_WM_STRUT_PARTIAL", "_NET_WM_STATE_FULLSCREEN", "_NET_NUMBER_OF_DESKTOPS",
"_NET_CURRENT_DESKTOP", "_NET_ACTIVE_WINDOW"]
atom.update_atom_cache(atoms)
ewmh.set_supported(atom.atom_cache)
#this is a list of the events that we don't want/need to filter by window.
#Some such as MapRequest and ConfigureRequest we usually don't have a reference
#for the window yet
self.no_window_events = [xproto.MappingNotifyEvent,
xproto.MapRequestEvent,
xproto.ConfigureRequestEvent]
# set substructure redirect on the root window.
eventmask = [xproto.EventMask.SubstructureRedirect | xproto.EventMask.SubstructureNotify]
err = self.conn.core.ChangeWindowAttributesChecked(self.root.win, xproto.CW.EventMask, eventmask)
try:
err.check()
except xproto.BadAccess:
print "We can't grab window manager status!, is there something else running?"
raise OtherWindowManagerRunning
self.conn.core.ChangeWindowAttributesChecked(self.root.win, xproto.CW.Cursor, [self.default_cursor])
self.do_workspaces(self.screen)
self.scan_windows()
self.logger.start()
def scan_windows(self):
q = self.conn.core.QueryTree(self.root.win).reply()
for item in q.children:
win = Window(item, self.conn, self)
attrs = win.get_attributes()
state = win.get_wm_state()
if attrs and attrs.map_state == xproto.MapState.Unmapped: #or attrs.overide_redirect:
continue
if state and state['state'] == icccm.State.Withdrawn:
continue
eventmask = [xproto.EventMask.EnterWindow]
self.conn.core.ChangeWindowAttributes(win.win, xproto.CW.EventMask, eventmask)
if not win.get_wm_transient_for():
event.connect('DestroyNotify', win.win, self.handle_unmap_event)
event.connect('UnmapNotify', win.win, self.handle_unmap_event)
if win.strut:
self.screen.set_strut(win.strut)
self.win_store.add(win)
event.connect('EnterNotify', win.win, self.handle_enter_event)
def do_workspaces(self, screen):
for x in range(config.workspaces):
self.workspaces.append(Workspace(screen, self.win_store))
self.current_workspace = self.workspaces[0]
self.current_workspace.shown = True
ewmh.set_number_of_desktops(config.workspaces)
ewmh.set_current_desktop(0)
def grab_mouse(self):
'''
This method grabs a button and binds to press and realease events
'''
mods, button = mousebind.parse_buttonstring(config.mouse_move_string)
mousebind.grab_button(self.root.win, mods, button)
event.connect('ButtonPress', self.root.win, self.handle_mouse_press)
event.connect('ButtonRelease', self.root.win, self.handle_mouse_release)
def register_handlers(self):
'''
Adds handlers for no_window_events, binds workspace moving
keys and grabs the mouse actions we care about
'''
print "Registering Handlers"
event.connect('MapRequest', None, self.handle_map_event)
event.connect('ConfigureRequest', None, self.handle_configure_event)
keybind.bind_global_key('KeyRelease', 'Mod4-Right', self.next_workspace)
keybind.bind_global_key('KeyRelease', 'Mod4-Left', self.prev_workspace)
keybind.bind_global_key('KeyRelease', 'Mod4-k', self.destroy_window)
self.grab_mouse()
def handle_map_event(self, evt):
'''
window mapping operations occur here
'''
win = self.win_store.get_window(evt.window)
if not win:
return
try:
state = win.get_wm_state()
if state and state['state'] == icccm.State.Withdrawn:
return
win.map()
except xproto.BadWindow:
return
self.win_store.add(win)
self.current_workspace.update()
event.connect('DestroyNotify', win.win, self.handle_unmap_event)
event.connect('UnmapNotify', win.win, self.handle_unmap_event)
eventmask = [xproto.EventMask.EnterWindow]
self.conn.core.ChangeWindowAttributes(win.win, xproto.CW.EventMask, eventmask)
event.connect('EnterNotify', win.win, self.handle_enter_event)
pointer_pos = self.conn.core.QueryPointer(self.root.win).reply()
ifwin = self.win_store.get_window_by_pos(pointer_pos.root_x, pointer_pos.root_y)
if ifwin:
ifwin.focus()
def handle_unmap_event(self, evt):
'''
This method handles both DestroyNotify and UnmapNotify
because they both seem to indicate the same thing:
We no longer need this window.
'''
event.disconnect('DestroyNotify', evt.window)
event.disconnect('UnmapNotify', evt.window)
win = self.win_store.get_window(evt.window)
self.win_store.remove(win)
self.current_workspace.update()
def handle_configure_event(self, evt):
'''
This is where ConfigureRequstEvents and ConfigureNotifyEvents get handled
Transient windows are floated, and others are not
'''
win = self.win_store.get_window(evt.window)
#if we don't have a window, we're stuck:
if not win:
return
try:
if win.get_wm_transient_for():
y = x = w = h = 0
bw = 2
if evt.value_mask & cw.X:
x = evt.x
if evt.value_mask & cw.Y:
y = evt.y
if evt.value_mask & cw.Height:
h = evt.height
if evt.value_mask & cw.Width:
w = evt.width
if evt.value_mask & cw.BorderWidth:
bw = evt.border_width
bw = 2
win.configure(x, y, w, h, bw)
else:
self.current_workspace.update()
except (xproto.BadWindow, xproto.BadMatch):
self.win_store.remove(win)
return
def handle_mouse_press(self, evt):
'''
Mouse button presses are handled here.
Basically set the swap window to None, change the cursor, get the window to be swapped
and set swap_win to that
'''
self.swap_win = None
err = self.conn.core.ChangeWindowAttributesChecked(self.root.win, xproto.CW.Cursor, [self.move_cursor])
dwin = self.win_store.get_window_by_pos(evt.root_x, evt.root_y)
self.swap_win = dwin
def handle_mouse_release(self, evt):
'''
Mouse button releases are handled here. we get the second window,
switch it with the first, revert the cursor back to normal, and set swap_win to None
'''
owin = self.win_store.get_window_by_pos(evt.root_x, evt.root_y)
self.current_workspace.grid.switch_windows(self.swap_win, owin)
err = self.conn.core.ChangeWindowAttributesChecked(self.root.win, xproto.CW.Cursor, [self.default_cursor])
self.swap_win = None
def handle_enter_event(self, evt):
'''
This event is used for setting the focus based on where the mouse is.
Most of the work occurs in Window.focus()
'''
window = self.win_store.get_window(evt.event)
if evt.mode != xproto.NotifyMode.Normal:
return
if window:
window.focus()
def next_workspace(self):
'''
Part of Mozzarella's primative workspaces
this switches to the next grid
'''
work_n = self.workspaces.index(self.current_workspace) + 1
print "workspace num is:", work_n
self.current_workspace_num += 1
if self.current_workspace_num > config.workspaces:
self.current_workspace_num = 1
if work_n > len(self.workspaces)-1:
work_n = 0
self.current_workspace.hide()
self.current_workspace = self.workspaces[work_n]
self.current_workspace.show()
ewmh.set_current_desktop(work_n)
def prev_workspace(self):
'''
Part of Mozzarella's primative workspaces
this switches to the previous grid
'''
work_p = self.workspaces.index(self.current_workspace) - 1
print "workspace num is:", work_p
self.current_workspace.hide()
self.current_workspace = self.workspaces[work_p]
self.current_workspace.show()
if work_p < 0:
work_p = len(self.workspaces)
self.current_workspace_num -= 1
if self.current_workspace_num <= 0:
self.current_workspace_num = config.workspaces
ewmh.set_current_desktop(work_p)
def destroy_window(self):
#will delete the currently focused
#window
if self.current_win:
self.current_win.destroy()
def no_window(self, evt):
'''
This method makes the event loop a little nicer to read, and shorter.
'''
for event in self.no_window_events:
if isinstance(evt, event):
return True
return False
def quit():
'''
Allows using a keypress (release actually) to
quit the window manager, and thus, usually, the X session
'''
global exit
exit = True
def mainloop():
wm = Manager(Xconn)
wm.register_handlers()
bind_launcher_keys()
#Should move these to Manager.register_handlers()
keybind.bind_global_key('KeyRelease', 'Mod4-x', quit)
try:
while True:
if exit:
break
event.read()
for e in event.queue():
#print e
w = None
if wm.no_window(e):
w = None
elif hasattr(e, 'window'):
w = e.window
elif hasattr(e, 'event'):
w = e.event
elif hasattr(e, 'requestor'):
w = e.requestor
key = (e.__class__, w)
for cb in event.__callbacks.get(key, []):
cb(e)
wm.conn.flush()
except xcb.Exception:
traceback.print_exc()
sys.exit(1)
if __name__ == '__main__':
mainloop()
print "Quitting Mozzarella"