all repos — openbox @ 0820c5100d7ea8daed00e7b1bf183edc9414728f

openbox fork - make it a bit more like ryudo

otk_c/display.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// -*- mode: C; indent-tabs-mode: nil; -*-

#include "../config.h"
#include "display.h"
#include "screeninfo.h"

#include <X11/keysym.h>

#ifdef    SHAPE
#include <X11/extensions/shape.h>
#endif // SHAPE

#ifdef    HAVE_STDIO_H
#  include <stdio.h>
#endif // HAVE_STDIO_H

#ifdef    HAVE_STDLIB_H
#  include <stdlib.h>
#endif // HAVE_STDLIB_H

#ifdef    HAVE_FCNTL_H
#  include <fcntl.h>
#endif // HAVE_FCNTL_H

#ifdef    HAVE_UNISTD_H
#  include <sys/types.h>
#  include <unistd.h>
#endif // HAVE_UNISTD_H

#include "../src/gettext.h"

extern PyTypeObject OtkDisplay_Type;

static int xerrorHandler(Display *d, XErrorEvent *e);

struct OtkDisplay *OBDisplay = NULL;

void OtkDisplay_Initialize(char *name)
{
  OtkDisplay* self;
  PyObject *disp_env;
  XModifierKeymap *modmap;
  unsigned int NumLockMask = 0, ScrollLockMask = 0;
  size_t cnt;
  int i;
  int junk;
  (void) junk;

  self = PyObject_New(OtkDisplay, &OtkDisplay_Type);

  // Open the X display
  if (!(self->display = XOpenDisplay(name))) {
    printf(_("Unable to open connection to the X server. Please set the \n\
DISPLAY environment variable approriately, or use the '-display' command \n\
line argument.\n\n"));
    exit(1);
  }
  if (fcntl(ConnectionNumber(self->display), F_SETFD, 1) == -1) {
    printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
    exit(1);
  }

  XSetErrorHandler(xerrorHandler);

  // set the DISPLAY environment variable for any lauched children, to the
  // display we're using, so they open in the right place.
  disp_env = PyString_FromFormat("DISPLAY=%s", DisplayString(self->display));
  if (putenv(PyString_AsString(disp_env))) {
    printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
    perror("putenv()");
  }
  Py_DECREF(disp_env);
  
  // find the availability of X extensions we like to use
#ifdef SHAPE
  self->shape = XShapeQueryExtension(self->display,
				     &self->shape_event_basep, &junk);
#endif

#ifdef XINERAMA
  self->xinerama = XineramaQueryExtension(self->display,
					  &self->xinerama_event_basep, &junk);
#endif // XINERAMA

  // get lock masks that are defined by the display (not constant)
  modmap = XGetModifierMapping(self->display);
  if (modmap && modmap->max_keypermod > 0) {
    const int mask_table[] = {
      ShiftMask, LockMask, ControlMask, Mod1Mask,
      Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
    };
    const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
      modmap->max_keypermod;
    // get the values of the keyboard lock modifiers
    // Note: Caps lock is not retrieved the same way as Scroll and Num lock
    // since it doesn't need to be.
    const KeyCode num_lock = XKeysymToKeycode(self->display, XK_Num_Lock);
    const KeyCode scroll_lock = XKeysymToKeycode(self->display,
						 XK_Scroll_Lock);

    for (cnt = 0; cnt < size; ++cnt) {
      if (! modmap->modifiermap[cnt]) continue;

      if (num_lock == modmap->modifiermap[cnt])
        NumLockMask = mask_table[cnt / modmap->max_keypermod];
      if (scroll_lock == modmap->modifiermap[cnt])
        ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
    }
  }

  if (modmap) XFreeModifiermap(modmap);

  self->mask_list[0] = 0;
  self->mask_list[1] = LockMask;
  self->mask_list[2] = NumLockMask;
  self->mask_list[3] = LockMask | NumLockMask;
  self->mask_list[4] = ScrollLockMask;
  self->mask_list[5] = ScrollLockMask | LockMask;
  self->mask_list[6] = ScrollLockMask | NumLockMask;
  self->mask_list[7] = ScrollLockMask | LockMask | NumLockMask;

  // set the global var, for the new screeninfo's
  OBDisplay = self;
  
  // Get information on all the screens which are available.
  self->screenInfoList = (PyListObject*)PyList_New(ScreenCount(self->display));
  for (i = 0; i < ScreenCount(self->display); ++i)
    PyList_SetItem((PyObject*)self->screenInfoList, i, OtkScreenInfo_New(i));

  Py_INCREF(OBDisplay); // make sure it stays around!!
}

void OtkDisplay_Grab(OtkDisplay *self)
{
  assert(self);
  if (self->grab_count == 0)
    XGrabServer(self->display);
  self->grab_count++;
}

void OtkDisplay_Ungrab(OtkDisplay *self)
{
  if (self->grab_count == 0) return;
  self->grab_count--;
  if (self->grab_count == 0)
    XUngrabServer(self->display);
}

OtkScreenInfo *OtkDisplay_ScreenInfo(OtkDisplay *self, int num)
{
  PyObject *py = PyList_GetItem((PyObject*)self->screenInfoList, num);
  return (OtkScreenInfo*) py;
}


static PyObject *otkdisplay_grab(OtkDisplay* self, PyObject* args)
{
  if (!PyArg_ParseTuple(args, ":grab"))
    return NULL;
  OtkDisplay_Grab(self);
  return Py_None;
}

static PyObject *otkdisplay_ungrab(OtkDisplay* self, PyObject* args)
{
  if (!PyArg_ParseTuple(args, ":ungrab"))
    return NULL;
  OtkDisplay_Ungrab(self);
  return Py_None;
}

static PyMethodDef get_methods[] = {
  {"grab", (PyCFunction)otkdisplay_grab, METH_VARARGS,
   "Grab the X display."},
  {"ungrab", (PyCFunction)otkdisplay_ungrab, METH_VARARGS,
   "Ungrab/Release the X display."},
  {NULL, NULL, 0, NULL}
};



static void otkdisplay_dealloc(PyObject* self)
{
  XCloseDisplay(((OtkDisplay*) self)->display);
  Py_DECREF(((OtkDisplay*) self)->screenInfoList);
  PyObject_Del(self);
}

static PyObject *otkdisplay_getattr(PyObject *obj, char *name)
{
  return Py_FindMethod(get_methods, obj, name);
}


PyTypeObject OtkDisplay_Type = {
  PyObject_HEAD_INIT(NULL)
  0,
  "OtkDisplay",
  sizeof(OtkDisplay),
  0,
  otkdisplay_dealloc, /*tp_dealloc*/
  0,          /*tp_print*/
  otkdisplay_getattr, /*tp_getattr*/
  0,          /*tp_setattr*/
  0,          /*tp_compare*/
  0,          /*tp_repr*/
  0,          /*tp_as_number*/
  0,          /*tp_as_sequence*/
  0,          /*tp_as_mapping*/
  0,          /*tp_hash */
};


static int xerrorHandler(Display *d, XErrorEvent *e)
{
#ifdef DEBUG
  char errtxt[128];

  //if (e->error_code != BadWindow)
  {
    XGetErrorText(d, e->error_code, errtxt, 128);
    printf("X Error: %s\n", errtxt);
  }
#else
  (void)d;
  (void)e;
#endif

  return False;
}