Advertisement
mchqwerty

Extend error handling

Apr 10th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.26 KB | None | 0 0
  1. diff --git a/src/wayland-client.c b/src/wayland-client.c
  2. index bd40313..28c2848 100644
  3. --- a/src/wayland-client.c
  4. +++ b/src/wayland-client.c
  5. @@ -78,7 +78,21 @@ struct wl_event_queue {
  6.  struct wl_display {
  7.     struct wl_proxy proxy;
  8.     struct wl_connection *connection;
  9. -   int last_error;
  10. +   struct {
  11. +       /* Code of the error. If the object that caused the error is
  12. +        * wl_display, than it contains appropriately set error value
  13. +        * (i. e. EINVAL, ENOMEM). Otherwise it contains code of
  14. +        * error that can be compared to interface's errors enumeration.
  15. +        * This code 'duality' is due to backward compatibility with
  16. +        * old behaviour of wl_display_get_error() */
  17. +       int code;
  18. +       /* interface (protocol) in wich the error occured */
  19. +       const struct wl_interface *interface;
  20. +       /* id of the proxy that caused the error. There's no warranty
  21. +        * that the proxy is still valid. It's up to client how it will
  22. +        * use it */
  23. +       unsigned int id;
  24. +   } error;
  25.     int fd;
  26.     pthread_t display_thread;
  27.     struct wl_map objects;
  28. @@ -97,27 +111,38 @@ struct wl_display {
  29.  static int debug_client = 0;
  30.  
  31.  static void
  32. -display_fatal_error(struct wl_display *display, int error)
  33. +object_fatal_error(struct wl_display *display, int error,
  34. +          unsigned int id, const struct wl_interface *intf)
  35.  {
  36.     struct wl_event_queue *iter;
  37.  
  38. -   if (display->last_error)
  39. +   if (display->error.code)
  40.         return;
  41.  
  42.     if (!error)
  43.         error = 1;
  44.  
  45. -   display->last_error = error;
  46. +   display->error.code = error;
  47. +   display->error.id = id;
  48. +   display->error.interface = intf;
  49.  
  50.     wl_list_for_each(iter, &display->event_queue_list, link)
  51.         pthread_cond_broadcast(&iter->cond);
  52.  }
  53.  
  54.  static void
  55. -wl_display_fatal_error(struct wl_display *display, int error)
  56. +display_fatal_error(struct wl_display *display, int error)
  57. +{
  58. +   struct wl_object *obj = &display->proxy.object;
  59. +   object_fatal_error(display, error, obj->id, obj->interface);
  60. +}
  61. +
  62. +static void
  63. +wl_display_fatal_error(struct wl_display *display, int error,
  64. +              unsigned int id, const struct wl_interface *intf)
  65.  {
  66.     pthread_mutex_lock(&display->mutex);
  67. -   display_fatal_error(display, error);
  68. +   object_fatal_error(display, error, id, intf);
  69.     pthread_mutex_unlock(&display->mutex);
  70.  }
  71.  
  72. @@ -584,20 +609,24 @@ display_handle_error(void *data,
  73.     wl_log("%s@%u: error %d: %s\n",
  74.            proxy->object.interface->name, proxy->object.id, code, message);
  75.  
  76. -   switch (code) {
  77. -   case WL_DISPLAY_ERROR_INVALID_OBJECT:
  78. -   case WL_DISPLAY_ERROR_INVALID_METHOD:
  79. -       err = EINVAL;
  80. -       break;
  81. -   case WL_DISPLAY_ERROR_NO_MEMORY:
  82. -       err = ENOMEM;
  83. -       break;
  84. -   default:
  85. -       err = EFAULT;
  86. -       break;
  87. -   }
  88. +   if (wl_interface_equal(proxy->object.interface, &wl_display_interface))
  89. +       switch (code) {
  90. +       case WL_DISPLAY_ERROR_INVALID_OBJECT:
  91. +       case WL_DISPLAY_ERROR_INVALID_METHOD:
  92. +           err = EINVAL;
  93. +           break;
  94. +       case WL_DISPLAY_ERROR_NO_MEMORY:
  95. +           err = ENOMEM;
  96. +           break;
  97. +       default:
  98. +           err = EFAULT;
  99. +           break;
  100. +       }
  101. +   else
  102. +       err = code;
  103.  
  104. -   wl_display_fatal_error(display, err);
  105. +   wl_display_fatal_error(display, err, proxy->object.id,
  106. +                  proxy->object.interface);
  107.  }
  108.  
  109.  static void
  110. @@ -1147,20 +1176,20 @@ dispatch_queue(struct wl_display *display, struct wl_event_queue *queue)
  111.  {
  112.     int count;
  113.  
  114. -   if (display->last_error)
  115. +   if (display->error.code)
  116.         goto err;
  117.  
  118.     count = 0;
  119.     while (!wl_list_empty(&display->display_queue.event_list)) {
  120.         dispatch_event(display, &display->display_queue);
  121. -       if (display->last_error)
  122. +       if (display->error.code)
  123.             goto err;
  124.         count++;
  125.     }
  126.  
  127.     while (!wl_list_empty(&queue->event_list)) {
  128.         dispatch_event(display, queue);
  129. -       if (display->last_error)
  130. +       if (display->error.code)
  131.             goto err;
  132.         count++;
  133.     }
  134. @@ -1168,7 +1197,7 @@ dispatch_queue(struct wl_display *display, struct wl_event_queue *queue)
  135.     return count;
  136.  
  137.  err:
  138. -   errno = display->last_error;
  139. +   errno = display->error.code;
  140.  
  141.     return -1;
  142.  }
  143. @@ -1482,13 +1511,50 @@ wl_display_get_error(struct wl_display *display)
  144.  
  145.     pthread_mutex_lock(&display->mutex);
  146.  
  147. -   ret = display->last_error;
  148. +   ret = display->error.code;
  149.  
  150.     pthread_mutex_unlock(&display->mutex);
  151.  
  152.     return ret;
  153.  }
  154.  
  155. +/** Retrieve the last error that occurred along with additional information
  156. + *
  157. + * \param display The display context object
  158. + * \param interface Store the interface that caused the error there (if not NULL)
  159. + * \param interface Store the id of object that caused the error there (if not NULL)
  160. + * \return The last error that occurred on \c display or 0 if no error occurred
  161. + *
  162. + * Return the last error that occurred on the display. This may be an error sent
  163. + * by the server or caused by the local client.
  164. + *
  165. + * \note Errors are \b fatal. If this function returns non-zero the display
  166. + * can no longer be used.
  167. + *
  168. + * \memberof wl_display
  169. + */
  170. +WL_EXPORT int
  171. +wl_display_get_error_full(struct wl_display *display,
  172. +             const struct wl_interface **interface,
  173. +             unsigned int *id)
  174. +{
  175. +   int ret;
  176. +
  177. +   pthread_mutex_lock(&display->mutex);
  178. +
  179. +   ret = display->error.code;
  180. +
  181. +   if (interface)
  182. +       *interface = display->error.interface;
  183. +   if (id)
  184. +       *id = display->error.id;
  185. +
  186. +   pthread_mutex_unlock(&display->mutex);
  187. +
  188. +   return ret;
  189. +}
  190. +
  191. +
  192.  /** Send all buffered requests on the display to the server
  193.   *
  194.   * \param display The display context object
  195. @@ -1513,8 +1579,8 @@ wl_display_flush(struct wl_display *display)
  196.  
  197.     pthread_mutex_lock(&display->mutex);
  198.  
  199. -   if (display->last_error) {
  200. -       errno = display->last_error;
  201. +   if (display->error.code) {
  202. +       errno = display->error.code;
  203.         ret = -1;
  204.     } else {
  205.         ret = wl_connection_flush(display->connection);
  206. diff --git a/src/wayland-client.h b/src/wayland-client.h
  207. index 2a32785..d6b0c4e 100644
  208. --- a/src/wayland-client.h
  209. +++ b/src/wayland-client.h
  210. @@ -161,6 +161,9 @@ int wl_display_dispatch_queue_pending(struct wl_display *display,
  211.                       struct wl_event_queue *queue);
  212.  int wl_display_dispatch_pending(struct wl_display *display);
  213.  int wl_display_get_error(struct wl_display *display);
  214. +int wl_display_get_error_full(struct wl_display *display,
  215. +                 const struct wl_interface **interface,
  216. +                 unsigned int *id);
  217.  
  218.  int wl_display_flush(struct wl_display *display);
  219.  int wl_display_roundtrip(struct wl_display *display);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement