Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/otk/property.cc

Go to the documentation of this file.
00001 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
00002 
00003 #ifdef    HAVE_CONFIG_H
00004 #  include "../config.h"
00005 #endif // HAVE_CONFIG_H
00006 
00007 #include "property.hh"
00008 #include "display.hh"
00009 
00010 extern "C" {
00011 #include <X11/Xatom.h>
00012 
00013 #include <assert.h>
00014 }
00015 
00016 #include <algorithm>
00017 
00018 namespace otk {
00019 
00020 Atoms Property::atoms;
00021 
00022 static Atom create(char *name) { return XInternAtom(**display, name, false); }
00023 
00024 void Property::initialize()
00025 {
00026   assert(display);
00027 
00028   // make sure asserts fire if there is a problem
00029   memset(&atoms, 0, sizeof(Atoms));
00030 
00031   atoms.cardinal = XA_CARDINAL;
00032   atoms.window = XA_WINDOW;
00033   atoms.pixmap = XA_PIXMAP;
00034   atoms.atom = XA_ATOM;
00035   atoms.string = XA_STRING;
00036   atoms.utf8 = create("UTF8_STRING");
00037   
00038   atoms.openbox_pid = create("_OPENBOX_PID");
00039 
00040   atoms.wm_colormap_windows = create("WM_COLORMAP_WINDOWS");
00041   atoms.wm_protocols = create("WM_PROTOCOLS");
00042   atoms.wm_state = create("WM_STATE");
00043   atoms.wm_change_state = create("WM_CHANGE_STATE");
00044   atoms.wm_delete_window = create("WM_DELETE_WINDOW");
00045   atoms.wm_take_focus = create("WM_TAKE_FOCUS");
00046   atoms.wm_name = create("WM_NAME");
00047   atoms.wm_icon_name = create("WM_ICON_NAME");
00048   atoms.wm_class = create("WM_CLASS");
00049   atoms.wm_window_role = create("WM_WINDOW_ROLE");
00050   atoms.motif_wm_hints = create("_MOTIF_WM_HINTS");
00051 
00052   atoms.openbox_show_root_menu = create("_OPENBOX_SHOW_ROOT_MENU");
00053   atoms.openbox_show_workspace_menu = create("_OPENBOX_SHOW_WORKSPACE_MENU");
00054 
00055   atoms.net_supported = create("_NET_SUPPORTED");
00056   atoms.net_client_list = create("_NET_CLIENT_LIST");
00057   atoms.net_client_list_stacking = create("_NET_CLIENT_LIST_STACKING");
00058   atoms.net_number_of_desktops = create("_NET_NUMBER_OF_DESKTOPS");
00059   atoms.net_desktop_geometry = create("_NET_DESKTOP_GEOMETRY");
00060   atoms.net_desktop_viewport = create("_NET_DESKTOP_VIEWPORT");
00061   atoms.net_current_desktop = create("_NET_CURRENT_DESKTOP");
00062   atoms.net_desktop_names = create("_NET_DESKTOP_NAMES");
00063   atoms.net_active_window = create("_NET_ACTIVE_WINDOW");
00064   atoms.net_workarea = create("_NET_WORKAREA");
00065   atoms.net_supporting_wm_check = create("_NET_SUPPORTING_WM_CHECK");
00066 //  atoms.net_virtual_roots = create("_NET_VIRTUAL_ROOTS");
00067 
00068   atoms.net_close_window = create("_NET_CLOSE_WINDOW");
00069   atoms.net_wm_moveresize = create("_NET_WM_MOVERESIZE");
00070 
00071 //  atoms.net_properties = create("_NET_PROPERTIES");
00072   atoms.net_wm_name = create("_NET_WM_NAME");
00073   atoms.net_wm_visible_name = create("_NET_WM_VISIBLE_NAME");
00074   atoms.net_wm_icon_name = create("_NET_WM_ICON_NAME");
00075   atoms.net_wm_visible_icon_name = create("_NET_WM_VISIBLE_ICON_NAME");
00076   atoms.net_wm_desktop = create("_NET_WM_DESKTOP");
00077   atoms.net_wm_window_type = create("_NET_WM_WINDOW_TYPE");
00078   atoms.net_wm_state = create("_NET_WM_STATE");
00079   atoms.net_wm_strut = create("_NET_WM_STRUT");
00080 //  atoms.net_wm_icon_geometry = create("_NET_WM_ICON_GEOMETRY");
00081 //  atoms.net_wm_icon = create("_NET_WM_ICON");
00082 //  atoms.net_wm_pid = create("_NET_WM_PID");
00083 //  atoms.net_wm_handled_icons = create("_NET_WM_HANDLED_ICONS");
00084   atoms.net_wm_allowed_actions = create("_NET_WM_ALLOWED_ACTIONS");
00085 
00086 //  atoms.net_wm_ping = create("_NET_WM_PING");
00087   
00088   atoms.net_wm_window_type_desktop = create("_NET_WM_WINDOW_TYPE_DESKTOP");
00089   atoms.net_wm_window_type_dock = create("_NET_WM_WINDOW_TYPE_DOCK");
00090   atoms.net_wm_window_type_toolbar = create("_NET_WM_WINDOW_TYPE_TOOLBAR");
00091   atoms.net_wm_window_type_menu = create("_NET_WM_WINDOW_TYPE_MENU");
00092   atoms.net_wm_window_type_utility = create("_NET_WM_WINDOW_TYPE_UTILITY");
00093   atoms.net_wm_window_type_splash = create("_NET_WM_WINDOW_TYPE_SPLASH");
00094   atoms.net_wm_window_type_dialog = create("_NET_WM_WINDOW_TYPE_DIALOG");
00095   atoms.net_wm_window_type_normal = create("_NET_WM_WINDOW_TYPE_NORMAL");
00096 
00097   atoms.net_wm_moveresize_size_topleft =
00098     create("_NET_WM_MOVERESIZE_SIZE_TOPLEFT");
00099   atoms.net_wm_moveresize_size_topright =
00100     create("_NET_WM_MOVERESIZE_SIZE_TOPRIGHT");
00101   atoms.net_wm_moveresize_size_bottomleft =
00102     create("_NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT");
00103   atoms.net_wm_moveresize_size_bottomright =
00104     create("_NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT");
00105   atoms.net_wm_moveresize_move =
00106     create("_NET_WM_MOVERESIZE_MOVE");
00107  
00108   atoms.net_wm_action_move = create("_NET_WM_ACTION_MOVE");
00109   atoms.net_wm_action_resize = create("_NET_WM_ACTION_RESIZE");
00110   atoms.net_wm_action_minimize = create("_NET_WM_ACTION_MINIMIZE");
00111   atoms.net_wm_action_shade = create("_NET_WM_ACTION_SHADE");
00112   atoms.net_wm_action_stick = create("_NET_WM_ACTION_STICK");
00113   atoms.net_wm_action_maximize_horz = create("_NET_WM_ACTION_MAXIMIZE_HORZ");
00114   atoms.net_wm_action_maximize_vert = create("_NET_WM_ACTION_MAXIMIZE_VERT");
00115   atoms.net_wm_action_fullscreen = create("_NET_WM_ACTION_FULLSCREEN");
00116   atoms.net_wm_action_change_desktop =
00117     create("_NET_WM_ACTION_CHANGE_DESKTOP");
00118   atoms.net_wm_action_close = create("_NET_WM_ACTION_CLOSE");
00119     
00120   atoms.net_wm_state_modal = create("_NET_WM_STATE_MODAL");
00121   atoms.net_wm_state_sticky = create("_NET_WM_STATE_STICKY");
00122   atoms.net_wm_state_maximized_vert = create("_NET_WM_STATE_MAXIMIZED_VERT");
00123   atoms.net_wm_state_maximized_horz = create("_NET_WM_STATE_MAXIMIZED_HORZ");
00124   atoms.net_wm_state_shaded = create("_NET_WM_STATE_SHADED");
00125   atoms.net_wm_state_skip_taskbar = create("_NET_WM_STATE_SKIP_TASKBAR");
00126   atoms.net_wm_state_skip_pager = create("_NET_WM_STATE_SKIP_PAGER");
00127   atoms.net_wm_state_hidden = create("_NET_WM_STATE_HIDDEN");
00128   atoms.net_wm_state_fullscreen = create("_NET_WM_STATE_FULLSCREEN");
00129   atoms.net_wm_state_above = create("_NET_WM_STATE_ABOVE");
00130   atoms.net_wm_state_below = create("_NET_WM_STATE_BELOW");
00131   
00132   atoms.kde_net_system_tray_windows = create("_KDE_NET_SYSTEM_TRAY_WINDOWS");
00133   atoms.kde_net_wm_system_tray_window_for =
00134     create("_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
00135   atoms.kde_net_wm_window_type_override =
00136     create("_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
00137 
00138   atoms.openbox_premax = create("_OPENBOX_PREMAX");
00139   atoms.openbox_active_window = create("_OPENBOX_ACTIVE_WINDOW");
00140 }
00141 
00142 void Property::set(Window win, Atom atom, Atom type, unsigned char* data,
00143                    int size, int nelements, bool append)
00144 {
00145   assert(win != None); assert(atom != None); assert(type != None);
00146   assert(nelements == 0 || (nelements > 0 && data != (unsigned char *) 0));
00147   assert(size == 8 || size == 16 || size == 32);
00148   XChangeProperty(**display, win, atom, type, size,
00149                   (append ? PropModeAppend : PropModeReplace),
00150                   data, nelements);
00151 }
00152 
00153 void Property::set(Window win, Atom atom, Atom type, unsigned long value)
00154 {
00155   set(win, atom, type, (unsigned char*) &value, 32, 1, false);
00156 }
00157 
00158 void Property::set(Window win, Atom atom, Atom type, unsigned long value[],
00159                    int elements)
00160 {
00161   set(win, atom, type, (unsigned char*) value, 32, elements, false);
00162 }
00163 
00164 void Property::set(Window win, Atom atom, StringType type,
00165                    const ustring &value)
00166 {
00167   Atom t;
00168   switch (type) {
00169   case ascii: t = atoms.string; assert(!value.utf8()); break;
00170   case utf8:  t = atoms.utf8;   assert(value.utf8());  break;
00171   default: assert(false); return; // unhandled StringType
00172   }
00173 
00174   // add 1 to the size to include the trailing null
00175   set(win, atom, t, (unsigned char*) value.c_str(), 8, value.bytes() + 1,
00176       false);
00177 }
00178 
00179 void Property::set(Window win, Atom atom, StringType type,
00180                      const StringVect &strings)
00181 {
00182   Atom t;
00183   bool u; // utf8 encoded?
00184   switch (type) {
00185   case ascii: t = atoms.string; u = false; break;
00186   case utf8:  t = atoms.utf8;   u = true;  break;
00187   default: assert(false); return; // unhandled StringType
00188   }
00189 
00190   ustring value(u);
00191 
00192   StringVect::const_iterator it = strings.begin();
00193   const StringVect::const_iterator end = strings.end();
00194   for (; it != end; ++it) {
00195     assert(it->utf8() == u); // the ustring is encoded correctly?
00196     value += *it;
00197     value += '\0';
00198   }
00199 
00200   // add 1 to the size to include the trailing null
00201   set(win, atom, t, (unsigned char*)value.c_str(), 8,
00202       value.bytes() + 1, false);
00203 }
00204 
00205 bool Property::get(Window win, Atom atom, Atom type, unsigned long *nelements,
00206                    unsigned char **value, int size)
00207 {
00208   assert(win != None); assert(atom != None); assert(type != None);
00209   assert(size == 8 || size == 16 || size == 32);
00210   assert(*nelements > 0);
00211   unsigned char *c_val = 0;        // value alloc'd in Xlib, must be XFree()d
00212   Atom ret_type;
00213   int ret_size;
00214   unsigned long ret_bytes;
00215   int result;
00216   unsigned long maxread = *nelements;
00217   bool ret = false;
00218 
00219   // try get the first element
00220   result = XGetWindowProperty(**display, win, atom, 0l, 1l,
00221                               false, AnyPropertyType, &ret_type, &ret_size,
00222                               nelements, &ret_bytes, &c_val);
00223   ret = (result == Success && ret_type == type && ret_size == size &&
00224          *nelements > 0);
00225   if (ret) {
00226     if (ret_bytes == 0 || maxread <= *nelements) {
00227       // we got the whole property's value
00228       *value = new unsigned char[*nelements * size/8 + 1];
00229       memcpy(*value, c_val, *nelements * size/8 + 1);
00230     } else {
00231       // get the entire property since it is larger than one long
00232       XFree(c_val);
00233       // the number of longs that need to be retreived to get the property's
00234       // entire value. The last + 1 is the first long that we retrieved above.
00235       int remain = (ret_bytes - 1)/sizeof(long) + 1 + 1;
00236       if (remain > size/8 * (signed)maxread) // dont get more than the max
00237         remain = size/8 * (signed)maxread;
00238       result = XGetWindowProperty(**display, win, atom, 0l,
00239                                   remain, false, type, &ret_type, &ret_size,
00240                                   nelements, &ret_bytes, &c_val);
00241       ret = (result == Success && ret_type == type && ret_size == size &&
00242              ret_bytes == 0);
00243       /*
00244         If the property has changed type/size, or has grown since our first
00245         read of it, then stop here and try again. If it shrank, then this will
00246         still work.
00247       */
00248       if (! ret)
00249         return get(win, atom, type, &maxread, value, size);
00250   
00251       *value = new unsigned char[*nelements * size/8 + 1];
00252       memcpy(*value, c_val, *nelements * size/8 + 1);
00253     }    
00254   }
00255   if (c_val) XFree(c_val);
00256   return ret;
00257 }
00258 
00259 bool Property::get(Window win, Atom atom, Atom type, unsigned long *nelements,
00260                    unsigned long **value)
00261 {
00262   return get(win, atom, type, nelements, (unsigned char**) value, 32);
00263 }
00264 
00265 bool Property::get(Window win, Atom atom, Atom type, unsigned long *value)
00266 {
00267   unsigned long *temp;
00268   unsigned long num = 1;
00269   if (! get(win, atom, type, &num, (unsigned char **) &temp, 32))
00270     return false;
00271   *value = temp[0];
00272   delete [] temp;
00273   return true;
00274 }
00275 
00276 bool Property::get(Window win, Atom atom, StringType type, ustring *value)
00277 {
00278   unsigned long n = 1;
00279   StringVect s;
00280 
00281   if (get(win, atom, type, &n, &s)) {
00282     *value = s[0];
00283     return true;
00284   }
00285   return false;
00286 }
00287 
00288 bool Property::get(Window win, Atom atom, StringType type,
00289                    unsigned long *nelements, StringVect *strings)
00290 {
00291   assert(*nelements > 0);
00292 
00293   Atom t;
00294   bool u; // utf8 encoded?
00295   switch (type) {
00296   case ascii: t = atoms.string; u = false; break;
00297   case utf8:  t = atoms.utf8;   u = true;  break;
00298   default: assert(false); return false; // unhandled StringType
00299   }
00300   
00301   unsigned char *value;
00302   unsigned long elements = (unsigned) -1;
00303   if (!get(win, atom, t, &elements, &value, 8) || elements < 1)
00304     return false;
00305 
00306   std::string s((char*)value, elements);
00307   delete [] value;
00308 
00309   std::string::const_iterator it = s.begin(), end = s.end();
00310   unsigned long num = 0;
00311   while(num < *nelements) {
00312     std::string::const_iterator tmp = it; // current string.begin()
00313     it = std::find(tmp, end, '\0');       // look for null between tmp and end
00314     strings->push_back(std::string(tmp, it));   // s[tmp:it)
00315     strings->back().setUtf8(u);
00316     ++num;
00317     if (it == end) break;
00318     ++it;
00319     if (it == end) break;
00320   }
00321 
00322   *nelements = num;
00323 
00324   return true;
00325 }
00326 
00327 
00328 /*
00329  * Removes a property entirely from a window.
00330  */
00331 void Property::erase(Window win, Atom atom)
00332 {
00333   XDeleteProperty(**display, win, atom);
00334 }
00335 
00336 }

Generated on Tue Feb 4 22:58:56 2003 for Openbox by doxygen1.3-rc2