all repos — openbox @ f8a47de5ec444c452093371e3db16857eb39a490

openbox fork - make it a bit more like ryudo

c/screenwrap.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "screenwrap.h"
#include "openbox.h"
#include "screen.h"
#include "kbind.h"
#include "mbind.h"

ScreenWrap *screenwrap_instance;

/***************************************************************************
 
   Define the type 'ScreenWrap'

 ***************************************************************************/

#define IS_SWRAP(v)  ((v)->ob_type == &ScreenWrapType)
#define CHECK_SWRAP(self, funcname) { \
    if (!IS_SWRAP(self)) { \
        PyErr_SetString(PyExc_TypeError, \
			"descriptor '" funcname "'  a 'Screen' object"); \
	return NULL; \
    } \
}

staticforward PyTypeObject ScreenWrapType;

/***************************************************************************
 
   Attribute methods
 
 ***************************************************************************/

static PyObject *swrap_number(ScreenWrap *self, PyObject *args)
{
    CHECK_SWRAP(self, "number");
    if (!PyArg_ParseTuple(args, ":number"))
	return NULL;
    return PyInt_FromLong(ob_screen);
}

static PyObject *swrap_rootWindow(ScreenWrap *self, PyObject *args)
{
    CHECK_SWRAP(self, "rootWindow");
    if (!PyArg_ParseTuple(args, ":rootWindow"))
	return NULL;
    return PyInt_FromLong(ob_root);
}

static PyObject *swrap_state(ScreenWrap *self, PyObject *args)
{
    CHECK_SWRAP(self, "state");
    if (!PyArg_ParseTuple(args, ":state"))
	return NULL;
    return PyInt_FromLong(ob_state);
}

static PyObject *swrap_numDesktops(ScreenWrap *self, PyObject *args)
{
    CHECK_SWRAP(self, "numDesktops");
    if (!PyArg_ParseTuple(args, ":numDesktops"))
	return NULL;
    return PyInt_FromLong(screen_num_desktops);
}

static PyObject *swrap_desktop(ScreenWrap *self, PyObject *args)
{
    CHECK_SWRAP(self, "desktop");
    if (!PyArg_ParseTuple(args, ":desktop"))
	return NULL;
    return PyInt_FromLong(screen_desktop);
}

static PyObject *swrap_physicalSize(ScreenWrap *self, PyObject *args)
{
    PyObject *tuple;

    CHECK_SWRAP(self, "physicalSize");
    if (!PyArg_ParseTuple(args, ":physicalSize"))
	return NULL;
    tuple = PyTuple_New(2);
    PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(screen_physical_size.width));
    PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(screen_physical_size.height));
    return tuple;
}

static PyObject *swrap_showingDesktop(ScreenWrap *self, PyObject *args)
{
    CHECK_SWRAP(self, "showingDesktop");
    if (!PyArg_ParseTuple(args, ":showingDesktop"))
	return NULL;
    return PyInt_FromLong(screen_showing_desktop ? 1 : 0);
}

static PyObject *swrap_desktopLayout(ScreenWrap *self, PyObject *args)
{
    PyObject *tuple;

    CHECK_SWRAP(self, "desktopLayout");
    if (!PyArg_ParseTuple(args, ":desktopLayout"))
	return NULL;
    tuple = PyTuple_New(4);
    PyTuple_SET_ITEM(tuple, 0,
		     PyInt_FromLong(screen_desktop_layout.orientation));
    PyTuple_SET_ITEM(tuple, 1,
		     PyInt_FromLong(screen_desktop_layout.start_corner));
    PyTuple_SET_ITEM(tuple, 2,
		     PyInt_FromLong(screen_desktop_layout.rows));
    PyTuple_SET_ITEM(tuple, 3,
		     PyInt_FromLong(screen_desktop_layout.columns));
    return tuple;
}

