]> icculus.org git repositories - dana/openbox.git/blob - otk_c/display.c
a color cache to be proud of!
[dana/openbox.git] / otk_c / display.c
1 // -*- mode: C; indent-tabs-mode: nil; -*-
2
3 #include "../config.h"
4 #include "display.h"
5 #include "screeninfo.h"
6
7 #include <X11/keysym.h>
8
9 #ifdef    SHAPE
10 #include <X11/extensions/shape.h>
11 #endif // SHAPE
12
13 #ifdef    HAVE_STDIO_H
14 #  include <stdio.h>
15 #endif // HAVE_STDIO_H
16
17 #ifdef    HAVE_STDLIB_H
18 #  include <stdlib.h>
19 #endif // HAVE_STDLIB_H
20
21 #ifdef    HAVE_FCNTL_H
22 #  include <fcntl.h>
23 #endif // HAVE_FCNTL_H
24
25 #ifdef    HAVE_UNISTD_H
26 #  include <sys/types.h>
27 #  include <unistd.h>
28 #endif // HAVE_UNISTD_H
29
30 #include "../src/gettext.h"
31
32 extern PyTypeObject OtkDisplay_Type;
33
34 static int xerrorHandler(Display *d, XErrorEvent *e);
35
36 struct OtkDisplay *OBDisplay = NULL;
37
38 void OtkDisplay_Initialize(char *name)
39 {
40   OtkDisplay* self;
41   PyObject *disp_env;
42   XModifierKeymap *modmap;
43   unsigned int NumLockMask = 0, ScrollLockMask = 0;
44   size_t cnt;
45   int i;
46   int junk;
47   (void) junk;
48
49   self = PyObject_New(OtkDisplay, &OtkDisplay_Type);
50
51   // Open the X display
52   if (!(self->display = XOpenDisplay(name))) {
53     printf(_("Unable to open connection to the X server. Please set the \n\
54 DISPLAY environment variable approriately, or use the '-display' command \n\
55 line argument.\n\n"));
56     exit(1);
57   }
58   if (fcntl(ConnectionNumber(self->display), F_SETFD, 1) == -1) {
59     printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
60     exit(1);
61   }
62
63   XSetErrorHandler(xerrorHandler);
64
65   // set the DISPLAY environment variable for any lauched children, to the
66   // display we're using, so they open in the right place.
67   disp_env = PyString_FromFormat("DISPLAY=%s", DisplayString(self->display));
68   if (putenv(PyString_AsString(disp_env))) {
69     printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
70     perror("putenv()");
71   }
72   Py_DECREF(disp_env);
73   
74   // find the availability of X extensions we like to use
75 #ifdef SHAPE
76   self->shape = XShapeQueryExtension(self->display,
77                                      &self->shape_event_basep, &junk);
78 #endif
79
80 #ifdef XINERAMA
81   self->xinerama = XineramaQueryExtension(self->display,
82                                           &self->xinerama_event_basep, &junk);
83 #endif // XINERAMA
84
85   // get lock masks that are defined by the display (not constant)
86   modmap = XGetModifierMapping(self->display);
87   if (modmap && modmap->max_keypermod > 0) {
88     const int mask_table[] = {
89       ShiftMask, LockMask, ControlMask, Mod1Mask,
90       Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
91     };
92     const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
93       modmap->max_keypermod;
94     // get the values of the keyboard lock modifiers
95     // Note: Caps lock is not retrieved the same way as Scroll and Num lock
96     // since it doesn't need to be.
97     const KeyCode num_lock = XKeysymToKeycode(self->display, XK_Num_Lock);
98     const KeyCode scroll_lock = XKeysymToKeycode(self->display,
99                                                  XK_Scroll_Lock);
100
101     for (cnt = 0; cnt < size; ++cnt) {
102       if (! modmap->modifiermap[cnt]) continue;
103
104       if (num_lock == modmap->modifiermap[cnt])
105         NumLockMask = mask_table[cnt / modmap->max_keypermod];
106       if (scroll_lock == modmap->modifiermap[cnt])
107         ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
108     }
109   }
110
111   if (modmap) XFreeModifiermap(modmap);
112
113   self->mask_list[0] = 0;
114   self->mask_list[1] = LockMask;
115   self->mask_list[2] = NumLockMask;
116   self->mask_list[3] = LockMask | NumLockMask;
117   self->mask_list[4] = ScrollLockMask;
118   self->mask_list[5] = ScrollLockMask | LockMask;
119   self->mask_list[6] = ScrollLockMask | NumLockMask;
120   self->mask_list[7] = ScrollLockMask | LockMask | NumLockMask;
121
122   // set the global var, for the new screeninfo's
123   OBDisplay = self;
124   
125   // Get information on all the screens which are available.
126   self->screenInfoList = PyList_New(ScreenCount(self->display));
127   for (i = 0; i < ScreenCount(self->display); ++i)
128     PyList_SetItem(self->screenInfoList, i, OtkScreenInfo_New(i));
129 }
130
131 void OtkDisplay_Grab(OtkDisplay *self)
132 {
133   assert(self);
134   if (self->grab_count == 0)
135     XGrabServer(self->display);
136   self->grab_count++;
137 }
138
139 void OtkDisplay_Ungrab(OtkDisplay *self)
140 {
141   if (self->grab_count == 0) return;
142   self->grab_count--;
143   if (self->grab_count == 0)
144     XUngrabServer(self->display);
145 }
146
147 OtkScreenInfo *OtkDisplay_ScreenInfo(OtkDisplay *self, int num)
148 {
149   PyObject *py = PyList_GetItem(self->screenInfoList, num);
150   return (OtkScreenInfo*) py;
151 }
152
153
154 static PyObject *otkdisplay_grab(OtkDisplay* self, PyObject* args)
155 {
156   if (!PyArg_ParseTuple(args, ":grab"))
157     return NULL;
158   OtkDisplay_Grab(self);
159   return Py_None;
160 }
161
162 static PyObject *otkdisplay_ungrab(OtkDisplay* self, PyObject* args)
163 {
164   if (!PyArg_ParseTuple(args, ":ungrab"))
165     return NULL;
166   OtkDisplay_Ungrab(self);
167   return Py_None;
168 }
169
170 static PyMethodDef get_methods[] = {
171   {"grab", (PyCFunction)otkdisplay_grab, METH_VARARGS,
172    "Grab the X display."},
173   {"ungrab", (PyCFunction)otkdisplay_ungrab, METH_VARARGS,
174    "Ungrab/Release the X display."},
175   {NULL, NULL, 0, NULL}
176 };
177
178
179
180 static void otkdisplay_dealloc(PyObject* self)
181 {
182   XCloseDisplay(((OtkDisplay*) self)->display);
183   Py_DECREF(((OtkDisplay*) self)->screenInfoList);
184   PyObject_Del(self);
185 }
186
187 static PyObject *otkdisplay_getattr(PyObject *obj, char *name)
188 {
189   return Py_FindMethod(get_methods, obj, name);
190 }
191
192
193 PyTypeObject OtkDisplay_Type = {
194   PyObject_HEAD_INIT(NULL)
195   0,
196   "OtkDisplay",
197   sizeof(OtkDisplay),
198   0,
199   otkdisplay_dealloc, /*tp_dealloc*/
200   0,          /*tp_print*/
201   otkdisplay_getattr, /*tp_getattr*/
202   0,          /*tp_setattr*/
203   0,          /*tp_compare*/
204   0,          /*tp_repr*/
205   0,          /*tp_as_number*/
206   0,          /*tp_as_sequence*/
207   0,          /*tp_as_mapping*/
208   0,          /*tp_hash */
209 };
210
211
212 static int xerrorHandler(Display *d, XErrorEvent *e)
213 {
214 #ifdef DEBUG
215   char errtxt[128];
216
217   //if (e->error_code != BadWindow)
218   {
219     XGetErrorText(d, e->error_code, errtxt, 128);
220     printf("X Error: %s\n", errtxt);
221   }
222 #else
223   (void)d;
224   (void)e;
225 #endif
226
227   return False;
228 }