-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcgr
executable file
·331 lines (278 loc) · 10.3 KB
/
mcgr
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
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Dependencies (on Ubuntu):
# - python-wnck
# - python-xlib
#
# If using on Compiz, set General Settings / Focus Stealing Prevention
# to None
from optparse import OptionParser
import logging
import gtk
import wnck
import cPickle as pickle
from os import stat, remove, mkfifo
import re
import os
from time import time, sleep
from stat import S_ISFIFO
from os.path import exists
from itertools import dropwhile
from collections import defaultdict
try:
import pygtk
pygtk.require('2.0')
import pynotify
except ImportError:
pynotify = None
print "Can't import pynotify"
# get largest Xinerama screen
from Xlib.display import Display
display = Display()
class WNCK_WINDOW:
CHANGE_X = 1 << 0
CHANGE_Y = 1 << 1
CHANGE_WIDTH = 1 << 2
CHANGE_HEIGHT = 1 << 3
FIFO_PATH = '/tmp/mcgr.fifo'
def flush_events():
while gtk.events_pending():
gtk.main_iteration()
def get_timestamp():
return int(time())
def get_window_class(w):
xid = w.get_xid()
gw = gtk.gdk.window_foreign_new(xid)
class_tuple = gw.property_get('WM_CLASS')
if class_tuple is None:
return None
class_str = class_tuple[2]
_, cls = class_str.split('\x00')[:2]
return cls
def matches(wm_class, pattern):
if wm_class is None:
return False
if pattern.startswith('/') and pattern.endswith('/'):
return re.search(pattern[1:-1], wm_class)
else:
return wm_class == pattern
class Screen(object):
def __init__(self, screen):
self.screen = screen
@classmethod
def get_default(cls):
_screen = wnck.screen_get_default()
flush_events()
return cls(_screen)
def get_current_ws(self):
return self.screen.get_active_workspace()
def get_workspace(self, n):
return self.screen.get_workspace(n)
def get_app_windows(self):
return self.screen.get_windows()
def iter_app_windows_by_class(self, wm_class):
return (w for w in self.get_app_windows()
if matches(get_window_class(w), wm_class))
def test():
from doctest import testmod
testmod()
class WmClassManager(list):
"""
Keep track of
* open windows
* last access time
for a given window class.
"""
def __init__(self):
super(WmClassManager, self).__init__()
self.timer = None
self.restart_timer()
def index(self, app_window):
try:
return super(WmClassManager, self).index(app_window)
except ValueError:
return -1
def update(self, app_windows):
self[:] = sorted(app_windows, key=lambda w: self.index(w))
def restart_timer(self):
self.timer = time()
class Switcher(object):
def __init__(self):
self.wm_classes = defaultdict(WmClassManager)
def do_unknown(self, *args, **kwargs):
logging.error('Uknown action %(original_action)s' % kwargs)
def do_tile_horizontally(self, edge, dividend, divisor, **kwargs):
xinerama_screen = sorted(display.xinerama_query_screens().screens,
key=lambda screen: -screen.width)[0]
screen = Screen.get_default().screen
current_window = screen.get_active_window()
ws = screen.get_active_workspace()
if current_window:
current_window.unmaximize()
current_window.maximize_vertically()
width = xinerama_screen.width
current_window.set_geometry(
wnck.WINDOW_GRAVITY_NORTHWEST,
WNCK_WINDOW.CHANGE_X | WNCK_WINDOW.CHANGE_WIDTH,
xinerama_screen.x + edge * (
width - dividend * width // divisor),
0,
dividend * width // divisor,
0)
def do_tile_half_left(self, **kwargs):
self.do_tile_horizontally(0, 1, 2)
def do_tile_half_right(self, **kwargs):
self.do_tile_horizontally(1, 1, 2)
def do_tile_two_thirds_left(self, **kwargs):
self.do_tile_horizontally(0, 2, 3)
def do_tile_two_thirds_right(self, **kwargs):
self.do_tile_horizontally(1, 2, 3)
def do_goto_or_run(self, wm_class, *cmdline, **kwargs):
logging.debug('received WM_CLASS: %s, command line: %s',
wm_class, ' '.join(cmdline))
screen = Screen.get_default()
logging.debug('getting app windows with class %s', wm_class)
app_windows = list(screen.iter_app_windows_by_class(wm_class))
logging.debug('found %d windows, getting xids...', len(app_windows))
app_window_xids = [w.get_xid() for w in app_windows]
logging.debug('found app windows of class %s with xids %r',
wm_class, app_window_xids)
manager = self.wm_classes[wm_class]
manager.update(app_window_xids)
logging.debug('manager now: %r', manager)
app_window_dict = dict((w.get_xid(), w) for w in app_windows)
if not manager:
from subprocess import Popen
self.notify(wm_class, '$ %s' % ' '.join(cmdline), icon=cmdline[0])
logging.info('executing: %s', ' '.join(cmdline))
try:
Popen(cmdline)
except OSError, e:
self.notify(' '.join(cmdline), unicode(e))
logging.error('%s: %s', e, cmdline)
# raise OSError('%s: %s' % (e, cmdline))
else:
logging.info('switching to: %s', wm_class)
manager.sort(key=lambda w: app_window_dict[w].is_active())
if manager.timer - time() < 1.0 or len(manager) == 1:
xid = manager[0]
else:
xid = manager[-2]
index = app_window_xids.index(xid)
w = app_window_dict[xid]
manager.restart_timer()
logging.debug('found windows: %r',
[app_window_dict[aw].get_name() for aw in manager])
ws = w.get_workspace()
logging.debug('workspace of %r: %r', w.get_name(), ws)
# workspace is None for pinned windows
if ws is not None:
logging.debug('activating %r', ws)
#ws.activate(get_timestamp())
ws.activate(0)
sleep(.01)
flush_events()
gw = gtk.gdk.window_foreign_new(w.get_xid())
logging.debug('gw.get_state() == %r', gw.get_state())
logging.debug('gw.get_window_type() == %r', gw.get_window_type())
logging.debug('focusing %r', gw)
self.notify(wm_class,
'%d/%d' % (index + 1, len(app_window_xids)),
icon=cmdline[0])
gw.focus()
flush_events()
def notify(self, title, text, icon='user-desktop'):
if self.notification:
self.notification.update(title, text, icon)
if not self.notification.show():
logging.error('Failed to show notification')
def init_notification(self):
self.notification = None
if pynotify:
logging.debug('Initializing pynotify')
if pynotify.init('metacity_goto_or_run'):
self.notification = pynotify.Notification(
'mcgr started', 'daemon waiting for fifo input')
self.notification.show()
else:
logging.error('Failed to initialize pynotify')
def serve(self):
self.init_notification()
if exists(FIFO_PATH) and not S_ISFIFO(stat(FIFO_PATH).st_mode):
logging.debug('Removing non-fifo %s', FIFO_PATH)
remove(FIFO_PATH)
if not exists(FIFO_PATH):
logging.debug('Creating %s', FIFO_PATH)
mkfifo(FIFO_PATH)
def read_fifo(blocking=True):
fifo = os.open(FIFO_PATH,
os.O_RDONLY | (not blocking and os.O_NONBLOCK))
buf = ['']
while True:
try:
newbuf = os.read(fifo, 256).split('\n')
except OSError as e:
if e.errno == 11:
# resource temporarily unavailable
# i.e. EAGAIN or EWOULDBLOCK
logging.warning(e)
logging.warning('Retrying to read FIFO in 1 second...')
sleep(1)
continue
raise
if newbuf == ['']:
if buf[-1] == '':
os.close(fifo)
return buf
sleep(1)
buf[-1] += newbuf[0]
buf.extend(newbuf[1:])
logging.debug('Flushing fifo')
buf = read_fifo(blocking=False)[-2:]
while True:
logging.debug('Executing from fifo %r', buf)
while len(buf) > 1:
line = buf.pop(0)
parts = line.strip().split()
action = parts[0].replace('-', '_')
meth = getattr(self, 'do_%s' % action, self.do_unknown)
logging.debug('%s: %s' % (action, ' '.join(parts[1:])))
meth(*parts[1:], **{'original_action': parts[0]})
assert buf == ['']
buf = read_fifo(blocking=True)
def cleanup_server(self):
if exists(FIFO_PATH):
remove(FIFO_PATH)
@classmethod
def load(cls, filepath='/tmp/metacity_goto_or_run.state'):
try:
return pickle.load(file(filepath))
except (IOError, KeyError, ValueError):
return cls()
def main():
p = OptionParser()
p.add_option('-u', '--unittest', action='store_true')
p.add_option('-v', '--verbose', action='count')
p.add_option('-d', '--daemon', action='store_true')
p.disable_interspersed_args()
(opts, args) = p.parse_args()
loglevel = 40 - 10 * (opts.verbose or 0)
logging.basicConfig(level=loglevel, format='%(levelname)-8s %(message)s')
logging.debug('Starting mcgr...')
if opts.unittest:
test()
elif opts.daemon:
switcher = Switcher()
try:
switcher.serve()
finally:
switcher.cleanup_server()
else:
wm_class = args[0]
cmdline = args[1:]
switcher = Switcher.load()
switcher.main(wm_class, cmdline)
pickle.dump(switcher, file('/tmp/metacity_goto_or_run.state', 'w'))
if __name__ == '__main__':
main()