new/better/cleaner scripting interface
[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 # raise the window also when it is focused                              ###
9 cycle_raise = 1                                                         ###
10 # raise as you cycle in stacked mode                                    ###
11 stacked_cycle_raise = 0                                                 ###
12 # send focus somewhere when nothing is left with the focus, if possible ###
13 fallback = 0                                                            ###
14 ###                                                                     ###
15 ###########################################################################
16
17 import ob
18
19 # maintain a list of clients, stacked in focus order
20 _clients = []
21 # maintaint he current focused window
22 _doing_stacked = 0
23
24 def _new_win(data):
25     global _clients
26     global _doing_stacked
27     global _cyc_w;
28
29     if _doing_stacked:
30         _clients.insert(_clients.index(_cyc_w), data.client.window())
31     else:
32         if not len(_clients):
33             _clients.append(data.client.window())
34         else:
35             _clients.insert(1, data.client.window()) # insert in 2nd slot
36
37 def _close_win(data):
38     global _clients
39     global _cyc_w;
40     global _doing_stacked
41
42     if not _doing_stacked:
43         # not in the middle of stacked cycling, so who cares
44         _clients.remove(data.client.window())
45     else:
46         # have to fix the cycling if we remove anything
47         win = data.client.window()
48         if _cyc_w == win:
49             _do_stacked_cycle(data) # cycle off the window first
50         _clients.remove(win)
51
52 def _focused(data):
53     global _clients
54     global _doing_stacked
55     global _cyc_w
56     
57     if data.client:
58         if not _doing_stacked: # only move the window when we're not cycling
59             win = data.client.window()
60             # move it to the top
61             _clients.remove(win)
62             _clients.insert(0, win)
63         else: # if we are cycling, then update our pointer
64             _cyc_w = data.client.window()
65     elif fallback: 
66         # pass around focus
67         desktop = ob.openbox.screen(_cyc_screen).desktop()
68         for w in _clients:
69             client = ob.openbox.findClient(w)
70             if client and (client.desktop() == desktop and \
71                            client.normal() and client.focus()):
72                 break
73
74 _cyc_mask = 0
75 _cyc_key = 0
76 _cyc_w = 0 # last window cycled to
77 _cyc_screen = 0
78
79 def _do_stacked_cycle(data, forward):
80     global _cyc_w
81     global stacked_cycle_raise
82     global _clients
83
84     clients = _clients[:] # make a copy
85
86     if not forward:
87         clients.reverse()
88
89     try:
90         i = clients.index(_cyc_w) + 1
91     except ValueError:
92         i = 1
93     clients = clients[i:] + clients[:i]
94         
95     desktop = ob.openbox.screen(data.screen).desktop()
96     for w in clients:
97         client = ob.openbox.findClient(w)
98         if client and (client.desktop() == desktop and \
99                        client.normal() and client.focus()):
100             if stacked_cycle_raise:
101                 ob.openbox.screen(data.screen).raiseWindow(client)
102             return
103
104 def _focus_stacked_ungrab(data):
105     global _cyc_mask;
106     global _cyc_key;
107     global _doing_stacked;
108
109     if data.action == ob.KeyAction.Release:
110         # have all the modifiers this started with been released?
111         if not _cyc_mask & data.state:
112             ob.kungrab() # ungrab ourself
113             _doing_stacked = 0;
114             if cycle_raise:
115                 client = ob.openbox.findClient(_cyc_w)
116                 if client:
117                     ob.openbox.screen(data.screen).raiseWindow(client)
118
119 def focus_next_stacked(data, forward=1):
120     """Focus the next (or previous, with forward=0) window in a stacked
121        order."""
122     global _cyc_mask
123     global _cyc_key
124     global _cyc_w
125     global _cyc_screen
126     global _doing_stacked
127
128     if _doing_stacked:
129         if _cyc_key == data.key:
130             _do_stacked_cycle(data,forward)
131     else:
132         _cyc_mask = data.state
133         _cyc_key = data.key
134         _cyc_w = 0
135         _cyc_screen = data.screen
136         _doing_stacked = 1
137
138         ob.kgrab(data.screen, _focus_stacked_ungrab)
139         focus_next_stacked(data, forward) # start with the first press
140
141 def focus_prev_stacked(data):
142     """Focus the previous window in a stacked order."""
143     focus_next_stacked(data, forward=0)
144
145 def focus_next(data, num=1, forward=1):
146     """Focus the next (or previous, with forward=0) window in a linear
147        order."""
148     screen = ob.openbox.screen(data.screen)
149     count = screen.clientCount()
150
151     if not count: return # no clients
152     
153     target = 0
154     if data.client:
155         client_win = data.client.window()
156         found = 0
157         r = range(count)
158         if not forward:
159             r.reverse()
160         for i in r:
161             if found:
162                 target = i
163                 found = 2
164                 break
165             elif screen.client(i).window() == client_win:
166                 found = 1
167         if found == 1: # wraparound
168             if forward: target = 0
169             else: target = count - 1
170
171     t = target
172     curdesk = screen.desktop()
173     while 1:
174         client = screen.client(t)
175         if client.normal() and \
176                (client.desktop() == curdesk or client.desktop() == 0xffffffff)\
177                and client.focus():
178             if cycle_raise:
179                 screen.raiseWindow(client)
180             return
181         if forward:
182             t += num
183             if t >= count: t -= count
184         else:
185             t -= num
186             if t < 0: t += count
187         if t == target: return # nothing to focus
188
189 def focus_prev(data, num=1):
190     """Focus the previous window in a linear order."""
191     focus_next(data, num, forward=0)
192
193
194 ob.ebind(ob.EventAction.NewWindow, _new_win)
195 ob.ebind(ob.EventAction.CloseWindow, _close_win)
196 ob.ebind(ob.EventAction.Focus, _focused)
197
198 print "Loaded focus.py"