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