backend_utils.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //========================================================================
  2. // GLFW 3.4
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2014 Kovid Goyal
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would
  17. // be appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not
  20. // be misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. //
  25. //========================================================================
  26. #pragma once
  27. #include "../kitty/monotonic.h"
  28. #include <poll.h>
  29. #include <unistd.h>
  30. #include <stdbool.h>
  31. #include <sys/types.h>
  32. #ifdef __has_include
  33. #if __has_include(<sys/eventfd.h>)
  34. #define HAS_EVENT_FD
  35. #include <sys/eventfd.h>
  36. #endif
  37. #else
  38. #define HAS_EVENT_FD
  39. #include <sys/eventfd.h>
  40. #endif
  41. typedef unsigned long long id_type;
  42. typedef void(*watch_callback_func)(int, int, void*);
  43. typedef void(*timer_callback_func)(id_type, void*);
  44. typedef void (* GLFWuserdatafreefun)(id_type, void*);
  45. typedef struct {
  46. int fd, events, enabled, ready;
  47. watch_callback_func callback;
  48. void *callback_data;
  49. GLFWuserdatafreefun free;
  50. id_type id;
  51. const char *name;
  52. } Watch;
  53. typedef struct {
  54. id_type id;
  55. monotonic_t interval, trigger_at;
  56. timer_callback_func callback;
  57. void *callback_data;
  58. GLFWuserdatafreefun free;
  59. const char *name;
  60. bool repeats;
  61. } Timer;
  62. typedef struct {
  63. struct pollfd fds[32];
  64. #ifdef HAS_EVENT_FD
  65. int wakeupFd;
  66. #else
  67. int wakeupFds[2];
  68. #endif
  69. bool wakeup_data_read, wakeup_fd_ready;
  70. nfds_t watches_count, timers_count;
  71. Watch watches[32];
  72. Timer timers[128];
  73. } EventLoopData;
  74. void check_for_wakeup_events(EventLoopData *eld);
  75. id_type addWatch(EventLoopData *eld, const char *name, int fd, int events, int enabled, watch_callback_func cb, void *cb_data);
  76. void removeWatch(EventLoopData *eld, id_type watch_id);
  77. void toggleWatch(EventLoopData *eld, id_type watch_id, int enabled);
  78. id_type addTimer(EventLoopData *eld, const char *name, monotonic_t interval, int enabled, bool repeats, timer_callback_func cb, void *cb_data, GLFWuserdatafreefun free);
  79. void removeTimer(EventLoopData *eld, id_type timer_id);
  80. void removeAllTimers(EventLoopData *eld);
  81. void toggleTimer(EventLoopData *eld, id_type timer_id, int enabled);
  82. void changeTimerInterval(EventLoopData *eld, id_type timer_id, monotonic_t interval);
  83. monotonic_t prepareForPoll(EventLoopData *eld, monotonic_t timeout);
  84. int pollWithTimeout(struct pollfd *fds, nfds_t nfds, monotonic_t timeout);
  85. int pollForEvents(EventLoopData *eld, monotonic_t timeout, watch_callback_func);
  86. unsigned dispatchTimers(EventLoopData *eld);
  87. void finalizePollData(EventLoopData *eld);
  88. bool initPollData(EventLoopData *eld, int display_fd);
  89. void wakeupEventLoop(EventLoopData *eld);
  90. char* utf_8_strndup(const char* source, size_t max_length);
  91. int createAnonymousFile(off_t size);