window_headless.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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}`: the pointer moves there and a left press is
  96. * queued, its release on the next event poll; `{move:X,Y}` only moves
  97. * it (hover). */
  98. static int ptr_x, ptr_y;
  99. static unsigned int ptr_buttons;
  100. static int click_pending; /* 1 press queued, 2 release queued */
  101. int host_headless_pointer(struct win_pointer *out)
  102. {
  103. memset(out, 0, sizeof(*out));
  104. out->x = ptr_x;
  105. out->y = ptr_y;
  106. out->buttons = ptr_buttons;
  107. out->focused = 1;
  108. out->inside = 1;
  109. return 0;
  110. }
  111. int host_headless_event(struct win_event *out)
  112. {
  113. if (!click_pending)
  114. return 0;
  115. out->x = ptr_x;
  116. out->y = ptr_y;
  117. if (click_pending == 1) {
  118. out->type = WIN_EV_POINTER_DOWN;
  119. ptr_buttons = 1;
  120. click_pending = 2;
  121. } else {
  122. out->type = WIN_EV_POINTER_UP;
  123. ptr_buttons = 0;
  124. click_pending = 0;
  125. }
  126. out->buttons = ptr_buttons;
  127. return 1;
  128. }
  129. int host_headless_scroll(struct win_scroll *out)
  130. {
  131. (void)out;
  132. return 0;
  133. }
  134. /* --keys: the next scripted key once it is due, else 0. */
  135. static const char *key_pos;
  136. static unsigned long long key_due;
  137. int host_headless_key(void)
  138. {
  139. static const struct {
  140. const char *name;
  141. int code;
  142. } names[] = {
  143. {"enter", '\n'}, {"esc", 0x1B}, {"tab", 0x09}, {"bs", 0x08},
  144. {"del", 0x108}, {"left", 0x100}, {"right", 0x101},
  145. {"up", 0x102}, {"down", 0x103}, {"ctrl-l", 0x0C}, {"wait", -1},
  146. };
  147. unsigned long long now = now_ms();
  148. int code;
  149. if (!host_opts.keys || !presented)
  150. return 0;
  151. if (!key_pos) {
  152. key_pos = host_opts.keys;
  153. key_due = first_present + 300;
  154. }
  155. if (!*key_pos || now < key_due)
  156. return 0;
  157. code = (unsigned char)*key_pos++;
  158. if (code == '{' && !strncmp(key_pos, "move:", 5)) {
  159. const char *end = strchr(key_pos, '}');
  160. sscanf(key_pos + 5, "%d,%d", &ptr_x, &ptr_y);
  161. key_pos = end ? end + 1 : key_pos + strlen(key_pos);
  162. key_due = now + 300;
  163. return 0;
  164. }
  165. if (code == '{' && !strncmp(key_pos, "click:", 6)) {
  166. const char *end = strchr(key_pos, '}');
  167. if (sscanf(key_pos + 6, "%d,%d", &ptr_x, &ptr_y) == 2)
  168. click_pending = 1;
  169. key_pos = end ? end + 1 : key_pos + strlen(key_pos);
  170. key_due = now + 300;
  171. return 0;
  172. }
  173. if (code == '{') {
  174. const char *end = strchr(key_pos, '}');
  175. unsigned int i;
  176. code = 0;
  177. for (i = 0; end && i < sizeof(names) / sizeof(names[0]); i++)
  178. if (strlen(names[i].name) == (size_t)(end - key_pos) &&
  179. !strncmp(key_pos, names[i].name,
  180. (size_t)(end - key_pos)))
  181. code = names[i].code;
  182. key_pos = end ? end + 1 : key_pos + strlen(key_pos);
  183. if (code < 0) {
  184. key_due = now + 1000;
  185. return 0;
  186. }
  187. }
  188. key_due = now + 100;
  189. return code;
  190. }
  191. int host_headless_screen(unsigned int *w, unsigned int *h)
  192. {
  193. *w = (unsigned int)win_w;
  194. *h = (unsigned int)win_h;
  195. return 0;
  196. }
  197. int host_headless_key_wait(unsigned int timeout_ms)
  198. {
  199. host_headless_pump(timeout_ms);
  200. return 0;
  201. }
  202. const char *host_headless_backend(void)
  203. {
  204. return "headless";
  205. }