main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* browser-host: run the NebOS browser as a Linux/Wayland program.
  2. *
  3. * Usage: browser-host [--size WxH] [--root DIR] [--headless]
  4. * [--wait MS] [--shot FILE] [--keys KEYS] [URL|FILE]
  5. *
  6. * --keys types into a headless window, one key every 100 ms from 300 ms
  7. * after the first frame: plain characters, plus {enter} {esc} {tab} {bs}
  8. * {del} {up} {down} {left} {right} {ctrl-l}, {wait} (a 1 s pause) and
  9. * {click:X,Y} (a left click at canvas coordinates X,Y) and {move:X,Y}
  10. * (move the pointer there without a click, for hover).
  11. */
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "host.h"
  17. extern void browser_start(const char *arg);
  18. static void usage(void)
  19. {
  20. fprintf(stderr,
  21. "usage: browser-host [--size WxH] [--root DIR] [--headless]\n"
  22. " [--wait MS] [--shot|--screenshot FILE]\n"
  23. " [--keys KEYS] [URL|FILE]\n");
  24. }
  25. static int parse_size(const char *s, int *w, int *h)
  26. {
  27. char *end;
  28. *w = (int)strtol(s, &end, 10);
  29. if (end == s || (*end != 'x' && *end != 'X'))
  30. return -1;
  31. s = end + 1;
  32. *h = (int)strtol(s, &end, 10);
  33. if (end == s || *end)
  34. return -1;
  35. return (*w > 0 && *h > 0) ? 0 : -1;
  36. }
  37. int main(int argc, char **argv)
  38. {
  39. char argbuf[4096];
  40. unsigned int arglen = 0;
  41. const char *root = 0;
  42. int i;
  43. memset(&host_opts, 0, sizeof(host_opts));
  44. host_opts.wait_ms = 4000;
  45. argbuf[0] = '\0';
  46. for (i = 1; i < argc; i++) {
  47. const char *a = argv[i];
  48. if (!strcmp(a, "--size") && i + 1 < argc) {
  49. if (parse_size(argv[++i], &host_opts.width,
  50. &host_opts.height) != 0) {
  51. usage();
  52. return 1;
  53. }
  54. } else if (!strcmp(a, "--root") && i + 1 < argc) {
  55. root = argv[++i];
  56. } else if (!strcmp(a, "--headless")) {
  57. host_opts.headless = 1;
  58. } else if (!strcmp(a, "--wait") && i + 1 < argc) {
  59. host_opts.wait_ms = (unsigned int)strtoul(argv[++i],
  60. 0, 10);
  61. } else if (!strcmp(a, "--shot") && i + 1 < argc) {
  62. host_opts.shot = argv[++i];
  63. } else if (!strcmp(a, "--screenshot") && i + 1 < argc) {
  64. host_opts.shot = argv[++i];
  65. } else if (!strcmp(a, "--keys") && i + 1 < argc) {
  66. host_opts.keys = argv[++i];
  67. } else if (!strcmp(a, "--help") || !strcmp(a, "-h")) {
  68. usage();
  69. return 0;
  70. } else {
  71. size_t n = strlen(a);
  72. if (arglen && arglen + 1 < sizeof(argbuf))
  73. argbuf[arglen++] = ' ';
  74. if (arglen + n >= sizeof(argbuf))
  75. n = sizeof(argbuf) - arglen - 1;
  76. memcpy(argbuf + arglen, a, n);
  77. arglen += (unsigned int)n;
  78. argbuf[arglen] = '\0';
  79. }
  80. }
  81. if (host_opts.headless && !host_opts.shot)
  82. host_opts.shot = "/tmp/opencode/browser.ppm";
  83. if (host_runtime_init(argv[0], root) != 0)
  84. return 1;
  85. signal(SIGPIPE, SIG_IGN);
  86. browser_start(argbuf[0] ? argbuf : 0);
  87. return 0;
  88. }