main.c 2.7 KB

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