camera_demo3.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <curses.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #define LOG_LEVEL 3
  16. #define LOG_PREFIX "camera_demo3"
  17. #include <syslog.h>
  18. #include "camera_manager.h"
  19. #include "param.h"
  20. #include "app_dialogs.h"
  21. #include "menu_process.h"
  22. cams_t *cam_session;
  23. WINDOW *menubar;
  24. WINDOW *messagebar;
  25. WINDOW *win_border;
  26. WINDOW *win_content;
  27. void init_curses()
  28. {
  29. initscr(); /* init curses */
  30. FILE *f = fopen("/dev/tty", "r+");
  31. SCREEN *screen = newterm(NULL, f, f);
  32. set_term(screen);
  33. start_color(); /* color init, then init color pair */
  34. init_pair(1, COLOR_WHITE, COLOR_BLACK);
  35. init_pair(2, COLOR_BLACK, COLOR_WHITE);
  36. init_pair(3, COLOR_RED, COLOR_WHITE);
  37. init_pair(4, COLOR_WHITE, COLOR_BLUE); // menu font
  38. init_pair(5, COLOR_YELLOW, COLOR_BLACK);
  39. init_pair(6, COLOR_GREEN, COLOR_WHITE);
  40. init_pair(7, COLOR_YELLOW, COLOR_WHITE);
  41. curs_set(0); /* Cursor mode: 0:hide/1:normal/2:hilight */
  42. noecho(); /* Do not display on screen */
  43. keypad(stdscr, TRUE); /* allow keypad map */
  44. }
  45. void uninit_curses()
  46. {
  47. echo();
  48. endwin();
  49. }
  50. void draw_menubar(WINDOW *menubar)
  51. {
  52. wbkgd(menubar, COLOR_PAIR(2));
  53. for (int i = 0; i < atoi(param[0][0]); i++) {
  54. wattron(menubar, COLOR_PAIR(3));
  55. mvwprintw(menubar, 0, i * 14 + 2, "%1d.", i + 1);
  56. wattroff(menubar, COLOR_PAIR(3));
  57. mvwprintw(menubar, 0, i * 14 + 4, "%-12s", param[0][i + 1]);
  58. }
  59. }
  60. WINDOW **draw_menu(int menu)
  61. {
  62. int i, start_col;
  63. WINDOW **items;
  64. items = (WINDOW **)malloc((atoi(param[menu][0]) + 1) * sizeof(WINDOW *));
  65. start_col = (menu - 1) * 14 + 2;
  66. items[0] = newwin(atoi(param[menu][0]) + 2, MENU_ITEM_MAX_LEN+2, 3, start_col);
  67. wbkgd(items[0], COLOR_PAIR(2));
  68. box(items[0], ACS_VLINE, ACS_HLINE);
  69. for (i = 1; i <= atoi(param[menu][0]); i++) {
  70. items[i] = subwin(items[0], 1, MENU_ITEM_MAX_LEN, 3 + i, start_col + 1);
  71. wprintw(items[i], "%s", param[menu][i]);
  72. }
  73. wbkgd(items[1], COLOR_PAIR(4));
  74. wrefresh(items[0]);
  75. return items;
  76. }
  77. void delete_menu(WINDOW **items, int count)
  78. {
  79. int i;
  80. for (i = 0; i < count; i++)
  81. delwin(items[i]);
  82. free(items);
  83. }
  84. int scroll_menu(WINDOW **items, int menu, int *menu_returned)
  85. {
  86. int key, count, selected = 0;
  87. count = atoi(param[menu][0]);
  88. LOG_D("Active: menu=%d, item=%d\n", menu, selected);
  89. while (1) {
  90. key = getch();
  91. if (key == KEY_DOWN || key == KEY_UP) {
  92. wbkgd(items[selected + 1], COLOR_PAIR(2));
  93. wnoutrefresh(items[selected + 1]);
  94. if (key == KEY_DOWN)
  95. selected = (selected + 1) % count;
  96. else
  97. selected = (selected + count - 1) % count;
  98. wbkgd(items[selected + 1], COLOR_PAIR(4));
  99. wnoutrefresh(items[selected + 1]);
  100. doupdate();
  101. } else if (key == KEY_LEFT || key == KEY_RIGHT) {
  102. delete_menu(items, count + 1);
  103. touchwin(stdscr);
  104. refresh();
  105. if (key == KEY_LEFT) {
  106. menu -= 1;
  107. if (menu <= 0) menu = atoi(param[0][0]);
  108. items = draw_menu(menu);
  109. *menu_returned = menu;
  110. LOG_D("Active: menu=%d, item=%d\n", *menu_returned, selected);
  111. return scroll_menu(items, menu, menu_returned);
  112. }
  113. if (key == KEY_RIGHT) {
  114. menu += 1;
  115. if (menu > atoi(param[0][0])) menu = 1;
  116. items = draw_menu(menu);
  117. *menu_returned = menu;
  118. LOG_D("Active: menu=%d, item=%d\n", *menu_returned, selected);
  119. return scroll_menu(items, menu, menu_returned);
  120. }
  121. } else if (key == ESCAPE || key == '0' || key == 'q') {
  122. delete_menu(items, count + 1);
  123. return -1;
  124. } else if (key == ENTER) {
  125. delete_menu(items, count + 1);
  126. return selected;
  127. }
  128. LOG_D("Active: menu=%d, item=%d\n", *menu_returned, selected);
  129. }
  130. }
  131. /* ss: display string,
  132. * status: 0:Normal, 1:OK, 2:Failed, 3:Warning */
  133. void message(char *ss, int status)
  134. {
  135. int color_pair;
  136. switch (status) {
  137. case 1:
  138. color_pair = 6; // GREEN:WHITE
  139. break;
  140. case 2:
  141. color_pair = 3; // RED:WHITE
  142. break;
  143. case 3:
  144. color_pair = 7; // YELLOW:WHITE
  145. break;
  146. default:
  147. color_pair = 2; // BLACK:WHITE
  148. break;
  149. };
  150. wbkgd(messagebar, COLOR_PAIR(2));
  151. wattron(messagebar, COLOR_PAIR(color_pair));
  152. mvwprintw(messagebar, 0, 0, "%80s", " ");
  153. mvwprintw(messagebar, 0, 1, "%s", ss);
  154. wattroff(messagebar, COLOR_PAIR(color_pair));
  155. wrefresh(messagebar);
  156. }
  157. void copyright()
  158. {
  159. char *str[] = { "Copyright (C) 2021 Alibaba Group Holding Limited",
  160. "Author: LuChongzhi, T-Head / IOT / OS Team",
  161. "mailto:[chongzhi.lcz@alibaba-inc.com]"};
  162. attron(A_UNDERLINE | COLOR_PAIR(1));
  163. for (int i = 0; i < sizeof(str) / sizeof(char *); i++)
  164. mvaddstr((WIN_ROWS - 2) / 2 + i, (WIN_COLS - strlen(str[0])) / 2, str[i]);
  165. attroff(A_UNDERLINE | COLOR_PAIR(1));
  166. refresh();
  167. }
  168. int main(int argc, char **argv)
  169. {
  170. int ret = -1;
  171. int key;
  172. WINDOW **menu_items;
  173. if (get_param(argv[0])) {
  174. LOG_E("\n Open %s.conf failed!\n", argv[0]);
  175. goto LAB_EXIT;
  176. }
  177. if (camera_create_session(&cam_session) != 0 || cam_session == NULL) {
  178. LOG_E("camera_create_session() failed\n");
  179. goto LAB_EXIT;
  180. }
  181. init_curses();
  182. if (init_dialog(NULL)) {
  183. fprintf(stderr, N_("Your display is too small to run Menuconfig!\n"));
  184. fprintf(stderr, N_("It must be at least 19 lines by 80 columns.\n"));
  185. goto LAB_EXIT_CURSES_INITED;
  186. }
  187. bkgd(COLOR_PAIR(1)); /* COLOR_WHITE, COLOR_BLACK */
  188. /* set windows postion: (win, lines, cols, begin_y, begin_x) */
  189. menubar = subwin(stdscr, 1, WIN_COLS, 1, 0); /* Menu bar line */
  190. messagebar = subwin(stdscr, 1, WIN_COLS, WIN_ROWS - 1, 0); /* Bottom line */
  191. win_border = subwin(stdscr, WIN_ROWS - 3, WIN_COLS, 2, 0); /* Content win with border */
  192. win_content = subwin(stdscr, WIN_ROWS - 5, WIN_COLS - 2, 3, 1); /* Content win without border */
  193. /* Set window title */
  194. char str_title[81];
  195. strcpy(str_title, "<<< CSI Camera Test Tool >>>");
  196. wattron(stdscr, COLOR_PAIR(5));
  197. mvwprintw(stdscr, 0, (80 - strlen(str_title)) / 2 - 1, "%s", str_title);
  198. wattroff(stdscr, COLOR_PAIR(5));
  199. /* Draw menu */
  200. draw_menubar(menubar);
  201. message("Use number key to choses menu. 'q' or '0' to Exit", 0);
  202. /* Draw box border */
  203. box(win_border, ACS_VLINE, ACS_HLINE);
  204. /* Show copyright */
  205. copyright();
  206. refresh();
  207. /* menu operate loop */
  208. do {
  209. key = wgetch(stdscr);
  210. if (isdigit(key) && key > '0' && key <= atoi(param[0][0]) + '0') {
  211. werase(messagebar);
  212. wrefresh(messagebar);
  213. int menu_init = key - '0'; // first press menu id
  214. int menu_final = menu_init; // return from scroll_menu
  215. menu_items = draw_menu(menu_init);
  216. int item_selected = scroll_menu(menu_items, menu_init, &menu_final);
  217. LOG_D("menu_final=%d, selected_item=%d\n",
  218. menu_final, item_selected);
  219. switch(menu_final) {
  220. case MENU_CAMERA:
  221. menu_camera_process(item_selected);
  222. break;
  223. case MENU_CHANNEL:
  224. menu_channel_process(item_selected);
  225. break;
  226. case MENU_EVENT_RUN:
  227. menu_event_run_process(item_selected);
  228. break;
  229. default:
  230. LOG_W("Not supported menu: %d\n", menu_final);
  231. break;
  232. }
  233. if (item_selected == 13) {
  234. LOG_D("Test dialog_inputbox\n");
  235. dialog_inputbox("inputbox", "prompt", 16, 80,
  236. "const char init");
  237. }
  238. touchwin(stdscr);
  239. refresh();
  240. } else if (key == KEY_RESIZE) {
  241. box(win_border, ACS_VLINE, ACS_HLINE);
  242. wrefresh(messagebar);
  243. }
  244. } while (key != 'q' && key != '0');
  245. ret = 0;
  246. LAB_EXIT_WIN_CREATED:
  247. delwin(win_content);
  248. delwin(win_border);
  249. delwin(menubar);
  250. delwin(messagebar);
  251. LAB_EXIT_CURSES_INITED:
  252. uninit_curses();
  253. LAB_EXIT_CAM_MGR_CREATED:
  254. camera_destory_session(&cam_session);
  255. LAB_EXIT:
  256. return ret;
  257. }