window.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Backend selection for the host window. */
  2. #include <unistd.h>
  3. #include "host.h"
  4. #include "window.h"
  5. static int open;
  6. static int headless;
  7. int host_window_open(unsigned int w, unsigned int h, const char *title)
  8. {
  9. int rv;
  10. if (open) {
  11. if ((unsigned int)host_window_width() == w &&
  12. (unsigned int)host_window_height() == h)
  13. return 0;
  14. return -1;
  15. }
  16. headless = host_opts.headless ? 1 : 0;
  17. rv = headless ? host_headless_open(w, h, title) :
  18. host_sdl_open(w, h, title);
  19. if (rv == 0)
  20. open = 1;
  21. return rv;
  22. }
  23. void host_window_close(void)
  24. {
  25. if (!open)
  26. return;
  27. if (headless)
  28. host_headless_close();
  29. else
  30. host_sdl_close();
  31. open = 0;
  32. }
  33. void *host_window_fb(void)
  34. {
  35. if (!open)
  36. return 0;
  37. return headless ? host_headless_fb() : host_sdl_fb();
  38. }
  39. unsigned int host_window_width(void)
  40. {
  41. if (!open)
  42. return 0;
  43. return headless ? host_headless_width() : host_sdl_width();
  44. }
  45. unsigned int host_window_height(void)
  46. {
  47. if (!open)
  48. return 0;
  49. return headless ? host_headless_height() : host_sdl_height();
  50. }
  51. void host_window_present(int x, int y, unsigned int w, unsigned int h)
  52. {
  53. (void)x;
  54. (void)y;
  55. (void)w;
  56. (void)h;
  57. if (!open)
  58. return;
  59. if (headless)
  60. host_headless_present();
  61. else
  62. host_sdl_present();
  63. }
  64. void host_window_pump(unsigned int timeout_ms)
  65. {
  66. if (!open) {
  67. if (timeout_ms)
  68. usleep(timeout_ms * 1000u);
  69. return;
  70. }
  71. if (headless)
  72. host_headless_pump(timeout_ms);
  73. else
  74. host_sdl_pump(timeout_ms);
  75. }
  76. int host_window_pointer(struct win_pointer *out)
  77. {
  78. if (!open)
  79. return -1;
  80. return headless ? host_headless_pointer(out) : host_sdl_pointer(out);
  81. }
  82. int host_window_event(struct win_event *out)
  83. {
  84. if (!open)
  85. return 0;
  86. return headless ? host_headless_event(out) : host_sdl_event(out);
  87. }
  88. int host_window_scroll(struct win_scroll *out)
  89. {
  90. if (!open)
  91. return 0;
  92. return headless ? host_headless_scroll(out) : host_sdl_scroll(out);
  93. }
  94. int host_window_key(void)
  95. {
  96. if (!open)
  97. return 0;
  98. return headless ? host_headless_key() : host_sdl_key();
  99. }
  100. int host_window_screen(unsigned int *w, unsigned int *h)
  101. {
  102. if (!open)
  103. return -1;
  104. return headless ? host_headless_screen(w, h) : host_sdl_screen(w, h);
  105. }
  106. int host_window_set_cursor(int shape)
  107. {
  108. if (!open)
  109. return -1;
  110. if (!headless)
  111. host_sdl_set_cursor(shape);
  112. return 0;
  113. }
  114. int host_window_key_wait(unsigned int timeout_ms)
  115. {
  116. if (!open) {
  117. host_window_pump(timeout_ms);
  118. return 0;
  119. }
  120. return headless ? host_headless_key_wait(timeout_ms) :
  121. host_sdl_key_wait(timeout_ms);
  122. }
  123. const char *host_window_backend(void)
  124. {
  125. if (!open)
  126. return "none";
  127. return headless ? host_headless_backend() : host_sdl_backend();
  128. }