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