window_headless.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. int host_headless_key(void)
  113. {
  114. return 0;
  115. }
  116. int host_headless_screen(unsigned int *w, unsigned int *h)
  117. {
  118. *w = (unsigned int)win_w;
  119. *h = (unsigned int)win_h;
  120. return 0;
  121. }
  122. int host_headless_key_wait(unsigned int timeout_ms)
  123. {
  124. host_headless_pump(timeout_ms);
  125. return 0;
  126. }
  127. const char *host_headless_backend(void)
  128. {
  129. return "headless";
  130. }