Unify error events
authorKristian Høgsberg <krh@bitplanet.net>
Wed, 11 May 2011 14:57:06 +0000 (10:57 -0400)
committerKristian Høgsberg <krh@bitplanet.net>
Wed, 11 May 2011 15:02:34 +0000 (11:02 -0400)
Consolidate the different error events into one.  This event will also be
usable for other interaces.

protocol/wayland.xml
wayland/wayland-client.c
wayland/wayland-server.c
wayland/wayland-server.h
wayland/wayland-shm.c

index 11976fa..aef8385 100644 (file)
       <arg name="key" type="uint"/>
     </request>
 
-    <!-- A request addressed a non-existent object id. This is
-         tyipcally a fatal error. -->
-    <event name="invalid_object">
-      <arg name="object_id" type="uint"/>
+    <!-- A fatal error has occurred. -->
+    <event name="error">
+      <arg name="object_id" type="object" interface="wl_object"/>
+      <arg name="code" type="uint"/>
+      <arg name="message" type="string"/>
     </event>
 
-    <!-- A request tried to invoke an opcode out of range.  This is
-         typically a fatal error. -->
-    <event name="invalid_method">
-      <arg name="object_id" type="uint"/>
-      <arg name="opcode" type="uint"/>
-    </event>
-
-    <!-- A request has failed due to an out of memory error. -->
-    <event name="no_memory"/>
+    <enum name="error">
+      <entry name="invalid_object" value="0"/>
+      <entry name="invalid_method" value="1"/>
+      <entry name="no_memory" value="2"/>
+    </enum>
 
     <!-- Notify the client of global objects. These are objects that
          are created by the server. Globals are published on the
@@ -88,7 +85,6 @@
     </request>
   </interface>
 
-
   <!-- Shared memory support -->
   <interface name="wl_shm" version="1">
     <!-- Transfer a shm buffer to the server.  The allocated buffer
index 59c68f7..3d28cc5 100644 (file)
@@ -258,27 +258,12 @@ wl_display_get_global(struct wl_display *display,
 }
 
 static void
-display_handle_invalid_object(void *data,
-                             struct wl_display *display, uint32_t id)
+display_handle_error(void *data,
+                    struct wl_display *display, struct wl_object *object,
+                    uint32_t code, const char *message)
 {
-       fprintf(stderr, "sent request to invalid object\n");
-       abort();
-}
-                             
-static void
-display_handle_invalid_method(void *data, 
-                             struct wl_display *display,
-                             uint32_t id, uint32_t opcode)
-{
-       fprintf(stderr, "sent invalid request opcode\n");
-       abort();
-}
-
-static void
-display_handle_no_memory(void *data,
-                        struct wl_display *display)
-{
-       fprintf(stderr, "server out of memory\n");
+       fprintf(stderr, "%s@%d: error %d: %s\n",
+               object->interface->name, object->id, code, message);
        abort();
 }
 
