]> git.tdb.fi Git - geometrycompositor.git/blob - source/main.c
13038c29ef83f6b8292a6e970edd06225c4de652
[geometrycompositor.git] / source / main.c
1 #define GL_GLEXT_PROTOTYPES
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <signal.h>
6 #include <X11/Xlib.h>
7 #include <X11/extensions/Xcomposite.h>
8 #include <X11/extensions/Xdamage.h>
9 #include <X11/extensions/shape.h>
10 #include <X11/extensions/Xrandr.h>
11 #include <GL/gl.h>
12 #include <GL/glx.h>
13
14 typedef struct CompositedWindow
15 {
16         Window window;
17         int x;
18         int y;
19         unsigned width;
20         unsigned height;
21         unsigned border;
22         int map_state;
23         Damage damage;
24         Pixmap pixmap;
25         GLXPixmap glx_pixmap;
26         unsigned texture;
27 } CompositedWindow;
28
29 typedef struct CompositedMonitor
30 {
31         int enabled;
32         int x;
33         int y;
34         unsigned width;
35         unsigned height;
36         unsigned vertex_buffer;
37         unsigned index_buffer;
38         unsigned vertex_array;
39         unsigned tessellation;
40         unsigned nelements;
41 } CompositedMonitor;
42
43 typedef struct CompositedScreen
44
45         int number;
46         Window root;
47         unsigned width;
48         unsigned height;
49         Window overlay;
50         Window render_window;
51         GLXFBConfig fbconfig;
52         GLXWindow glx_window;
53         GLXContext glx_context;
54         unsigned shaders[2];
55         unsigned program;
56         unsigned geometry_loc;
57         unsigned window_vertex_buffer;
58         unsigned window_vertex_array;
59         unsigned framebuffer;
60         unsigned fb_texture;
61         CompositedWindow *windows;
62         unsigned nwindows;
63         unsigned windows_capacity;
64         CompositedMonitor *monitors;
65         unsigned nmonitors;
66         int dirty;
67 } CompositedScreen;
68
69 typedef struct Compositor
70 {
71         Display *display;
72         CompositedScreen *screens;
73         unsigned nscreens;
74         int damage_event;
75         PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
76         PFNGLXBINDTEXIMAGEEXTPROC glXBindTexImageEXT;
77         PFNGLXRELEASETEXIMAGEEXTPROC glXReleaseTexImageEXT;
78         int dirty;
79 } Compositor;
80
81 static const char *vshader =
82         "#version 150\n"
83         "uniform vec4 geometry;\n"
84         "in vec2 vertex;\n"
85         "in vec2 texture_coord;\n"
86         "out vec2 texcoord;\n"
87         "void main()\n"
88         "{\n"
89         "  gl_Position = vec4((geometry.xy+vertex*geometry.zw)*2.0-1.0, 0.0, 1.0);\n"
90         "  texcoord = texture_coord;\n"
91         "}\n";
92
93 static const char *fshader =
94         "#version 150\n"
95         "uniform sampler2D image;\n"
96         "in vec2 texcoord;\n"
97         "out vec4 frag_color;\n"
98         "void main()\n"
99         "{\n"
100         "       frag_color = texture(image, texcoord);\n"
101         "}\n";
102
103 static const float window_vertices[] =
104 {
105         /* vertex    texcoord */
106         0.0f, 1.0f,  0.0f, 0.0f,
107         0.0f, 0.0f,  0.0f, 1.0f,
108         1.0f, 1.0f,  1.0f, 0.0f,
109         1.0f, 0.0f,  1.0f, 1.0f
110 };
111
112 int terminate_requested = 0;
113
114 int x_error_handler(Display *display, XErrorEvent *event)
115 {
116         printf("Ignoring X error %d on resource %lx\n", event->error_code, event->resourceid);
117         (void)display;
118         return 0;
119 }
120
121 int with_error(const char *message)
122 {
123         fprintf(stderr, "%s\n", message);
124         return 0;
125 }
126
127 int initialize_gl(Compositor *compositor, CompositedScreen *screen)
128 {
129         int attribs[9];
130         unsigned i;
131         GLXFBConfig *configs;
132         int nconfigs;
133         XVisualInfo *vi;
134         XSetWindowAttributes win_attr;
135
136         i = 0;
137         attribs[i++] = GLX_DRAWABLE_TYPE;
138         attribs[i++] = GLX_WINDOW_BIT;
139         attribs[i++] = GLX_RENDER_TYPE;
140         attribs[i++] = GLX_RGBA_BIT;
141         attribs[i++] = GLX_DOUBLEBUFFER;
142         attribs[i++] = True;
143         attribs[i++] = GLX_BIND_TO_TEXTURE_RGBA_EXT;
144         attribs[i++] = True;
145         attribs[i] = None;
146
147         configs = glXChooseFBConfig(compositor->display, screen->number, attribs, &nconfigs);
148         if(!configs || !nconfigs)
149                 return with_error("Could not find a suitable FBConfig");
150         screen->fbconfig = configs[0];
151         XFree(configs);
152
153         vi = glXGetVisualFromFBConfig(compositor->display, screen->fbconfig);
154         win_attr.colormap = XCreateColormap(compositor->display, screen->root, vi->visual, AllocNone);
155         screen->render_window = XCreateWindow(compositor->display, screen->overlay, 0, 0, screen->width, screen->height, 0, vi->depth, InputOutput, vi->visual, CWColormap, &win_attr);
156         XMapWindow(compositor->display, screen->render_window);
157
158         screen->glx_window = glXCreateWindow(compositor->display, screen->fbconfig, screen->render_window, NULL);
159
160         i = 0;
161         attribs[i++] = GLX_CONTEXT_FLAGS_ARB;
162         attribs[i++] = GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
163         attribs[i++] = GLX_CONTEXT_MAJOR_VERSION_ARB;
164         attribs[i++] = 3;
165         attribs[i++] = GLX_CONTEXT_MINOR_VERSION_ARB;
166         attribs[i++] = 1;
167         attribs[i] = None;
168
169         screen->glx_context = compositor->glXCreateContextAttribsARB(compositor->display, screen->fbconfig, NULL, True, attribs);
170
171         XFree(vi);
172
173         return 1;
174 }
175
176 void use_gl(Compositor *compositor, CompositedScreen *screen)
177 {
178         glXMakeContextCurrent(compositor->display, screen->glx_window, screen->glx_window, screen->glx_context);
179 }
180
181 unsigned compile_shader(GLenum type, const char *source)
182 {
183         unsigned shader;
184         int status;
185         char info_log[1024];
186         GLsizei length;
187         
188         shader = glCreateShader(type);
189         glShaderSource(shader, 1, &source, NULL);
190         glCompileShader(shader);
191         glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
192         glGetShaderInfoLog(shader, sizeof(info_log), &length, info_log);
193         if(!status)
194         {
195                 fprintf(stderr, "Shader compilation failed:\n%s\n", info_log);
196                 glDeleteShader(shader);
197                 return 0;
198         }
199         else if(length)
200                 printf("Shader info log:\n%s\n", info_log);
201
202         return shader;
203 }
204
205 unsigned link_program(unsigned *shaders, unsigned nshaders)
206 {
207         unsigned program;
208         unsigned i;
209         int status;
210         char info_log[1024];
211         GLsizei length;
212
213         program = glCreateProgram();
214         for(i=0; i<nshaders; ++i)
215                 glAttachShader(program, shaders[i]);
216         glBindAttribLocation(program, 0, "vertex");
217         glBindAttribLocation(program, 1, "texture_coord");
218         glBindFragDataLocation(program, 0, "frag_color");
219         glLinkProgram(program);
220
221         glGetProgramiv(program, GL_LINK_STATUS, &status);
222         glGetProgramInfoLog(program, sizeof(info_log), &length, info_log);
223         if(!status)
224         {
225                 fprintf(stderr, "Program link failed:\n%s\n", info_log);
226                 glDeleteProgram(program);
227                 return 0;
228         }
229         else if(length)
230                 printf("Program info log:\n%s\n", info_log);
231
232         return program;
233 }
234
235 int create_gl_resources(Compositor *compositor, CompositedScreen *screen)
236 {
237         unsigned stride;
238
239         use_gl(compositor, screen);
240
241         screen->shaders[0] = compile_shader(GL_VERTEX_SHADER, vshader);
242         screen->shaders[1] = compile_shader(GL_FRAGMENT_SHADER, fshader);
243         if(!screen->shaders[0] || !screen->shaders[1])
244                 return 0;
245
246         screen->program = link_program(screen->shaders, 2);
247         if(!screen->program)
248                 return 0;
249
250         screen->geometry_loc = glGetUniformLocation(screen->program, "geometry");
251
252         glGenBuffers(1, &screen->window_vertex_buffer);
253         glBindBuffer(GL_ARRAY_BUFFER, screen->window_vertex_buffer);
254         glBufferData(GL_ARRAY_BUFFER, sizeof(window_vertices), window_vertices, GL_STATIC_DRAW);
255
256         stride = 4*sizeof(float);
257         glGenVertexArrays(1, &screen->window_vertex_array);
258         glBindVertexArray(screen->window_vertex_array);
259         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, stride, NULL);
260         glEnableVertexAttribArray(0);
261         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void *)(2*sizeof(float)));
262         glEnableVertexAttribArray(1);
263         glBindVertexArray(0);
264
265         glBindBuffer(GL_ARRAY_BUFFER, 0);
266
267         glGenTextures(1, &screen->fb_texture);
268         glBindTexture(GL_TEXTURE_2D, screen->fb_texture);
269         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screen->width, screen->height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
270         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
271         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
272         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
273         glBindTexture(GL_TEXTURE_2D, 0);
274
275         glGenFramebuffers(1, &screen->framebuffer);
276         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, screen->framebuffer);
277         glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, screen->fb_texture, 0);
278         glDepthMask(GL_FALSE);
279         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
280
281         return 1;
282 }
283
284 CompositedWindow *find_window(CompositedScreen *screen, Window w)
285 {
286         unsigned i;
287
288         for(i=0; i<screen->nwindows; ++i)
289                 if(screen->windows[i].window==w)
290                         return &screen->windows[i];
291
292         return NULL;
293 }
294
295 void create_window_pixmap(Compositor *compositor, CompositedScreen *screen, CompositedWindow *window)
296 {
297         int attribs[5];
298         unsigned i;
299
300         if(window->pixmap)
301         {
302                 glXDestroyPixmap(compositor->display, window->glx_pixmap);
303                 XFreePixmap(compositor->display, window->pixmap);
304         }
305
306         i = 0;
307         attribs[i++] = GLX_TEXTURE_TARGET_EXT;
308         attribs[i++] = GLX_TEXTURE_2D_EXT;
309         attribs[i++] = GLX_TEXTURE_FORMAT_EXT;
310         attribs[i++] = GLX_TEXTURE_FORMAT_RGBA_EXT;
311         attribs[i++] = None;
312
313         window->pixmap = XCompositeNameWindowPixmap(compositor->display, window->window);
314         window->glx_pixmap = glXCreatePixmap(compositor->display, screen->fbconfig, window->pixmap, attribs);
315 }
316
317 CompositedWindow *add_window(Compositor *compositor, CompositedScreen *screen, Window w)
318 {
319         CompositedWindow *window;
320         XWindowAttributes win_attr;
321
322         if(w==screen->root || w==screen->overlay)
323                 return NULL;
324
325         if(!XGetWindowAttributes(compositor->display, w, &win_attr))
326         {
327                 printf("XGetWindowAttributes failed; probably the window was already destroyed\n");
328                 return NULL;
329         }
330         if(win_attr.class==InputOnly)
331                 return NULL;
332
333         if(find_window(screen, w))
334                 return NULL;
335
336         if(screen->nwindows==screen->windows_capacity)
337         {
338                 screen->windows = (CompositedWindow *)realloc(screen->windows, (screen->windows_capacity+1)*sizeof(CompositedWindow));
339                 ++screen->windows_capacity;
340         }
341
342         window = &screen->windows[screen->nwindows++];
343
344         window->window = w;
345         window->x = win_attr.x;
346         window->y = win_attr.y;
347         window->width = win_attr.width;
348         window->height = win_attr.height;
349         window->border = win_attr.border_width;
350         window->map_state = win_attr.map_state;
351
352         window->damage = XDamageCreate(compositor->display, window->window, XDamageReportNonEmpty);
353         window->pixmap = None;
354         window->glx_pixmap = None;
355
356         XCompositeRedirectWindow(compositor->display, window->window, CompositeRedirectManual);
357         if(window->map_state==IsViewable)
358                 create_window_pixmap(compositor, screen, window);
359
360         glGenTextures(1, &window->texture);
361         glBindTexture(GL_TEXTURE_2D, window->texture);
362         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
363         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
364         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
365
366         return window;
367 }
368
369 void remove_window(Compositor *compositor, CompositedScreen *screen, CompositedWindow *window, int destroyed)
370 {
371         unsigned i;
372
373         glDeleteTextures(1, &window->texture);
374         if(!destroyed)
375         {
376                 XDamageDestroy(compositor->display, window->damage);
377                 if(window->pixmap)
378                 {
379                         glXDestroyPixmap(compositor->display, window->glx_pixmap);
380                         XFreePixmap(compositor->display, window->pixmap);
381                 }
382                 XCompositeUnredirectWindow(compositor->display, window->window, CompositeRedirectManual);
383         }
384
385         --screen->nwindows;
386         for(i=window-screen->windows; i<screen->nwindows; ++i)
387                 screen->windows[i] = screen->windows[i+1];
388 }
389
390 CompositedWindow *reorder_window(CompositedScreen *screen, CompositedWindow *window, Window above)
391 {
392         unsigned i, j;
393         CompositedWindow hold;
394
395         i = window-screen->windows;
396         if(above)
397         {
398                 for(j=0; j<screen->nwindows; ++j)
399                         if(screen->windows[j].window==above)
400                                 break;
401
402                 if(j>=screen->nwindows || i==j+1)
403                         return window;
404
405                 if(j<i)
406                         ++j;
407         }
408         else
409                 j = 0;
410
411         hold = *window;
412         if(i<j)
413         {
414                 for(; i<j; ++i)
415                         screen->windows[i] = screen->windows[i+1];
416         }
417         else
418         {
419                 for(; i>j; --i)
420                         screen->windows[i] = screen->windows[i-1];
421         }
422         screen->windows[j] = hold;
423
424         return &screen->windows[j];
425 }
426
427 CompositedScreen *find_screen_by_root(Compositor *compositor, Window root)
428 {
429         unsigned i;
430
431         for(i=0; i<compositor->nscreens; ++i)
432                 if(compositor->screens[i].root==root)
433                         return &compositor->screens[i];
434
435         return NULL;
436 }
437
438 CompositedScreen *find_screen_by_window(Compositor *compositor, Window w)
439 {
440         unsigned i, j;
441
442         for(i=0; i<compositor->nscreens; ++i)
443                 for(j=0; j<compositor->screens[i].nwindows; ++j)
444                         if(compositor->screens[i].windows[j].window==w)
445                                 return &compositor->screens[i];
446
447         return NULL;
448 }
449
450 void update_monitor_vertices(CompositedScreen *screen, CompositedMonitor *monitor)
451 {
452         unsigned t;
453         unsigned data_size;
454         float *vertex_data;
455         unsigned short *index_data;
456         unsigned x, y;
457         unsigned i;
458
459         t = monitor->tessellation;
460
461         data_size = (t+1)*(t+1)*4*sizeof(float);
462         vertex_data = (float *)malloc(data_size);
463         for(y=0; y<=t; ++y)
464                 for(x=0; x<=t; ++x)
465                 {
466                         float xf, yf;
467                         float *v;
468
469                         xf = (float)x/t;
470                         yf = (float)y/t;
471                         v = vertex_data+(y*(t+1)+x)*4;
472                         v[0] = xf;
473                         v[1] = yf;
474                         v[2] = (monitor->x+xf*monitor->width)/screen->width;
475                         v[3] = (monitor->y+yf*monitor->height)/screen->height;
476                 }
477         glBindBuffer(GL_ARRAY_BUFFER, monitor->vertex_buffer);
478         glBufferData(GL_ARRAY_BUFFER, data_size, vertex_data, GL_STATIC_DRAW);
479         glBindBuffer(GL_ARRAY_BUFFER, 0);
480         free(vertex_data);
481
482         monitor->nelements = t*((t+1)*2+1)-1;
483         data_size = monitor->nelements*sizeof(unsigned short);
484         index_data = (unsigned short *)malloc(data_size);
485         i = 0;
486         for(y=0; y<t; ++y)
487         {
488                 if(y>0)
489                         index_data[i++] = 0xFFFF;
490                 for(x=0; x<=t; ++x)
491                 {
492                         index_data[i++] = (y+1)*(t+1)+x;
493                         index_data[i++] = y*(t+1)+x;
494                 }
495         }
496         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, monitor->index_buffer);
497         glBufferData(GL_ELEMENT_ARRAY_BUFFER, data_size, index_data, GL_STATIC_DRAW);
498         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
499         free(index_data);
500 }
501
502 int initialize_monitor(Compositor *compositor, CompositedScreen *screen, CompositedMonitor *monitor, XRRScreenResources *xrr_res, unsigned index)
503 {
504         XRROutputInfo *output;
505         XRRCrtcInfo *crtc;
506         unsigned buffers[2];
507         unsigned stride;
508
509         output = XRRGetOutputInfo(compositor->display, xrr_res, xrr_res->outputs[index]);
510         monitor->enabled = !!output->crtc;
511         if(!monitor->enabled)
512         {
513                 XRRFreeOutputInfo(output);
514                 return 1;
515         }
516
517         crtc = XRRGetCrtcInfo(compositor->display, xrr_res, output->crtc);
518         monitor->x = crtc->x;
519         monitor->y = crtc->y;
520         monitor->width = crtc->width;
521         monitor->height = crtc->height;
522         XRRFreeCrtcInfo(crtc);
523         XRRFreeOutputInfo(output);
524
525         glGenBuffers(2, buffers);
526         monitor->vertex_buffer = buffers[0];
527         monitor->index_buffer = buffers[1];
528
529         monitor->tessellation = 20;
530         update_monitor_vertices(screen, monitor);
531         stride = 4*sizeof(float);
532
533         glGenVertexArrays(1, &monitor->vertex_array);
534         glBindVertexArray(monitor->vertex_array);
535         glBindBuffer(GL_ARRAY_BUFFER, monitor->vertex_buffer);
536         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, stride, NULL);
537         glEnableVertexAttribArray(0);
538         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void *)(2*sizeof(float)));
539         glEnableVertexAttribArray(1);
540         glBindBuffer(GL_ARRAY_BUFFER, 0);
541         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, monitor->index_buffer);
542         glBindVertexArray(0);
543
544         return 1;
545 }
546
547 int initialize_screen(Compositor *compositor, unsigned number)
548 {
549         CompositedScreen *screen;
550         const char *extensions;
551         Window dummy_root;
552         int x, y;
553         unsigned border;
554         unsigned depth;
555         XRRScreenResources *xrr_res;
556         Window dummy_parent;
557         Window *children;
558         unsigned nchildren;
559         unsigned i;
560
561         screen = &compositor->screens[number];
562         screen->number = number;
563
564         extensions = glXQueryExtensionsString(compositor->display, screen->number);
565         if(!strstr(extensions, "GLX_ARB_create_context"))
566                 return with_error("GLX_ARB_create_context is required");
567         if(!strstr(extensions, "GLX_EXT_texture_from_pixmap"))
568                 return with_error("GLX_EXT_texture_from_pixmap is required");
569
570         screen->root = RootWindow(compositor->display, screen->number);
571         XSelectInput(compositor->display, screen->root, SubstructureNotifyMask);
572         screen->overlay = XCompositeGetOverlayWindow(compositor->display, screen->root);
573         XGetGeometry(compositor->display, screen->overlay, &dummy_root, &x, &y, &screen->width, &screen->height, &border, &depth);
574         XShapeCombineRectangles(compositor->display, screen->overlay, ShapeInput, 0, 0, NULL, 0, ShapeSet, Unsorted);
575
576         if(!initialize_gl(compositor, screen))
577                 return 0;
578
579         if(!create_gl_resources(compositor, screen))
580                 return 0;
581
582         xrr_res = XRRGetScreenResources(compositor->display, screen->root);
583         screen->nmonitors = xrr_res->noutput;
584         screen->monitors = (CompositedMonitor *)malloc(screen->nmonitors*sizeof(CompositedMonitor));
585         for(i=0; i<screen->nmonitors; ++i)
586                 if(!initialize_monitor(compositor, screen, &screen->monitors[i], xrr_res, i))
587                         return 0;
588         XRRFreeScreenResources(xrr_res);
589
590         XQueryTree(compositor->display, screen->root, &dummy_root, &dummy_parent, &children, &nchildren);
591
592         screen->windows = (CompositedWindow *)malloc(nchildren*sizeof(CompositedWindow));
593         screen->nwindows = 0;
594         screen->windows_capacity = nchildren;
595
596         for(i=0; i<nchildren; ++i)
597                 add_window(compositor, screen, children[i]);
598
599         XFree(children);
600
601         screen->dirty = 1;
602
603         return 1;
604 }
605
606 int initialize_compositor(Compositor *compositor)
607 {
608         int event_base;
609         int error_base;
610         int major_ver;
611         int minor_ver;
612         unsigned i;
613
614         compositor->display = XOpenDisplay(NULL);
615         if(!compositor->display)
616                 return with_error("Could not open X display");
617
618         XSetErrorHandler(&x_error_handler);
619
620         if(!XCompositeQueryExtension(compositor->display, &event_base, &error_base))
621                 return with_error("XComposite is required but was not found");
622         else if(!XCompositeQueryVersion(compositor->display, &major_ver, &minor_ver))
623                 return with_error("Cannot determine XComposite version");
624         else if(major_ver==0 && minor_ver<3)
625                 return with_error("XComposite 0.3 or later is required");
626
627         if(!glXQueryExtension(compositor->display, &event_base, &error_base))
628                 return with_error("GLX is required but was not found");
629         else if(!glXQueryVersion(compositor->display, &major_ver, &minor_ver))
630                 return with_error("Cannot determine GLX version");
631         else if(major_ver<1 || (major_ver==1 && minor_ver<4))
632                 return with_error("GLX 1.4 or later is required");
633
634         if(!XDamageQueryExtension(compositor->display, &compositor->damage_event, &error_base))
635                 return with_error("XDamage is required but was not found");
636         else if(!XDamageQueryVersion(compositor->display, &major_ver, &minor_ver))
637                 return with_error("Cannot determine XDamage version");
638         else if(major_ver<1)
639                 return with_error("XDamage 1.0 or later is required");
640
641         if(!XShapeQueryExtension(compositor->display, &event_base, &error_base))
642                 return with_error("XShape is required but was not found");
643         else if(!XShapeQueryVersion(compositor->display, &major_ver, &minor_ver))
644                 return with_error("Cannot determine XShape version");
645         else if(major_ver<1 || (major_ver==1 && minor_ver<1))
646                 return with_error("XShape 1.1 or later is required");
647
648         if(!XRRQueryExtension(compositor->display, &event_base, &error_base))
649                 return with_error("XRandR is required but was not found");
650         else if(!XRRQueryVersion(compositor->display, &major_ver, &minor_ver))
651                 return with_error("Cannot determine XRandR version");
652         else if(major_ver<1 || (major_ver==1 && minor_ver<2))
653                 return with_error("XRandR 1.2 or later is required");
654
655         compositor->glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((unsigned char *)"glXCreateContextAttribsARB");
656         compositor->glXBindTexImageEXT = (PFNGLXBINDTEXIMAGEEXTPROC)glXGetProcAddress((unsigned char *)"glXBindTexImageEXT");
657         compositor->glXReleaseTexImageEXT = (PFNGLXRELEASETEXIMAGEEXTPROC)glXGetProcAddress((unsigned char *)"glXReleaseTexImageEXT");
658
659         compositor->nscreens = ScreenCount(compositor->display);
660         compositor->screens = (CompositedScreen *)malloc(compositor->nscreens*sizeof(CompositedScreen));
661         for(i=0; i<compositor->nscreens; ++i)
662                 if(!initialize_screen(compositor, i))
663                         return 0;
664
665         compositor->dirty = 1;
666
667         return 1;
668 }
669
670 void shutdown_screen(Compositor *compositor, CompositedScreen *screen)
671 {
672         unsigned i;
673
674         use_gl(compositor, screen);
675
676         for(i=0; i<screen->nwindows; ++i)
677         {
678                 glDeleteTextures(1, &screen->windows[i].texture);
679                 if(screen->windows[i].pixmap)
680                 {
681                         glXDestroyPixmap(compositor->display, screen->windows[i].glx_pixmap);
682                         XFreePixmap(compositor->display, screen->windows[i].pixmap);
683                         XDamageDestroy(compositor->display, screen->windows[i].damage);
684                 }
685         }
686
687         for(i=0; i<screen->nmonitors; ++i)
688                 if(screen->monitors[i].enabled)
689                 {
690                         glDeleteBuffers(1, &screen->monitors[i].vertex_buffer);
691                         glDeleteBuffers(1, &screen->monitors[i].index_buffer);
692                         glDeleteVertexArrays(1, &screen->monitors[i].vertex_array);
693                 }
694
695         glDeleteBuffers(1, &screen->window_vertex_buffer);
696         glDeleteVertexArrays(1, &screen->window_vertex_array);
697         glDeleteFramebuffers(1, &screen->framebuffer);
698         glDeleteTextures(1, &screen->fb_texture);
699         glDeleteProgram(screen->program);
700         glDeleteShader(screen->shaders[0]);
701         glDeleteShader(screen->shaders[1]);
702
703         glXMakeContextCurrent(compositor->display, 0, 0, NULL);
704         glXDestroyContext(compositor->display, screen->glx_context);
705         glXDestroyWindow(compositor->display, screen->glx_window);
706         XDestroyWindow(compositor->display, screen->render_window);
707
708         XCompositeReleaseOverlayWindow(compositor->display, screen->overlay);
709
710         free(screen->windows);
711         free(screen->monitors);
712 }
713
714 void shutdown_compositor(Compositor *compositor)
715 {
716         unsigned i;
717
718         for(i=0; i<compositor->nscreens; ++i)
719                 shutdown_screen(compositor, &compositor->screens[i]);
720         free(compositor->screens);
721
722         XCloseDisplay(compositor->display);
723 }
724
725 void mark_dirty(Compositor *compositor, CompositedScreen *screen)
726 {
727         compositor->dirty = 1;
728         screen->dirty = 1;
729 }
730
731 void process_create_window_event(Compositor *compositor, XCreateWindowEvent *event)
732 {
733         CompositedScreen *screen;
734
735         if((screen = find_screen_by_root(compositor, event->parent)))
736                 add_window(compositor, screen, event->window);
737 }
738
739 void process_destroy_window_event(Compositor *compositor, XDestroyWindowEvent *event)
740 {
741         CompositedScreen *screen;
742         CompositedWindow *window;
743
744         if((screen = find_screen_by_root(compositor, event->event)))
745                 if((window = find_window(screen, event->window)))
746                         remove_window(compositor, screen, window, 1);
747 }
748
749 void process_map_event(Compositor *compositor, XMapEvent *event)
750 {
751         CompositedScreen *screen;
752         CompositedWindow *window;
753
754         screen = find_screen_by_root(compositor, event->event);
755         if(!screen)
756                 return;
757
758         window = find_window(screen, event->window);
759         if(!window)
760                 return;
761
762         window->map_state = IsViewable;
763         create_window_pixmap(compositor, screen, window);
764
765         mark_dirty(compositor, screen);
766 }
767
768 void process_unmap_event(Compositor *compositor, XUnmapEvent *event)
769 {
770         CompositedScreen *screen;
771         CompositedWindow *window;
772
773         screen = find_screen_by_root(compositor, event->event);
774         if(!screen)
775                 return;
776
777         window = find_window(screen, event->window);
778         if(window)
779                 window->map_state = IsUnviewable;
780
781         mark_dirty(compositor, screen);
782 }
783
784 void process_reparent_event(Compositor *compositor, XReparentEvent *event)
785 {
786         CompositedScreen *screen;
787         CompositedWindow *window;
788
789         screen = find_screen_by_root(compositor, event->event);
790         if(!screen)
791                 return;
792
793         if(event->parent==screen->root)
794                 window = add_window(compositor, screen, event->window);
795         else
796         {
797                 window = find_window(screen, event->window);
798                 if(!window)
799                         return;
800
801                 remove_window(compositor, screen, window, 0);
802         }
803
804         if(window && window->map_state==IsViewable)
805                 mark_dirty(compositor, screen);
806 }
807
808 void process_configure_event(Compositor *compositor, XConfigureEvent *event)
809 {
810         CompositedScreen *screen;
811         CompositedWindow *window;
812
813         screen = find_screen_by_root(compositor, event->event);
814         if(!screen)
815                 return;
816
817         window = find_window(screen, event->window);
818         if(!window)
819                 return;
820
821         window->x = event->x;
822         window->y = event->y;
823         if((unsigned)event->width!=window->width || (unsigned)event->height!=window->height || (unsigned)event->border_width!=window->border)
824         {
825                 window->width = event->width;
826                 window->height = event->height;
827                 window->border = event->border_width;
828                 create_window_pixmap(compositor, screen, window);
829         }
830         reorder_window(screen, window, event->above);
831
832         if(window->map_state==IsViewable)
833                 mark_dirty(compositor, screen);
834 }
835
836 void process_damage_event(Compositor *compositor, XDamageNotifyEvent *event)
837 {
838         CompositedScreen *screen;
839         CompositedWindow *window;
840
841         screen = find_screen_by_window(compositor, event->drawable);
842         if(!screen)
843                 return;
844
845         window = find_window(screen, event->drawable);
846         if(window->map_state==IsViewable)
847                 mark_dirty(compositor, screen);
848 }
849
850 int process_event(Compositor *compositor)
851 {
852         XEvent event;
853         if(compositor->dirty)
854         {
855                 if(!XCheckMaskEvent(compositor->display, -1, &event))
856                         return 0;
857         }
858         else
859                 XNextEvent(compositor->display, &event);
860
861         switch(event.type)
862         {
863         case CreateNotify:
864                 process_create_window_event(compositor, &event.xcreatewindow);
865                 break;
866         case DestroyNotify:
867                 process_destroy_window_event(compositor, &event.xdestroywindow);
868                 break;
869         case MapNotify:
870                 process_map_event(compositor, &event.xmap);
871                 break;
872         case UnmapNotify:
873                 process_unmap_event(compositor, &event.xunmap);
874                 break;
875         case ReparentNotify:
876                 process_reparent_event(compositor, &event.xreparent);
877                 break;
878         case ConfigureNotify:
879                 process_configure_event(compositor, &event.xconfigure);
880                 break;
881         default:
882                 if(event.type==compositor->damage_event+XDamageNotify)
883                         process_damage_event(compositor, (XDamageNotifyEvent *)&event);
884                 else
885                         printf("Event %d\n", event.type);
886         }
887
888         return 1;
889 }
890
891 void refresh_screen(Compositor *compositor, CompositedScreen *screen)
892 {
893         unsigned i;
894
895         use_gl(compositor, screen);
896
897         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, screen->framebuffer);
898
899         glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
900         glClear(GL_COLOR_BUFFER_BIT);
901
902         glUseProgram(screen->program);
903         glBindVertexArray(screen->window_vertex_array);
904         for(i=0; i<screen->nwindows; ++i)
905         {
906                 CompositedWindow *window = &screen->windows[i];
907                 if(window->map_state!=IsViewable)
908                         continue;
909
910                 glBindTexture(GL_TEXTURE_2D, window->texture);
911                 compositor->glXBindTexImageEXT(compositor->display, window->glx_pixmap, GLX_FRONT_LEFT_EXT, NULL);
912                 glUniform4f(screen->geometry_loc,
913                         (float)window->x/screen->width, ((float)screen->height-window->y-window->height)/screen->height,
914                         (float)(window->width+2*window->border)/screen->width, (float)(window->height+2*window->border)/screen->height);
915                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
916                 compositor->glXReleaseTexImageEXT(compositor->display, window->glx_pixmap, GLX_FRONT_LEFT_EXT);
917                 XDamageSubtract(compositor->display, window->damage, None, None);
918         }
919
920         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
921         glClearColor(0.5f, 0.0f, 0.5f, 0.0f);
922         glClear(GL_COLOR_BUFFER_BIT);
923         glBindTexture(GL_TEXTURE_2D, screen->fb_texture);
924         glEnable(GL_PRIMITIVE_RESTART);
925         glPrimitiveRestartIndex(0xFFFF);
926
927         for(i=0; i<screen->nmonitors; ++i)
928         {
929                 CompositedMonitor *monitor = &screen->monitors[i];
930                 if(!monitor->enabled)
931                         continue;
932
933                 glUniform4f(screen->geometry_loc,
934                         (float)monitor->x/screen->width, (float)monitor->y/screen->height,
935                         (float)monitor->width/screen->width, (float)monitor->height/screen->height);
936
937                 glBindVertexArray(monitor->vertex_array);
938                 glDrawElements(GL_TRIANGLE_STRIP, monitor->nelements, GL_UNSIGNED_SHORT, NULL);
939         }
940
941         glXSwapBuffers(compositor->display, screen->glx_window);
942
943         screen->dirty = 0;
944 }
945
946 void refresh_all_screens(Compositor *compositor)
947 {
948         unsigned i;
949
950         for(i=0; i<compositor->nscreens; ++i)
951         {
952                 CompositedScreen *screen = &compositor->screens[i];
953                 if(screen->dirty)
954                         refresh_screen(compositor, screen);
955         }
956
957         compositor->dirty = 0;
958 }
959
960 void sighandler(int sig)
961 {
962         terminate_requested = 1;
963         (void)sig;
964 }
965
966 int main()
967 {
968         Compositor compositor;
969
970         signal(SIGINT, &sighandler);
971         signal(SIGTERM, &sighandler);
972
973         if(!initialize_compositor(&compositor))
974                 return 1;
975
976         while(!terminate_requested)
977         {
978                 if(!process_event(&compositor))
979                         refresh_all_screens(&compositor);
980         }
981
982         shutdown_compositor(&compositor);
983
984         return 0;
985 }