]> Dogcows Code - chaz/openbox/blob - otk_c/display.c
rm consts that wont compile on some libfreetype's
[chaz/openbox] / otk_c / display.c
1 // -*- mode: C; indent-tabs-mode: nil; c-basic-offset: 2; -*-
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 = (PyListObject*)PyList_New(ScreenCount(self->display));
127 for (i = 0; i < ScreenCount(self->display); ++i)
128 PyList_SetItem((PyObject*)self->screenInfoList, i, OtkScreenInfo_New(i));
129
130 Py_INCREF(OBDisplay); // make sure it stays around!!
131 }
132
133 void OtkDisplay_Grab(OtkDisplay *self)
134 {
135 assert(self);
136 if (self->grab_count == 0)
137 XGrabServer(self->display);
138 self->grab_count++;
139 }
140
141 void OtkDisplay_Ungrab(OtkDisplay *self)
142 {
143 if (self->grab_count == 0) return;
144 self->grab_count--;
145 if (self->grab_count == 0)
146 XUngrabServer(self->display);
147 }
148
149 OtkScreenInfo *OtkDisplay_ScreenInfo(OtkDisplay *self, int num)
150 {
151 assert(num >= 0);
152 return (OtkScreenInfo*)PyList_GetItem((PyObject*)self->screenInfoList, num);
153 }
154
155
156 static PyObject *otkdisplay_grab(OtkDisplay* self, PyObject* args)
157 {
158 if (!PyArg_ParseTuple(args, ":grab"))
159 return NULL;
160 OtkDisplay_Grab(self);
161 return Py_None;
162 }
163
164 static PyObject *otkdisplay_ungrab(OtkDisplay* self, PyObject* args)
165 {
166 if (!PyArg_ParseTuple(args, ":ungrab"))
167 return NULL;
168 OtkDisplay_Ungrab(self);
169 return Py_None;
170 }
171
172 static PyMethodDef get_methods[] = {
173 {"grab", (PyCFunction)otkdisplay_grab, METH_VARARGS,
174 "Grab the X display."},
175 {"ungrab", (PyCFunction)otkdisplay_ungrab, METH_VARARGS,
176 "Ungrab/Release the X display."},
177 {NULL, NULL, 0, NULL}
178 };
179
180
181
182 static void otkdisplay_dealloc(PyObject* self)
183 {
184 XCloseDisplay(((OtkDisplay*) self)->display);
185 Py_DECREF(((OtkDisplay*) self)->screenInfoList);
186 PyObject_Del(self);
187 }
188
189 static PyObject *otkdisplay_getattr(PyObject *obj, char *name)
190 {
191 return Py_FindMethod(get_methods, obj, name);
192 }
193
194
195 PyTypeObject OtkDisplay_Type = {
196 PyObject_HEAD_INIT(NULL)
197 0,
198 "OtkDisplay",
199 sizeof(OtkDisplay),
200 0,
201 otkdisplay_dealloc, /*tp_dealloc*/
202 0, /*tp_print*/
203 otkdisplay_getattr, /*tp_getattr*/
204 0, /*tp_setattr*/
205 0, /*tp_compare*/
206 0, /*tp_repr*/
207 0, /*tp_as_number*/
208 0, /*tp_as_sequence*/
209 0, /*tp_as_mapping*/
210 0, /*tp_hash */
211 };
212
213
214 static int xerrorHandler(Display *d, XErrorEvent *e)
215 {
216 #ifdef DEBUG
217 char errtxt[128];
218
219 //if (e->error_code != BadWindow)
220 {
221 XGetErrorText(d, e->error_code, errtxt, 128);
222 printf("X Error: %s\n", errtxt);
223 }
224 #else
225 (void)d;
226 (void)e;
227 #endif
228
229 return False;
230 }
This page took 0.045245 seconds and 4 git commands to generate.