main.c 2.6 KB

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