ucmd_service.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* service: init's CLI and GUI client.
  2. *
  3. * service list services
  4. * service status <name>
  5. * service start|stop|restart|kill <name>
  6. * service logs <name> last lines
  7. * service logtail <name> follow until Ctrl-C
  8. * service --gui
  9. *
  10. * State and logs are plain files in /share; actions are one-line requests on
  11. * init's control tty (see service_proto.c). The client never spawns or kills
  12. * a service itself.
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "ngui.h"
  17. #include "service_proto.h"
  18. #include "ucmd_lib.h"
  19. #define LOG_LINES 40
  20. #define LOOP_MS 50
  21. #define POLL_MS 500
  22. static char log_buf[SVC_LOG_MAX + 1];
  23. /* ---- shared helpers -------------------------------------------------- */
  24. static int read_state_file(const char *name, struct svc_state *st)
  25. {
  26. char path[64], buf[512];
  27. int n;
  28. if (svc_make_state_path(path, sizeof(path), name) != 0)
  29. return -1;
  30. n = u_read_file(path, buf, sizeof(buf) - 1);
  31. if (n <= 0)
  32. return -1;
  33. buf[n] = '\0';
  34. return svc_parse_state(buf, (unsigned int)n, st);
  35. }
  36. static int read_init_file(struct init_state *is)
  37. {
  38. char buf[256];
  39. int n = u_read_file(SVC_INIT_STATE, buf, sizeof(buf) - 1);
  40. if (n <= 0)
  41. return -1;
  42. buf[n] = '\0';
  43. return svc_parse_init_state(buf, (unsigned int)n, is);
  44. }
  45. static int init_is_running(const struct init_state *is)
  46. {
  47. return is->valid && is->pid && u_alive(is->pid);
  48. }
  49. static int wm_is_running(const struct init_state *is)
  50. {
  51. return is->valid && is->wm && u_alive(is->wm);
  52. }
  53. static void trim_line(char *s)
  54. {
  55. unsigned int i = 0;
  56. while (s[i] && s[i] != '\n' && s[i] != '\r')
  57. i++;
  58. s[i] = '\0';
  59. }
  60. static void fmt_age(unsigned int started, char *out, unsigned int max)
  61. {
  62. unsigned int s, m, h;
  63. if (!started) {
  64. snprintf(out, max, "-");
  65. return;
  66. }
  67. s = (u_ticks_ms() - started) / 1000u;
  68. m = s / 60u;
  69. h = m / 60u;
  70. if (h)
  71. snprintf(out, max, "%uh%um", h, m % 60u);
  72. else if (m)
  73. snprintf(out, max, "%um%us", m, s % 60u);
  74. else
  75. snprintf(out, max, "%us", s);
  76. }
  77. static int read_log(const char *name)
  78. {
  79. char path[64];
  80. int n;
  81. if (strcmp(name, "init") == 0) {
  82. n = u_read_file(SVC_INIT_LOG, log_buf, SVC_LOG_MAX);
  83. } else if (strcmp(name, "wm") == 0) {
  84. return -1;
  85. } else {
  86. if (svc_make_log_path(path, sizeof(path), name) != 0)
  87. return -1;
  88. n = u_read_file(path, log_buf, SVC_LOG_MAX);
  89. }
  90. if (n < 0)
  91. n = 0;
  92. log_buf[n] = '\0';
  93. return n;
  94. }
  95. static void print_last_lines(int n, int want)
  96. {
  97. int i = n, lines = 0;
  98. while (i > 0 && lines < want) {
  99. i--;
  100. if (log_buf[i] == '\n' && i + 1 < n)
  101. lines++;
  102. }
  103. u_puts(log_buf + i);
  104. }
  105. static int request_action(const char *op, const char *name, char *msg,
  106. unsigned int msgmax, int *pid)
  107. {
  108. int code = 0;
  109. int r;
  110. if (pid)
  111. *pid = 0;
  112. r = svc_request(op, name, 2500, &code, pid, msg, msgmax);
  113. if (r == -2) {
  114. snprintf(msg, msgmax, "init is not running");
  115. return -2;
  116. }
  117. if (r != 0) {
  118. snprintf(msg, msgmax, "init is not responding");
  119. return -1;
  120. }
  121. trim_line(msg);
  122. return code;
  123. }
  124. /* ---- CLI ------------------------------------------------------------- */
  125. static void cli_list(void)
  126. {
  127. struct init_state is;
  128. struct svc_state st;
  129. char age[16], line[160];
  130. unsigned int i;
  131. if (read_init_file(&is) != 0)
  132. memset(&is, 0, sizeof(is));
  133. u_puts("NAME STATE PID AGE DESCRIPTION\n");
  134. if (init_is_running(&is)) {
  135. snprintf(line, sizeof(line), "%-10s %-10s %-5u %-7s %s\n",
  136. "init", "running", is.pid, "-", "service manager");
  137. } else {
  138. snprintf(line, sizeof(line), "%-10s %-10s %-5s %-7s %s\n",
  139. "init", "down", "-", "-", "service manager");
  140. }
  141. u_puts(line);
  142. if (wm_is_running(&is)) {
  143. snprintf(line, sizeof(line), "%-10s %-10s %-5u %-7s %s\n",
  144. "wm", "running", is.wm, "-",
  145. "window manager (system)");
  146. } else {
  147. snprintf(line, sizeof(line), "%-10s %-10s %-5s %-7s %s\n",
  148. "wm", "stopped", "-", "-",
  149. "window manager (system)");
  150. }
  151. u_puts(line);
  152. for (i = 0; i < svc_def_count; i++) {
  153. const char *state = "stopped";
  154. unsigned int pid = 0;
  155. unsigned int started = 0;
  156. if (read_state_file(svc_defs[i].name, &st) == 0) {
  157. pid = st.pid;
  158. started = st.started;
  159. if (pid && !u_alive(pid))
  160. pid = 0;
  161. if (pid)
  162. state = svc_state_name(st.state);
  163. }
  164. if (pid)
  165. fmt_age(started, age, sizeof(age));
  166. else
  167. snprintf(age, sizeof(age), "-");
  168. snprintf(line, sizeof(line), "%-10s %-10s %-5u %-7s %s\n",
  169. svc_defs[i].name, state, pid, age, svc_defs[i].desc);
  170. u_puts(line);
  171. }
  172. if (!is.valid)
  173. u_puts("service: init state is missing (use `service` after boot)\n");
  174. else if (!init_is_running(&is))
  175. u_puts("service: init is not running; the kernel will restart it\n");
  176. }
  177. static void cli_status(const char *name)
  178. {
  179. struct init_state is;
  180. struct svc_state st;
  181. char age[16];
  182. if (strcmp(name, "init") == 0) {
  183. if (read_init_file(&is) != 0) {
  184. u_puts("init: not running\n");
  185. return;
  186. }
  187. u_puts(init_is_running(&is) ? "init: running\n" : "init: down\n");
  188. u_puts(" pid "); u_putdec(is.pid);
  189. u_puts(", control tty "); u_putdec(is.ctl);
  190. u_puts(", wm "); u_putdec(wm_is_running(&is) ? is.wm : 0);
  191. u_puts("\n log " SVC_INIT_LOG "\n");
  192. return;
  193. }
  194. if (strcmp(name, "wm") == 0) {
  195. read_init_file(&is);
  196. if (wm_is_running(&is)) {
  197. u_puts("wm: running\n pid ");
  198. u_putdec(is.wm);
  199. u_puts("\n system service (restart/kill only)\n");
  200. } else {
  201. u_puts("wm: stopped\n");
  202. }
  203. return;
  204. }
  205. if (svc_find(name) < 0) {
  206. u_puts("service: unknown service\n");
  207. return;
  208. }
  209. if (read_state_file(name, &st) != 0) {
  210. u_puts(name); u_puts(": stopped (no state file)\n");
  211. return;
  212. }
  213. if (st.pid && !u_alive(st.pid))
  214. st.pid = 0;
  215. fmt_age(st.started, age, sizeof(age));
  216. u_puts(name); u_puts(": ");
  217. u_puts(st.pid ? svc_state_name(st.state) : "stopped");
  218. u_puts("\n pid "); u_putdec(st.pid);
  219. u_puts(", tty "); u_putdec(st.tty);
  220. u_puts(", age "); u_puts(age);
  221. u_puts("\n desired "); u_puts(st.desired ? "running" : "stopped");
  222. u_puts("\n ");
  223. {
  224. int i = svc_find(name);
  225. u_puts(svc_defs[i].desc);
  226. }
  227. u_puts("\n");
  228. }
  229. static int cli_action(const char *op, const char *name)
  230. {
  231. char msg[96];
  232. int pid = 0;
  233. int code = request_action(op, name, msg, sizeof(msg), &pid);
  234. if (code < 0) {
  235. u_puts("service: "); u_puts(msg); return 1;
  236. }
  237. u_puts(name); u_puts(": "); u_puts(msg);
  238. if (pid > 0) {
  239. u_puts(" (pid "); u_putdec((unsigned int)pid); u_puts(")");
  240. }
  241. u_puts("\n");
  242. if (code == 0 &&
  243. (strcmp(op, "start") == 0 || strcmp(op, "restart") == 0)) {
  244. struct svc_state st;
  245. u_sleep_ms(350);
  246. if (read_state_file(name, &st) == 0 &&
  247. (!st.pid || !u_alive(st.pid))) {
  248. int n = read_log(name);
  249. u_puts("service: "); u_puts(name);
  250. u_puts(" did not stay up; last log lines:\n");
  251. if (n > 0)
  252. print_last_lines(n, 12);
  253. return 1;
  254. }
  255. }
  256. return code ? 1 : 0;
  257. }
  258. static int cli_logs(const char *name, int follow)
  259. {
  260. unsigned int seen = 0;
  261. int n;
  262. if (strcmp(name, "wm") == 0) {
  263. u_puts("wm: no captured log (the WM writes to the serial console)\n");
  264. return 1;
  265. }
  266. if (read_log(name) < 0) {
  267. u_puts("service: no log for "); u_puts(name); u_puts("\n");
  268. return 1;
  269. }
  270. if (!follow) {
  271. n = (int)strlen(log_buf);
  272. if (!n) {
  273. u_puts("service: log is empty\n");
  274. return 0;
  275. }
  276. print_last_lines(n, LOG_LINES);
  277. return 0;
  278. }
  279. for (;;) {
  280. n = read_log(name);
  281. if (n < 0)
  282. n = 0;
  283. if ((unsigned int)n < seen)
  284. seen = 0;
  285. if ((unsigned int)n > seen) {
  286. u_puts(log_buf + seen);
  287. seen = (unsigned int)n;
  288. }
  289. u_sleep_ms(250);
  290. }
  291. }
  292. static void cli_usage(void)
  293. {
  294. u_puts("usage: service [list]\n"
  295. " service status <name>\n"
  296. " service start|stop|restart|kill <name>\n"
  297. " service logs <name> | logtail <name>\n"
  298. " service --gui\n");
  299. }
  300. static void cli(const char *arg)
  301. {
  302. char cmd[16], name[16];
  303. unsigned int i = 0, n;
  304. while (arg && (arg[i] == ' ' || arg[i] == '\t'))
  305. i++;
  306. n = 0;
  307. while (arg && arg[i] && arg[i] != ' ' && arg[i] != '\t' &&
  308. n + 1 < sizeof(cmd))
  309. cmd[n++] = arg[i++];
  310. cmd[n] = '\0';
  311. while (arg && (arg[i] == ' ' || arg[i] == '\t'))
  312. i++;
  313. n = 0;
  314. while (arg && arg[i] && arg[i] != ' ' && arg[i] != '\t' &&
  315. n + 1 < sizeof(name))
  316. name[n++] = arg[i++];
  317. name[n] = '\0';
  318. if (!cmd[0] || strcmp(cmd, "list") == 0) {
  319. cli_list();
  320. return;
  321. }
  322. if (strcmp(cmd, "help") == 0) {
  323. cli_usage();
  324. return;
  325. }
  326. if (strcmp(cmd, "status") == 0) {
  327. if (!name[0]) { cli_usage(); return; }
  328. cli_status(name);
  329. return;
  330. }
  331. if (strcmp(cmd, "logs") == 0 || strcmp(cmd, "logtail") == 0) {
  332. if (!name[0]) { cli_usage(); return; }
  333. cli_logs(name, strcmp(cmd, "logtail") == 0);
  334. return;
  335. }
  336. if (strcmp(cmd, "start") == 0 || strcmp(cmd, "stop") == 0 ||
  337. strcmp(cmd, "restart") == 0 || strcmp(cmd, "kill") == 0) {
  338. if (!name[0]) { cli_usage(); return; }
  339. cli_action(cmd, name);
  340. return;
  341. }
  342. cli_usage();
  343. }
  344. /* ---- GUI ------------------------------------------------------------- */
  345. #define VIEW_MAX (2 + SVC_MAX)
  346. #define ROW_H 52
  347. enum {
  348. ID_REFRESH = 1,
  349. ID_START,
  350. ID_STOP,
  351. ID_RESTART,
  352. ID_KILL,
  353. ID_ROW_BASE = 100
  354. };
  355. struct view {
  356. char name[16];
  357. char state[16];
  358. char desc[80];
  359. unsigned int pid;
  360. int running;
  361. int is_init;
  362. int is_wm;
  363. };
  364. static struct ngui ui;
  365. static struct win_info wi;
  366. static struct view views[VIEW_MAX];
  367. static int nviews;
  368. static int selected;
  369. static int pending_action;
  370. static int kill_arm;
  371. static unsigned int kill_arm_at;
  372. static char gui_status[96];
  373. static unsigned int gui_status_at;
  374. static unsigned int log_len;
  375. static struct ngui_scroll log_scroll;
  376. static int log_follow = 1;
  377. static void gui_refresh(void)
  378. {
  379. struct init_state is;
  380. struct svc_state st;
  381. unsigned int i;
  382. memset(&is, 0, sizeof(is));
  383. read_init_file(&is);
  384. nviews = 0;
  385. memset(&views[nviews], 0, sizeof(views[nviews]));
  386. snprintf(views[nviews].name, sizeof(views[nviews].name), "init");
  387. snprintf(views[nviews].state, sizeof(views[nviews].state),
  388. init_is_running(&is) ? "running" : "down");
  389. snprintf(views[nviews].desc, sizeof(views[nviews].desc),
  390. "service manager (pid 1)");
  391. views[nviews].pid = init_is_running(&is) ? is.pid : 0;
  392. views[nviews].running = init_is_running(&is);
  393. views[nviews].is_init = 1;
  394. nviews++;
  395. memset(&views[nviews], 0, sizeof(views[nviews]));
  396. snprintf(views[nviews].name, sizeof(views[nviews].name), "wm");
  397. snprintf(views[nviews].state, sizeof(views[nviews].state),
  398. wm_is_running(&is) ? "running" : "stopped");
  399. snprintf(views[nviews].desc, sizeof(views[nviews].desc),
  400. "window manager (system service)");
  401. views[nviews].pid = wm_is_running(&is) ? is.wm : 0;
  402. views[nviews].running = wm_is_running(&is);
  403. views[nviews].is_wm = 1;
  404. nviews++;
  405. for (i = 0; i < svc_def_count && i < SVC_MAX; i++) {
  406. struct view *v = &views[nviews];
  407. memset(v, 0, sizeof(*v));
  408. snprintf(v->name, sizeof(v->name), "%s", svc_defs[i].name);
  409. snprintf(v->desc, sizeof(v->desc), "%s", svc_defs[i].desc);
  410. snprintf(v->state, sizeof(v->state), "stopped");
  411. if (read_state_file(svc_defs[i].name, &st) == 0) {
  412. v->pid = st.pid;
  413. if (v->pid && !u_alive(v->pid))
  414. v->pid = 0;
  415. if (v->pid)
  416. snprintf(v->state, sizeof(v->state), "%s",
  417. svc_state_name(st.state));
  418. }
  419. v->running = v->pid != 0;
  420. nviews++;
  421. }
  422. if (selected >= nviews)
  423. selected = nviews - 1;
  424. if (selected < 0)
  425. selected = 0;
  426. }
  427. static void gui_read_log(void)
  428. {
  429. struct view *v = &views[selected];
  430. int n;
  431. if (v->is_wm) {
  432. log_len = 0;
  433. return;
  434. }
  435. n = read_log(v->name);
  436. if (n < 0)
  437. n = 0;
  438. log_len = (unsigned int)n;
  439. }
  440. static void gui_status_set(const char *s, int err)
  441. {
  442. snprintf(gui_status, sizeof(gui_status), "%s", s);
  443. gui_status_at = u_ticks_ms();
  444. (void)err;
  445. }
  446. static void gui_action(const char *op)
  447. {
  448. char msg[96];
  449. int pid = 0, code;
  450. code = request_action(op, views[selected].name, msg, sizeof(msg), &pid);
  451. if (code < 0) {
  452. gui_status_set(msg, 1);
  453. return;
  454. }
  455. if (pid > 0) {
  456. char b[80];
  457. snprintf(b, sizeof(b), "%s: %s (pid %d)", views[selected].name,
  458. msg, pid);
  459. gui_status_set(b, code != 0);
  460. } else {
  461. char b[80];
  462. snprintf(b, sizeof(b), "%s: %s", views[selected].name, msg);
  463. gui_status_set(b, code != 0);
  464. }
  465. gui_refresh();
  466. ngui_invalidate(&ui);
  467. }
  468. static int draw_button(unsigned int id, struct ngui_rect r,
  469. const char *label, int enabled)
  470. {
  471. if (enabled)
  472. return ngui_button(&ui, id, r, label);
  473. ngui_fill_rect(&ui, r, ui.theme.panel_alt);
  474. ngui_text(&ui, r.x + 10, r.y + (r.h - 16) / 2, label,
  475. ui.theme.text_dim);
  476. return 0;
  477. }
  478. static void gui_draw(void)
  479. {
  480. struct ngui_rect list = { 10, 52, 300, ui.h - 62 };
  481. struct ngui_rect right = { 320, 52, ui.w - 330, ui.h - 62 };
  482. struct ngui_rect btn;
  483. struct view *v = &views[selected];
  484. int i, enabled;
  485. unsigned int now = u_ticks_ms();
  486. int line_h = ngui_text_line_height(&ui, 12, 1);
  487. char sub[64], tr[16];
  488. ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
  489. ui.theme.bg);
  490. {
  491. char head[96];
  492. if (views[0].running)
  493. snprintf(head, sizeof(head), "Init: running (pid %u)",
  494. views[0].pid);
  495. else
  496. snprintf(head, sizeof(head), "Init: not running");
  497. ngui_text(&ui, 12, 16, head, ui.theme.text);
  498. }
  499. for (i = 0; i < nviews; i++) {
  500. struct view *r = &views[i];
  501. struct ngui_row row;
  502. struct ngui_rect rr = { list.x, list.y + i * ROW_H,
  503. list.w, ROW_H };
  504. if (rr.y + rr.h > list.y + list.h)
  505. break;
  506. snprintf(tr, sizeof(tr), "%u", r->pid);
  507. snprintf(sub, sizeof(sub), "%s%s%s", r->state,
  508. r->pid ? " pid " : "", r->pid ? tr : "");
  509. row.title = r->name;
  510. row.subtitle = sub;
  511. row.trailing = r->desc;
  512. row.bars = -1;
  513. row.selected = (i == selected);
  514. if (ngui_list_row(&ui, ID_ROW_BASE + (unsigned int)i, rr, &row)) {
  515. selected = i;
  516. kill_arm = 0;
  517. log_scroll.offset = 0;
  518. log_follow = 1;
  519. gui_read_log();
  520. ngui_invalidate(&ui);
  521. }
  522. }
  523. btn = (struct ngui_rect){ right.x, right.y, 84, 30 };
  524. enabled = !v->is_init && !v->running;
  525. if (draw_button(ID_START, btn, "Start", enabled))
  526. pending_action = ID_START;
  527. btn.x += 92;
  528. enabled = !v->is_init && !v->is_wm && v->running;
  529. if (draw_button(ID_STOP, btn, "Stop", enabled))
  530. pending_action = ID_STOP;
  531. btn.x += 92;
  532. enabled = !v->is_init;
  533. if (draw_button(ID_RESTART, btn, "Restart", enabled))
  534. pending_action = ID_RESTART;
  535. btn.x += 92;
  536. enabled = !v->is_init && v->running;
  537. if (draw_button(ID_KILL, btn, "Kill", enabled))
  538. pending_action = ID_KILL;
  539. btn.x += 92;
  540. if (draw_button(ID_REFRESH, btn, "Refresh", 1))
  541. pending_action = ID_REFRESH;
  542. if (gui_status[0] && now - gui_status_at < 5000) {
  543. ngui_text_ellipsis(&ui, (struct ngui_rect){ right.x,
  544. right.y + 38, right.w, 20 }, right.x,
  545. right.y + 38, gui_status,
  546. ui.theme.text_dim);
  547. } else {
  548. ngui_text_ellipsis(&ui, (struct ngui_rect){ right.x,
  549. right.y + 38, right.w, 20 }, right.x,
  550. right.y + 38,
  551. v->is_wm ? "System service: restart/kill only"
  552. : v->desc,
  553. ui.theme.text_dim);
  554. }
  555. {
  556. struct ngui_rect body = { right.x, right.y + 64, right.w,
  557. right.h - 64 };
  558. int lines = 1, max, wheel;
  559. unsigned int i2;
  560. ngui_fill_rect(&ui, body, ui.theme.panel);
  561. if (v->is_wm) {
  562. ngui_text(&ui, body.x + 10, body.y + 10,
  563. "The WM writes to the serial console; no log.",
  564. ui.theme.text_dim);
  565. return;
  566. }
  567. if (!log_len) {
  568. ngui_text(&ui, body.x + 10, body.y + 10,
  569. "No log yet.", ui.theme.text_dim);
  570. return;
  571. }
  572. for (i2 = 0; i2 < log_len; i2++)
  573. if (log_buf[i2] == '\n')
  574. lines++;
  575. if (line_h < 10)
  576. line_h = 10;
  577. log_scroll.content_h = lines * line_h;
  578. log_scroll.view_h = body.h;
  579. log_scroll.hide_bar = 0;
  580. ngui_scroll_begin(&ui, body, &log_scroll);
  581. wheel = ngui_scroll_wheel(&ui, body, &log_scroll);
  582. max = log_scroll.content_h - log_scroll.view_h;
  583. if (max < 0)
  584. max = 0;
  585. if (wheel)
  586. log_follow = 0;
  587. if (log_follow)
  588. ngui_scroll_to(&log_scroll, max);
  589. if (log_scroll.offset >= max - 1)
  590. log_follow = 1;
  591. {
  592. unsigned int start = 0, end = 0;
  593. int idx = 0;
  594. int ly;
  595. while (end < log_len) {
  596. while (end < log_len && log_buf[end] != '\n')
  597. end++;
  598. ly = body.y + idx * line_h - log_scroll.offset;
  599. if (ly + line_h > body.y && ly < body.y + body.h)
  600. ngui_text_px(&ui, body.x + 6, ly,
  601. log_buf + start,
  602. end - start,
  603. ui.theme.text, 12, 0, 0, 1);
  604. idx++;
  605. if (end < log_len)
  606. end++;
  607. start = end;
  608. }
  609. }
  610. ngui_scroll_end(&ui, body, &log_scroll);
  611. }
  612. }
  613. static void gui_handle_action(int id)
  614. {
  615. unsigned int now = u_ticks_ms();
  616. struct view *v = &views[selected];
  617. if (id == ID_REFRESH) {
  618. gui_refresh();
  619. gui_read_log();
  620. ngui_invalidate(&ui);
  621. return;
  622. }
  623. if (id == ID_KILL) {
  624. if (v->is_wm) {
  625. gui_action("kill");
  626. return;
  627. }
  628. if (kill_arm && now - kill_arm_at < 3000) {
  629. kill_arm = 0;
  630. gui_action("kill");
  631. } else {
  632. kill_arm = 1;
  633. kill_arm_at = now;
  634. gui_status_set("Press Kill again to confirm", 1);
  635. ngui_invalidate(&ui);
  636. }
  637. return;
  638. }
  639. if (id == ID_START)
  640. gui_action("start");
  641. else if (id == ID_STOP)
  642. gui_action("stop");
  643. else if (id == ID_RESTART)
  644. gui_action("restart");
  645. }
  646. static void gui(const char *arg)
  647. {
  648. unsigned int sw = 0, sh = 0;
  649. unsigned int w = 820, h = 560;
  650. unsigned int last_poll = 0;
  651. int quit = 0;
  652. (void)arg;
  653. if (u_screen_size(&sw, &sh) == 0) {
  654. if (sw && w > sw - 30)
  655. w = sw - 30;
  656. if (sh && h > sh - 40)
  657. h = sh - 40;
  658. }
  659. if (w < 520)
  660. w = 520;
  661. if (h < 360)
  662. h = 360;
  663. if (u_win_create(w, h, "Services", &wi) != 0) {
  664. u_puts("service: no graphics window\n");
  665. u_exit(1);
  666. }
  667. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  668. ngui_font_attach(&ui);
  669. gui_refresh();
  670. gui_read_log();
  671. ngui_invalidate(&ui);
  672. for (;;) {
  673. unsigned int now = u_ticks_ms();
  674. if (now - last_poll >= POLL_MS) {
  675. last_poll = now;
  676. gui_refresh();
  677. gui_read_log();
  678. ngui_invalidate(&ui);
  679. }
  680. if (!ngui_begin(&ui)) {
  681. ngui_end_ex(&ui, 0);
  682. u_sleep_ms(LOOP_MS);
  683. continue;
  684. }
  685. {
  686. int k;
  687. while ((k = ngui_key(&ui)) != 0) {
  688. if (k == NGUI_KEY_ESC) {
  689. quit = 1;
  690. break;
  691. }
  692. if (k == NGUI_KEY_UP && selected > 0) {
  693. selected--;
  694. kill_arm = 0;
  695. log_scroll.offset = 0;
  696. log_follow = 1;
  697. gui_read_log();
  698. ngui_invalidate(&ui);
  699. } else if (k == NGUI_KEY_DOWN &&
  700. selected + 1 < nviews) {
  701. selected++;
  702. kill_arm = 0;
  703. log_scroll.offset = 0;
  704. log_follow = 1;
  705. gui_read_log();
  706. ngui_invalidate(&ui);
  707. }
  708. }
  709. }
  710. if (quit)
  711. break;
  712. pending_action = 0;
  713. gui_draw();
  714. ngui_end(&ui);
  715. if (pending_action)
  716. gui_handle_action(pending_action);
  717. u_sleep_ms(LOOP_MS);
  718. }
  719. u_win_close();
  720. u_exit(0);
  721. }
  722. void _start(const char *arg)
  723. {
  724. const char *a = arg;
  725. while (a && (*a == ' ' || *a == '\t'))
  726. a++;
  727. if (a && ((strncmp(a, "--gui", 5) == 0 &&
  728. (a[5] == '\0' || a[5] == ' ' || a[5] == '\t')) ||
  729. strcmp(a, "gui") == 0)) {
  730. gui(arg);
  731. return;
  732. }
  733. cli(arg);
  734. u_exit(0);
  735. }