static PyObject *swrap_desktopNames(ScreenWrap *self, PyObject *args)
{
    PyObject *list;
    guint s, i;

    CHECK_SWRAP(self, "desktopNames");
    if (!PyArg_ParseTuple(args, ":desktopNames"))
	return NULL;
    s = screen_desktop_names->len;
    list = PyList_New(s);
    for (i = 0; i < s; ++i)
	PyList_SET_ITEM(list, i, PyString_FromString
			(g_ptr_array_index(screen_desktop_names, i)));
    return list;
}

static PyObject *swrap_area(ScreenWrap *self, PyObject *args)
{
    PyObject * tuple;
    Rect *r;
    guint i;

    CHECK_SWRAP(self, "area");
    if (!PyArg_ParseTuple(args, "i:area", &i))
	return NULL;
    r = screen_area(i);
    if (r == NULL) {
	PyErr_SetString(PyExc_IndexError,
			"the requested desktop was not valid");
	return NULL;
    }
    tuple = PyTuple_New(4);
    PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(r->x));
    PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(r->y));
    PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(r->width));
    PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(r->height));
    return tuple;
}

static PyObject *swrap_strut(ScreenWrap *self, PyObject *args)
{
    PyObject *tuple;
    Strut *s;
    guint i;

    CHECK_SWRAP(self, "strut");
    if (!PyArg_ParseTuple(args, "i:strut", &i))
	return NULL;
    s = screen_strut(i);
    if (s == NULL) {
	PyErr_SetString(PyExc_IndexError,
			"the requested desktop was not valid");
	return NULL;
    }
    tuple = PyTuple_New(4);
    PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(s->left));
    PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(s->top));
    PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(s->right));
    PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(s->bottom));
    return tuple;
}

static PyObject *swrap_grabKey(ScreenWrap *self, PyObject *args)
{
    PyObject *item, *tuple;
    GList *keylist = NULL, *it;
    int i, s;
    gboolean grab = FALSE;

    CHECK_SWRAP(self, "grabKey");
    if (!PyArg_ParseTuple(args, "O:grabKey", &tuple))
	return NULL;

    if (PyTuple_Check(tuple)) {
	s = PyTuple_GET_SIZE(tuple);
	if (s > 0) {
	    for (i = 0; i < s; ++i) {
		item = PyTuple_GET_ITEM(tuple, i);
		if (!PyString_Check(item))
		    break;
		keylist = g_list_append(keylist,
					g_strdup(PyString_AsString(item)));
	    }
	    if (i == s)
		grab = kbind_add(keylist);

	    for (it = keylist; it != NULL; it = it->next)
		g_free(it->data);
	    g_list_free(it);

	    if (grab) {
		Py_INCREF(Py_None);
		return Py_None;
	    } else {
		PyErr_SetString(PyExc_ValueError,
				"the key could not be grabbed");
		return NULL;
	    }
	}
    }

    PyErr_SetString(PyExc_TypeError, "expected a tuple of strings");
    return NULL;
}

