]> icculus.org git repositories - dana/openbox.git/blob - otk/display.cc
add an OBDisplay class and the old ScreenInfo class to the toolkit.
[dana/openbox.git] / otk / display.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "display.hh"
8 #include "screeninfo.hh"
9 #include "gccache.hh"
10
11 extern "C" {
12 #include <X11/keysym.h>
13
14 #ifdef    HAVE_STDIO_H
15 #  include <stdio.h>
16 #endif // HAVE_STDIO_H
17
18 #ifdef    HAVE_STDLIB_H
19 #  include <stdlib.h>
20 #endif // HAVE_STDLIB_H
21
22 #ifdef    HAVE_SIGNAL_H
23 #  include <signal.h>
24 #endif // HAVE_SIGNAL_H
25
26 #ifdef    HAVE_FCNTL_H
27 #  include <fcntl.h>
28 #endif // HAVE_FCNTL_H
29
30 #ifdef    HAVE_UNISTD_H
31 #  include <sys/types.h>
32 #  include <unistd.h>
33 #endif // HAVE_UNISTD_H
34
35 #include "gettext.h"
36 #define _(str) gettext(str)
37 }
38
39 namespace otk {
40
41
42 Display *display = (Display*) 0;
43
44
45 int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e)
46 {
47 #ifdef DEBUG
48   char errtxt[128];
49
50   XGetErrorText(d, e->error_code, errtxt, 128);
51   printf("X Error: %s\n", errtxt);
52 #else
53   (void)d;
54   (void)e;
55 #endif
56
57   return false;
58 }
59
60
61 void OBDisplay::initialize(char *name)
62 {
63   int junk;
64   (void)junk;
65
66   // Open the X display
67   if (!(display = XOpenDisplay(name))) {
68     printf(_("Unable to open connection to the X server. Please set the \n\
69 DISPLAY environment variable approriately, or use the '-display' command \n\
70 line argument.\n\n"));
71     ::exit(1);
72   }
73   if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
74     printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
75     ::exit(1);
76   }
77
78   // set our error handler for X errors
79   XSetErrorHandler(xerrorHandler);
80
81   // set the DISPLAY environment variable for any lauched children, to the
82   // display we're using, so they open in the right place.
83   // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display);
84   if (putenv(const_cast<char*>((std::string("DISPLAY=") +
85                                 DisplayString(display)).c_str()))) {
86     printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
87     perror("putenv()");
88   }
89   
90   // find the availability of X extensions we like to use
91 #ifdef SHAPE
92   _shape = XShapeQueryExtension(display, &_shape_event_basep, &junk);
93 #else
94   _shape = false;
95 #endif
96
97 #ifdef XINERAMA
98   _xinerama = XineramaQueryExtension(display, &_xinerama_event_basep, &junk);
99 #else
100   _xinerama = false;
101 #endif // XINERAMA
102
103   // get lock masks that are defined by the display (not constant)
104   XModifierKeymap *modmap;
105   unsigned int NumLockMask = 0, ScrollLockMask = 0;
106
107   modmap = XGetModifierMapping(display);
108   if (modmap && modmap->max_keypermod > 0) {
109     const int mask_table[] = {
110       ShiftMask, LockMask, ControlMask, Mod1Mask,
111       Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
112     };
113     const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
114       modmap->max_keypermod;
115     // get the values of the keyboard lock modifiers
116     // Note: Caps lock is not retrieved the same way as Scroll and Num lock
117     // since it doesn't need to be.
118     const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock);
119     const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock);
120
121     for (size_t cnt = 0; cnt < size; ++cnt) {
122       if (! modmap->modifiermap[cnt]) continue;
123
124       if (num_lock == modmap->modifiermap[cnt])
125         NumLockMask = mask_table[cnt / modmap->max_keypermod];
126       if (scroll_lock == modmap->modifiermap[cnt])
127         ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
128     }
129   }
130
131   if (modmap) XFreeModifiermap(modmap);
132
133   _mask_list[0] = 0;
134   _mask_list[1] = LockMask;
135   _mask_list[2] = NumLockMask;
136   _mask_list[3] = LockMask | NumLockMask;
137   _mask_list[4] = ScrollLockMask;
138   _mask_list[5] = ScrollLockMask | LockMask;
139   _mask_list[6] = ScrollLockMask | NumLockMask;
140   _mask_list[7] = ScrollLockMask | LockMask | NumLockMask;
141
142   // Get information on all the screens which are available.
143   _screenInfoList.reserve(ScreenCount(display));
144   for (int i = 0; i < ScreenCount(display); ++i)
145     _screenInfoList.push_back(ScreenInfo(i));
146
147   _gccache = new BGCCache(_screenInfoList.size());
148 }
149
150
151 void OBDisplay::destroy()
152 {
153   delete _gccache;
154   XCloseDisplay(display);
155 }
156
157
158 }