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