mobile/app: call the Start/Stop callbacks in x11.go

Fixes #9534

Change-Id: I6a2f9ffb7626a243c501cb50c5b523d927d80e9e
Reviewed-on: https://go-review.googlesource.com/6262
Reviewed-by: David Crawshaw <crawshaw@golang.org>
diff --git a/app/x11.c b/app/x11.c
index fd9ea07..bb7d654 100644
--- a/app/x11.c
+++ b/app/x11.c
@@ -12,6 +12,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+static Atom wm_delete_window;
+
 static Window
 new_window(Display *x_dpy, EGLDisplay e_dpy, int w, int h, EGLContext *ctx, EGLSurface *surf) {
 	static const EGLint attribs[] = {
@@ -99,12 +101,20 @@
 	EGLContext e_ctx;
 	EGLSurface e_surf;
 	Window win = new_window(x_dpy, e_dpy, 400, 400, &e_ctx, &e_surf);
+
+	wm_delete_window = XInternAtom(x_dpy, "WM_DELETE_WINDOW", True);
+	if (wm_delete_window != None) {
+		XSetWMProtocols(x_dpy, win, &wm_delete_window, 1);
+	}
+
 	XMapWindow(x_dpy, win);
 	if (!eglMakeCurrent(e_dpy, e_surf, e_surf, e_ctx)) {
 		fprintf(stderr, "eglMakeCurrent failed\n");
 		exit(1);
 	}
 
+	onStart();
+
 	while (1) {
 		XEvent ev;
 		XNextEvent(x_dpy, &ev);
@@ -137,6 +147,12 @@
 			onResize(ev.xconfigure.width, ev.xconfigure.height);
 			glViewport(0, 0, (GLint)ev.xconfigure.width, (GLint)ev.xconfigure.height);
 			break;
+		case ClientMessage:
+			if (wm_delete_window != None && (Atom)ev.xclient.data.l[0] == wm_delete_window) {
+				onStop();
+				return;
+			}
+			break;
 		}
 	}
 }
diff --git a/app/x11.go b/app/x11.go
index 1934320..0982d55 100644
--- a/app/x11.go
+++ b/app/x11.go
@@ -88,3 +88,17 @@
 		cb.Draw()
 	}
 }
+
+//export onStart
+func onStart() {
+	if cb.Start != nil {
+		cb.Start()
+	}
+}
+
+//export onStop
+func onStop() {
+	if cb.Stop != nil {
+		cb.Stop()
+	}
+}