]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/builtins.py
make A-1 mouse focus
[mikachu/openbox.git] / scripts / builtins.py
1 ###########################################################################
2 ### Functions that can be used as callbacks for mouse/keyboard bindings ###
3 ###########################################################################
4
5 def state_above(data, add=2):
6     """Toggles, adds or removes the 'above' state on a window."""
7     if not data.client: return
8     send_client_msg(display.screenInfo(data.screen).rootWindow(),
9                     Property_atoms().net_wm_state, data.client.window(), add,
10                     Property_atoms().net_wm_state_above)
11     
12 def state_below(data, add=2):
13     """Toggles, adds or removes the 'below' state on a window."""
14     if not data.client: return
15     send_client_msg(display.screenInfo(data.screen).rootWindow(),
16                     Property_atoms().net_wm_state, data.client.window(), add,
17                     Property_atoms().net_wm_state_below)
18     
19 def state_shaded(data, add=2):
20     """Toggles, adds or removes the 'shaded' state on a window."""
21     if not data.client: return
22     send_client_msg(display.screenInfo(data.screen).rootWindow(),
23                     Property_atoms().net_wm_state, data.client.window(), add,
24                     Property_atoms().net_wm_state_shaded)
25
26 def iconify(data):
27     """Iconifies the window on which the event occured"""
28     if not data.client: return
29     send_client_msg(display.screenInfo(data.screen).rootWindow(),
30                     Property_atoms().wm_change_state,
31                     data.client.window(), 3) # IconicState
32     
33 def restore(data):
34     """Un-iconifies the window on which the event occured, but does not focus
35        if. If you want to focus the window too, it is recommended that you
36        use the activate() function."""
37     if not data.client: return
38     send_client_msg(display.screenInfo(data.screen).rootWindow(),
39                     Property_atoms().wm_change_state,
40                     data.client.window(), 1) # NormalState
41     
42 def close(data):
43     """Closes the window on which the event occured"""
44     if not data.client: return
45     send_client_msg(display.screenInfo(data.screen).rootWindow(),
46                     Property_atoms().net_close_window, data.client.window(), 0)
47
48 def focus(data):
49     """Focuses the window on which the event occured"""
50     if not data.client: return
51     # !normal windows dont get focus from window enter events
52     if data.action == EventEnterWindow and not data.client.normal():
53         return
54     data.client.focus()
55
56 def move(data):
57     """Moves the window interactively. This should only be used with
58        MouseMotion events"""
59     if not data.client: return
60
61     # !normal windows dont get moved
62     if not data.client.normal(): return
63
64     dx = data.xroot - data.pressx
65     dy = data.yroot - data.pressy
66     data.client.move(data.press_clientx + dx, data.press_clienty + dy)
67
68 def resize(data):
69     """Resizes the window interactively. This should only be used with
70        MouseMotion events"""
71     if not data.client: return
72
73     # !normal windows dont get moved
74     if not data.client.normal(): return
75
76     px = data.pressx
77     py = data.pressy
78     dx = data.xroot - px
79     dy = data.yroot - py
80
81     # pick a corner to anchor
82     if not (resize_nearest or data.context == MC_Grip):
83         corner = Client.TopLeft
84     else:
85         x = px - data.press_clientx
86         y = py - data.press_clienty
87         if y < data.press_clientheight / 2:
88             if x < data.press_clientwidth / 2:
89                 corner = Client.BottomRight
90                 dx *= -1
91             else:
92                 corner = Client.BottomLeft
93             dy *= -1
94         else:
95             if x < data.press_clientwidth / 2:
96                 corner = Client.TopRight
97                 dx *= -1
98             else:
99                 corner = Client.TopLeft
100
101     data.client.resize(corner,
102                        data.press_clientwidth + dx,
103                        data.press_clientheight + dy);
104
105 def restart(data, other = ""):
106     """Restarts openbox, optionally starting another window manager."""
107     openbox.restart(other)
108
109 def raise_win(data):
110     """Raises the window on which the event occured"""
111     if not data.client: return
112     openbox.screen(data.screen).raiseWindow(data.client)
113
114 def lower_win(data):
115     """Lowers the window on which the event occured"""
116     if not data.client: return
117     openbox.screen(data.screen).lowerWindow(data.client)
118
119 def toggle_shade(data):
120     """Toggles the shade status of the window on which the event occured"""
121     state_shaded(data)
122
123 def shade(data):
124     """Shades the window on which the event occured"""
125     state_shaded(data, 1)
126
127 def unshade(data):
128     """Unshades the window on which the event occured"""
129     state_shaded(data, 0)
130
131 def change_desktop(data, num):
132     """Switches to a specified desktop"""
133     root = display.screenInfo(data.screen).rootWindow()
134     send_client_msg(root, Property_atoms().net_current_desktop, root, num)
135
136 def next_desktop(data, no_wrap=0):
137     """Switches to the next desktop, optionally (by default) cycling around to
138        the first when going past the last."""
139     screen = openbox.screen(data.screen)
140     d = screen.desktop()
141     n = screen.numDesktops()
142     if (d < (n-1)):
143         d = d + 1
144     elif not no_wrap:
145         d = 0
146     change_desktop(data, d)
147     
148 def prev_desktop(data, no_wrap=0):
149     """Switches to the previous desktop, optionally (by default) cycling around
150        to the last when going past the first."""
151     screen = openbox.screen(data.screen)
152     d = screen.desktop()
153     n = screen.numDesktops()
154     if (d > 0):
155         d = d - 1
156     elif not no_wrap:
157         d = n - 1
158     change_desktop(data, d)
159
160 def send_to_desktop(data, num):
161     """Sends a client to a specified desktop"""
162     if not data.client: return
163     send_client_msg(display.screenInfo(data.screen).rootWindow(),
164                     Property_atoms().net_wm_desktop, data.client.window(), num)
165
166 def toggle_all_desktops(data):
167     """Toggles between sending a client to all desktops and to the current
168        desktop."""
169     if not data.client: return
170     if not data.client.desktop() == 0xffffffff:
171         send_to_desktop(data, 0xffffffff)
172     else:
173         send_to_desktop(data, openbox.screen(data.screen).desktop())
174     
175 def send_to_all_desktops(data):
176     """Sends a client to all desktops"""
177     if not data.client: return
178     send_to_desktop(data, 0xffffffff)
179     
180 def send_to_next_desktop(data, no_wrap=0, follow=1):
181     """Sends a window to the next desktop, optionally (by default) cycling
182        around to the first when going past the last. Also optionally moving to
183        the new desktop after sending the window."""
184     if not data.client: return
185     screen = openbox.screen(data.screen)
186     d = screen.desktop()
187     n = screen.numDesktops()
188     if (d < (n-1)):
189         d = d + 1
190     elif not no_wrap:
191         d = 0
192     send_to_desktop(data, d)
193     if follow:
194         change_desktop(data, d)
195     
196 def send_to_prev_desktop(data, no_wrap=0, follow=1):
197     """Sends a window to the previous desktop, optionally (by default) cycling
198        around to the last when going past the first. Also optionally moving to
199        the new desktop after sending the window."""
200     if not data.client: return
201     screen = openbox.screen(data.screen)
202     d = screen.desktop()
203     n = screen.numDesktops()
204     if (d > 0):
205         d = d - 1
206     elif not no_wrap:
207         d = n - 1
208     send_to_desktop(data, d)
209     if follow:
210         change_desktop(data, d)
211
212 #########################################
213 ### Convenience functions for scripts ###
214 #########################################
215
216 def execute(bin, screen = 0):
217     """Executes a command on the specified screen. It is recommended that you
218        use this call instead of a python system call. If the specified screen
219        is beyond your range of screens, the default is used instead."""
220     openbox.execute(screen, bin)
221
222 def setup_click_focus(click_raise = 1):
223     """Sets up for focusing windows by clicking on or in the window.
224        Optionally, clicking on or in a window can raise the window to the
225        front of its stacking layer."""
226     mbind("Left", MC_Titlebar, MousePress, focus)
227     mbind("Left", MC_Handle, MousePress, focus)
228     mbind("Left", MC_Grip, MousePress, focus)
229     mbind("Left", MC_Window, MousePress, focus)
230     mbind("A-Left", MC_Frame, MousePress, focus)
231     if click_raise:
232         mbind("Left", MC_Titlebar, MousePress, raise_win)
233         mbind("Left", MC_Handle, MousePress, raise_win)
234         mbind("Left", MC_Grip, MousePress, raise_win)
235         mbind("Left", MC_Window, MousePress, raise_win)    
236
237 def setup_sloppy_focus(click_focus = 1, click_raise = 0):
238     """Sets up for focusing windows when the mouse pointer enters them.
239        Optionally, clicking on or in a window can focus it if your pointer
240        ends up inside a window without focus. Also, optionally, clicking on or
241        in a window can raise the window to the front of its stacking layer."""
242     ebind(EventEnterWindow, focus)
243     if click_focus:
244         mbind("Left", MC_Titlebar, MousePress, focus)
245         mbind("Left", MC_Handle, MousePress, focus)
246         mbind("Left", MC_Grip, MousePress, focus)
247         mbind("Left", MC_Window, MousePress, focus)
248         if click_raise:
249             mbind("Left", MC_Titlebar, MousePress, raise_win)
250             mbind("Left", MC_Handle, MousePress, raise_win)
251             mbind("Left", MC_Grip, MousePress, raise_win)
252             mbind("Left", MC_Window, MousePress, raise_win)
253
254 def setup_window_clicks():
255     """Sets up the default bindings for various mouse buttons for various
256        contexts.
257        This includes:
258         * Alt-left drag anywhere on a window will move it
259         * Alt-right drag anywhere on a window will resize it
260         * Left drag on a window's titlebar/handle will move it
261         * Left drag on a window's handle grips will resize it
262         * Alt-left press anywhere on a window's will raise it to the front of
263           its stacking layer.
264         * Left press on a window's titlebar/handle will raise it to the front
265           of its stacking layer.
266         * Alt-middle click anywhere on a window's will lower it to the bottom
267           of its stacking layer.
268         * Middle click on a window's titlebar/handle will lower it to the
269           bottom of its stacking layer.
270         * Double-left click on a window's titlebar will toggle shading it
271     """
272     mbind("A-Left", MC_Frame, MouseMotion, move)
273     mbind("Left", MC_Titlebar, MouseMotion, move)
274     mbind("Left", MC_Handle, MouseMotion, move)
275
276     mbind("A-Right", MC_Frame, MouseMotion, resize)
277     mbind("Left", MC_Grip, MouseMotion, resize)
278
279     mbind("Left", MC_Titlebar, MousePress, raise_win)
280     mbind("Left", MC_Handle, MousePress, raise_win)
281     mbind("A-Left", MC_Frame, MousePress, raise_win)
282     mbind("A-Middle", MC_Frame, MouseClick, lower_win)
283     mbind("Middle", MC_Titlebar, MouseClick, lower_win)
284     mbind("Middle", MC_Handle, MouseClick, lower_win)
285
286     mbind("Left", MC_Titlebar, MouseDoubleClick, toggle_shade)
287
288 def setup_window_buttons():
289     """Sets up the default behaviors for the buttons in the window titlebar."""
290     mbind("Left", MC_AllDesktopsButton, MouseClick, toggle_all_desktops)
291     mbind("Left", MC_CloseButton, MouseClick, close)
292     mbind("Left", MC_IconifyButton, MouseClick, iconify)
293
294 def setup_scroll():
295     """Sets up the default behaviors for the mouse scroll wheel.
296        This includes:
297         * scrolling on a window titlebar will shade/unshade it
298         * alt-scrolling anywhere will switch to the next/previous desktop
299         * control-alt-scrolling on a window will send it to the next/previous
300           desktop, and switch to the desktop with the window
301     """
302     mbind("Up", MC_Titlebar, MouseClick, shade)
303     mbind("Down", MC_Titlebar, MouseClick, unshade)
304
305     mbind("A-Up", MC_Frame, MouseClick, next_desktop)
306     mbind("A-Up", MC_Root, MouseClick, next_desktop)
307     mbind("A-Down", MC_Frame, MouseClick, prev_desktop)
308     mbind("A-Down", MC_Root, MouseClick, prev_desktop)
309
310     mbind("C-A-Up", MC_Frame, MouseClick, send_to_next_desktop)
311     mbind("C-A-Down", MC_Frame, MouseClick, send_to_prev_desktop)
312
313 def setup_fallback_focus():
314     """Sets up a focus fallback routine so that when no windows are focused,
315        the last window to have focus on the desktop will be focused."""
316     global ob_focus_fallback # see focus.py
317     ob_focus_fallback = 1
318     
319 ############################################################################
320 ### Window placement algorithms, choose one of these and ebind it to the ###
321 ### EventPlaceWindow action.                                             ###
322 ############################################################################
323
324 ob_rand = None
325 import random
326 def placewindows_random(data):
327     if not data.client: return
328     client_area = data.client.area()
329     frame_size = data.client.frame.size()
330     screen = display.screenInfo(data.screen)
331     width = screen.width() - (client_area.width() +
332                               frame_size.left + frame_size.right)
333     height = screen.height() - (client_area.height() + 
334                                 frame_size.top + frame_size.bottom)
335     global ob_rand
336     if not ob_rand: ob_rand = random.Random()
337     x = ob_rand.randrange(0, width-1)
338     y = ob_rand.randrange(0, height-1)
339     data.client.move(x, y)
340
341
342 print "Loaded builtins.py"