event-loop.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "event-loop.h"
  2. #include "purple.h"
  3. #include <glib.h>
  4. /**
  5. * The following eventloop functions are used in both pidgin and purple-text. If your
  6. * application uses glib mainloop, you can safely use this verbatim.
  7. */
  8. #define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
  9. #define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
  10. typedef struct _PurpleGLibIOClosure {
  11. PurpleInputFunction function;
  12. guint result;
  13. gpointer data;
  14. } PurpleGLibIOClosure;
  15. static void purple_glib_io_destroy(gpointer data)
  16. {
  17. g_free(data);
  18. }
  19. static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
  20. {
  21. PurpleGLibIOClosure *closure = data;
  22. PurpleInputCondition purple_cond = 0;
  23. if (condition & PURPLE_GLIB_READ_COND)
  24. purple_cond |= PURPLE_INPUT_READ;
  25. if (condition & PURPLE_GLIB_WRITE_COND)
  26. purple_cond |= PURPLE_INPUT_WRITE;
  27. closure->function(closure->data, g_io_channel_unix_get_fd(source),
  28. purple_cond);
  29. return TRUE;
  30. }
  31. static guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function,
  32. gpointer data)
  33. {
  34. PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
  35. GIOChannel *channel;
  36. GIOCondition cond = 0;
  37. closure->function = function;
  38. closure->data = data;
  39. if (condition & PURPLE_INPUT_READ)
  40. cond |= PURPLE_GLIB_READ_COND;
  41. if (condition & PURPLE_INPUT_WRITE)
  42. cond |= PURPLE_GLIB_WRITE_COND;
  43. #if defined _WIN32 && !defined WINPIDGIN_USE_GLIB_IO_CHANNEL
  44. channel = wpurple_g_io_channel_win32_new_socket(fd);
  45. #else
  46. channel = g_io_channel_unix_new(fd);
  47. #endif
  48. closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
  49. purple_glib_io_invoke, closure, purple_glib_io_destroy);
  50. g_io_channel_unref(channel);
  51. return closure->result;
  52. }
  53. static PurpleEventLoopUiOps glib_eventloops =
  54. {
  55. g_timeout_add,
  56. g_source_remove,
  57. glib_input_add,
  58. g_source_remove,
  59. NULL,
  60. #if GLIB_CHECK_VERSION(2,14,0)
  61. g_timeout_add_seconds,
  62. #else
  63. NULL,
  64. #endif
  65. /* padding */
  66. NULL,
  67. NULL,
  68. NULL
  69. };
  70. void
  71. sapphire_set_eventloop(void)
  72. {
  73. purple_eventloop_set_ui_ops(&glib_eventloops);
  74. }
  75. /*** End of the eventloop functions. ***/