close windows with netwm messages
[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     client = Openbox_findClient(openbox, data.window())
8     if not client: return
9     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
10     window = OBClient_window(client)
11     above = OBProperty_atom(Openbox_property(openbox),
12                             OBProperty_net_wm_state_above)
13     send_client_msg(root, OBProperty_net_wm_state, window, add,
14                     above)
15     
16 def state_below(data, add=2):
17     """Toggles, adds or removes the 'below' state on a window."""
18     client = Openbox_findClient(openbox, data.window())
19     if not client: return
20     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
21     window = OBClient_window(client)
22     below = OBProperty_atom(Openbox_property(openbox),
23                             OBProperty_net_wm_state_below)
24     send_client_msg(root, OBProperty_net_wm_state, window, add,
25                     below)
26     
27 def state_shaded(data, add=2):
28     """Toggles, adds or removes the 'shaded' state on a window."""
29     client = Openbox_findClient(openbox, data.window())
30     if not client: return
31     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
32     window = OBClient_window(client)
33     shaded = OBProperty_atom(Openbox_property(openbox),
34                             OBProperty_net_wm_state_shaded)
35     send_client_msg(root, OBProperty_net_wm_state, window, add,
36                     shaded)
37     
38 def close(data):
39     """Closes the window on which the event occured"""
40     client = Openbox_findClient(openbox, data.window())
41     if not client: return
42     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
43     window = OBClient_window(client)
44     send_client_msg(root, OBProperty_net_close_window, window)
45
46 def focus(data):
47     """Focuses the window on which the event occured"""
48     client = Openbox_findClient(openbox, data.window())
49     if not client: return
50     type = OBClient_type(client)
51     # !normal windows dont get focus from window enter events
52     if data.action() == EventEnterWindow and not OBClient_normal(client):
53         return
54     OBClient_focus(client)
55
56 def move(data):
57     """Moves the window interactively. This should only be used with
58        MouseMotion events"""
59     client = Openbox_findClient(openbox, data.window())
60     if not client: return
61
62     # !normal windows dont get moved
63     if not OBClient_normal(client): return
64
65     dx = data.xroot() - data.pressx()
66     dy = data.yroot() - data.pressy()
67     OBClient_move(client, data.press_clientx() + dx, data.press_clienty() + dy)
68
69 def resize(data):
70     """Resizes the window interactively. This should only be used with
71        MouseMotion events"""
72     client = Openbox_findClient(openbox, data.window())
73     if not client: return
74
75     # !normal windows dont get moved
76     if not OBClient_normal(client): return
77
78     px = data.pressx()
79     py = data.pressy()
80     dx = data.xroot() - px
81     dy = data.yroot() - py
82
83     # pick a corner to anchor
84     if not (resize_nearest or data.context() == MC_Grip):
85         corner = OBClient_TopLeft
86     else:
87         x = px - data.press_clientx()
88         y = py - data.press_clienty()
89         if y < data.press_clientheight() / 2:
90             if x < data.press_clientwidth() / 2:
91                 corner = OBClient_BottomRight
92                 dx *= -1
93             else:
94                 corner = OBClient_BottomLeft
95             dy *= -1
96         else:
97             if x < data.press_clientwidth() / 2:
98                 corner = OBClient_TopRight
99                 dx *= -1
100             else:
101                 corner = OBClient_TopLeft
102
103     OBClient_resize(client, corner,
104                     data.press_clientwidth() + dx,
105                     data.press_clientheight() + dy);
106
107 def restart(data):
108     """Restarts openbox"""
109     Openbox_restart(openbox, "")
110
111 def raise_win(data):
112     """Raises the window on which the event occured"""
113     client = Openbox_findClient(openbox, data.window())
114     if not client: return
115     screen = Openbox_screen(openbox, OBClient_screen(client))
116     OBScreen_restack(screen, 1, client)
117
118 def lower_win(data):
119     """Lowers the window on which the event occured"""
120     client = Openbox_findClient(openbox, data.window())
121     if not client: return
122     screen = Openbox_screen(openbox, OBClient_screen(client))
123     OBScreen_restack(screen, 0, client)
124
125 def toggle_shade(data):
126     """Toggles the shade status of the window on which the event occured"""
127     state_shaded(data)
128
129 def shade(data):
130     """Shades the window on which the event occured"""
131     state_shaded(data, 1)
132
133 def unshade(data):
134     """Unshades the window on which the event occured"""
135     state_shaded(data, 0)
136
137 def change_desktop(data, num):
138     """Switches to a specified desktop"""
139     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
140     send_client_msg(root, OBProperty_net_current_desktop, root, num)
141
142 def next_desktop(data, no_wrap=0):
143     """Switches to the next desktop, optionally (by default) cycling around to
144        the first when going past the last."""
145     screen = Openbox_screen(openbox, data.screen())
146     d = OBScreen_desktop(screen)
147     n = OBScreen_numDesktops(screen)
148     if (d < (n-1)):
149         d = d + 1
150     elif not no_wrap:
151         d = 0
152     change_desktop(data, d)
153     
154 def prev_desktop(data, no_wrap=0):
155     """Switches to the previous desktop, optionally (by default) cycling around
156        to the last when going past the first."""
157     screen = Openbox_screen(openbox, data.screen())
158     d = OBScreen_desktop(screen)
159     n = OBScreen_numDesktops(screen)
160     if (d > 0):
161         d = d - 1
162     elif not no_wrap:
163         d = n - 1
164     change_desktop(data, d)
165
166 def send_to_desktop(data, num):
167     """Sends a client to a specified desktop"""
168     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
169     client = Openbox_findClient(openbox, data.window())
170     if client:
171         window = OBClient_window(client)
172         send_client_msg(root, OBProperty_net_wm_desktop, window, num)
173
174 def send_to_next_desktop(data, no_wrap=0, follow=1):
175     """Sends a window to the next desktop, optionally (by default) cycling
176        around to the first when going past the last. Also optionally moving to
177        the new desktop after sending the window."""
178     client = Openbox_findClient(openbox, data.window())
179     if not client: return
180     screen = Openbox_screen(openbox, data.screen())
181     d = OBScreen_desktop(screen)
182     n = OBScreen_numDesktops(screen)
183     if (d < (n-1)):
184         d = d + 1
185     elif not no_wrap:
186         d = 0
187     send_to_desktop(data, d)
188     if follow:
189         change_desktop(data, d)
190     
191 def send_to_prev_desktop(data, no_wrap=0, follow=1):
192     """Sends a window to the previous desktop, optionally (by default) cycling
193        around to the last when going past the first. Also optionally moving to
194        the new desktop after sending the window."""
195     client = Openbox_findClient(openbox, data.window())
196     if not client: return
197     screen = Openbox_screen(openbox, data.screen())
198     d = OBScreen_desktop(screen)
199     n = OBScreen_numDesktops(screen)
200     if (d > 0):
201         d = d - 1
202     elif not no_wrap:
203         d = n - 1
204     send_to_desktop(data, d)
205     if follow:
206         change_desktop(data, d)
207
208 #########################################
209 ### Convenience functions for scripts ###
210 #########################################
211
212 def execute(bin, screen = 0):
213     """Executes a command on the specified screen. It is recommended that you
214        use this call instead of a python system call. If the specified screen
215        is beyond your range of screens, the default is used instead."""
216     Openbox_execute(openbox, screen, bin)
217
218 print "Loaded builtins.py"