]> git.cworth.org Git - apitrace/blobdiff - glsnapshot.cpp
apitrace: Replace tracedump program with new "apitrace dump" command
[apitrace] / glsnapshot.cpp
index 98a11fb1d5d78b55b9505286c6b0030f03861f34..1f95db1d22408aa0a33731fe1f6a6237726ed464 100644 (file)
 #include "glsize.hpp"
 
 
+#if !defined(_WIN32) && !defined(__APPLE__)
+
+
+#include <X11/Xproto.h>
+
+
+static int
+errorHandler(Display *display, XErrorEvent *event)
+{
+    if (event->error_code == BadDrawable &&
+        event->request_code == X_GetGeometry) {
+        return 0;
+    }
+
+    char error_text[512];
+    XGetErrorText(display, event->error_code, error_text, sizeof error_text);
+    fprintf(stderr, "X Error of failed request:  %s\n", error_text);
+
+    return 0;
+}
+
+
+#endif /* !_WIN32 && !__APPLE__ */
+
+
 namespace glsnapshot {
 
 
 /**
  * Get the contents of the current drawable into an image.
  */
-static Image::Image *
+static image::Image *
 getDrawableImage(void) {
 #if defined(_WIN32)
 
@@ -91,7 +116,13 @@ getDrawableImage(void) {
      * XXX: This does not work for drawables created with glXCreateWindow
      */
 
-    if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
+    int (*oldErrorHandler)(Display *, XErrorEvent *);
+    Status status;
+
+    oldErrorHandler = XSetErrorHandler(errorHandler);
+    status = XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth);
+    XSetErrorHandler(oldErrorHandler);
+    if (!status) {
         return false;
     }
 
@@ -102,7 +133,7 @@ getDrawableImage(void) {
         return NULL;
     }
 
-    Image::Image *image = NULL;
+    image::Image *image = NULL;
 
     if (ximage->depth          == 24 &&
         ximage->bits_per_pixel == 32 &&
@@ -110,13 +141,13 @@ getDrawableImage(void) {
         ximage->green_mask     == 0x0000ff00 &&
         ximage->blue_mask      == 0x000000ff) {
 
-        image = new Image::Image(w, h, 4);
+        image = new image::Image(w, h, 4);
 
         if (image) {
             const uint32_t *src = (const uint32_t *)ximage->data;
             uint32_t *dst = (uint32_t*) image->start();
-            for (int y = 0; y < h; ++y) {
-                for (int x = 0; x < w; ++x) {
+            for (unsigned y = 0; y < h; ++y) {
+                for (unsigned x = 0; x < w; ++x) {
                     uint32_t bgra = src[x];
                     uint32_t rgba = (bgra & 0xff00ff00)
                                   | ((bgra >> 16) & 0xff)
@@ -129,7 +160,7 @@ getDrawableImage(void) {
             }
         }
     } else {
-        OS::DebugMessage("apitrace: unexpected XImage: "
+        os::log("apitrace: unexpected XImage: "
                          "bits_per_pixel = %i, "
                          "depth = %i, "
                          "red_mask = 0x%08lx, "
@@ -172,12 +203,12 @@ void snapshot(unsigned call_no) {
     ++frame_no;
 
     if (snapshot_prefix) {
-        Image::Image *src = getDrawableImage();
+        image::Image *src = getDrawableImage();
         if (src) {
             char filename[PATH_MAX];
             snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
             if (src->writePNG(filename)) {
-                OS::DebugMessage("apitrace: wrote %s\n", filename);
+                os::log("apitrace: wrote %s\n", filename);
             }
 
             delete src;