]> Dogcows Code - chaz/openbox/blobdiff - otk/display.cc
valgrind fixes, and fixes for writing shit all over the environment. yay~!!!!!!!
[chaz/openbox] / otk / display.cc
index 9e6685b9864d7fb1a5eb7cea0e0adf061d50930d..98288bd02dcdf54990437673a55fb1e9d14264f4 100644 (file)
@@ -1,4 +1,4 @@
-// -*- mode: C++; indent-tabs-mode: nil; -*-
+// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
 
 #ifdef HAVE_CONFIG_H
 # include "../config.h"
@@ -7,18 +7,27 @@
 #include "display.hh"
 #include "screeninfo.hh"
 #include "gccache.hh"
+#include "util.hh"
 
 extern "C" {
 #include <X11/keysym.h>
 
+#ifdef    XKB
+#include <X11/XKBlib.h>
+#endif // XKB
+
+#ifdef    SHAPE
+#include <X11/extensions/shape.h>
+#endif // SHAPE
+
+#ifdef    XINERAMA
+#include <X11/extensions/Xinerama.h>
+#endif // XINERAMA
+
 #ifdef    HAVE_STDIO_H
 #  include <stdio.h>
 #endif // HAVE_STDIO_H
 
-#ifdef    HAVE_STDLIB_H
-#  include <stdlib.h>
-#endif // HAVE_STDLIB_H
-
 #ifdef    HAVE_SIGNAL_H
 #  include <signal.h>
 #endif // HAVE_SIGNAL_H
@@ -39,7 +48,19 @@ extern "C" {
 namespace otk {
 
 
-Display *display = (Display*) 0;
+Display *OBDisplay::display = (Display*) 0;
+bool OBDisplay::_xkb = false;
+int  OBDisplay::_xkb_event_basep = 0;
+bool OBDisplay::_shape = false;
+int  OBDisplay::_shape_event_basep = 0;
+bool OBDisplay::_xinerama = false;
+int  OBDisplay::_xinerama_event_basep = 0;
+unsigned int OBDisplay::_mask_list[8];
+unsigned int OBDisplay::_scrollLockMask = 0;
+unsigned int OBDisplay::_numLockMask = 0;
+OBDisplay::ScreenInfoList OBDisplay::_screenInfoList;
+BGCCache *OBDisplay::_gccache = (BGCCache*) 0;
+int OBDisplay::_grab_count = 0;
 
 
 int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e)
@@ -47,8 +68,13 @@ int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e)
 #ifdef DEBUG
   char errtxt[128];
 
-  XGetErrorText(d, e->error_code, errtxt, 128);
-  printf("X Error: %s\n", errtxt);
+  //if (e->error_code != BadWindow)
+  {
+    XGetErrorText(d, e->error_code, errtxt, 128);
+    printf("X Error: %s\n", errtxt);
+    if (e->error_code != BadWindow)
+      abort();
+  }
 #else
   (void)d;
   (void)e;
@@ -81,28 +107,24 @@ line argument.\n\n"));
   // set the DISPLAY environment variable for any lauched children, to the
   // display we're using, so they open in the right place.
   // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display);
-  if (putenv(const_cast<char*>((std::string("DISPLAY=") +
-                                DisplayString(display)).c_str()))) {
-    printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
-    perror("putenv()");
-  }
+  putenv(std::string("DISPLAY=") + DisplayString(display));
   
   // find the availability of X extensions we like to use
+#ifdef XKB
+  _xkb = XkbQueryExtension(display, &junk, &_xkb_event_basep, &junk, NULL, 
+                           NULL);
+#endif
+
 #ifdef SHAPE
   _shape = XShapeQueryExtension(display, &_shape_event_basep, &junk);
-#else
-  _shape = false;
 #endif
 
 #ifdef XINERAMA
   _xinerama = XineramaQueryExtension(display, &_xinerama_event_basep, &junk);
-#else
-  _xinerama = false;
 #endif // XINERAMA
 
   // get lock masks that are defined by the display (not constant)
   XModifierKeymap *modmap;
-  unsigned int NumLockMask = 0, ScrollLockMask = 0;
 
   modmap = XGetModifierMapping(display);
   if (modmap && modmap->max_keypermod > 0) {
@@ -122,9 +144,9 @@ line argument.\n\n"));
       if (! modmap->modifiermap[cnt]) continue;
 
       if (num_lock == modmap->modifiermap[cnt])
-        NumLockMask = mask_table[cnt / modmap->max_keypermod];
+        _numLockMask = mask_table[cnt / modmap->max_keypermod];
       if (scroll_lock == modmap->modifiermap[cnt])
-        ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
+        _scrollLockMask = mask_table[cnt / modmap->max_keypermod];
     }
   }
 
