main.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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} and {wait} (a 1 s pause).
  9. */
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "host.h"
  15. extern void browser_start(const char *arg);
  16. static void usage(void)
  17. {
  18. fprintf(stderr,
  19. "usage: browser-host [--size WxH] [--root DIR] [--headless]\n"
  20. " [--wait MS] [--shot|--screenshot FILE]\n"
  21. " [--keys KEYS] [URL|FILE]\n");
  22. }
  23. static int parse_size(const char *s, int *w, int *h)
  24. {
  25. char *end;
  26. *w = (int)strtol(s, &end, 10);
  27. if (end == s || (*end != 'x' && *end != 'X'))
  28. return -1;
  29. s = end + 1;
  30. *h = (int)strtol(s, &end, 10);
  31. if (end == s || *end)
  32. return -1;
  33. return (*w > 0 && *h > 0) ? 0 : -1;
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. char argbuf[4096];
  38. unsigned int arglen = 0;
  39. const char *root = 0;
  40. int i;
  41. memset(&host_opts, 0, sizeof(host_opts));
  42. host_opts.wait_ms = 4000;
  43. argbuf[0] = '\0';
  44. for (i = 1; i < argc; i++) {
  45. const char *a = argv[i];
  46. if (!strcmp(a, "--size") && i + 1 < argc) {
  47. if (parse_size(argv[++i], &host_opts.width,
  48. &host_opts.height) != 0) {
  49. usage();
  50. return 1;
  51. }
  52. } else if (!strcmp(a, "--root") && i + 1 < argc) {
  53. root = argv[++i];
  54. } else if (!strcmp(a, "--headless")) {
  55. host_opts.headless = 1;
  56. } else if (!strcmp(a, "--wait") && i + 1 < argc) {
  57. host_opts.wait_ms = (unsigned int)strtoul(argv[++i],
  58. 0, 10);
  59. } else if (!strcmp(a, "--shot") && i + 1 < argc) {
  60. host_opts.shot = argv[++i];
  61. } else if (!strcmp(a, "--screenshot") && i + 1 < argc) {
  62. host_opts.shot = argv[++i];
  63. } else if (!strcmp(a, "--keys") && i + 1 < argc) {
  64. host_opts.keys = argv[++i];
  65. } else if (!strcmp(a, "--help") || !strcmp(a, "-h")) {
  66. usage();
  67. return 0;
  68. } else {
  69. size_t n = strlen(a);
  70. if (arglen && arglen + 1 < sizeof(argbuf))
  71. argbuf[arglen++] = ' ';
  72. if (arglen + n >= sizeof(argbuf))
  73. n = sizeof(argbuf) - arglen - 1;
  74. memcpy(argbuf + arglen, a, n);
  75. arglen += (unsigned int)n;
  76. argbuf[arglen] = '\0';
  77. }
  78. }
  79. if (host_opts.headless && !host_opts.shot)
  80. host_opts.shot = "/tmp/opencode/browser.ppm";
  81. if (host_runtime_init(argv[0], root) != 0)
  82. return 1;
  83. signal(SIGPIPE, SIG_IGN);
  84. browser_start(argbuf[0] ? argbuf : 0);
  85. return 0;
  86. }