]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/focus.py
wrap around right for focus cycling
[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
13 def ob_new_win(data):
14     global ob_clients
15     if not len(ob_clients): ob_clients.append(data.client.window())
16     else: ob_clients.insert(1, data.client.window()) # insert in 2nd slot
17
18 def ob_close_win(data):
19     global ob_clients
20     ob_clients.remove(data.client.window())
21
22 def ob_focused(data):
23     global ob_clients
24     if data.client:
25         win = data.client.window()
26         ob_focused = win
27         # move it to the top
28         ob_clients.remove(win)
29         ob_clients.insert(0, win)
30     elif ob_focus_fallback:
31         # pass around focus
32         ob_focused = 0
33         desktop = openbox.screen(data.screen).desktop()
34         for w in ob_clients:
35             client = openbox.findClient(w)
36             if client and (client.desktop() == desktop and \
37                            client.normal() and client.focus()):
38                 break
39
40 ebind(EventNewWindow, ob_new_win)
41 ebind(EventCloseWindow, ob_close_win)
42 ebind(EventFocus, ob_focused)
43
44 def focus_next(data, num=1, forward=1):
45     """Focus the next (or previous, with forward=0) window in a linear
46        order."""
47     screen = openbox.screen(data.screen)
48     count = screen.clientCount()
49
50     if not count: return # no clients
51     
52     target = 0
53     if data.client:
54         client_win = data.client.window()
55         found = 0
56         r = range(count)
57         if not forward:
58             r.reverse()
59         for i in r:
60             if found:
61                 target = i
62                 found = 2
63                 break
64             elif screen.client(i).window() == client_win:
65                 found = 1
66         if found == 1: # wraparound
67             if forward: target = 0
68             else: target = count - 1
69
70     t = target
71     curdesk = screen.desktop()
72     while 1:
73         client = screen.client(t)
74         if client.normal() and \
75                (client.desktop() == curdesk or client.desktop() == 0xffffffff)\
76                and client.focus():
77             if ob_focus_raise:
78                 screen.raiseWindow(client)
79             return
80         if forward:
81             t += num
82             if t >= count: t -= count
83         else:
84             t -= num
85             if t < 0: t += count
86         if t == target: return # nothing to focus
87
88 def focus_prev(data, num=1):
89     """Focus the previous window in a linear order."""
90     focus_next(data, num, forward=0)
91
92
93 print "Loaded focus.py"