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