otk_c/screeninfo.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 |
// -*- mode: C; indent-tabs-mode: nil; c-basic-offset: 2; -*- #include "../config.h" #include "screeninfo.h" #include "display.h" #include "rect.h" #include <X11/Xutil.h> #ifdef HAVE_STRING_H # include <string.h> #endif #include "../src/gettext.h" extern PyTypeObject OtkScreenInfo_Type; PyObject *OtkScreenInfo_New(int num) { OtkScreenInfo* self; char *dstr, *dstr2; int i; self = PyObject_New(OtkScreenInfo, &OtkScreenInfo_Type); self->screen = num; self->root_window = RootWindow(OBDisplay->display, self->screen); self->rect = (OtkRect*) OtkRect_New(0, 0, WidthOfScreen(ScreenOfDisplay(OBDisplay->display, self->screen)), HeightOfScreen(ScreenOfDisplay(OBDisplay->display, self->screen))); /* If the default depth is at least 8 we will use that, otherwise we try to find the largest TrueColor visual. Preference is given to 24 bit over larger depths if 24 bit is an option. */ self->depth = DefaultDepth(OBDisplay->display, self->screen); self->visual = DefaultVisual(OBDisplay->display, self->screen); self->colormap = DefaultColormap(OBDisplay->display, self->screen); if (self->depth < 8) { // search for a TrueColor Visual... if we can't find one... // we will use the default visual for the screen XVisualInfo vinfo_template, *vinfo_return; int vinfo_nitems; int best = -1; vinfo_template.screen = self->screen; vinfo_template.class = TrueColor; vinfo_return = XGetVisualInfo(OBDisplay->display, VisualScreenMask | VisualClassMask, &vinfo_template, &vinfo_nitems); if (vinfo_return) { int max_depth = 1; for (i = 0; i < vinfo_nitems; ++i) { if (vinfo_return[i].depth > max_depth) { if (max_depth == 24 && vinfo_return[i].depth > 24) break; // prefer 24 bit over 32 max_depth = vinfo_return[i].depth; best = i; } } if (max_depth < self->depth) best = -1; } if (best != -1) { self->depth = vinfo_return[best].depth; self->visual = vinfo_return[best].visual; self->colormap = XCreateColormap(OBDisplay->display, self->root_window, self->visual, AllocNone); } XFree(vinfo_return); } // get the default display string and strip the screen number self->display_string = (PyStringObject*) PyString_FromFormat("DISPLAY=%s",DisplayString(OBDisplay->display)); dstr = PyString_AsString((PyObject*)self->display_string); dstr2 = strrchr(dstr, '.'); if (dstr2) { PyObject *str; _PyString_Resize((PyObject**)&self->display_string, dstr2 - dstr); str = PyString_FromFormat(".%d", self->screen); PyString_Concat((PyObject**)&self->display_string, str); } #ifdef XINERAMA self->xinerama_active = False; if (OtkDisplay->hasXineramaExtensions()) { if (OtkDisplay->getXineramaMajorVersion() == 1) { // we know the version 1(.1?) protocol /* in this version of Xinerama, we can't query on a per-screen basis, but in future versions we should be able, so the 'activeness' is checked on a pre-screen basis anyways. */ if (XineramaIsActive(OBDisplay->display)) { /* If Xinerama is being used, there there is only going to be one screen present. We still, of course, want to use the screen class, but that is why no screen number is used in this function call. There should never be more than one screen present with Xinerama active. */ int num; XineramaScreenInfo *info = XineramaQueryScreens(OBDisplay->display, &num); if (num > 0 && info) { self->xinerama_areas = PyList_New(num); for (i = 0; i < num; ++i) { PyList_SetItem(self->xinerama_areas, i, OtkRect_New(info[i].x_org, info[i].y_org, info[i].width, info[i].height)); } XFree(info); // if we can't find any xinerama regions, then we act as if it is not // active, even though it said it was self->xinerama_active = True; } } } } #endif // XINERAMA return (PyObject*)self; } static PyObject *otkscreeninfo_getscreen(OtkScreenInfo* self, PyObject* args) { if (!PyArg_ParseTuple(args, ":getScreen")) return NULL; return PyInt_FromLong(self->screen); } static OtkRect *otkscreeninfo_getrect(OtkScreenInfo* self, PyObject* args) { if (!PyArg_ParseTuple(args, ":getRect")) return NULL; return self->rect; } static PyMethodDef get_methods[] = { {"getScreen", (PyCFunction)otkscreeninfo_getscreen, METH_VARARGS, "Get the screen number."}, {"getRect", (PyCFunction)otkscreeninfo_getrect, METH_VARARGS, "Get the area taken up by the screen."}, {NULL, NULL, 0, NULL} }; static void otkscreeninfo_dealloc(OtkScreenInfo* self) { Py_DECREF(self->display_string); Py_DECREF(self->rect); #ifdef XINERAMA Py_DECREF(self->xinerama_areas); #endif PyObject_Del((PyObject*)self); } static PyObject *otkscreeninfo_getattr(PyObject *obj, char *name) { return Py_FindMethod(get_methods, obj, name); } PyTypeObject OtkScreenInfo_Type = { PyObject_HEAD_INIT(NULL) 0, "OtkScreenInfo", sizeof(OtkScreenInfo), 0, (destructor)otkscreeninfo_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ otkscreeninfo_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 */ }; |