From b5f04656a4b3d1b43a66585ead8ef55536a4fdbc Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Sun, 3 Nov 2019 21:13:40 +0100
Subject: Add puglStrerror() and improve test program error reporting

---
 pugl/detail/implementation.c | 19 +++++++++++++++++++
 pugl/pugl.h                  |  7 +++++++
 test/pugl_cairo_test.c       |  5 ++++-
 test/pugl_gl3_test.c         |  6 ++++--
 test/pugl_test.c             | 12 +++++++-----
 5 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/pugl/detail/implementation.c b/pugl/detail/implementation.c
index 80a7a32..a2ff2ec 100644
--- a/pugl/detail/implementation.c
+++ b/pugl/detail/implementation.c
@@ -25,6 +25,25 @@
 #include <stdlib.h>
 #include <string.h>
 
+const char*
+puglStrerror(const PuglStatus status)
+{
+	switch (status) {
+	case PUGL_SUCCESS:               return "Success";
+	case PUGL_FAILURE:               return "Non-fatal failure";
+	case PUGL_UNKNOWN_ERROR:         return "Unknown system error";
+	case PUGL_BAD_BACKEND:           return "Invalid or missing backend";
+	case PUGL_BACKEND_FAILED:        return "Backend initialisation failed";
+	case PUGL_REGISTRATION_FAILED:   return "Window class registration failed";
+	case PUGL_CREATE_WINDOW_FAILED:  return "Window creation failed";
+	case PUGL_SET_FORMAT_FAILED:     return "Failed to set pixel format";
+	case PUGL_CREATE_CONTEXT_FAILED: return "Failed to create drawing context";
+	case PUGL_UNSUPPORTED_TYPE:      return "Unsupported data type";
+	}
+
+	return "Unknown error";
+}
+
 void
 puglSetString(char** dest, const char* string)
 {
diff --git a/pugl/pugl.h b/pugl/pugl.h
index f08925f..0232d86 100644
--- a/pugl/pugl.h
+++ b/pugl/pugl.h
@@ -409,6 +409,13 @@ typedef union {
 	PuglEventFocus     focus;      /**< PUGL_FOCUS_IN, PUGL_FOCUS_OUT. */
 } PuglEvent;
 
+/**
+   Return a string describing a status code.
+*/
+PUGL_API
+const char*
+puglStrerror(PuglStatus status);
+
 /**
    @anchor world
    @name World
diff --git a/test/pugl_cairo_test.c b/test/pugl_cairo_test.c
index 1aa291a..1ce9edd 100644
--- a/test/pugl_cairo_test.c
+++ b/test/pugl_cairo_test.c
@@ -228,7 +228,10 @@ main(int argc, char** argv)
 	puglSetViewHint(view, PUGL_IGNORE_KEY_REPEAT, opts.ignoreKeyRepeat);
 	puglSetEventFunc(view, onEvent);
 
-	if (puglCreateWindow(view, "Pugl Test")) {
+	PuglStatus st = puglCreateWindow(view, "Pugl Test");
+	if (st) {
+		fprintf(stderr, "error: Failed to create window (%s)\n",
+		        puglStrerror(st));
 		return 1;
 	}
 
diff --git a/test/pugl_gl3_test.c b/test/pugl_gl3_test.c
index 8228336..21db4b2 100644
--- a/test/pugl_gl3_test.c
+++ b/test/pugl_gl3_test.c
@@ -313,8 +313,10 @@ main(int argc, char** argv)
 	puglSetHandle(app.view, &app);
 	puglSetEventFunc(app.view, onEvent);
 
-	if (puglCreateWindow(app.view, "Pugl OpenGL 3")) {
-		fprintf(stderr, "error: Failed to create window\n");
+	const PuglStatus st = puglCreateWindow(app.view, "Pugl OpenGL 3");
+	if (st) {
+		fprintf(stderr, "error: Failed to create window (%s)\n",
+		        puglStrerror(st));
 		return 1;
 	}
 
diff --git a/test/pugl_test.c b/test/pugl_test.c
index 7033d45..6558bf6 100644
--- a/test/pugl_test.c
+++ b/test/pugl_test.c
@@ -323,10 +323,12 @@ main(int argc, char** argv)
 	puglSetHandle(app.parent, &app);
 	puglSetEventFunc(app.parent, onParentEvent);
 
+	PuglStatus st         = PUGL_SUCCESS;
 	const uint8_t title[] = { 'P', 'u', 'g', 'l', ' ',
 	                          'P', 'r', 0xC3, 0xBC, 'f', 'u', 'n', 'g', 0 };
-	if (puglCreateWindow(app.parent, (const char*)title)) {
-		fprintf(stderr, "error: Failed to create parent window\n");
+	if ((st = puglCreateWindow(app.parent, (const char*)title))) {
+		fprintf(stderr, "error: Failed to create parent window (%s)\n",
+		        puglStrerror(st));
 		return 1;
 	}
 
@@ -341,9 +343,9 @@ main(int argc, char** argv)
 	puglSetHandle(app.child, &app);
 	puglSetEventFunc(app.child, onEvent);
 
-	const int st = puglCreateWindow(app.child, NULL);
-	if (st) {
-		fprintf(stderr, "error: Failed to create child window (%d)\n", st);
+	if ((st = puglCreateWindow(app.child, NULL))) {
+		fprintf(stderr, "error: Failed to create child window (%s)\n",
+		        puglStrerror(st));
 		return 1;
 	}
 
-- 
cgit v1.2.1