add segl
[mesa.git] / progs / egl / segl / segl_x11.c
1 #include <stdlib.h>
2 #include <sys/time.h>
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5
6 #include "segl.h"
7
8 static Window
9 x11_create_window(struct segl_winsys *winsys, const char *name,
10 EGLint width, EGLint height, EGLint visual)
11 {
12 XVisualInfo vinfo_template, *vinfo = NULL;
13 EGLint val, num_vinfo;
14 Window root, win;
15 XSetWindowAttributes attrs;
16 unsigned long mask;
17 EGLint x = 0, y = 0;
18
19 vinfo_template.visualid = (VisualID) val;
20 vinfo = XGetVisualInfo(winsys->dpy, VisualIDMask, &vinfo_template, &num_vinfo);
21 if (!num_vinfo) {
22 if (vinfo)
23 XFree(vinfo);
24 return None;
25 }
26
27 root = DefaultRootWindow(winsys->dpy);
28
29 /* window attributes */
30 attrs.background_pixel = 0;
31 attrs.border_pixel = 0;
32 attrs.colormap = XCreateColormap(winsys->dpy, root, vinfo->visual, AllocNone);
33 attrs.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
34 attrs.override_redirect = False;
35 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
36
37 win = XCreateWindow(winsys->dpy, root, x, y, width, height, 0,
38 vinfo->depth, InputOutput, vinfo->visual, mask, &attrs);
39 XFree(vinfo);
40
41 if (!win)
42 return None;
43
44 /* set hints and properties */
45 {
46 XSizeHints sizehints;
47 sizehints.x = x;
48 sizehints.y = y;
49 sizehints.width = width;
50 sizehints.height = height;
51 sizehints.flags = USSize | USPosition;
52 XSetNormalHints(winsys->dpy, win, &sizehints);
53 XSetStandardProperties(winsys->dpy, win, name, name,
54 None, (char **)NULL, 0, &sizehints);
55 }
56
57 XMapWindow(winsys->dpy, win);
58
59 return win;
60 }
61
62 static void
63 x11_destroy_window(struct segl_winsys *winsys, Window win)
64 {
65 if (win)
66 XDestroyWindow(winsys->dpy, win);
67 }
68
69
70 static Pixmap
71 x11_create_pixmap(struct segl_winsys *winsys, EGLint width, EGLint height,
72 EGLint depth)
73 {
74 Window root = DefaultRootWindow(winsys->dpy);
75 Pixmap pix;
76
77 pix = XCreatePixmap(winsys->dpy, (Drawable) root, width, height, depth);
78
79 return pix;
80 }
81
82 static void
83 x11_destroy_pixmap(struct segl_winsys *winsys, Pixmap pix)
84 {
85 if (pix)
86 XFreePixmap(winsys->dpy, pix);
87 }
88
89 static double
90 x11_now(struct segl_winsys *winsys)
91 {
92 struct timeval tv;
93
94 gettimeofday(&tv, NULL);
95
96 return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
97 }
98
99 struct segl_winsys *
100 segl_get_winsys(EGLNativeDisplayType dpy)
101 {
102 struct segl_winsys *winsys;
103
104 winsys = calloc(1, sizeof(*winsys));
105 if (winsys) {
106 winsys->dpy = dpy;
107
108 winsys->create_window = x11_create_window;
109 winsys->destroy_window = x11_destroy_window;
110 winsys->create_pixmap = x11_create_pixmap;
111 winsys->destroy_pixmap = x11_destroy_pixmap;
112
113 winsys->now = x11_now;
114 }
115
116 return winsys;
117 }