static PyObject *swrap_clearKeyGrabs(ScreenWrap *self, PyObject *args)
{
    CHECK_SWRAP(self, "clearKeyGrabs");
    if (!PyArg_ParseTuple(args, ":clearKeyGrabs"))
	return NULL;
    kbind_clearall();
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *swrap_grabKeyboard(ScreenWrap *self, PyObject *args)
{
    int grab;

    CHECK_SWRAP(self, "grabKeyboard");
    if (!PyArg_ParseTuple(args, "i:grabKeyboard", &grab))
	return NULL;
    if (!kbind_grab_keyboard(grab)) {
	PyErr_SetString(PyExc_RuntimeError, "failed to grab keyboard");
	return NULL;
    }
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *swrap_grabButton(ScreenWrap *self, PyObject *args)
{
    char *name;
    char *context_str;
    GQuark context;

    CHECK_SWRAP(self, "grabButton");
    if (!PyArg_ParseTuple(args, "ss:grabKey", &name, &context_str))
	return NULL;

    context = g_quark_try_string(context_str);

    if (!context) {
	PyErr_SetString(PyExc_ValueError, "invalid context");
	return NULL;
    }

    if (mbind_add(name, context)) {
	Py_INCREF(Py_None);
	return Py_None;
    } else {
	PyErr_SetString(PyExc_ValueError,
			"the button could not be grabbed");
	return NULL;
    }
}

static PyObject *swrap_clearButtonGrabs(ScreenWrap *self, PyObject *args)
{
    CHECK_SWRAP(self, "clearButtonGrabs");
    if (!PyArg_ParseTuple(args, ":clearButtonGrabs"))
	return NULL;
    mbind_clearall();
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *swrap_grabPointer(ScreenWrap *self, PyObject *args)
{
    int grab;

    CHECK_SWRAP(self, "grabPointer");
    if (!PyArg_ParseTuple(args, "i:grabPointer", &grab))
	return NULL;
    if (!mbind_grab_pointer(grab)) {
	PyErr_SetString(PyExc_RuntimeError, "failed to grab pointer");
	return NULL;
    }
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef ScreenWrapAttributeMethods[] = {
    {"number", (PyCFunction)swrap_number, METH_VARARGS,
     "Screen.number() -- Returns the number of the screen on the X server on "
     "which this Openbox instance is running."},
    {"rootWindow", (PyCFunction)swrap_rootWindow, METH_VARARGS,
     "Screen.rootWindow() -- Returns the window id of the root window."},
    {"state", (PyCFunction)swrap_state, METH_VARARGS,
     "Screen.state() -- Returns the running state of Openbox. One of the "
     "ob.State_ constants."},
    {"numDesktops", (PyCFunction)swrap_numDesktops, METH_VARARGS,
     "Screen.numDesktops() -- Returns the number of desktops available."},
    {"desktop", (PyCFunction)swrap_desktop, METH_VARARGS,
     "Screen.desktop() -- Returns the currently visible desktop."},
    {"physicalSize", (PyCFunction)swrap_physicalSize, METH_VARARGS,
     "Screen.physicalSize() -- Returns the physical size (in pixels) of the "
     "display in a tuple. The tuple is formatted as (width, height)."},
    {"showingDesktop", (PyCFunction)swrap_showingDesktop, METH_VARARGS,
     "Screen.showingDesktop() -- Returns if in showing-the-desktop mode or "
     "not."},
    {"desktopNames", (PyCFunction)swrap_desktopNames, METH_VARARGS,
     "Screen.desktopNames() -- Returns a list of the names of all the "
     "desktops, and possibly for desktops beyond those. The names are encoded "
     "as UTF-8."},
    {"desktopLayout", (PyCFunction)swrap_desktopLayout, METH_VARARGS,
     "Screen.desktopLayout() -- Returns the layout of the desktops, as "
     "specified by a compliant pager, in a tuple. The format of the tuple is "
     "(orientation, corner, rows, columns). Where, orientation is one of the "
     "ob.Orientation_ constants, corner is one of the ob.Corner_ constants, "
     "and rows and columns specify the size of the layout. The rows and "
     "columns will always include all the desktops."},
    {"area", (PyCFunction)swrap_area, METH_VARARGS,
     "Screen.area(d) -- Returns the usuable area on the Screen for a desktop, "
     "in the form of a tuple. The tuples format is (x, y, width, height). The "
     "desktop must be in the range of desktops on the screen, or 0xffffffff "
     "to get a combined area for 'all desktops'."},
    {"strut", (PyCFunction)swrap_area, METH_VARARGS,
     "Screen.strut(d) -- Returns the combined strut of all clients on a "
     "desktop, in the form of a tuple. The tuples format is "
     "(left, top, right, bottom). The desktop must be in the range of "
     "desktops on the screen, or 0xffffffff to get a combined strut for "
     "'all desktops'."},
    {"grabKey", (PyCFunction)swrap_grabKey, METH_VARARGS,
     "Screen.grabKey(('Mod1-C-a', 'd')) -- Grabs a key chain so that key "
     "events for it will occur. The argument must be a tuple of one or "
     "more elements. Each key element is made up of "
     "Modifier-Modifier-...-Key, where Modifier is one of Mod1, Mod2, "
     "Mod3, Mod4, Mod5, S (for Shift), or C (for Control)."},
    {"clearKeyGrabs", (PyCFunction)swrap_clearKeyGrabs, METH_VARARGS,
     "Screen.clearKeyGrabs() -- Removes all key grabs that have been done "
     "with grabKey()."},
    {"grabKeyboard", (PyCFunction)swrap_grabKeyboard, METH_VARARGS,
     "Screen.grabKeyboard(grab) -- Grabs or ungrabs the entire keyboard. When "
     "the keyboard is grabbed, all key presses will be sent to the "
     "hooks.keyboard hook. (grabbed keys will go to the hooks.events hook "
     "too. "},
    {"grabButton", (PyCFunction)swrap_grabButton, METH_VARARGS,
     "Screen.grabButton('C-1', \"frame\") -- Grabs a pointer button "
     "for the given context. The context must be one of the ob.Context_* "
     "constants. The button definition is made up of "
     "Modifier-Modifier-...-Button, where Modifier is one of Mod1, Mod2, "
     "Mod3, Mod4, Mod5, S (for Shift), or C (for Control)."},
    {"clearButtonGrabs", (PyCFunction)swrap_clearButtonGrabs, METH_VARARGS,
     "Screen.clearButtonGrabs() -- Removes all button grabs that have been "
     "done with grabButton()."},
    {"grabPointer", (PyCFunction)swrap_grabPointer, METH_VARARGS,
     "grabPointer(grab) -- Grabs or ungrabs the pointer device. When the "
     "pointer is grabbed, all pointer events will be sent to the "
     "hooks.pointer  hook. (grabbed buttons will NOT go to the hooks.events "
     "hook while the pointer is grabbed)."},
    {NULL, NULL, 0, NULL}
};

/***************************************************************************
 
   Type methods/struct
 
 ***************************************************************************/

static PyObject *swrap_getattr(ScreenWrap *self, char *name)
{
    CHECK_SWRAP(self, "getattr");
    return Py_FindMethod(ScreenWrapAttributeMethods, (PyObject*)self, name);
}

static void swrap_dealloc(ScreenWrap *self)
{
    PyObject_Del((PyObject*) self);
}

static PyTypeObject ScreenWrapType = {
    PyObject_HEAD_INIT(NULL)
    0,
    "Screen",
    sizeof(ScreenWrap),
    0,
    (destructor) swrap_dealloc,     /*tp_dealloc*/
    0,                              /*tp_print*/
    (getattrfunc) swrap_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 */
};

/***************************************************************************
 
   External methods
 
 ***************************************************************************/

void screenwrap_startup()
{
    PyObject *ob, *obdict;
    ScreenWrap *swrap;

    ScreenWrapType.ob_type = &PyType_Type;
    ScreenWrapType.tp_doc = "Wraps information and functionality global to an "
	"instance of Openbox.";
    PyType_Ready(&ScreenWrapType);
    swrap = PyObject_New(ScreenWrap, &ScreenWrapType);

    /* get the ob module/dict */
    ob = PyImport_ImportModule("ob"); /* new */
    g_assert(ob != NULL);
    obdict = PyModule_GetDict(ob); /* borrowed */
    g_assert(obdict != NULL);

    PyDict_SetItemString(obdict, "Screen", (PyObject*)swrap);
    Py_DECREF(swrap);
    Py_DECREF(ob);
}

void screenwrap_shutdown()
{
}