xglcomp
view src/main.cc @ 2:876efea9424c
OpenGL
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 22 Jan 2016 06:31:03 +0200 |
parents | b2b7cb950c28 |
children | e831d38e6faa |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <X11/Xlib.h>
5 #include <X11/Xutil.h>
6 #include <X11/Xatom.h>
7 #include <X11/extensions/Xcomposite.h>
8 #include <X11/extensions/Xdamage.h>
9 #include "cwin.h"
10 #include "opengl.h"
11 #include "logger.h"
13 static bool register_compositor();
14 static void start_comp();
15 static void manage_window(Window xwin);
16 static void unmanage_window(Window xwin);
17 static void redraw();
18 static void reshape(int x, int y);
20 Display *dpy;
21 int screen_num;
22 Window root_win, comp_win;
23 int root_width, root_height;
25 int main(int argc, char **argv)
26 {
27 if(!(dpy = XOpenDisplay(0))) {
28 log_error("failed to open X display\n");
29 return 1;
30 }
31 screen_num = DefaultScreen(dpy);
32 root_win = RootWindow(dpy, screen_num);
33 root_width = DisplayWidth(dpy, screen_num);
34 root_height = DisplayHeight(dpy, screen_num);
35 log_info("display size %dx%d\n", root_width, root_height);
37 int xcomp_ev_base, xcomp_err_base;
38 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
39 log_error("X server doesn't support the composite extension\n");
40 return 1;
41 }
42 int xcomp_ver_major = 0, xcomp_ver_minor = 4;
43 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
44 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
45 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
46 // for NameWindowPixmap & CompositeGetoverlayWindow
47 log_error("I need at least version 0.3\n");
48 return 1;
49 }
51 int xdmg_ev_base, xdmg_err_base;
52 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
53 log_error("X server doesn't support the damage extension\n");
54 return 1;
55 }
56 // TODO also XFixes ?
58 if(!register_compositor()) {
59 return 1;
60 }
62 comp_win = XCompositeGetOverlayWindow(dpy, root_win);
63 if(!create_gl(comp_win)) {
64 return 1;
65 }
67 start_comp();
68 reshape(root_width, root_height);
69 redraw();
71 for(;;) {
72 XEvent ev;
73 XNextEvent(dpy, &ev);
75 switch(ev.type) {
76 case CreateNotify:
77 manage_window(ev.xcreatewindow.window);
78 break;
80 case ConfigureNotify:
81 break;
83 case DestroyNotify:
84 unmanage_window(ev.xdestroywindow.window);
85 break;
87 case MapNotify:
88 break;
89 case UnmapNotify:
90 break;
92 case ReparentNotify:
93 if(ev.xreparent.parent == root_win) {
94 manage_window(ev.xreparent.window);
95 } else {
96 unmanage_window(ev.xreparent.window);
97 }
98 break;
100 case CirculateNotify:
101 break;
103 case Expose:
104 if(ev.xexpose.window == root_win) {
105 log_debug("expose\n");
106 // TODO
107 }
108 break;
110 default:
111 if(ev.type == xdmg_ev_base + XDamageNotify) {
112 }
113 }
114 }
115 }
117 static bool register_compositor()
118 {
119 char atom_name[64];
120 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
121 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
123 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
124 if(win != None) {
125 log_error("Another compositor is running. Stop it and try again.\n");
126 return false;
127 }
129 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
130 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
131 return true;
132 }
134 static void start_comp()
135 {
136 XGrabServer(dpy);
137 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
138 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
140 // manage all top-level windows
141 Window root_ret, parent;
142 Window *children;
143 unsigned int num_children;
144 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
146 for(unsigned int i=0; i<num_children; i++) {
147 manage_window(children[i]);
148 }
149 log_info("starting compositor, managing %u top-level windows\n", num_children);
150 XFree(children);
152 XUngrabServer(dpy);
153 }
155 static void manage_window(Window xwin)
156 {
157 //log_debug("manage_window %u\n", xwin);
158 CompWindow *cwin = new CompWindow(xwin);
159 XGetWindowAttributes(dpy, xwin, &cwin->attr);
160 add_window(cwin);
161 }
163 static void unmanage_window(Window xwin)
164 {
165 //log_debug("unmanage_window %u\n", xwin);
166 CompWindow *cwin = find_window_xid(xwin);
167 if(cwin) {
168 remove_window(cwin);
169 delete cwin;
170 }
171 }
173 static void redraw()
174 {
175 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
177 glEnable(GL_BLEND);
178 glBlendFunc(GL_ONE, GL_ONE);
180 int num_win = get_window_count();
181 for(int i=0; i<num_win; i++) {
182 CompWindow *cwin = get_window(i);
184 glBegin(GL_QUADS);
185 glColor3f(0.4, 0.1, 0.05);
186 glVertex2f(cwin->attr.x, cwin->attr.y);
187 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
188 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
189 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
190 glEnd();
191 }
193 glDisable(GL_BLEND);
195 glXSwapBuffers(dpy, comp_win);
196 }
198 static void reshape(int x, int y)
199 {
200 glViewport(0, 0, x, y);
202 glMatrixMode(GL_PROJECTION);
203 glLoadIdentity();
204 glOrtho(0, x, y, 0, -1, 1);
205 }