window_headless.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. int host_headless_pointer(struct win_pointer *out)
  96. {
  97. memset(out, 0, sizeof(*out));
  98. out->focused = 1;
  99. out->inside = 1;
  100. return 0;
  101. }
  102. int host_headless_event(struct win_event *out)
  103. {
  104. (void)out;
  105. return 0;
  106. }
  107. int host_headless_scroll(struct win_scroll *out)
  108. {
  109. (void)out;
  110. return 0;
  111. }
  112. /* --keys: the next scripted key once it is due, else 0. */
  113. static const char *key_pos;
  114. static unsigned long long key_due;
  115. int host_headless_key(void)
  116. {
  117. static const struct {
  118. const char *name;
  119. int code;
  120. } names[] = {
  121. {"enter", '\n'}, {"esc", 0x1B}, {"tab", 0x09}, {"bs", 0x08},
  122. {"del", 0x108}, {"left", 0x100}, {"right", 0x101},
  123. {"up", 0x102}, {"down", 0x103}, {"ctrl-l", 0x0C}, {"wait", -1},
  124. };
  125. unsigned long long now = now_ms();
  126. int code;
  127. if (!host_opts.keys || !presented)
  128. return 0;
  129. if (!key_pos) {
  130. key_pos = host_opts.keys;
  131. key_due = first_present + 300;
  132. }
  133. if (!*key_pos || now < key_due)
  134. return 0;
  135. code = (unsigned char)*key_pos++;
  136. if (code == '{') {
  137. const char *end = strchr(key_pos, '}');
  138. unsigned int i;
  139. code = 0;
  140. for (i = 0; end && i < sizeof(names) / sizeof(names[0]); i++)
  141. if (strlen(names[i].name) == (size_t)(end - key_pos) &&
  142. !strncmp(key_pos, names[i].name,
  143. (size_t)(end - key_pos)))
  144. code = names[i].code;
  145. key_pos = end ? end + 1 : key_pos + strlen(key_pos);
  146. if (code < 0) {
  147. key_due = now + 1000;
  148. return 0;
  149. }
  150. }
  151. key_due = now + 100;
  152. return code;
  153. }
  154. int host_headless_screen(unsigned int *w, unsigned int *h)
  155. {
  156. *w = (unsigned int)win_w;
  157. *h = (unsigned int)win_h;
  158. return 0;
  159. }
  160. int host_headless_key_wait(unsigned int timeout_ms)
  161. {
  162. host_headless_pump(timeout_ms);
  163. return 0;
  164. }
  165. const char *host_headless_backend(void)
  166. {
  167. return "headless";
  168. }