]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/focus.py
allow python to grab the keyboard. have release events go to the grabs callback....
[mikachu/openbox.git] / scripts / focus.py
1 ###########################################################################
2 ###          Functions for helping out with your window focus.          ###
3 ###########################################################################
4
5 ob_focus_raise = 1
6 ob_focus_fallback = 0
7
8 # maintain a list of clients, stacked in focus order
9 ob_clients = []
10 # maintaint he current focused window
11 ob_focused = 0
12 ob_hold_client_list = 0
13
14 def ob_new_win(data):
15     global ob_clients
16     if not len(ob_clients): ob_clients.append(data.client.window())
17     else: ob_clients.insert(1, data.client.window()) # insert in 2nd slot
18
19 def ob_close_win(data):
20     global ob_clients
21     ob_clients.remove(data.client.window())
22
23 def ob_focused(data):
24     global ob_clients
25     if data.client:
26         if not ob_hold_client_list:
27             win = data.client.window()
28             ob_focused = win
29             # move it to the top
30             ob_clients.remove(win)
31             ob_clients.insert(0, win)
32     elif ob_focus_fallback:
33         ob_old_client_list = 0 # something is wrong.. stop holding
34         # pass around focus
35         ob_focused = 0
36         desktop = openbox.screen(data.screen).desktop()
37         for w in ob_clients:
38             client = openbox.findClient(w)
39             if client and (client.desktop() == desktop and \
40                            client.normal() and client.focus()):
41                 break
42
43 ebind(EventNewWindow, ob_new_win)
44 ebind(EventCloseWindow, ob_close_win)
45 ebind(EventFocus, ob_focused)
46
47 ob_cyc_mask = 0
48 ob_cyc_key = 0;
49
50 def focus_next_stacked_grab(data):
51     global ob_cyc_mask;
52     global ob_cyc_key;
53
54     if data.action == EventKeyRelease:
55         print "release: " + str(ob_cyc_mask) + "..." + str(data.state)
56         # have all the modifiers this started with been released?
57         if not ob_cyc_mask & data.state:
58             kungrab() # ungrab ourself
59             print "UNGRABBED!"
60     else:
61         print "press: " + str(ob_cyc_mask) + "..." + str(data.state) + \
62               "..." + data.key
63         if ob_cyc_key == data.key:
64             print "CYCLING!!"
65
66 def focus_next_stacked(data, forward=1):
67     global ob_cyc_mask;
68     global ob_cyc_key;
69     ob_cyc_mask = data.state
70     ob_cyc_key = data.key
71
72     kgrab(focus_next_stacked_grab)
73     print "GRABBED!"
74     focus_next_stacked_grab(data) # start with the first press
75
76 def focus_prev_stacked(data):
77     return
78
79 def focus_next(data, num=1, forward=1):
80     """Focus the next (or previous, with forward=0) window in a linear
81        order."""
82     screen = openbox.screen(data.screen)
83     count = screen.clientCount()
84
85     if not count: return # no clients
86     
87     target = 0
88     if data.client:
89         client_win = data.client.window()
90         found = 0
91         r = range(count)
92         if not forward:
93             r.reverse()
94         for i in r:
95             if found:
96                 target = i
97                 found = 2
98                 break
99             elif screen.client(i).window() == client_win:
100                 found = 1
101         if found == 1: # wraparound
102             if forward: target = 0
103             else: target = count - 1
104
105     t = target
106     curdesk = screen.desktop()
107     while 1:
108         client = screen.client(t)
109         if client.normal() and \
110                (client.desktop() == curdesk or client.desktop() == 0xffffffff)\
111                and client.focus():
112             if ob_focus_raise:
113                 screen.raiseWindow(client)
114             return
115         if forward:
116             t += num
117             if t >= count: t -= count
118         else:
119             t -= num
120             if t < 0: t += count
121         if t == target: return # nothing to focus
122
123 def focus_prev(data, num=1):
124     """Focus the previous window in a linear order."""
125     focus_next(data, num, forward=0)
126
127
128 print "Loaded focus.py"