| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /* browser-host: run the NebOS browser as a Linux/Wayland program.
- *
- * Usage: browser-host [--size WxH] [--root DIR] [--headless]
- * [--wait MS] [--shot FILE] [--keys KEYS] [URL|FILE]
- *
- * --keys types into a headless window, one key every 100 ms from 300 ms
- * after the first frame: plain characters, plus {enter} {esc} {tab} {bs}
- * {del} {up} {down} {left} {right} {ctrl-l}, {wait} (a 1 s pause) and
- * {click:X,Y} (a left click at canvas coordinates X,Y),
- * {rclick:X,Y} (a right click) and {move:X,Y}
- * (move the pointer there without a click, for hover).
- */
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "host.h"
- extern void browser_start(const char *arg);
- static void usage(void)
- {
- fprintf(stderr,
- "usage: browser-host [--size WxH] [--root DIR] [--headless]\n"
- " [--wait MS] [--shot|--screenshot FILE]\n"
- " [--keys KEYS] [URL|FILE]\n");
- }
- static int parse_size(const char *s, int *w, int *h)
- {
- char *end;
- *w = (int)strtol(s, &end, 10);
- if (end == s || (*end != 'x' && *end != 'X'))
- return -1;
- s = end + 1;
- *h = (int)strtol(s, &end, 10);
- if (end == s || *end)
- return -1;
- return (*w > 0 && *h > 0) ? 0 : -1;
- }
- int main(int argc, char **argv)
- {
- char argbuf[4096];
- unsigned int arglen = 0;
- const char *root = 0;
- int i;
- memset(&host_opts, 0, sizeof(host_opts));
- host_opts.wait_ms = 4000;
- argbuf[0] = '\0';
- for (i = 1; i < argc; i++) {
- const char *a = argv[i];
- if (!strcmp(a, "--size") && i + 1 < argc) {
- if (parse_size(argv[++i], &host_opts.width,
- &host_opts.height) != 0) {
- usage();
- return 1;
- }
- } else if (!strcmp(a, "--root") && i + 1 < argc) {
- root = argv[++i];
- } else if (!strcmp(a, "--headless")) {
- host_opts.headless = 1;
- } else if (!strcmp(a, "--wait") && i + 1 < argc) {
- host_opts.wait_ms = (unsigned int)strtoul(argv[++i],
- 0, 10);
- } else if (!strcmp(a, "--shot") && i + 1 < argc) {
- host_opts.shot = argv[++i];
- } else if (!strcmp(a, "--screenshot") && i + 1 < argc) {
- host_opts.shot = argv[++i];
- } else if (!strcmp(a, "--keys") && i + 1 < argc) {
- host_opts.keys = argv[++i];
- } else if (!strcmp(a, "--help") || !strcmp(a, "-h")) {
- usage();
- return 0;
- } else {
- size_t n = strlen(a);
- if (arglen && arglen + 1 < sizeof(argbuf))
- argbuf[arglen++] = ' ';
- if (arglen + n >= sizeof(argbuf))
- n = sizeof(argbuf) - arglen - 1;
- memcpy(argbuf + arglen, a, n);
- arglen += (unsigned int)n;
- argbuf[arglen] = '\0';
- }
- }
- if (host_opts.headless && !host_opts.shot)
- host_opts.shot = "/tmp/opencode/browser.ppm";
- if (host_runtime_init(argv[0], root) != 0)
- return 1;
- signal(SIGPIPE, SIG_IGN);
- browser_start(argbuf[0] ? argbuf : 0);
- return 0;
- }
|