From 42ed6047036a36fdfc063d83583158d2ec067334 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 20 Dec 2015 02:06:32 +0200 Subject: [PATCH] Dynamically reconfigure monitors based on XRandR changes --- source/main.c | 148 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 119 insertions(+), 29 deletions(-) diff --git a/source/main.c b/source/main.c index cd2f1ac..ea508d2 100644 --- a/source/main.c +++ b/source/main.c @@ -98,6 +98,7 @@ typedef struct Compositor unsigned nscreens; int damage_event; int shape_event; + int randr_event; PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB; PFNGLXBINDTEXIMAGEEXTPROC glXBindTexImageEXT; PFNGLXRELEASETEXIMAGEEXTPROC glXReleaseTexImageEXT; @@ -596,6 +597,48 @@ CompositedScreen *find_screen_by_root(Compositor *compositor, Window root) return NULL; } +void create_geometry_correction(CompositedMonitor *monitor) +{ + unsigned buffers[2]; + unsigned stride; + + glGenBuffers(2, buffers); + monitor->vertex_buffer = buffers[0]; + monitor->index_buffer = buffers[1]; + + monitor->tessellation = 50; + monitor->geometry_data_size = (monitor->tessellation+1)*(monitor->tessellation+1)*3+16; + monitor->geometry_data = (short *)malloc(monitor->geometry_data_size*sizeof(short)); + stride = 4*sizeof(float); + + glGenVertexArrays(1, &monitor->vertex_array); + glBindVertexArray(monitor->vertex_array); + glBindBuffer(GL_ARRAY_BUFFER, monitor->vertex_buffer); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, stride, NULL); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void *)(2*sizeof(float))); + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, monitor->index_buffer); + glBindVertexArray(0); +} + +void free_geometry_correction(CompositedMonitor *monitor) +{ + unsigned buffers[2]; + + buffers[0] = monitor->vertex_buffer; + buffers[1] = monitor->index_buffer; + glDeleteBuffers(2, buffers); + monitor->vertex_buffer = 0; + monitor->index_buffer = 0; + + glDeleteVertexArrays(1, &monitor->vertex_array); + + free(monitor->geometry_data); + monitor->geometry_data = 0; +} + void update_monitor_vertices(CompositedScreen *screen, CompositedMonitor *monitor) { unsigned t; @@ -735,8 +778,6 @@ int initialize_monitor(Compositor *compositor, CompositedScreen *screen, XRRScre XRROutputInfo *output; int namelen; XRRCrtcInfo *crtc; - unsigned buffers[2]; - unsigned stride; monitor = &screen->monitors[index]; @@ -748,6 +789,9 @@ int initialize_monitor(Compositor *compositor, CompositedScreen *screen, XRRScre if(!monitor->enabled) { XRRFreeOutputInfo(output); + monitor->vertex_buffer = 0; + monitor->index_buffer = 0; + monitor->vertex_array = 0; monitor->geometry_data = NULL; return 1; } @@ -766,26 +810,8 @@ int initialize_monitor(Compositor *compositor, CompositedScreen *screen, XRRScre monitor->vertical_center = 0.5f; monitor->perspective = 1.0f; - glGenBuffers(2, buffers); - monitor->vertex_buffer = buffers[0]; - monitor->index_buffer = buffers[1]; - - monitor->tessellation = 50; - monitor->geometry_data_size = (monitor->tessellation+1)*(monitor->tessellation+1)*3+16; - monitor->geometry_data = (short *)malloc(monitor->geometry_data_size*sizeof(short)); + create_geometry_correction(monitor); update_monitor_vertices(screen, monitor); - stride = 4*sizeof(float); - - glGenVertexArrays(1, &monitor->vertex_array); - glBindVertexArray(monitor->vertex_array); - glBindBuffer(GL_ARRAY_BUFFER, monitor->vertex_buffer); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, stride, NULL); - glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void *)(2*sizeof(float))); - glEnableVertexAttribArray(1); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, monitor->index_buffer); - glBindVertexArray(0); return 1; } @@ -819,6 +845,50 @@ CompositedMonitor *find_monitor_by_name_global(Compositor *compositor, char *nam return NULL; } +void update_monitors(Compositor *compositor, CompositedScreen *screen) +{ + XRRScreenResources *xrr_res; + int i; + + xrr_res = XRRGetScreenResources(compositor->display, screen->root); + for(i=0; inoutput; ++i) + { + XRROutputInfo *output; + CompositedMonitor *monitor; + int was_enabled; + XRRCrtcInfo *crtc; + + output = XRRGetOutputInfo(compositor->display, xrr_res, xrr_res->outputs[i]); + monitor = find_monitor_by_name(screen, output->name); + if(!monitor) + continue; + + was_enabled = monitor->enabled; + monitor->enabled = !!output->crtc; + if(!monitor->enabled) + { + XRRFreeOutputInfo(output); + if(was_enabled) + free_geometry_correction(monitor); + continue; + } + + crtc = XRRGetCrtcInfo(compositor->display, xrr_res, output->crtc); + monitor->x = crtc->x; + monitor->y = crtc->y; + monitor->width = crtc->width; + monitor->height = crtc->height; + XRRFreeCrtcInfo(crtc); + XRRFreeOutputInfo(output); + + if(!was_enabled) + create_geometry_correction(monitor); + + update_monitor_vertices(screen, monitor); + } + XRRFreeScreenResources(xrr_res); +} + void update_geometry_correction(Compositor *compositor, CompositedScreen *screen) { Atom prop_type; @@ -939,6 +1009,7 @@ int initialize_screen(Compositor *compositor, unsigned number) screen->root = RootWindow(compositor->display, screen->number); XSelectInput(compositor->display, screen->root, SubstructureNotifyMask|PropertyChangeMask); screen->overlay = XCompositeGetOverlayWindow(compositor->display, screen->root); + XSelectInput(compositor->display, screen->overlay, StructureNotifyMask); 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); @@ -957,6 +1028,7 @@ int initialize_screen(Compositor *compositor, unsigned number) if(!initialize_monitor(compositor, screen, xrr_res, i)) return 0; XRRFreeScreenResources(xrr_res); + XRRSelectInput(compositor->display, screen->root, RRScreenChangeNotifyMask); screen->root_pixmap = None; screen->root_glx_pixmap = None; @@ -1022,7 +1094,7 @@ int initialize_compositor(Compositor *compositor) else if(major_ver<1 || (major_ver==1 && minor_ver<1)) return with_error("XShape 1.1 or later is required"); - if(!XRRQueryExtension(compositor->display, &event_base, &error_base)) + if(!XRRQueryExtension(compositor->display, &compositor->randr_event, &error_base)) return with_error("XRandR is required but was not found"); else if(!XRRQueryVersion(compositor->display, &major_ver, &minor_ver)) return with_error("Cannot determine XRandR version"); @@ -1074,13 +1146,7 @@ void shutdown_screen(Compositor *compositor, CompositedScreen *screen) { 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); - } - if(screen->monitors[i].geometry_data) - free(screen->monitors[i].geometry_data); + free_geometry_correction(&screen->monitors[i]); } glDeleteTextures(1, &screen->root_texture); @@ -1215,6 +1281,19 @@ void process_configure_event(Compositor *compositor, XConfigureEvent *event) if(!screen) return; + if(event->window==screen->overlay) + { + screen->width = event->width; + screen->height = event->height; + + XResizeWindow(compositor->display, screen->render_window, screen->width, screen->height); + use_gl(compositor, screen); + glBindTexture(GL_TEXTURE_2D, screen->fb_texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screen->width, screen->height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + return; + } + window = find_window(screen, event->window); if(!window) return; @@ -1306,6 +1385,14 @@ void process_shape_event(Compositor *compositor, XShapeEvent *event) } } +void process_randr_event(Compositor *compositor, XRRScreenChangeNotifyEvent *event) +{ + CompositedScreen *screen; + + if((screen = find_screen_by_root(compositor, event->root))) + update_monitors(compositor, screen); +} + void process_events(Compositor *compositor) { int pending; @@ -1352,6 +1439,8 @@ void process_events(Compositor *compositor) process_damage_event(compositor, (XDamageNotifyEvent *)&event); else if(event.type==compositor->shape_event+ShapeNotify) process_shape_event(compositor, (XShapeEvent *)&event); + else if(event.type==compositor->randr_event+RRScreenChangeNotify) + process_randr_event(compositor, (XRRScreenChangeNotifyEvent *)&event); } } } @@ -1368,6 +1457,7 @@ void refresh_screen(Compositor *compositor, CompositedScreen *screen) use_gl(compositor, screen); + glViewport(0, 0, screen->width, screen->height); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, screen->framebuffer); -- 2.45.2