window_headless.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Headless window backend: renders into the same RGB565 canvas and writes a
  2. * PPM after the first present plus host_opts.wait_ms. */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <unistd.h>
  8. #include "host.h"
  9. #include "window.h"
  10. static unsigned short *fb;
  11. static int win_w, win_h;
  12. static int presented;
  13. static unsigned long long first_present;
  14. static unsigned long long now_ms(void)
  15. {
  16. struct timespec ts;
  17. clock_gettime(CLOCK_MONOTONIC, &ts);
  18. return (unsigned long long)ts.tv_sec * 1000ull +
  19. (unsigned long long)ts.tv_nsec / 1000000ull;
  20. }
  21. static int write_ppm(const char *path)
  22. {
  23. FILE *f;
  24. int x, y;
  25. if (!path || !path[0])
  26. return -1;
  27. f = fopen(path, "wb");
  28. if (!f)
  29. return -1;
  30. fprintf(f, "P6\n%d %d\n255\n", win_w, win_h);
  31. for (y = 0; y < win_h; y++) {
  32. for (x = 0; x < win_w; x++) {
  33. unsigned int v = fb[y * win_w + x];
  34. unsigned char rgb[3];
  35. rgb[0] = (unsigned char)(((v >> 11) & 31) * 255 / 31);
  36. rgb[1] = (unsigned char)(((v >> 5) & 63) * 255 / 63);
  37. rgb[2] = (unsigned char)((v & 31) * 255 / 31);
  38. fwrite(rgb, 1, 3, f);
  39. }
  40. }
  41. fclose(f);
  42. fprintf(stderr, "browser-host: wrote %s (%dx%d)\n", path, win_w,
  43. win_h);
  44. return 0;
  45. }
  46. int host_headless_open(unsigned int w, unsigned int h, const char *title)
  47. {
  48. (void)title;
  49. fb = (unsigned short *)calloc((size_t)w * h, sizeof(unsigned short));
  50. if (!fb)
  51. return -1;
  52. win_w = (int)w;
  53. win_h = (int)h;
  54. presented = 0;
  55. first_present = 0;
  56. return 0;
  57. }
  58. void host_headless_close(void)
  59. {
  60. free(fb);
  61. fb = 0;
  62. }
  63. void *host_headless_fb(void)
  64. {
  65. return fb;
  66. }
  67. unsigned int host_headless_width(void)
  68. {
  69. return (unsigned int)win_w;
  70. }
  71. unsigned int host_headless_height(void)
  72. {
  73. return (unsigned int)win_h;
  74. }
  75. void host_headless_present(void)
  76. {
  77. if (!presented) {
  78. presented = 1;
  79. first_present = now_ms();
  80. }
  81. }
  82. void host_headless_pump(unsigned int timeout_ms)
  83. {
  84. if (presented && host_opts.wait_ms) {
  85. unsigned long long now = now_ms();
  86. if (now - first_present >= host_opts.wait_ms) {
  87. write_ppm(host_opts.shot);
  88. fflush(stdout);
  89. _exit(0);
  90. }
  91. }
  92. if (timeout_ms)
  93. usleep(timeout_ms * 1000u);
  94. }
  95. /* --keys `{click:X,Y}` / `{rclick:X,Y}`: the pointer moves there and a
  96. * left/right press is queued, its release on the next event poll;
  97. * `{move:X,Y}` only moves it (hover). */
  98. static int ptr_x, ptr_y;
  99. static unsigned int ptr_buttons;
  100. static unsigned int click_button; /* bit0 left, bit1 right */
  101. static int click_pending; /* 1 press queued, 2 release queued */
  102. int host_headless_pointer(struct win_pointer *out)
  103. {
  104. memset(out, 0, sizeof(*out));
  105. out->x = ptr_x;
  106. out->y = ptr_y;
  107. out->buttons = ptr_buttons;
  108. out->focused = 1;
  109. out->inside = 1;
  110. return 0;
  111. }
  112. int host_headless_event(struct win_event *out)
  113. {
  114. if (!click_pending)
  115. return 0;
  116. out->x = ptr_x;
  117. out->y = ptr_y;
  118. out->button = click_button;
  119. if (click_pending == 1) {
  120. out->type = WIN_EV_POINTER_DOWN;
  121. ptr_buttons |= click_button;
  122. click_pending = 2;
  123. } else {
  124. out->type = WIN_EV_POINTER_UP;
  125. ptr_buttons &= ~click_button;
  126. click_pending = 0;
  127. }
  128. out->buttons = ptr_buttons;
  129. return 1;
  130. }
  131. int host_headless_scroll(struct win_scroll *out)
  132. {
  133. (void)out;
  134. return 0;
  135. }
  136. /* --keys: the next scripted key once it is due, else 0. */
  137. static const char *key_pos;
  138. static unsigned long long key_due;
  139. int host_headless_key(void)
  140. {
  141. static const struct {
  142. const char *name;
  143. int code;
  144. } names[] = {
  145. {"enter", '\n'}, {"esc", 0x1B}, {"tab", 0x09}, {"bs", 0x08},
  146. {"del", 0x108}, {"left", 0x100}, {"right", 0x101},
  147. {"up", 0x102}, {"down", 0x103}, {"ctrl-l", 0x0C}, {"wait", -1},
  148. };
  149. unsigned long long now = now_ms();
  150. int code;
  151. if (!host_opts.keys || !presented)
  152. return 0;
  153. if (!key_pos) {
  154. key_pos = host_opts.keys;
  155. key_due = first_present + 300;
  156. }
  157. if (!*key_pos || now < key_due)
  158. return 0;
  159. code = (unsigned char)*key_pos++;
  160. if (code == '{' && !strncmp(key_pos, "move:", 5)) {
  161. const char *end = strchr(key_pos, '}');
  162. sscanf(key_pos + 5, "%d,%d", &ptr_x, &ptr_y);
  163. key_pos = end ? end + 1 : key_pos + strlen(key_pos);
  164. key_due = now + 300;
  165. return 0;
  166. }
  167. if (code == '{' && (!strncmp(key_pos, "click:", 6) ||
  168. !strncmp(key_pos, "rclick:", 7))) {
  169. const char *end = strchr(key_pos, '}');
  170. int right = key_pos[0] == 'r';
  171. if (sscanf(key_pos + (right ? 7 : 6), "%d,%d",
  172. &ptr_x, &ptr_y) == 2) {
  173. click_button = right ? 2u : 1u;
  174. click_pending = 1;
  175. }
  176. key_pos = end ? end + 1 : key_pos + strlen(key_pos);
  177. key_due = now + 300;
  178. return 0;
  179. }
  180. if (code == '{') {
  181. const char *end = strchr(key_pos, '}');
  182. unsigned int i;
  183. code = 0;
  184. for (i = 0; end && i < sizeof(names) / sizeof(names[0]); i++)
  185. if (strlen(names[i].name) == (size_t)(end - key_pos) &&
  186. !strncmp(key_pos, names[i].name,
  187. (size_t)(end - key_pos)))
  188. code = names[i].code;
  189. key_pos = end ? end + 1 : key_pos + strlen(key_pos);
  190. if (code < 0) {
  191. key_due = now + 1000;
  192. return 0;
  193. }
  194. }
  195. key_due = now + 100;
  196. return code;
  197. }
  198. int host_headless_screen(unsigned int *w, unsigned int *h)
  199. {
  200. *w = (unsigned int)win_w;
  201. *h = (unsigned int)win_h;
  202. return 0;
  203. }
  204. int host_headless_key_wait(unsigned int timeout_ms)
  205. {
  206. host_headless_pump(timeout_ms);
  207. return 0;
  208. }
  209. const char *host_headless_backend(void)
  210. {
  211. return "headless";
  212. }