#include <stdlib.h>
#include <string.h>
#include <signal.h>
+#include <math.h>
#include <X11/Xlib.h>
+#include <X11/Xatom.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdamage.h>
#include <X11/extensions/shape.h>
typedef struct CompositedMonitor
{
+ char *name;
int enabled;
int x;
int y;
unsigned width;
unsigned height;
+ float keystone_vertical;
+ float cylinder_depth;
+ float vertical_center;
+ float perspective;
unsigned vertex_buffer;
unsigned index_buffer;
unsigned vertex_array;
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
PFNGLXBINDTEXIMAGEEXTPROC glXBindTexImageEXT;
PFNGLXRELEASETEXIMAGEEXTPROC glXReleaseTexImageEXT;
+ Atom correction_atom;
+ Atom monitors_atom;
int dirty;
} Compositor;
unsigned short *index_data;
unsigned x, y;
unsigned i;
+ float aspect;
+ float cyl_w_sq;
+ float sin_ksv;
+ float cos_ksv;
t = monitor->tessellation;
data_size = (t+1)*(t+1)*4*sizeof(float);
vertex_data = (float *)malloc(data_size);
+ aspect = (float)monitor->width/monitor->height;
+ cyl_w_sq = 0.25f-(0.5f-monitor->cylinder_depth)*(0.5f-monitor->cylinder_depth);
+ sin_ksv = monitor->keystone_vertical/sqrt(1.0f+monitor->keystone_vertical*monitor->keystone_vertical);
+ cos_ksv = sqrt(1.0f-sin_ksv*sin_ksv);
for(y=0; y<=t; ++y)
for(x=0; x<=t; ++x)
{
- float xf, yf;
+ float xf, yf, z, rz;
+ float scale;
float *v;
xf = (float)x/t;
yf = (float)y/t;
+
v = vertex_data+(y*(t+1)+x)*4;
- v[0] = xf;
- v[1] = yf;
v[2] = (monitor->x+xf*monitor->width)/screen->width;
- v[3] = (monitor->y+yf*monitor->height)/screen->height;
+ v[3] = ((float)screen->height-monitor->y-monitor->height+yf*monitor->height)/screen->height;
+
+ z = sqrt(0.25f-(xf-0.5f)*(xf-0.5f)*4.0f*cyl_w_sq)-0.5f+monitor->cylinder_depth;
+ if(monitor->keystone_vertical>0)
+ {
+ rz = (1.0f-yf)/aspect*sin_ksv+z*cos_ksv;
+ yf = 1.0f-((1.0f-yf)*cos_ksv-z*aspect*sin_ksv);
+ z = rz;
+ }
+ else if(monitor->keystone_vertical<0)
+ {
+ rz = z*cos_ksv-yf/aspect*sin_ksv;
+ yf = yf*cos_ksv+z*aspect*sin_ksv;
+ z = rz;
+ }
+ scale = monitor->perspective/(monitor->perspective+z);
+ v[0] = 0.5f+(xf-0.5f)*scale;
+ v[1] = monitor->vertical_center+(yf-monitor->vertical_center)*scale;
}
glBindBuffer(GL_ARRAY_BUFFER, monitor->vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, data_size, vertex_data, GL_STATIC_DRAW);
free(index_data);
}
-int initialize_monitor(Compositor *compositor, CompositedScreen *screen, CompositedMonitor *monitor, XRRScreenResources *xrr_res, unsigned index)
+int initialize_monitor(Compositor *compositor, CompositedScreen *screen, XRRScreenResources *xrr_res, unsigned index)
{
+ CompositedMonitor *monitor;
XRROutputInfo *output;
+ int namelen;
XRRCrtcInfo *crtc;
unsigned buffers[2];
unsigned stride;
+ monitor = &screen->monitors[index];
+
output = XRRGetOutputInfo(compositor->display, xrr_res, xrr_res->outputs[index]);
+ namelen = strlen(output->name);
+ monitor->name = (char *)malloc(namelen+1);
+ strcpy(monitor->name, output->name);
monitor->enabled = !!output->crtc;
if(!monitor->enabled)
{
XRRFreeCrtcInfo(crtc);
XRRFreeOutputInfo(output);
+ monitor->keystone_vertical = 0.0f;
+ monitor->cylinder_depth = 0.0f;
+ monitor->vertical_center = 0.5f;
+ monitor->perspective = 1.0f;
+
glGenBuffers(2, buffers);
monitor->vertex_buffer = buffers[0];
monitor->index_buffer = buffers[1];
- monitor->tessellation = 20;
+ monitor->tessellation = 50;
update_monitor_vertices(screen, monitor);
stride = 4*sizeof(float);
return 1;
}
+CompositedMonitor *find_monitor_by_name(CompositedScreen *screen, char *name)
+{
+ unsigned i;
+
+ for(i=0; i<screen->nmonitors; ++i)
+ if(!strcmp(screen->monitors[i].name, name))
+ return &screen->monitors[i];
+
+ return NULL;
+}
+
+void update_geometry_correction(Compositor *compositor, CompositedScreen *screen)
+{
+ Atom prop_type;
+ int prop_format;
+ unsigned long overflow;
+ unsigned long names_length;
+ char *names;
+ unsigned long values_length;
+ short *values;
+ char *name_ptr;
+ unsigned i;
+
+ XGetWindowProperty(compositor->display, screen->root, compositor->monitors_atom, 0, 64, False, XA_STRING,
+ &prop_type, &prop_format, &names_length, &overflow, (unsigned char **)&names);
+ if(prop_type!=XA_STRING || prop_format!=8)
+ return;
+
+ XGetWindowProperty(compositor->display, screen->root, compositor->correction_atom, 0, 64, False, XA_INTEGER,
+ &prop_type, &prop_format, &values_length, &overflow, (unsigned char **)&values);
+ if(prop_type!=XA_INTEGER || prop_format!=16)
+ {
+ XFree(names);
+ return;
+ }
+
+ use_gl(compositor, screen);
+
+ name_ptr = names;
+ for(i=0; i*4<values_length; ++i)
+ {
+ CompositedMonitor *monitor;
+
+ if(name_ptr>=names+names_length)
+ break;
+
+ monitor = find_monitor_by_name(screen, name_ptr);
+ if(monitor)
+ {
+ monitor->keystone_vertical = values[i*4]/10000.0f;
+ monitor->cylinder_depth = values[i*4+1]/10000.0f;
+ monitor->vertical_center = values[i*4+2]/10000.0f;
+ monitor->perspective = values[i*4+3]/10000.0f;
+
+ update_monitor_vertices(screen, monitor);
+ }
+
+ name_ptr += strlen(name_ptr)+1;
+ }
+
+ XFree(names);
+ XFree(values);
+}
+
int initialize_screen(Compositor *compositor, unsigned number)
{
CompositedScreen *screen;
return with_error("GLX_EXT_texture_from_pixmap is required");
screen->root = RootWindow(compositor->display, screen->number);
- XSelectInput(compositor->display, screen->root, SubstructureNotifyMask);
+ XSelectInput(compositor->display, screen->root, SubstructureNotifyMask|PropertyChangeMask);
screen->overlay = XCompositeGetOverlayWindow(compositor->display, screen->root);
XGetGeometry(compositor->display, screen->overlay, &dummy_root, &x, &y, &screen->width, &screen->height, &border, &depth);
XShapeCombineRectangles(compositor->display, screen->overlay, ShapeInput, 0, 0, NULL, 0, ShapeSet, Unsorted);
screen->nmonitors = xrr_res->noutput;
screen->monitors = (CompositedMonitor *)malloc(screen->nmonitors*sizeof(CompositedMonitor));
for(i=0; i<screen->nmonitors; ++i)
- if(!initialize_monitor(compositor, screen, &screen->monitors[i], xrr_res, i))
+ if(!initialize_monitor(compositor, screen, xrr_res, i))
return 0;
XRRFreeScreenResources(xrr_res);
+ update_geometry_correction(compositor, screen);
+
XQueryTree(compositor->display, screen->root, &dummy_root, &dummy_parent, &children, &nchildren);
screen->windows = (CompositedWindow *)malloc(nchildren*sizeof(CompositedWindow));
compositor->glXBindTexImageEXT = (PFNGLXBINDTEXIMAGEEXTPROC)glXGetProcAddress((unsigned char *)"glXBindTexImageEXT");
compositor->glXReleaseTexImageEXT = (PFNGLXRELEASETEXIMAGEEXTPROC)glXGetProcAddress((unsigned char *)"glXReleaseTexImageEXT");
+ compositor->correction_atom = XInternAtom(compositor->display, "GEOMETRY_CORRECTION", False);
+ compositor->monitors_atom = XInternAtom(compositor->display, "GEOMETRY_CORRECTION_MONITORS", False);
+
compositor->nscreens = ScreenCount(compositor->display);
compositor->screens = (CompositedScreen *)malloc(compositor->nscreens*sizeof(CompositedScreen));
for(i=0; i<compositor->nscreens; ++i)
}
for(i=0; i<screen->nmonitors; ++i)
+ {
+ free(screen->monitors[i].name);
if(screen->monitors[i].enabled)
{
glDeleteBuffers(1, &screen->monitors[i].vertex_buffer);
glDeleteBuffers(1, &screen->monitors[i].index_buffer);
glDeleteVertexArrays(1, &screen->monitors[i].vertex_array);
}
+ }
glDeleteBuffers(1, &screen->window_vertex_buffer);
glDeleteVertexArrays(1, &screen->window_vertex_array);
mark_dirty(compositor, screen);
}
+void process_property_event(Compositor *compositor, XPropertyEvent *event)
+{
+ CompositedScreen *screen;
+
+ if(event->atom!=compositor->correction_atom)
+ return;
+
+ screen = find_screen_by_root(compositor, event->window);
+ if(!screen)
+ return;
+
+ update_geometry_correction(compositor, screen);
+}
+
void process_damage_event(Compositor *compositor, XDamageNotifyEvent *event)
{
CompositedScreen *screen;
case ConfigureNotify:
process_configure_event(compositor, &event.xconfigure);
break;
+ case PropertyNotify:
+ process_property_event(compositor, &event.xproperty);
+ break;
default:
if(event.type==compositor->damage_event+XDamageNotify)
process_damage_event(compositor, (XDamageNotifyEvent *)&event);
continue;
glUniform4f(screen->geometry_loc,
- (float)monitor->x/screen->width, (float)monitor->y/screen->height,
+ (float)monitor->x/screen->width, ((float)screen->height-monitor->y-monitor->height)/screen->height,
(float)monitor->width/screen->width, (float)monitor->height/screen->height);
glBindVertexArray(monitor->vertex_array);
glDrawElements(GL_TRIANGLE_STRIP, monitor->nelements, GL_UNSIGNED_SHORT, NULL);
}
+ glBindVertexArray(0);
+
glXSwapBuffers(compositor->display, screen->glx_window);
screen->dirty = 0;