]> icculus.org git repositories - dana/openbox.git/blob - scripts/focus.py
add py-compile
[dana/openbox.git] / scripts / focus.py
1 ###########################################################################
2 ###          Functions for helping out with your window focus.          ###
3 ###########################################################################
4
5 ###########################################################################
6 ###         Options that affect the behavior of the focus module.       ###
7 ###########################################################################
8 AVOID_SKIP_TASKBAR = 1
9 """Don't focus windows which have requested to not be displayed in taskbars.
10    You will still be able to focus the windows, but not through cycling, and
11    they won't be focused as a fallback if 'fallback' is enabled."""
12 FALLBACK = 0
13 """Send focus somewhere when nothing is left with the focus, if possible."""
14 ###########################################################################
15
16 ###########################################################################
17 ###########################################################################
18
19 ###########################################################################
20 ###      Internal stuff, should not be accessed outside the module.     ###
21 ###########################################################################
22
23 import ob
24
25 # maintain a list of clients, stacked in focus order
26 _clients = []
27 _disable = 0
28
29 def _focusable(client, desktop):
30     if not client.normal(): return 0
31     if not (client.canFocus() or client.focusNotify()): return 0
32     if AVOID_SKIP_TASKBAR and client.skipTaskbar(): return 0
33
34     desk = client.desktop()
35     if not (desk == 0xffffffff or desk == desktop): return 0
36
37     return 1
38
39 def _remove(client):
40     """This function exists because Swig pointers don't define a __eq__
41        function, so list.remove(ptr) does not work."""
42     win = client.window()
43     for i in range(len(_clients)):
44         if _clients[i].window() == win:
45             _clients.pop(i)
46             return
47     raise ValueError("_remove(x): x not in _clients list.")
48
49 def _focused(data):
50     global _clients
51
52     if _disable: return
53
54     if data.client:
55         # move it to the top
56         try:
57             _remove(data.client)
58         except ValueError: pass # happens if _focused comes before _newwindow
59         _clients.insert(0, data.client)
60     elif FALLBACK:
61         # pass around focus
62         desktop = ob.openbox.screen(data.screen).desktop()
63         for c in _clients:
64             if _focusable(c, desktop) and c.focus():
65                 break
66
67 def _newwindow(data):
68     # make sure its not already in the list
69     win = data.client.window()
70     for i in range(len(_clients)):
71         if _clients[i].window() == win:
72             return
73     _clients.append(data.client)
74         
75 def _closewindow(data):
76     _remove(data.client)
77
78 ob.ebind(ob.EventAction.NewWindow, _newwindow)
79 ob.ebind(ob.EventAction.CloseWindow, _closewindow)
80 ob.ebind(ob.EventAction.Focus, _focused)
81
82 print "Loaded focus.py"