Ejemplo n.º 1
0
void
processEvents(void) {
	while (XPending(x_dpy)) {
		XEvent ev;
		XNextEvent(x_dpy, &ev);
		switch (ev.type) {
		case ButtonPress:
			onTouchBegin((float)ev.xbutton.x, (float)ev.xbutton.y);
			break;
		case ButtonRelease:
			onTouchEnd((float)ev.xbutton.x, (float)ev.xbutton.y);
			break;
		case MotionNotify:
			onTouchMove((float)ev.xmotion.x, (float)ev.xmotion.y);
			break;
		case ConfigureNotify:
			onResize(ev.xconfigure.width, ev.xconfigure.height);
			break;
		case ClientMessage:
			if (wm_delete_window != None && (Atom)ev.xclient.data.l[0] == wm_delete_window) {
				onStop();
				return;
			}
			break;
		}
	}
}
Ejemplo n.º 2
0
void
runApp(void) {
	Display *x_dpy = XOpenDisplay(NULL);
	if (!x_dpy) {
		fprintf(stderr, "XOpenDisplay failed\n");
		exit(1);
	}
	EGLDisplay e_dpy = eglGetDisplay(x_dpy);
	if (!e_dpy) {
		fprintf(stderr, "eglGetDisplay failed\n");
		exit(1);
	}
	EGLint e_major, e_minor;
	if (!eglInitialize(e_dpy, &e_major, &e_minor)) {
		fprintf(stderr, "eglInitialize failed\n");
		exit(1);
	}
	eglBindAPI(EGL_OPENGL_ES_API);
	EGLContext e_ctx;
	EGLSurface e_surf;
	Window win = new_window(x_dpy, e_dpy, 400, 400, &e_ctx, &e_surf);
	XMapWindow(x_dpy, win);
	if (!eglMakeCurrent(e_dpy, e_surf, e_surf, e_ctx)) {
		fprintf(stderr, "eglMakeCurrent failed\n");
		exit(1);
	}

	while (1) {
		XEvent ev;
		XNextEvent(x_dpy, &ev);
		switch (ev.type) {
		case ButtonPress:
			onTouchStart((float)ev.xbutton.x, (float)ev.xbutton.y);
			break;
		case ButtonRelease:
			onTouchEnd((float)ev.xbutton.x, (float)ev.xbutton.y);
			break;
		case MotionNotify:
			onTouchMove((float)ev.xmotion.x, (float)ev.xmotion.y);
			break;
		case Expose:
			onDraw();
			eglSwapBuffers(e_dpy, e_surf);

			// TODO(nigeltao): subscribe to vblank events instead of forcing another
			// expose event to keep the event loop ticking over.
			// TODO(nigeltao): no longer #include <string.h> when we don't use memset.
			XExposeEvent fakeEvent;
			memset(&fakeEvent, 0, sizeof(XExposeEvent));
			fakeEvent.type = Expose;
			fakeEvent.window = win;
			XSendEvent(x_dpy, win, 0, 0, (XEvent*)&fakeEvent);
			XFlush(x_dpy);

			break;
		case ConfigureNotify:
			onResize(ev.xconfigure.width, ev.xconfigure.height);
			glViewport(0, 0, (GLint)ev.xconfigure.width, (GLint)ev.xconfigure.height);
			break;
		}
	}
}