dialog_channel_select.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <ctype.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <curses.h>
  15. #define LOG_LEVEL 3
  16. #define LOG_PREFIX "dlg_chn_select"
  17. #include <syslog.h>
  18. #include <camera_manager.h>
  19. #include <camera_string.h>
  20. #include "param.h"
  21. #include "app_dialogs.h"
  22. extern cams_t *cam_session;
  23. /* action: 0:close, 1:open, 2:start/stop */
  24. int dialog_channel_select(csi_camera_channel_cfg_s **selected_chn, int action)
  25. {
  26. int ret_key = KEY_ESC;
  27. int ret;
  28. char str_buf[256];
  29. *selected_chn = NULL;
  30. if (cam_session == NULL) {
  31. LOG_E("cam_session is NULL\n");
  32. return KEY_ESC;
  33. }
  34. if (cam_session->camera_handle == NULL) {
  35. csi_camera_info_s *info =
  36. &(cam_session->camera_infos.info[cam_session->camera_id]);
  37. snprintf(str_buf, sizeof(str_buf),
  38. "Please open camera first\n");
  39. dialog_textbox_simple("Error", str_buf, 6, 30);
  40. return KEY_ESC;
  41. }
  42. again:
  43. item_reset();
  44. if (camera_channel_query_list(cam_session) != 0) {
  45. LOG_E("camera_channel_query_list() failed\n");
  46. snprintf(str_buf, sizeof(str_buf),
  47. "Failed to query channel list from Camera failed!\n");
  48. dialog_textbox_simple("Error", str_buf, 10, 40);
  49. return KEY_ESC;
  50. }
  51. /* Prepare for checklist nodes */
  52. for (int i = CSI_CAMERA_CHANNEL_0; i < CSI_CAMERA_CHANNEL_MAX_COUNT; i++) {
  53. csi_camera_channel_cfg_s *chn_cfg = &(cam_session->chn_cfg[i]);
  54. if (chn_cfg->status == CSI_CAMERA_CHANNEL_INVALID) {
  55. continue;
  56. }
  57. item_make("Channel[%d]", chn_cfg->chn_id);
  58. item_add_str(" (%s) --->", camera_string_chn_status(chn_cfg->status));
  59. item_set_data(chn_cfg);
  60. if (chn_cfg->status == CSI_CAMERA_CHANNEL_OPENED || chn_cfg->status == CSI_CAMERA_CHANNEL_RUNNING) {
  61. item_set_tag('X');
  62. item_set_selected(1);
  63. }
  64. }
  65. if (action == 0) { // action is close
  66. char *button_names[] = {" Close ", " Help "};
  67. ret = dialog_checklist(
  68. "Select Channel", /* Title */
  69. "Select the channel to close",
  70. CHECKLIST_HEIGTH_MIN + 10,
  71. WIN_COLS - CHECKLIST_WIDTH_MIN - 16, 8,
  72. button_names, sizeof(button_names) / sizeof(button_names[0]));
  73. } else if (action == 1) { // action is open
  74. ret = dialog_checklist(
  75. "Select Channel", /* Title */
  76. "Select the channel to config and open",
  77. CHECKLIST_HEIGTH_MIN + 10,
  78. WIN_COLS - CHECKLIST_WIDTH_MIN - 16, 8,
  79. NULL, 0);
  80. } else { // action is start/stop
  81. ret = dialog_checklist(
  82. "Select Channel", /* Title */
  83. "Select the channel to Start or Stop",
  84. CHECKLIST_HEIGTH_MIN + 10,
  85. WIN_COLS - CHECKLIST_WIDTH_MIN - 16, 8,
  86. NULL, 0);
  87. }
  88. int selected = item_activate_selected();
  89. LOG_D("ret=%d, selected=%s\n", ret, selected ? "true" : "false");
  90. switch (ret) {
  91. case 0: // item is selected/close
  92. if (!selected)
  93. break;
  94. /* Show operation result */
  95. *selected_chn = (csi_camera_channel_cfg_s *)item_data();
  96. if (action == 0) { // to close
  97. if ((*selected_chn)->status == CSI_CAMERA_CHANNEL_RUNNING) {
  98. dialog_textbox_simple("Infomation",
  99. "The channel is Running, \nplease stop first", 6, 40);
  100. goto again;
  101. }
  102. } else if (action == 1) { // to open
  103. if ((*selected_chn)->status == CSI_CAMERA_CHANNEL_OPENED ||
  104. (*selected_chn)->status == CSI_CAMERA_CHANNEL_RUNNING) {
  105. dialog_textbox_simple("Infomation",
  106. "The channel is Opened or Running, \nplease close first", 6, 40);
  107. goto again;
  108. }
  109. }
  110. /* Show message bar */
  111. snprintf(str_buf, sizeof(str_buf),
  112. "The Camera[%d] channel[%d] is selected\n",
  113. cam_session->camera_id,
  114. (*selected_chn)->chn_id);
  115. message(str_buf, 0);
  116. ret = 0;
  117. break;
  118. case 1: // need help
  119. LOG_W("Help is not supported yet\n");
  120. goto again;
  121. case KEY_ESC: // specific KEY
  122. ret = KEY_ESC;
  123. break;
  124. case -ERRDISPLAYTOOSMALL: // error
  125. default:
  126. LOG_W("Oops? why return ret=%d\n", ret);
  127. ret = KEY_ESC;
  128. break;
  129. }
  130. LOG_D("ret=%d\n", ret);
  131. return ret;
  132. }