| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /* Headless window backend: renders into the same RGB565 canvas and writes a
- * PPM after the first present plus host_opts.wait_ms. */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- #include "host.h"
- #include "window.h"
- static unsigned short *fb;
- static int win_w, win_h;
- static int presented;
- static unsigned long long first_present;
- static unsigned long long now_ms(void)
- {
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (unsigned long long)ts.tv_sec * 1000ull +
- (unsigned long long)ts.tv_nsec / 1000000ull;
- }
- static int write_ppm(const char *path)
- {
- FILE *f;
- int x, y;
- if (!path || !path[0])
- return -1;
- f = fopen(path, "wb");
- if (!f)
- return -1;
- fprintf(f, "P6\n%d %d\n255\n", win_w, win_h);
- for (y = 0; y < win_h; y++) {
- for (x = 0; x < win_w; x++) {
- unsigned int v = fb[y * win_w + x];
- unsigned char rgb[3];
- rgb[0] = (unsigned char)(((v >> 11) & 31) * 255 / 31);
- rgb[1] = (unsigned char)(((v >> 5) & 63) * 255 / 63);
- rgb[2] = (unsigned char)((v & 31) * 255 / 31);
- fwrite(rgb, 1, 3, f);
- }
- }
- fclose(f);
- fprintf(stderr, "browser-host: wrote %s (%dx%d)\n", path, win_w,
- win_h);
- return 0;
- }
- int host_headless_open(unsigned int w, unsigned int h, const char *title)
- {
- (void)title;
- fb = (unsigned short *)calloc((size_t)w * h, sizeof(unsigned short));
- if (!fb)
- return -1;
- win_w = (int)w;
- win_h = (int)h;
- presented = 0;
- first_present = 0;
- return 0;
- }
- void host_headless_close(void)
- {
- free(fb);
- fb = 0;
- }
- void *host_headless_fb(void)
- {
- return fb;
- }
- unsigned int host_headless_width(void)
- {
- return (unsigned int)win_w;
- }
- unsigned int host_headless_height(void)
- {
- return (unsigned int)win_h;
- }
- void host_headless_present(void)
- {
- if (!presented) {
- presented = 1;
- first_present = now_ms();
- }
- }
- void host_headless_pump(unsigned int timeout_ms)
- {
- if (presented && host_opts.wait_ms) {
- unsigned long long now = now_ms();
- if (now - first_present >= host_opts.wait_ms) {
- write_ppm(host_opts.shot);
- fflush(stdout);
- _exit(0);
- }
- }
- if (timeout_ms)
- usleep(timeout_ms * 1000u);
- }
- int host_headless_pointer(struct win_pointer *out)
- {
- memset(out, 0, sizeof(*out));
- out->focused = 1;
- out->inside = 1;
- return 0;
- }
- int host_headless_event(struct win_event *out)
- {
- (void)out;
- return 0;
- }
- int host_headless_scroll(struct win_scroll *out)
- {
- (void)out;
- return 0;
- }
- int host_headless_key(void)
- {
- return 0;
- }
- int host_headless_screen(unsigned int *w, unsigned int *h)
- {
- *w = (unsigned int)win_w;
- *h = (unsigned int)win_h;
- return 0;
- }
- int host_headless_key_wait(unsigned int timeout_ms)
- {
- host_headless_pump(timeout_ms);
- return 0;
- }
- const char *host_headless_backend(void)
- {
- return "headless";
- }
|