From 1bc104ad7ae722badfbd4a6971499c161143546e Mon Sep 17 00:00:00 2001
From: Carl Worth <cworth@cworth.org>
Date: Sun, 12 Aug 2012 17:09:15 -0700
Subject: [PATCH] Add the source of a very simple program using OpenGL through
 GLX.

We'll use this program to generate traces for use in the test suite,
(to exercise using "apitrace trim" to trim away unused textures,
shader programs, etc.).

There is a Makefile here, for convenience to any test writers wanting
to expand this program. Note that this Makefile us intentionally not
integrated with the cmake setup of the apitrace-tests project. That's
because it is not expected that this test program should be built or
run as a standard part of the test suite. Instead, useful trace files
generated from glxsimple will be directly committed to the
apitrace-tests repository.
---
 cli/src/.gitignore  |   2 +
 cli/src/Makefile    |   9 +++
 cli/src/glxsimple.c | 138 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 cli/src/.gitignore
 create mode 100644 cli/src/Makefile
 create mode 100644 cli/src/glxsimple.c

diff --git a/cli/src/.gitignore b/cli/src/.gitignore
new file mode 100644
index 0000000..db85a30
--- /dev/null
+++ b/cli/src/.gitignore
@@ -0,0 +1,2 @@
+*~
+glxsimple
diff --git a/cli/src/Makefile b/cli/src/Makefile
new file mode 100644
index 0000000..87102fc
--- /dev/null
+++ b/cli/src/Makefile
@@ -0,0 +1,9 @@
+PROGS=glxsimple
+
+all: $(PROGS)
+
+%:%.c
+	$(CC) -Wall -Wextra -g -o $@ $^ -lGL -lX11 -lGLEW
+
+clean:
+	rm -f *.o $(PROGS)
diff --git a/cli/src/glxsimple.c b/cli/src/glxsimple.c
new file mode 100644
index 0000000..242fe94
--- /dev/null
+++ b/cli/src/glxsimple.c
@@ -0,0 +1,138 @@
+/**************************************************************************
+ * Copyright 2012 Intel corporation
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#include <stdio.h>
+
+#include <X11/Xlib.h>
+#include <GL/glew.h>
+#include <GL/gl.h>
+#include <GL/glx.h>
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+int width = 64;
+int height = 64;
+
+static void
+draw (Display *dpy, Window window, int width, int height)
+{
+	GLenum glew_err;
+
+        int visual_attr[] = {
+                GLX_RGBA,
+                GLX_RED_SIZE,		8,
+                GLX_GREEN_SIZE, 	8,
+                GLX_BLUE_SIZE,		8,
+                GLX_ALPHA_SIZE, 	8,
+                GLX_DOUBLEBUFFER,
+                GLX_DEPTH_SIZE,		24,
+                GLX_STENCIL_SIZE,	8,
+                GLX_X_VISUAL_TYPE,	GLX_DIRECT_COLOR,
+                None
+        };
+        XVisualInfo *visual_info = glXChooseVisual(dpy, 0, visual_attr);
+        GLXContext ctx = glXCreateContext(dpy, visual_info, NULL, True);
+        glXMakeCurrent(dpy, window, ctx);
+
+	glew_err = glewInit();
+	if (glew_err != GLEW_OK)
+	{
+		fprintf (stderr, "glewInit failed: %s\n",
+			 glewGetErrorString(glew_err));
+		exit (1);
+	}
+
+        glViewport(0, 0, width, height);
+        glClearColor(1, 0, 1, 1);
+        glClear(GL_COLOR_BUFFER_BIT);
+
+        glXSwapBuffers (dpy, window);
+
+        glXDestroyContext (dpy, ctx);
+}
+
+static void
+handle_events(Display *dpy, Window window, int width, int height)
+{
+        XEvent xev;
+        KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q"));
+
+        XNextEvent (dpy, &xev);
+
+        while (1) {
+                XNextEvent (dpy, &xev);
+                switch (xev.type) {
+                case KeyPress:
+                        if (xev.xkey.keycode == quit_code) {
+                                return;
+                        }
+                        break;
+                case ConfigureNotify:
+                        width = xev.xconfigure.width;
+                        height = xev.xconfigure.height;
+                        break;
+                case Expose:
+                        if (xev.xexpose.count == 0) {
+                                draw (dpy, window, width, height);
+                                return;
+                        }
+                        break;
+                }
+        }
+}
+
+int
+main (void)
+{
+        Display *dpy;
+        Window window;
+
+        dpy = XOpenDisplay (NULL);
+
+        if (dpy == NULL) {
+                fprintf(stderr, "Failed to open display %s\n",
+                        XDisplayName(NULL));
+                return 1;
+        }
+
+        window = XCreateSimpleWindow(dpy, DefaultRootWindow (dpy),
+                                     0, 0, width, height, 0,
+                                     BlackPixel (dpy, DefaultScreen (dpy)),
+                                     BlackPixel (dpy, DefaultScreen (dpy)));
+
+        XSelectInput(dpy, window,
+                     KeyPressMask | StructureNotifyMask | ExposureMask);
+
+        XMapWindow (dpy, window);
+
+        handle_events (dpy, window, width, height);
+
+        XDestroyWindow (dpy, window);
+        XCloseDisplay (dpy);
+
+        return 0;
+}
-- 
2.45.2