]> git.tdb.fi Git - geometrycompositor.git/blob - source/control.c
Implement a spherical curvature correction
[geometrycompositor.git] / source / control.c
1 #define GL_GLEXT_PROTOTYPES
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <X11/Xlib.h>
6 #include <X11/Xatom.h>
7 #include <GL/gl.h>
8 #include <GL/glx.h>
9
10 typedef struct GeometryCorrection
11 {
12         char *monitor_name;
13         float keystone_vertical;
14         int curvature_type;
15         float curvature_depth;
16         float vertical_center;
17         float perspective;
18 } GeometryCorrection;
19
20 typedef struct InteractiveView
21 {
22         Window window;
23         GLXContext glx_context;
24         GLXWindow glx_window;
25         Atom geometry_data_atom;
26         unsigned shaders[5];
27         unsigned programs[2];
28         unsigned buffers[2];
29         unsigned nelements;
30 } InteractiveView;
31
32 static unsigned short frustum_indices[] = { 2, 1, 3, 0, 1, 0xFFFF, 3, 4, 2, 0, 4, 0xFFFF };
33 static float view_matrix[] =
34 {
35          0.70711f,  0.40825f, -0.57735f, 0.0f,
36          0.0f,      0.81650f,  0.57735f, 0.0f,
37          0.70711f, -0.40725f,  0.57735f, 0.0f,
38         -0.2f,      0.0f,     -2.5f,     1.0f
39 };
40 static float projection_matrix[] =
41 {
42         2.0f, 0.0f,     0.0f,   0.0f,
43         0.0f, 2.6667f,  0.0f,   0.0f,
44         0.0f, 0.0f,    -1.1f,  -1.0f,
45         0.0f, 0.0f,    -1.05f,  0.0f
46 };
47
48 static const char *vshader_src =
49         "#version 150\n"
50         "uniform mat4 view;\n"
51         "in vec4 vertex;\n"
52         "void main()\n"
53         "{\n"
54         "  gl_Position = view*vertex;\n"
55         "}\n";
56
57 static const char *gshader_src =
58         "#version 150\n"
59         "layout(triangles) in;\n"
60         "layout(triangle_strip, max_vertices=3) out;\n"
61         "uniform mat4 projection;\n"
62         "out vec3 normal;\n"
63         "void main()\n"
64         "{\n"
65         "  vec3 v0 = gl_in[0].gl_Position.xyz;\n"
66         "  vec3 v1 = gl_in[1].gl_Position.xyz;\n"
67         "  vec3 v2 = gl_in[2].gl_Position.xyz;\n"
68         "  vec3 tri_normal = normalize(cross(v1-v0, v2-v0));\n"
69         "  for(int i=0; i<3; ++i)\n"
70         "  {\n"
71         "    normal = tri_normal;\n"
72         "    gl_Position = projection*gl_in[i].gl_Position;\n"
73         "    EmitVertex();\n"
74         "  }\n"
75         "  EndPrimitive();\n"
76         "}\n";
77
78 static const char *fshader_src =
79         "#version 150\n"
80         "in vec3 normal;\n"
81         "out vec4 frag_color;\n"
82         "void main()\n"
83         "{\n"
84         "  frag_color = vec4(vec3(0.5+clamp(normal.z, 0.0, 1.0)*0.5), 1.0);\n"
85         "}\n";
86
87 static const char *flat_vshader_src =
88         "#version 150\n"
89         "uniform mat4 view;\n"
90         "uniform mat4 projection;\n"
91         "in vec4 vertex;\n"
92         "void main()\n"
93         "{\n"
94         "  gl_Position = projection*view*vertex;\n"
95         "}\n";
96
97 static const char *flat_fshader_src =
98         "#version 150\n"
99         "uniform vec4 color;\n"
100         "out vec4 frag_color;\n"
101         "void main()\n"
102         "{\n"
103         "  frag_color = color;\n"
104         "}\n";
105
106
107 GeometryCorrection *get_corrections(Display *display)
108 {
109         Window root;
110         Atom monitors_atom;
111         Atom correction_atom;
112         Atom prop_type;
113         int prop_format;
114         unsigned long overflow;
115         unsigned long names_length;
116         char *names;
117         unsigned long values_length;
118         short *values;
119         unsigned ncorrections;
120         unsigned i;
121         GeometryCorrection *corrections;
122         char *name_ptr;
123
124         root = DefaultRootWindow(display);
125         monitors_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION_MONITORS", False);
126         correction_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION", False);
127
128         XGetWindowProperty(display, root, monitors_atom, 0, 64, False, XA_STRING,
129                 &prop_type, &prop_format, &names_length, &overflow, (unsigned char **)&names);
130         if(prop_type!=XA_STRING || prop_format!=8)
131                 return NULL;
132
133         XGetWindowProperty(display, root, correction_atom, 0, 64, False, XA_INTEGER,
134                 &prop_type, &prop_format, &values_length, &overflow, (unsigned char **)&values);
135         if(prop_type!=XA_INTEGER || prop_format!=16)
136         {
137                 XFree(names);
138                 return NULL;
139         }
140
141         ncorrections = (names_length>0);
142         for(i=0; i<names_length; ++i)
143                 if(!names[i])
144                         ++ncorrections;
145         if(ncorrections*5>values_length)
146                 ncorrections = values_length/5;
147         corrections = (GeometryCorrection *)malloc((ncorrections+1)*sizeof(GeometryCorrection));
148
149         name_ptr = names;
150         for(i=0; i<ncorrections; ++i)
151         {
152                 unsigned namelen;
153
154                 namelen = strlen(name_ptr);
155                 corrections[i].monitor_name = (char *)malloc(namelen+1);
156                 strcpy(corrections[i].monitor_name, name_ptr);
157
158                 corrections[i].keystone_vertical = values[i*5]/4096.0f;
159                 corrections[i].curvature_type = values[i*5+1];
160                 corrections[i].curvature_depth = values[i*5+2]/4096.0f;
161                 corrections[i].vertical_center = values[i*5+3]/4096.0f;
162                 corrections[i].perspective = values[i*5+4]/4096.0f;
163
164                 name_ptr += namelen+1;
165         }
166
167         corrections[ncorrections].monitor_name = NULL;
168
169         XFree(names);
170         XFree(values);
171
172         return corrections;
173 }
174
175 void set_corrections(Display *display, GeometryCorrection *corrections)
176 {
177         unsigned ncorrections;
178         unsigned total_len;
179         unsigned i;
180         char *names;
181         char *name_ptr;
182         short *values;
183         Window root;
184         Atom monitors_atom;
185         Atom correction_atom;
186
187         ncorrections = 0;
188         total_len = 0;
189         for(i=0; corrections[i].monitor_name; ++i)
190         {
191                 ++ncorrections;
192                 total_len += strlen(corrections[i].monitor_name);
193         }
194
195         names = (char *)malloc(total_len+ncorrections);
196         values = (short *)malloc(ncorrections*5*sizeof(short));
197         name_ptr = names;
198         for(i=0; i<ncorrections; ++i)
199         {
200                 strcpy(name_ptr, corrections[i].monitor_name);
201                 name_ptr += strlen(name_ptr)+1;
202
203                 values[i*5] = corrections[i].keystone_vertical*4096;
204                 values[i*5+1] = corrections[i].curvature_type;
205                 values[i*5+2] = corrections[i].curvature_depth*4096;
206                 values[i*5+3] = corrections[i].vertical_center*4096;
207                 values[i*5+4] = corrections[i].perspective*4096;
208         }
209
210         root = DefaultRootWindow(display);
211         monitors_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION_MONITORS", False);
212         correction_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION", False);
213         XChangeProperty(display, root, monitors_atom, XA_STRING, 8, PropModeReplace, (unsigned char *)names, total_len+ncorrections-1);
214         XChangeProperty(display, root, correction_atom, XA_INTEGER, 16, PropModeReplace, (unsigned char *)values, ncorrections*5);
215
216         free(names);
217         free(values);
218 }
219
220 unsigned create_shader(GLenum type, const char *src)
221 {
222         unsigned shader;
223
224         shader = glCreateShader(type);
225         glShaderSource(shader, 1, &src, NULL);
226         glCompileShader(shader);
227
228         return shader;
229 }
230
231 unsigned create_program(unsigned *shaders, unsigned nshaders)
232 {
233         unsigned program;
234         unsigned i;
235
236         program = glCreateProgram();
237         for(i=0; i<nshaders; ++i)
238                 glAttachShader(program, shaders[i]);
239         glLinkProgram(program);
240
241         glUseProgram(program);
242         glUniformMatrix4fv(glGetUniformLocation(program, "view"), 1, GL_FALSE, view_matrix);
243         glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, projection_matrix);
244
245         return program;
246 }
247
248 int initialize_view(Display *display, InteractiveView *view)
249 {
250         int major_ver;
251         int minor_ver;
252         int attribs[5];
253         unsigned i;
254         GLXFBConfig *configs;
255         int nconfigs;
256         XVisualInfo *vi;
257         XSetWindowAttributes win_attr;
258         Window root;
259
260         if(!glXQueryVersion(display, &major_ver, &minor_ver) || major_ver<1 || (major_ver==1 && minor_ver<4))
261                 return 0;
262
263         i = 0;
264         attribs[i++] = GLX_DOUBLEBUFFER;
265         attribs[i++] = True;
266         attribs[i++] = GLX_DEPTH_SIZE;
267         attribs[i++] = 16;
268         attribs[i] = None;
269
270         configs = glXChooseFBConfig(display, DefaultScreen(display), attribs, &nconfigs);
271         if(!configs || !nconfigs)
272                 return 0;
273
274         vi = glXGetVisualFromFBConfig(display, configs[0]);
275         root = DefaultRootWindow(display);
276         win_attr.colormap = XCreateColormap(display, root, vi->visual, AllocNone);
277         view->window = XCreateWindow(display, root, 0, 0, 1024, 768, 0, vi->depth, InputOutput, vi->visual, CWColormap, &win_attr);
278         XStoreName(display, view->window, "Geometry correction control");
279         XMapWindow(display, view->window);
280
281         view->glx_window = glXCreateWindow(display, configs[0], view->window, NULL);
282         view->glx_context = glXCreateNewContext(display, configs[0], GLX_RGBA_TYPE, NULL, True);
283         glXMakeContextCurrent(display, view->glx_window, view->glx_window, view->glx_context);
284
285         XFree(configs);
286         XFree(vi);
287
288         view->shaders[0] = create_shader(GL_VERTEX_SHADER, vshader_src);
289         view->shaders[1] = create_shader(GL_GEOMETRY_SHADER, gshader_src);
290         view->shaders[2] = create_shader(GL_FRAGMENT_SHADER, fshader_src);
291         view->programs[0] = create_program(view->shaders, 3);
292         view->shaders[3] = create_shader(GL_VERTEX_SHADER, flat_vshader_src);
293         view->shaders[4] = create_shader(GL_FRAGMENT_SHADER, flat_fshader_src);
294         view->programs[1] = create_program(view->shaders+3, 2);
295         glUniform4f(glGetUniformLocation(view->programs[1], "color"), 0.0f, 0.6f, 1.0f, 1.0f);
296
297         glGenBuffers(2, view->buffers);
298         glBindBuffer(GL_ARRAY_BUFFER, view->buffers[0]);
299         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), NULL);
300         glEnableVertexAttribArray(0);
301         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, view->buffers[1]);
302
303         glEnable(GL_PRIMITIVE_RESTART);
304         glPrimitiveRestartIndex(0xFFFF);
305
306         glEnable(GL_DEPTH_TEST);
307         glDepthFunc(GL_LEQUAL);
308
309         view->geometry_data_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION_DATA", False);
310         XSelectInput(display, view->window, KeyPressMask|ExposureMask);
311
312         return 1;
313 }
314
315 void request_geometry_data(Display *display, InteractiveView *view, GeometryCorrection *target)
316 {
317         XChangeProperty(display, view->window, view->geometry_data_atom, XA_STRING, 8, PropModeReplace, (unsigned char *)target->monitor_name, strlen(target->monitor_name));
318         XConvertSelection(display, view->geometry_data_atom, view->geometry_data_atom, view->geometry_data_atom, view->window, CurrentTime);
319 }
320
321 void update_view(Display *display, InteractiveView *view, XSelectionEvent *event)
322 {
323         Atom prop_type;
324         int prop_format;
325         unsigned long overflow;
326         unsigned long data_length;
327         short *geometry_data;
328         unsigned tessellation;
329         unsigned nvertices;
330         float *vertex_data;
331         unsigned nindices;
332         unsigned short *index_data;
333         unsigned i;
334         int x, y;
335
336         XGetWindowProperty(display, event->requestor, event->property, 0, 32262, False, XA_INTEGER,
337                 &prop_type, &prop_format, &data_length, &overflow, (unsigned char **)&geometry_data);
338
339         tessellation = geometry_data[0];
340         nvertices = 5+(tessellation+1)*(tessellation+1);
341
342         vertex_data = (float *)malloc(nvertices*3*sizeof(float));
343         for(i=0; i<nvertices*3; ++i)
344                 vertex_data[i] = geometry_data[1+i]/4096.0f;
345
346         view->nelements = tessellation*((tessellation+1)*2+1)-1;
347
348         nindices = 12+view->nelements;
349         index_data = (unsigned short *)malloc(nindices*sizeof(unsigned short));
350         memcpy(index_data, frustum_indices, sizeof(frustum_indices));
351         i = 12;
352         for(y=0; y<(int)tessellation; ++y)
353         {
354                 if(y>0)
355                         index_data[i++] = 0xFFFF;
356                 for(x=0; x<=(int)tessellation; ++x)
357                 {
358                         index_data[i++] = 5+(y+1)*(tessellation+1)+x;
359                         index_data[i++] = 5+y*(tessellation+1)+x;
360                 }
361         }
362
363         glBufferData(GL_ARRAY_BUFFER, nvertices*3*sizeof(float), vertex_data, GL_STATIC_DRAW);
364         glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices*sizeof(unsigned short), index_data, GL_STATIC_DRAW);
365
366         free(vertex_data);
367         free(index_data);
368         XFree(geometry_data);
369 }
370
371 int interactive(Display *display, GeometryCorrection *corrections, GeometryCorrection *target)
372 {
373         InteractiveView view;
374         int update_pending;
375         int done;
376
377         initialize_view(display, &view);
378         request_geometry_data(display, &view, target);
379
380         done = 0;
381         update_pending = 0;
382         while(!done)
383         {
384                 XEvent event;
385                 KeySym keysym;
386
387                 XNextEvent(display, &event);
388
389                 switch(event.type)
390                 {
391                 case KeyPress:
392                         keysym = XLookupKeysym(&event.xkey, 0);
393                         if(keysym==XK_Escape)
394                         {
395                                 done = 1;
396                                 break;
397                         }
398                         else if(update_pending)
399                                 break;
400                         else if(keysym==XK_q)
401                                 target->keystone_vertical += 1.0f/64;
402                         else if(keysym==XK_a)
403                                 target->keystone_vertical -= 1.0f/64;
404                         else if(keysym==XK_w)
405                                 target->curvature_depth += 1.0f/256;
406                         else if(keysym==XK_s)
407                                 target->curvature_depth -= 1.0f/256;
408                         else if(keysym==XK_e)
409                                 target->vertical_center += 1.0f/32;
410                         else if(keysym==XK_d)
411                                 target->vertical_center -= 1.0f/32;
412                         else if(keysym==XK_r)
413                                 target->perspective += 1.0f/16;
414                         else if(keysym==XK_f)
415                                 target->perspective -= 1.0f/16;
416                         else if(keysym==XK_z)
417                                 target->curvature_type = target->curvature_type%2+1;
418                         else
419                                 break;
420
421                         set_corrections(display, corrections);
422                         request_geometry_data(display, &view, target);
423                         update_pending = 1;
424
425                         break;
426                 case SelectionNotify:
427                         if(event.xselection.property==view.geometry_data_atom)
428                         {
429                                 update_view(display, &view, &event.xselection);
430                                 update_pending = 0;
431                         }
432                         break;
433                 default:
434                         printf("event %d\n", event.type);
435                         break;
436                 }
437
438                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
439                 glUseProgram(view.programs[0]);
440                 glDrawElements(GL_TRIANGLE_STRIP, view.nelements, GL_UNSIGNED_SHORT, (void *)(12*sizeof(unsigned short)));
441                 glUseProgram(view.programs[1]);
442                 glDrawElements(GL_LINE_STRIP, 11, GL_UNSIGNED_SHORT, NULL);
443                 glXSwapBuffers(display, view.glx_window);
444         }
445
446         return 0;
447 }
448
449 int main(int argc, char **argv)
450 {
451         Display *display;
452         GeometryCorrection *corrections;
453         unsigned i;
454
455         if(argc!=2 && argc!=7)
456         {
457                 fprintf(stderr, "Usage: %s <monitor> [<keystone> <curvature> <depth> <vcenter> <perspective>]\n", argv[0]);
458                 return 1;
459         }
460
461         display = XOpenDisplay(NULL);
462
463         corrections = get_corrections(display);
464         if(corrections)
465         {
466                 for(i=0; corrections[i].monitor_name; ++i)
467                         if(!strcmp(corrections[i].monitor_name, argv[1]))
468                                 break;
469         }
470         else
471                 i = 0;
472
473         if(!corrections || !corrections[i].monitor_name)
474         {
475                 unsigned namelen;
476
477                 corrections = (GeometryCorrection *)realloc(corrections, (i+2)*sizeof(GeometryCorrection));
478                 namelen = strlen(argv[1]);
479                 corrections[i].monitor_name = (char *)malloc(namelen+1);
480                 strcpy(corrections[i].monitor_name, argv[1]);
481                 corrections[i+1].monitor_name = NULL;
482                 corrections[i].keystone_vertical = 0.0f;
483                 corrections[i].curvature_type = 1;
484                 corrections[i].curvature_depth = 0.0f;
485                 corrections[i].vertical_center = 0.5f;
486                 corrections[i].perspective = 1.0f;
487         }
488
489         if(argc==2)
490                 interactive(display, corrections, &corrections[i]);
491         else if(argc==7)
492         {
493                 if(!strcmp(argv[3], "cylindrical"))
494                         corrections[i].curvature_type = 1;
495                 else if(!strcmp(argv[3], "spherical"))
496                         corrections[i].curvature_type = 2;
497                 else
498                 {
499                         fprintf(stderr, "Invalid curvature\n");
500                         return 1;
501                 }
502                 corrections[i].keystone_vertical = strtod(argv[2], NULL);
503                 corrections[i].curvature_depth = strtod(argv[4], NULL);
504                 corrections[i].vertical_center = strtod(argv[5], NULL);
505                 corrections[i].perspective = strtod(argv[6], NULL);
506
507                 set_corrections(display, corrections);
508         }
509
510         XCloseDisplay(display);
511         for(i=0; corrections[i].monitor_name; ++i)
512                 free(corrections[i].monitor_name);
513         free(corrections);
514
515         return 0;
516 }