@@ -132,12 +154,12 @@ line argument.\n\n"));
 
   _mask_list[0] = 0;
   _mask_list[1] = LockMask;
-  _mask_list[2] = NumLockMask;
-  _mask_list[3] = LockMask | NumLockMask;
-  _mask_list[4] = ScrollLockMask;
-  _mask_list[5] = ScrollLockMask | LockMask;
-  _mask_list[6] = ScrollLockMask | NumLockMask;
-  _mask_list[7] = ScrollLockMask | LockMask | NumLockMask;
+  _mask_list[2] = _numLockMask;
+  _mask_list[3] = LockMask | _numLockMask;
+  _mask_list[4] = _scrollLockMask;
+  _mask_list[5] = _scrollLockMask | LockMask;
+  _mask_list[6] = _scrollLockMask | _numLockMask;
+  _mask_list[7] = _scrollLockMask | LockMask | _numLockMask;
 
   // Get information on all the screens which are available.
   _screenInfoList.reserve(ScreenCount(display));
@@ -151,8 +173,101 @@ line argument.\n\n"));
 void OBDisplay::destroy()
 {
   delete _gccache;
+  while (_grab_count > 0)
+    ungrab();
   XCloseDisplay(display);
 }
 
 
+const ScreenInfo* OBDisplay::screenInfo(int snum) {
+  assert(snum >= 0);
+  assert(snum < static_cast<int>(_screenInfoList.size()));
+  return &_screenInfoList[snum];
+}
+
+
+const ScreenInfo* OBDisplay::findScreen(Window root)
+{
+  ScreenInfoList::iterator it, end = _screenInfoList.end();
+  for (it = _screenInfoList.begin(); it != end; ++it)
+    if (it->rootWindow() == root)
+      return &(*it);
+  return 0;
+}
+
+
+void OBDisplay::grab()
+{
+  if (_grab_count == 0)
+    XGrabServer(display);
+  _grab_count++;
+}
+
+
+void OBDisplay::ungrab()
+{
+  if (_grab_count == 0) return;
+  _grab_count--;
+  if (_grab_count == 0)
+    XUngrabServer(display);
+}
+
+
+
+
+
+
+
+/*
+ * Grabs a button, but also grabs the button in every possible combination
+ * with the keyboard lock keys, so that they do not cancel out the event.
+
+ * if allow_scroll_lock is true then only the top half of the lock mask
+ * table is used and scroll lock is ignored.  This value defaults to false.
+ */
+void OBDisplay::grabButton(unsigned int button, unsigned int modifiers,
+                         Window grab_window, bool owner_events,
+                         unsigned int event_mask, int pointer_mode,
+                         int keyboard_mode, Window confine_to,
+                         Cursor cursor, bool allow_scroll_lock) {
+  unsigned int length = (allow_scroll_lock) ? 8 / 2:
+                                              8;
+  for (size_t cnt = 0; cnt < length; ++cnt)
+    XGrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
+                grab_window, owner_events, event_mask, pointer_mode,
+                keyboard_mode, confine_to, cursor);
+}
+
+
+/*
+ * Releases the grab on a button, and ungrabs all possible combinations of the
+ * keyboard lock keys.
+ */
+void OBDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
+                           Window grab_window) {
+  for (size_t cnt = 0; cnt < 8; ++cnt)
+    XUngrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
+                  grab_window);
+}
+
+void OBDisplay::grabKey(unsigned int keycode, unsigned int modifiers,
+                        Window grab_window, bool owner_events,
+                        int pointer_mode, int keyboard_mode,
+                        bool allow_scroll_lock)
+{
+  unsigned int length = (allow_scroll_lock) ? 8 / 2:
+                                              8;
+  for (size_t cnt = 0; cnt < length; ++cnt)
+    XGrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt],
+                grab_window, owner_events, pointer_mode, keyboard_mode);
+}
+
+void OBDisplay::ungrabKey(unsigned int keycode, unsigned int modifiers,
+                          Window grab_window)
+{
+  for (size_t cnt = 0; cnt < 8; ++cnt)
+    XUngrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt],
+               grab_window);
+}
+
 }
This page took 0.029851 seconds and 4 git commands to generate.