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
|
/*
* utils_x11.c - X11 utilities
*
* xvba-video (C) 2009-2011 Splitted-Desktop Systems
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "sysdeps.h"
#include <X11/Xutil.h>
#include "utils_x11.h"
#include "utils.h"
// X error trap
static int x11_error_code = 0;
static int (*old_error_handler)(Display *, XErrorEvent *);
static int error_handler(Display *dpy, XErrorEvent *error)
{
x11_error_code = error->error_code;
return 0;
}
void x11_trap_errors(void)
{
x11_error_code = 0;
old_error_handler = XSetErrorHandler(error_handler);
}
int x11_untrap_errors(void)
{
XSetErrorHandler(old_error_handler);
return x11_error_code;
}
// X window management
static const int x11_event_mask = (KeyPressMask |
KeyReleaseMask |
ButtonPressMask |
ButtonReleaseMask |
PointerMotionMask |
EnterWindowMask |
ExposureMask |
StructureNotifyMask);
/**
* x11_create_window:
* @dpy: an X11 #Display
* @w: the requested width, in pixels
* @h: the requested height, in pixels
* @vis: the request visual
* @cmap: the request colormap
*
* Creates a border-less window with the specified dimensions. If @vis
* is %NULL, the default visual for @display will be used. If @cmap is
* %None, no specific colormap will be bound to the window. Also note
* the default background color is black.
*
* Return value: the newly created X #Window.
*/
Window
x11_create_window(
Display *dpy,
unsigned int w,
unsigned int h,
Visual *vis,
Colormap cmap
)
{
Window rootwin, win;
int screen, depth;
XSetWindowAttributes xswa;
unsigned long xswa_mask;
XWindowAttributes wattr;
unsigned long black_pixel, white_pixel;
screen = DefaultScreen(dpy);
rootwin = RootWindow(dpy, screen);
black_pixel = BlackPixel(dpy, screen);
white_pixel = WhitePixel(dpy, screen);
if (!vis)
vis = DefaultVisual(dpy, screen);
XGetWindowAttributes(dpy, rootwin, &wattr);
depth = wattr.depth;
if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
depth = 24;
xswa_mask = CWBorderPixel | CWBackPixel;
xswa.border_pixel = black_pixel;
xswa.background_pixel = black_pixel;
if (cmap) {
xswa_mask |= CWColormap;
xswa.colormap = cmap;
}
win = XCreateWindow(
dpy,
rootwin,
0, 0, w, h,
0,
depth,
InputOutput,
vis,
xswa_mask, &xswa
);
if (!win)
return None;
XSelectInput(dpy, win, x11_event_mask);
return win;
}
int
x11_get_geometry(
Display *dpy,
Drawable drawable,
int *px,
int *py,
unsigned int *pwidth,
unsigned int *pheight
)
{
Window rootwin;
int x, y;
unsigned int width, height, border_width, depth;
x11_trap_errors();
XGetGeometry(
dpy,
drawable,
&rootwin,
&x, &y, &width, &height,
&border_width,
&depth
);
if (x11_untrap_errors())
return 0;
if (px) *px = x;
if (py) *py = y;
if (pwidth) *pwidth = width;
if (pheight) *pheight = height;
return 1;
}
void x11_wait_event(Display *dpy, Window w, int type)
{
XEvent e;
while (!XCheckTypedWindowEvent(dpy, w, type, &e))
delay_usec(10);
}
// Returns X window background color at specified location
int
x11_get_window_colorkey(Display *dpy, Window w, int x, int y, unsigned int *ck)
{
XClearArea(dpy, w, x, y, 1, 1, False);
XImage *img = XGetImage(dpy, w, x, y, 1, 1, AllPlanes, ZPixmap);
if (img == NULL)
return -1;
if (ck)
*ck = XGetPixel(img, 0, 0);
XDestroyImage(img);
return 0;
}
|