]> icculus.org git repositories - mikachu/openbox.git/blob - src/XAtom.cc
warping works!
[mikachu/openbox.git] / src / XAtom.cc
1 // XAtom.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (xor at orodu.net)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22 #include "../config.h"
23
24 #include <assert.h>
25
26 #include "XAtom.hh"
27 #include "Screen.hh"
28 #include "Util.hh"
29
30 XAtom::XAtom(Display *d) {
31   _display = d;
32
33   // make sure asserts fire if there is a problem
34   memset(_atoms, 0, sizeof(_atoms));
35
36   _atoms[cardinal] = XA_CARDINAL;
37   _atoms[window] = XA_WINDOW;
38   _atoms[pixmap] = XA_PIXMAP;
39   _atoms[atom] = XA_ATOM;
40   _atoms[string] = XA_STRING;
41   _atoms[utf8_string] = create("UTF8_STRING");
42   
43 #ifdef    HAVE_GETPID
44   _atoms[blackbox_pid] = create("_BLACKBOX_PID");
45 #endif // HAVE_GETPID
46
47   _atoms[wm_colormap_windows] = create("WM_COLORMAP_WINDOWS");
48   _atoms[wm_protocols] = create("WM_PROTOCOLS");
49   _atoms[wm_state] = create("WM_STATE");
50   _atoms[wm_change_state] = create("WM_CHANGE_STATE");
51   _atoms[wm_delete_window] = create("WM_DELETE_WINDOW");
52   _atoms[wm_take_focus] = create("WM_TAKE_FOCUS");
53   _atoms[wm_name] = create("WM_NAME");
54   _atoms[wm_icon_name] = create("WM_ICON_NAME");
55   _atoms[wm_class] = create("WM_CLASS");
56   _atoms[motif_wm_hints] = create("_MOTIF_WM_HINTS");
57   _atoms[blackbox_hints] = create("_BLACKBOX_HINTS");
58   _atoms[blackbox_attributes] = create("_BLACKBOX_ATTRIBUTES");
59   _atoms[blackbox_change_attributes] = create("_BLACKBOX_CHANGE_ATTRIBUTES");
60   _atoms[blackbox_structure_messages] = create("_BLACKBOX_STRUCTURE_MESSAGES");
61   _atoms[blackbox_notify_startup] = create("_BLACKBOX_NOTIFY_STARTUP");
62   _atoms[blackbox_notify_window_add] = create("_BLACKBOX_NOTIFY_WINDOW_ADD");
63   _atoms[blackbox_notify_window_del] = create("_BLACKBOX_NOTIFY_WINDOW_DEL");
64   _atoms[blackbox_notify_current_workspace] = 
65     create("_BLACKBOX_NOTIFY_CURRENT_WORKSPACE");
66   _atoms[blackbox_notify_workspace_count] =
67     create("_BLACKBOX_NOTIFY_WORKSPACE_COUNT");
68   _atoms[blackbox_notify_window_focus] =
69     create("_BLACKBOX_NOTIFY_WINDOW_FOCUS");
70   _atoms[blackbox_notify_window_raise] =
71     create("_BLACKBOX_NOTIFY_WINDOW_RAISE");
72   _atoms[blackbox_notify_window_lower] =
73     create("_BLACKBOX_NOTIFY_WINDOW_LOWER");
74   
75   _atoms[blackbox_change_workspace] = create("_BLACKBOX_CHANGE_WORKSPACE");
76   _atoms[blackbox_change_window_focus] =
77     create("_BLACKBOX_CHANGE_WINDOW_FOCUS");
78   _atoms[blackbox_cycle_window_focus] = create("_BLACKBOX_CYCLE_WINDOW_FOCUS");
79
80   _atoms[net_supported] = create("_NET_SUPPORTED");
81   _atoms[net_client_list] = create("_NET_CLIENT_LIST");
82   _atoms[net_client_list_stacking] = create("_NET_CLIENT_LIST_STACKING");
83   _atoms[net_number_of_desktops] = create("_NET_NUMBER_OF_DESKTOPS");
84   _atoms[net_desktop_geometry] = create("_NET_DESKTOP_GEOMETRY");
85   _atoms[net_desktop_viewport] = create("_NET_DESKTOP_VIEWPORT");
86   _atoms[net_current_desktop] = create("_NET_CURRENT_DESKTOP");
87   _atoms[net_desktop_names] = create("_NET_DESKTOP_NAMES");
88   _atoms[net_active_window] = create("_NET_ACTIVE_WINDOW");
89   _atoms[net_workarea] = create("_NET_WORKAREA");
90   _atoms[net_supporting_wm_check] = create("_NET_SUPPORTING_WM_CHECK");
91 //  _atoms[net_virtual_roots] = create("_NET_VIRTUAL_ROOTS");
92
93   _atoms[net_close_window] = create("_NET_CLOSE_WINDOW");
94   _atoms[net_wm_moveresize] = create("_NET_WM_MOVERESIZE");
95
96 //  _atoms[net_properties] = create("_NET_PROPERTIES");
97   _atoms[net_wm_name] = create("_NET_WM_NAME");
98   _atoms[net_wm_visible_name] = create("_NET_WM_VISIBLE_NAME");
99   _atoms[net_wm_icon_name] = create("_NET_WM_ICON_NAME");
100   _atoms[net_wm_visible_icon_name] = create("_NET_WM_VISIBLE_ICON_NAME");
101   _atoms[net_wm_desktop] = create("_NET_WM_DESKTOP");
102   _atoms[net_wm_window_type] = create("_NET_WM_WINDOW_TYPE");
103   _atoms[net_wm_state] = create("_NET_WM_STATE");
104   _atoms[net_wm_strut] = create("_NET_WM_STRUT");
105 //  _atoms[net_wm_icon_geometry] = create("_NET_WM_ICON_GEOMETRY");
106 //  _atoms[net_wm_icon] = create("_NET_WM_ICON");
107 //  _atoms[net_wm_pid] = create("_NET_WM_PID");
108 //  _atoms[net_wm_handled_icons] = create("_NET_WM_HANDLED_ICONS");
109   _atoms[net_wm_allowed_actions] = create("_NET_WM_ALLOWED_ACTIONS");
110
111 //  _atoms[net_wm_ping] = create("_NET_WM_PING");
112   
113   _atoms[net_wm_window_type_desktop] = create("_NET_WM_WINDOW_TYPE_DESKTOP");
114   _atoms[net_wm_window_type_dock] = create("_NET_WM_WINDOW_TYPE_DOCK");
115   _atoms[net_wm_window_type_toolbar] = create("_NET_WM_WINDOW_TYPE_TOOLBAR");
116   _atoms[net_wm_window_type_menu] = create("_NET_WM_WINDOW_TYPE_MENU");
117   _atoms[net_wm_window_type_utility] = create("_NET_WM_WINDOW_TYPE_UTILITY");
118   _atoms[net_wm_window_type_splash] = create("_NET_WM_WINDOW_TYPE_SPLASH");
119   _atoms[net_wm_window_type_dialog] = create("_NET_WM_WINDOW_TYPE_DIALOG");
120   _atoms[net_wm_window_type_normal] = create("_NET_WM_WINDOW_TYPE_NORMAL");
121
122   _atoms[net_wm_moveresize_size_topleft] =
123     create("_NET_WM_MOVERESIZE_SIZE_TOPLEFT");
124   _atoms[net_wm_moveresize_size_topright] =
125     create("_NET_WM_MOVERESIZE_SIZE_TOPRIGHT");
126   _atoms[net_wm_moveresize_size_bottomleft] =
127     create("_NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT");
128   _atoms[net_wm_moveresize_size_bottomright] =
129     create("_NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT");
130   _atoms[net_wm_moveresize_move] =
131     create("_NET_WM_MOVERESIZE_MOVE");
132  
133   _atoms[net_wm_action_move] = create("_NET_WM_ACTION_MOVE");
134   _atoms[net_wm_action_resize] = create("_NET_WM_ACTION_RESIZE");
135   _atoms[net_wm_action_shade] = create("_NET_WM_ACTION_SHADE");
136   _atoms[net_wm_action_maximize_horz] = create("_NET_WM_ACTION_MAXIMIZE_HORZ");
137   _atoms[net_wm_action_maximize_vert] = create("_NET_WM_ACTION_MAXIMIZE_VERT");
138   _atoms[net_wm_action_change_desktop] =
139     create("_NET_WM_ACTION_CHANGE_DESKTOP");
140   _atoms[net_wm_action_close] = create("_NET_WM_ACTION_CLOSE");
141     
142   _atoms[net_wm_state_modal] = create("_NET_WM_STATE_MODAL");
143   _atoms[net_wm_state_maximized_vert] = create("_NET_WM_STATE_MAXIMIZED_VERT");
144   _atoms[net_wm_state_maximized_horz] = create("_NET_WM_STATE_MAXIMIZED_HORZ");
145   _atoms[net_wm_state_shaded] = create("_NET_WM_STATE_SHADED");
146   _atoms[net_wm_state_skip_taskbar] = create("_NET_WM_STATE_SKIP_TASKBAR");
147   _atoms[net_wm_state_skip_pager] = create("_NET_WM_STATE_SKIP_PAGER");
148   _atoms[net_wm_state_hidden] = create("_NET_WM_STATE_HIDDEN");
149   _atoms[net_wm_state_fullscreen] = create("_NET_WM_STATE_FULLSCREEN");
150   
151   _atoms[kde_net_system_tray_windows] = create("_KDE_NET_SYSTEM_TRAY_WINDOWS");
152   _atoms[kde_net_wm_system_tray_window_for] =
153     create("_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
154   _atoms[kde_net_wm_window_type_override] =
155     create("_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
156 }
157
158
159 /*
160  * clean up the class' members
161  */
162 XAtom::~XAtom() {
163   while (!_support_windows.empty()) {
164     // make sure we aren't fucking with this somewhere
165     assert(_support_windows.back() != None);
166     XDestroyWindow(_display, _support_windows.back());
167     _support_windows.pop_back();
168   }
169 }
170
171
172 /*
173  * Returns an atom from the Xserver, creating it if necessary.
174  */
175 Atom XAtom::create(const char *name) const {
176   return XInternAtom(_display, name, False);
177 }
178
179
180 /*
181  * Sets which atoms are supported for NETWM, by Openbox, on the root window.
182  */
183 void XAtom::setSupported(const ScreenInfo *screen) {
184   Window root = screen->getRootWindow();
185
186   // create the netwm support window
187   Window w = XCreateSimpleWindow(_display, root, 0, 0, 1, 1, 0, 0, 0);
188   assert(w != None);
189   _support_windows.push_back(w);
190   
191   // set supporting window
192   setValue(root, net_supporting_wm_check, window, w);
193  
194   //set properties on the supporting window
195   setValue(w, net_wm_name, utf8, "Openbox");
196   setValue(w, net_supporting_wm_check, window, w);
197   
198   // we don't support any yet..
199   // yes we do!
200
201   Atom supported[] = {
202     _atoms[net_current_desktop],
203     _atoms[net_number_of_desktops],
204     _atoms[net_desktop_geometry],
205     _atoms[net_desktop_viewport],
206     _atoms[net_active_window],
207     _atoms[net_workarea],
208     _atoms[net_client_list],
209     _atoms[net_client_list_stacking],
210     _atoms[net_desktop_names],
211     _atoms[net_close_window],
212     _atoms[net_wm_name],
213     _atoms[net_wm_visible_name],
214     _atoms[net_wm_icon_name],
215     _atoms[net_wm_visible_icon_name],
216     _atoms[net_wm_desktop],
217     _atoms[net_wm_strut],
218     _atoms[net_wm_window_type],
219     _atoms[net_wm_window_type_desktop],
220     _atoms[net_wm_window_type_dock],
221     _atoms[net_wm_window_type_toolbar],
222     _atoms[net_wm_window_type_menu],
223     _atoms[net_wm_window_type_utility],
224     _atoms[net_wm_window_type_splash],
225     _atoms[net_wm_window_type_dialog],
226     _atoms[net_wm_window_type_normal],
227     _atoms[net_wm_moveresize],
228     _atoms[net_wm_moveresize_size_topleft],
229     _atoms[net_wm_moveresize_size_topright],
230     _atoms[net_wm_moveresize_size_bottomleft],
231     _atoms[net_wm_moveresize_size_bottomright],
232     _atoms[net_wm_moveresize_move],
233     _atoms[net_wm_allowed_actions],
234     _atoms[net_wm_action_move],
235     _atoms[net_wm_action_resize],
236     _atoms[net_wm_action_shade],
237     _atoms[net_wm_action_maximize_horz],
238     _atoms[net_wm_action_maximize_vert],
239     _atoms[net_wm_action_change_desktop],
240     _atoms[net_wm_action_close],
241     _atoms[net_wm_state],
242     _atoms[net_wm_state_modal],
243     _atoms[net_wm_state_maximized_vert],
244     _atoms[net_wm_state_maximized_horz],
245     _atoms[net_wm_state_shaded],
246     _atoms[net_wm_state_skip_taskbar],
247     _atoms[net_wm_state_skip_pager],
248     _atoms[net_wm_state_hidden],
249     _atoms[net_wm_state_fullscreen],
250   };
251   const int num_supported = sizeof(supported)/sizeof(Atom);
252
253   setValue(root, net_supported, atom, supported, num_supported);
254 }
255   
256
257 /*
258  * Internal setValue.
259  * Sets a window property on a window, optionally appending to the existing
260  * value.
261  */
262 void XAtom::setValue(Window win, Atom atom, Atom type,
263                      unsigned char* data, int size, int nelements,
264                      bool append) const {
265   assert(win != None); assert(atom != None); assert(type != None);
266   assert(nelements == 0 || (nelements > 0 && data != (unsigned char *) 0));
267   assert(size == 8 || size == 16 || size == 32);
268   XChangeProperty(_display, win, atom, type, size,
269                   (append ? PropModeAppend : PropModeReplace),
270                   data, nelements);
271 }
272
273
274 /*
275  * Set a 32-bit property value on a window.
276  */
277 void XAtom::setValue(Window win, Atoms atom, Atoms type,
278                      unsigned long value) const {
279   assert(atom >= 0 && atom < NUM_ATOMS);
280   assert(type >= 0 && type < NUM_ATOMS);
281   setValue(win, _atoms[atom], _atoms[type],
282            reinterpret_cast<unsigned char*>(&value), 32, 1, False);
283 }
284
285
286 /*
287  * Set an array of 32-bit properties value on a window.
288  */
289 void XAtom::setValue(Window win, Atoms atom, Atoms type,
290                      unsigned long value[], int elements) const {
291   assert(atom >= 0 && atom < NUM_ATOMS);
292   assert(type >= 0 && type < NUM_ATOMS);
293   setValue(win, _atoms[atom], _atoms[type],
294            reinterpret_cast<unsigned char*>(value), 32, elements, False);
295 }
296
297
298 /*
299  * Set an string property value on a window.
300  */
301 void XAtom::setValue(Window win, Atoms atom, StringType type,
302                      const std::string &value) const {
303   assert(atom >= 0 && atom < NUM_ATOMS);
304   assert(type >= 0 && type < NUM_STRING_TYPE);
305   
306   Atom t;
307   switch (type) {
308   case ansi: t = _atoms[string]; break;
309   case utf8: t = _atoms[utf8_string]; break;
310   default: assert(False); return; // unhandled StringType
311   }
312   setValue(win, _atoms[atom], t,
313            reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
314            8, value.size() + 1, False); // add 1 to the size to include the null
315 }
316
317
318 /*
319  * Set an array of string property values on a window.
320  */
321 void XAtom::setValue(Window win, Atoms atom, StringType type,
322                      const StringVect &strings) const {
323   assert(atom >= 0 && atom < NUM_ATOMS);
324   assert(type >= 0 && type < NUM_STRING_TYPE);
325
326   Atom t;
327   switch (type) {
328   case ansi: t = _atoms[string]; break;
329   case utf8: t = _atoms[utf8_string]; break;
330   default: assert(False); return; // unhandled StringType
331   }
332
333   std::string value;
334
335   StringVect::const_iterator it = strings.begin();
336   const StringVect::const_iterator end = strings.end();
337   for (; it != end; ++it)
338       value += *it + '\0';
339
340   setValue(win, _atoms[atom], t,
341            reinterpret_cast<unsigned char *>(const_cast<char *>(value.c_str())),
342            8, value.size(), False);
343 }
344
345
346 /*
347  * Internal getValue function used by all of the typed getValue functions.
348  * Gets an property's value from a window.
349  * Returns True if the property was successfully retrieved; False if the
350  * property did not exist on the window, or has a different type/size format
351  * than the user tried to retrieve.
352  */
353 bool XAtom::getValue(Window win, Atom atom, Atom type,
354                      unsigned long &nelements, unsigned char **value,
355                      int size) const {
356   assert(win != None); assert(atom != None); assert(type != None);
357   assert(size == 8 || size == 16 || size == 32);
358   assert(nelements > 0);
359   unsigned char *c_val = 0;        // value alloc'd with c malloc
360   Atom ret_type;
361   int ret_size;
362   unsigned long ret_bytes;
363   int result;
364   const unsigned long maxread = nelements;
365   // try get the first element
366   result = XGetWindowProperty(_display, win, atom, 0l, 1l, False,
367                               AnyPropertyType, &ret_type, &ret_size,
368                               &nelements, &ret_bytes, &c_val);
369   if (result != Success || ret_type != type || ret_size != size ||
370       nelements < 1) {
371     // an error occured, the property does not exist on the window, or is empty,
372     // or the wrong data is in property for the request
373     if (c_val) XFree(c_val);
374     return False;
375   }
376   // the data is correct, now, is there more elements left?
377   if (ret_bytes == 0 || maxread <= nelements) {
378     // we got the whole property's value
379     *value = new unsigned char[nelements * size/8 + 1];
380     memcpy(*value, c_val, nelements * size/8 + 1);
381     XFree(c_val);
382     return True;    
383   }
384   // get the entire property since it is larger than one long
385   XFree(c_val);
386   // the number of longs that need to be retreived to get the property's entire
387   // value. The last + 1 is the first long that we retrieved above.
388   int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
389   if (remain > size/8 * (signed)maxread) // dont get more than the max
390     remain = size/8 * (signed)maxread;
391   result = XGetWindowProperty(_display, win, atom, 0l, remain, False, type,
392                               &ret_type, &ret_size, &nelements, &ret_bytes,
393                               &c_val);
394   assert(result == Success);
395   assert(ret_bytes == 0);
396   *value = new unsigned char[nelements * size/8 + 1];
397   memcpy(*value, c_val, nelements * size/8 + 1);
398   XFree(c_val);
399   return True;    
400 }
401
402
403 /*
404  * Gets a 32-bit property's value from a window.
405  */
406 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
407                          unsigned long &nelements,
408                          unsigned long **value) const {
409   assert(atom >= 0 && atom < NUM_ATOMS);
410   assert(type >= 0 && type < NUM_ATOMS);
411   return getValue(win, _atoms[atom], _atoms[type], nelements,
412                   reinterpret_cast<unsigned char **>(value), 32);
413 }
414
415
416 /*
417  * Gets a single 32-bit property's value from a window.
418  */
419 bool XAtom::getValue(Window win, Atoms atom, Atoms type,
420                      unsigned long &value) const {
421   assert(atom >= 0 && atom < NUM_ATOMS);
422   assert(type >= 0 && type < NUM_ATOMS);
423   unsigned long *temp;
424   unsigned long num = 1;
425   if (! getValue(win, _atoms[atom], _atoms[type], num,
426                  reinterpret_cast<unsigned char **>(&temp), 32))
427     return False;
428   value = temp[0];
429   delete [] temp;
430   return True;
431 }
432
433
434 /*
435  * Gets an string property's value from a window.
436  */
437 bool XAtom::getValue(Window win, Atoms atom, StringType type,
438                      std::string &value) const {
439   unsigned long n = 1;
440   StringVect s;
441   if (getValue(win, atom, type, n, s)) {
442     value = s[0];
443     return True;
444   }
445   return False;
446 }
447
448
449 bool XAtom::getValue(Window win, Atoms atom, StringType type,
450                      unsigned long &nelements, StringVect &strings) const {
451   assert(atom >= 0 && atom < NUM_ATOMS);
452   assert(type >= 0 && type < NUM_STRING_TYPE);
453   assert(win != None); assert(_atoms[atom] != None);
454   assert(nelements > 0);
455
456   Atom t;
457   switch (type) {
458   case ansi: t = _atoms[string]; break;
459   case utf8: t = _atoms[utf8_string]; break;
460   default: assert(False); return False; // unhandled StringType
461   }
462   
463   unsigned char *value;
464   unsigned long elements = (unsigned) -1;
465   if (!getValue(win, _atoms[atom], t, elements, &value, 8) || elements < 1)
466     return False;
467
468   std::string s(reinterpret_cast<char *>(value), elements);
469   delete [] value;
470
471   std::string::const_iterator it = s.begin(), end = s.end();
472   unsigned long num = 0;
473   while(num < nelements) {
474     std::string::const_iterator tmp = it; // current string.begin()
475     it = std::find(tmp, end, '\0');       // look for null between tmp and end
476     strings.push_back(std::string(tmp, it));   // s[tmp:it)
477     ++num;
478     if (it == end) break;
479     ++it;
480     if (it == end) break;
481   }
482
483   nelements = num;
484
485   return True;
486 }
487
488
489 /*
490  * Removes a property entirely from a window.
491  */
492 void XAtom::eraseValue(Window win, Atoms atom) const {
493   assert(atom >= 0 && atom < NUM_ATOMS);
494   XDeleteProperty(_display, win, _atoms[atom]);
495 }
496
497
498 void XAtom::sendClientMessage(Window target, Atoms type, Window about,
499                               long data, long data1, long data2,
500                               long data3) const {
501   assert(atom >= 0 && atom < NUM_ATOMS);
502   assert(target != None);
503
504   XEvent e;
505   e.xclient.type = ClientMessage;
506   e.xclient.format = 32;
507   e.xclient.message_type = _atoms[type];
508   e.xclient.window = about;
509   e.xclient.data.l[0] = data;
510   e.xclient.data.l[1] = data1;
511   e.xclient.data.l[2] = data2;
512   e.xclient.data.l[3] = data3;
513
514   XSendEvent(_display, target, False,
515              SubstructureRedirectMask | SubstructureNotifyMask,
516              &e);
517 }