doorbell
view spycam/src/main.cc @ 0:a5755687dd75
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 08 Mar 2016 03:26:01 +0200 |
parents | |
children | daade2a35e69 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/select.h>
5 #include <X11/Xlib.h>
6 #include <X11/Xatom.h>
7 #include <GLES2/gl2.h>
8 #include <EGL/egl.h>
9 #include "spycam.h"
11 static void cleanup();
12 static bool create_glwin(int xsz, int ysz);
13 static bool handle_event(XEvent *ev);
14 static void set_window_title(const char *title);
16 static int win_width, win_height;
17 static bool quit, redraw_pending;
18 static Display *dpy;
19 static Window win;
20 static EGLDisplay egl_dpy;
21 static EGLContext egl_ctx;
22 static EGLSurface egl_win;
23 static Atom xa_wm_proto, xa_del_window;
25 int main(int argc, char **argv)
26 {
27 if(!(dpy = XOpenDisplay(0))) {
28 fprintf(stderr, "failed to connect to the X server.\n");
29 return 1;
30 }
31 xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", False);
32 xa_del_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
34 egl_dpy = eglGetDisplay(dpy);
35 int egl_ver_major, egl_ver_minor;
36 if(!eglInitialize(egl_dpy, &egl_ver_major, &egl_ver_minor)) {
37 fprintf(stderr, "failed to initialize EGL\n");
38 return 1;
39 }
40 printf("initialized EGL %d.%d\n", egl_ver_major, egl_ver_minor);
42 if(!create_glwin(800, 600)) {
43 cleanup();
44 return 1;
45 }
47 if(!app_init()) {
48 cleanup();
49 return 1;
50 }
52 for(;;) {
53 while(XPending(dpy) || !redraw_pending) {
54 XEvent ev;
55 XNextEvent(dpy, &ev);
56 if(!handle_event(&ev) || quit) {
57 goto break_main_loop;
58 }
59 }
61 if(redraw_pending) {
62 app_draw();
63 eglSwapBuffers(egl_dpy, egl_win);
64 }
65 }
66 break_main_loop:
68 cleanup();
69 return 0;
70 }
72 void app_quit()
73 {
74 quit = true;
75 }
77 void app_redisplay()
78 {
79 redraw_pending = true;
80 }
82 static void cleanup()
83 {
84 if(egl_dpy) {
85 eglMakeCurrent(egl_dpy, 0, 0, 0);
86 if(egl_win)
87 eglDestroySurface(egl_dpy, egl_win);
88 if(egl_ctx)
89 eglDestroyContext(egl_dpy, egl_ctx);
90 eglTerminate(egl_dpy);
91 }
92 if(dpy) {
93 if(win)
94 XDestroyWindow(dpy, win);
95 XCloseDisplay(dpy);
96 }
97 }
99 static bool create_glwin(int xsz, int ysz)
100 {
101 static const int egl_attr[] = {
102 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
103 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
104 EGL_RED_SIZE, 5,
105 EGL_GREEN_SIZE, 5,
106 EGL_BLUE_SIZE, 5,
107 EGL_DEPTH_SIZE, 16,
108 EGL_NONE
109 };
111 int scr = DefaultScreen(dpy);
112 Window root_win = RootWindow(dpy, scr);
114 EGLConfig cfg;
115 int num_cfg;
116 if(!eglChooseConfig(egl_dpy, egl_attr, &cfg, 1, &num_cfg)) {
117 fprintf(stderr, "failed to find matching EGL visual\n");
118 return false;
119 }
120 int rsize, gsize, bsize, zsize, ssize, vis_id;
121 eglGetConfigAttrib(egl_dpy, cfg, EGL_RED_SIZE, &rsize);
122 eglGetConfigAttrib(egl_dpy, cfg, EGL_GREEN_SIZE, &gsize);
123 eglGetConfigAttrib(egl_dpy, cfg, EGL_BLUE_SIZE, &bsize);
124 eglGetConfigAttrib(egl_dpy, cfg, EGL_DEPTH_SIZE, &zsize);
125 eglGetConfigAttrib(egl_dpy, cfg, EGL_STENCIL_SIZE, &ssize);
126 eglGetConfigAttrib(egl_dpy, cfg, EGL_NATIVE_VISUAL_ID, &vis_id);
127 printf("got visual %d: %d bpp (%d%d%d), %d zbuffer, %d stencil\n", vis_id, rsize + gsize + bsize,
128 rsize, gsize, bsize, zsize, ssize);
130 int num_vis;
131 XVisualInfo *vis_info, vis_template;
132 vis_template.visualid = vis_id;
133 if(!(vis_info = XGetVisualInfo(dpy, VisualIDMask, &vis_template, &num_vis))) {
134 fprintf(stderr, "visual id %d is invalid (?!)\n", vis_id);
135 return false;
136 }
138 XSetWindowAttributes xattr;
139 xattr.border_pixel = xattr.backing_pixel = BlackPixel(dpy, scr);
140 xattr.colormap = XCreateColormap(dpy, root_win, vis_info->visual, AllocNone);
141 unsigned int xattr_mask = CWColormap | CWBorderPixel | CWBackPixel;
143 win = XCreateWindow(dpy, root_win, 0, 0, xsz, ysz, 0, vis_info->depth, InputOutput,
144 vis_info->visual, xattr_mask, &xattr);
145 if(!win) {
146 fprintf(stderr, "failed to create window\n");
147 XFree(vis_info);
148 return false;
149 }
150 XFree(vis_info);
151 XSelectInput(dpy, win, ExposureMask | StructureNotifyMask | KeyPressMask | KeyReleaseMask);
153 XSetWMProtocols(dpy, win, &xa_del_window, 1);
154 set_window_title("spycam");
156 eglBindAPI(EGL_OPENGL_ES_API);
158 if(!(egl_win = eglCreateWindowSurface(egl_dpy, cfg, win, 0))) {
159 fprintf(stderr, "failed to create EGL window\n");
160 XDestroyWindow(dpy, win);
161 return false;
162 }
164 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
165 if(!(egl_ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, ctxattr))) {
166 fprintf(stderr, "failed to create EGL context\n");
167 eglDestroySurface(egl_dpy, egl_win);
168 XDestroyWindow(dpy, win);
169 return false;
170 }
171 eglMakeCurrent(egl_dpy, egl_win, egl_win, egl_ctx);
173 XMapWindow(dpy, win);
175 eglQuerySurface(dpy, egl_win, EGL_WIDTH, &win_width);
176 eglQuerySurface(dpy, egl_win, EGL_HEIGHT, &win_height);
177 app_reshape(win_width, win_height);
179 return true;
180 }
182 static bool handle_event(XEvent *ev)
183 {
184 static bool mapped;
186 switch(ev->type) {
187 case Expose:
188 if(mapped) {
189 redraw_pending = true;
190 }
191 break;
193 case MapNotify:
194 mapped = true;
195 redraw_pending = true;
196 break;
198 case UnmapNotify:
199 mapped = false;
200 redraw_pending = false;
201 break;
203 case ConfigureNotify:
204 if(win_width != (int)ev->xconfigure.width || win_height != (int)ev->xconfigure.height) {
205 win_width = ev->xconfigure.width;
206 win_height = ev->xconfigure.height;
207 app_reshape(win_width, win_height);
208 }
209 break;
211 case KeyPress:
212 case KeyRelease:
213 app_keyboard(XLookupKeysym(&ev->xkey, 0) & 0xff, ev->type == KeyPress);
214 break;
216 case ClientMessage:
217 if(ev->xclient.message_type == xa_wm_proto &&
218 (Atom)ev->xclient.data.l[0] == xa_del_window) {
219 return false;
220 }
221 break;
223 default:
224 break;
225 }
226 return true;
227 }
229 static void set_window_title(const char *title)
230 {
231 XTextProperty text_prop;
232 XStringListToTextProperty((char**)&title, 1, &text_prop);
233 XSetWMName(dpy, win, &text_prop);
234 XSetWMIconName(dpy, win, &text_prop);
235 XFree(text_prop.value);
236 }