]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/focus.py
some intermediate stage. stacked focus cycling is very broken. dont try it. going...
[mikachu/openbox.git] / scripts / focus.py
1 ###########################################################################
2 ###          Functions for helping out with your window focus.          ###
3 ###########################################################################
4
5 # raise the window also when it is focused
6 ob_focus_raise = 1
7 # send focus somewhere when nothing is left with the focus if possible
8 ob_focus_fallback = 0
9
10 # maintain a list of clients, stacked in focus order
11 ob_clients = []
12 # maintaint he current focused window
13 ob_doing_stacked = 0
14
15 def ob_new_win(data):
16     global ob_clients
17     global ob_doing_stacked
18     global ob_cyc_w;
19
20     if ob_doing_stacked:
21         ob_clients.insert(ob_clients.index(ob_cyc_w), data.client.window())
22     else:
23         if not len(ob_clients):
24             ob_clients.append(data.client.window())
25         else:
26             ob_clients.insert(1, data.client.window()) # insert in 2nd slot
27
28 def ob_close_win(data):
29     global ob_clients
30     global ob_cyc_w;
31     global ob_doing_stacked
32
33     if not ob_doing_stacked:
34         # not in the middle of stacked cycling, so who cares
35         ob_clients.remove(data.client.window())
36     else:
37         # have to fix the cycling if we remove anything
38         win = data.client.window()
39         if ob_cyc_w == win:
40             do_stacked_cycle(data) # cycle off the window first
41         ob_clients.remove(win)
42
43 def ob_focused(data):
44     global ob_clients
45     global ob_doing_stacked
46     global ob_cyc_w
47     
48     if data.client:
49         if not ob_doing_stacked: # only move the window when we're not cycling
50             win = data.client.window()
51             # move it to the top
52             ob_clients.remove(win)
53             ob_clients.insert(0, win)
54         else: # if we are cycling, then update our pointer
55             ob_cyc_w = data.client.window()
56     elif ob_focus_fallback: 
57         # pass around focus
58         desktop = openbox.screen(ob_cyc_screen).desktop()
59         for w in ob_clients:
60             client = openbox.findClient(w)
61             if client and (client.desktop() == desktop and \
62                            client.normal() and client.focus()):
63                 break
64
65 ebind(EventNewWindow, ob_new_win)
66 ebind(EventCloseWindow, ob_close_win)
67 ebind(EventFocus, ob_focused)
68
69 ob_cyc_mask = 0
70 ob_cyc_key = 0
71 ob_cyc_w = 0 # last window cycled to
72 ob_cyc_screen = 0
73
74 def do_stacked_cycle(data):
75     global ob_cyc_w
76
77     try:
78         i = ob_clients.index(ob_cyc_w) + 1
79     except ValueError:
80         i = 0
81         
82     clients = ob_clients[i:] + ob_clients[:i]
83     for w in clients:
84         client = openbox.findClient(w)
85         if client and (client.desktop() == desktop and \
86                        client.normal() and client.focus()):
87             return
88
89 def focus_next_stacked_grab(data):
90     global ob_cyc_mask;
91     global ob_cyc_key;
92     global ob_cyc_w;
93     global ob_doing_stacked;
94
95     if data.action == EventKeyRelease:
96         # have all the modifiers this started with been released?
97         if not ob_cyc_mask & data.state:
98             kungrab() # ungrab ourself
99             ob_doing_stacked = 0;
100             print "UNGRABBED!"
101     else:
102         if ob_cyc_key == data.key:
103             # the next window to try focusing in ob_clients[ob_cyc_i]
104             print "CYCLING!!"
105             do_stacked_cycle(data)
106
107 def focus_next_stacked(data, forward=1):
108     global ob_cyc_mask
109     global ob_cyc_key
110     global ob_cyc_w
111     global ob_cyc_screen
112     global ob_doing_stacked
113     ob_cyc_mask = data.state
114     ob_cyc_key = data.key
115     ob_cyc_w = 0
116     ob_cyc_screen = data.screen
117     ob_doing_stacked = 1
118
119     kgrab(data.screen, focus_next_stacked_grab)
120     print "GRABBED!"
121     focus_next_stacked_grab(data) # start with the first press
122
123 def focus_prev_stacked(data):
124     return
125
126 def focus_next(data, num=1, forward=1):
127     """Focus the next (or previous, with forward=0) window in a linear
128        order."""
129     screen = openbox.screen(data.screen)
130     count = screen.clientCount()
131
132     if not count: return # no clients
133     
134     target = 0
135     if data.client:
136         client_win = data.client.window()
137         found = 0
138         r = range(count)
139         if not forward:
140             r.reverse()
141         for i in r:
142             if found:
143                 target = i
144                 found = 2
145                 break
146             elif screen.client(i).window() == client_win:
147                 found = 1
148         if found == 1: # wraparound
149             if forward: target = 0
150             else: target = count - 1
151
152     t = target
153     curdesk = screen.desktop()
154     while 1:
155         client = screen.client(t)
156         if client.normal() and \
157                (client.desktop() == curdesk or client.desktop() == 0xffffffff)\
158                and client.focus():
159             if ob_focus_raise:
160                 screen.raiseWindow(client)
161             return
162         if forward:
163             t += num
164             if t >= count: t -= count
165         else:
166             t -= num
167             if t < 0: t += count
168         if t == target: return # nothing to focus
169
170 def focus_prev(data, num=1):
171     """Focus the previous window in a linear order."""
172     focus_next(data, num, forward=0)
173
174
175 print "Loaded focus.py"