@@ -345,9 +330,7 @@ display_handle_key(void *data,
 }
 
 static const struct wl_display_listener display_listener = {
-       display_handle_invalid_object,
-       display_handle_invalid_method,
-       display_handle_no_memory,
+       display_handle_error,
        display_handle_global,
        display_handle_range,
        display_handle_key
index 8d5caf4..572a5a9 100644 (file)
@@ -113,6 +113,21 @@ wl_client_post_event(struct wl_client *client, struct wl_object *sender,
        wl_closure_destroy(closure);
 }
 
+WL_EXPORT void
+wl_client_post_error(struct wl_client *client, struct wl_object *object,
+                    uint32_t code, const char *msg, ...)
+{
+       char buffer[128];
+       va_list ap;
+
+       va_start(ap, msg);
+       vsnprintf(buffer, sizeof buffer, msg, ap);
+       va_end(ap);
+
+       wl_client_post_event(client, &client->display->object,
+                            WL_DISPLAY_ERROR, object, code, buffer);
+}
+
 static int
 wl_client_connection_data(int fd, uint32_t mask, void *data)
 {
@@ -145,16 +160,20 @@ wl_client_connection_data(int fd, uint32_t mask, void *data)
 
                object = wl_hash_table_lookup(client->display->objects, p[0]);
                if (object == NULL) {
-                       wl_client_post_event(client, &client->display->object,
-                                            WL_DISPLAY_INVALID_OBJECT, p[0]);
+                       wl_client_post_error(client, &client->display->object,
+                                            WL_DISPLAY_ERROR_INVALID_OBJECT,
+                                            "invalid object %d", p[0]);
                        wl_connection_consume(connection, size);
                        len -= size;
                        continue;
                }
 
                if (opcode >= object->interface->method_count) {
-                       wl_client_post_event(client, &client->display->object,
-                                            WL_DISPLAY_INVALID_METHOD, p[0], opcode);
+                       wl_client_post_error(client, &client->display->object,
+                                            WL_DISPLAY_ERROR_INVALID_METHOD,
+                                            "invalid method %d, object %s@%d",
+                                            object->interface->name,
+                                            object->id, opcode);
                        wl_connection_consume(connection, size);
                        len -= size;
                        continue;
@@ -167,9 +186,11 @@ wl_client_connection_data(int fd, uint32_t mask, void *data)
                len -= size;
 
                if (closure == NULL && errno == EINVAL) {
-                       wl_client_post_event(client, &client->display->object,
-                                            WL_DISPLAY_INVALID_METHOD,
-                                            p[0], opcode);
+                       wl_client_post_error(client, &client->display->object,
+                                            WL_DISPLAY_ERROR_INVALID_METHOD,
+                                            "invalid arguments for %s@%d.%s",
+                                            object->interface->name,
+                                            object->id, message->name);
                        continue;
                } else if (closure == NULL && errno == ENOMEM) {
                        wl_client_post_no_memory(client);
@@ -270,9 +291,8 @@ wl_client_add_resource(struct wl_client *client,
 WL_EXPORT void
 wl_client_post_no_memory(struct wl_client *client)
 {
-       wl_client_post_event(client,
-                            &client->display->object,
-                            WL_DISPLAY_NO_MEMORY);
+       wl_client_post_error(client, &client->display->object,
+                            WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
 }
 
 WL_EXPORT void
index 7e752de..6a042cd 100644 (file)
@@ -95,6 +95,8 @@ int wl_display_add_global(struct wl_display *display,
 
 struct wl_client *wl_client_create(struct wl_display *display, int fd);
 void wl_client_destroy(struct wl_client *client);
+void wl_client_post_error(struct wl_client *client, struct wl_object *object,
+                         uint32_t code, const char *msg, ...);
 void wl_client_post_no_memory(struct wl_client *client);
 void wl_client_post_global(struct wl_client *client, struct wl_object *object);
 
index 2886c52..84441e2 100644 (file)
@@ -124,18 +124,17 @@ shm_create_buffer(struct wl_client *client, struct wl_shm *shm,
        /* FIXME: Define a real exception event instead of abusing the
         * display.invalid_object error */
        if (visual->object.interface != &wl_visual_interface) {
-               wl_client_post_event(client, (struct wl_object *) display,
-                                    WL_DISPLAY_INVALID_OBJECT, 0);
-               fprintf(stderr, "invalid visual in create_buffer\n");
+               wl_client_post_error(client, (struct wl_object *) display,
+                                    WL_DISPLAY_ERROR_INVALID_OBJECT,
+                                    "invalid visual in create_buffer\n");
                close(fd);
                return;
        }
 
        if (width < 0 || height < 0 || stride < width) {
-               wl_client_post_event(client, (struct wl_object *) display,
-                                    WL_DISPLAY_INVALID_OBJECT, 0);
-               fprintf(stderr,
-                       "invalid width, height or stride in create_buffer\n");
+               wl_client_post_error(client, (struct wl_object *) display,
+                                    WL_DISPLAY_ERROR_INVALID_OBJECT,
+                                    "invalid width, height or stride in create_buffer\n");
                close(fd);
                return;
        }
@@ -147,9 +146,9 @@ shm_create_buffer(struct wl_client *client, struct wl_shm *shm,
        if (data == MAP_FAILED) {
                /* FIXME: Define a real exception event instead of
                 * abusing this one */
-               wl_client_post_event(client, (struct wl_object *) display,
-                                    WL_DISPLAY_INVALID_OBJECT, 0);
-               fprintf(stderr, "failed to create image for fd %d\n", fd);
+               wl_client_post_error(client, (struct wl_object *) display,
+                                    WL_DISPLAY_ERROR_INVALID_OBJECT,
+                                    "failed to create image for fd %d\n");
                return;
        }