| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- /* 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);
- }
- /* --keys `{click:X,Y}` / `{rclick:X,Y}`: the pointer moves there and a
- * left/right press is queued, its release on the next event poll;
- * `{move:X,Y}` only moves it (hover). */
- static int ptr_x, ptr_y;
- static unsigned int ptr_buttons;
- static unsigned int click_button; /* bit0 left, bit1 right */
- static int click_pending; /* 1 press queued, 2 release queued */
- int host_headless_pointer(struct win_pointer *out)
- {
- memset(out, 0, sizeof(*out));
- out->x = ptr_x;
- out->y = ptr_y;
- out->buttons = ptr_buttons;
- out->focused = 1;
- out->inside = 1;
- return 0;
- }
- int host_headless_event(struct win_event *out)
- {
- if (!click_pending)
- return 0;
- out->x = ptr_x;
- out->y = ptr_y;
- out->button = click_button;
- if (click_pending == 1) {
- out->type = WIN_EV_POINTER_DOWN;
- ptr_buttons |= click_button;
- click_pending = 2;
- } else {
- out->type = WIN_EV_POINTER_UP;
- ptr_buttons &= ~click_button;
- click_pending = 0;
- }
- out->buttons = ptr_buttons;
- return 1;
- }
- int host_headless_scroll(struct win_scroll *out)
- {
- (void)out;
- return 0;
- }
- /* --keys: the next scripted key once it is due, else 0. */
- static const char *key_pos;
- static unsigned long long key_due;
- int host_headless_key(void)
- {
- static const struct {
- const char *name;
- int code;
- } names[] = {
- {"enter", '\n'}, {"esc", 0x1B}, {"tab", 0x09}, {"bs", 0x08},
- {"del", 0x108}, {"left", 0x100}, {"right", 0x101},
- {"up", 0x102}, {"down", 0x103}, {"ctrl-l", 0x0C}, {"wait", -1},
- };
- unsigned long long now = now_ms();
- int code;
- if (!host_opts.keys || !presented)
- return 0;
- if (!key_pos) {
- key_pos = host_opts.keys;
- key_due = first_present + 300;
- }
- if (!*key_pos || now < key_due)
- return 0;
- code = (unsigned char)*key_pos++;
- if (code == '{' && !strncmp(key_pos, "move:", 5)) {
- const char *end = strchr(key_pos, '}');
- sscanf(key_pos + 5, "%d,%d", &ptr_x, &ptr_y);
- key_pos = end ? end + 1 : key_pos + strlen(key_pos);
- key_due = now + 300;
- return 0;
- }
- if (code == '{' && (!strncmp(key_pos, "click:", 6) ||
- !strncmp(key_pos, "rclick:", 7))) {
- const char *end = strchr(key_pos, '}');
- int right = key_pos[0] == 'r';
- if (sscanf(key_pos + (right ? 7 : 6), "%d,%d",
- &ptr_x, &ptr_y) == 2) {
- click_button = right ? 2u : 1u;
- click_pending = 1;
- }
- key_pos = end ? end + 1 : key_pos + strlen(key_pos);
- key_due = now + 300;
- return 0;
- }
- if (code == '{') {
- const char *end = strchr(key_pos, '}');
- unsigned int i;
- code = 0;
- for (i = 0; end && i < sizeof(names) / sizeof(names[0]); i++)
- if (strlen(names[i].name) == (size_t)(end - key_pos) &&
- !strncmp(key_pos, names[i].name,
- (size_t)(end - key_pos)))
- code = names[i].code;
- key_pos = end ? end + 1 : key_pos + strlen(key_pos);
- if (code < 0) {
- key_due = now + 1000;
- return 0;
- }
- }
- key_due = now + 100;
- return code;
- }
- 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";
- }
|