ScreenTabsPanel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. htop - ScreenTabsPanel.c
  3. (C) 2023 htop dev team
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "ScreenTabsPanel.h"
  9. #include <assert.h>
  10. #include <ctype.h>
  11. #include <stdbool.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "CRT.h"
  15. #include "FunctionBar.h"
  16. #include "Hashtable.h"
  17. #include "Macros.h"
  18. #include "ProvideCurses.h"
  19. #include "Settings.h"
  20. #include "XUtils.h"
  21. static HandlerResult ScreenNamesPanel_eventHandlerNormal(Panel* super, int ch);
  22. ObjectClass ScreenTabListItem_class = {
  23. .extends = Class(ListItem),
  24. .display = ListItem_display,
  25. .delete = ListItem_delete,
  26. .compare = ListItem_compare
  27. };
  28. static void ScreenNamesPanel_fill(ScreenNamesPanel* this, DynamicScreen* ds) {
  29. const Settings* settings = this->settings;
  30. Panel* super = (Panel*) this;
  31. Panel_prune(super);
  32. for (unsigned int i = 0; i < settings->nScreens; i++) {
  33. const ScreenSettings* ss = settings->screens[i];
  34. if (ds == NULL) {
  35. if (ss->dynamic != NULL)
  36. continue;
  37. /* built-in (processes, not dynamic) - e.g. Main or I/O */
  38. } else {
  39. if (ss->dynamic == NULL)
  40. continue;
  41. if (!String_eq(ds->name, ss->dynamic))
  42. continue;
  43. /* matching dynamic screen found, add it into the Panel */
  44. }
  45. Panel_add(super, (Object*) ListItem_new(ss->heading, i));
  46. }
  47. this->ds = ds;
  48. }
  49. static void ScreenTabsPanel_delete(Object* object) {
  50. Panel* super = (Panel*) object;
  51. ScreenTabsPanel* this = (ScreenTabsPanel*) object;
  52. Panel_done(super);
  53. free(this);
  54. }
  55. static HandlerResult ScreenTabsPanel_eventHandler(Panel* super, int ch) {
  56. ScreenTabsPanel* const this = (ScreenTabsPanel* const) super;
  57. HandlerResult result = IGNORED;
  58. int selected = Panel_getSelectedIndex(super);
  59. switch (ch) {
  60. case EVENT_SET_SELECTED:
  61. result = HANDLED;
  62. break;
  63. case KEY_F(5):
  64. case KEY_CTRL('N'):
  65. /* pass onto the Names panel for creating new screen */
  66. return ScreenNamesPanel_eventHandlerNormal(&this->names->super, ch);
  67. case KEY_UP:
  68. case KEY_DOWN:
  69. case KEY_NPAGE:
  70. case KEY_PPAGE:
  71. case KEY_HOME:
  72. case KEY_END: {
  73. int previous = selected;
  74. Panel_onKey(super, ch);
  75. selected = Panel_getSelectedIndex(super);
  76. if (previous != selected)
  77. result = HANDLED;
  78. break;
  79. }
  80. default:
  81. if (ch < 255 && isalpha(ch))
  82. result = Panel_selectByTyping(super, ch);
  83. if (result == BREAK_LOOP)
  84. result = IGNORED;
  85. break;
  86. }
  87. if (result == HANDLED) {
  88. ScreenTabListItem* focus = (ScreenTabListItem*) Panel_getSelected(super);
  89. if (focus) {
  90. ScreenNamesPanel_fill(this->names, focus->ds);
  91. }
  92. }
  93. return result;
  94. }
  95. PanelClass ScreenTabsPanel_class = {
  96. .super = {
  97. .extends = Class(Panel),
  98. .delete = ScreenTabsPanel_delete,
  99. },
  100. .eventHandler = ScreenTabsPanel_eventHandler
  101. };
  102. static ScreenTabListItem* ScreenTabListItem_new(const char* value, DynamicScreen* ds) {
  103. ScreenTabListItem* this = AllocThis(ScreenTabListItem);
  104. ListItem_init((ListItem*)this, value, 0);
  105. this->ds = ds;
  106. return this;
  107. }
  108. static void addDynamicScreen(ATTR_UNUSED ht_key_t key, void* value, void* userdata) {
  109. DynamicScreen* screen = (DynamicScreen*) value;
  110. Panel* super = (Panel*) userdata;
  111. const char* name = screen->heading ? screen->heading : screen->name;
  112. Panel_add(super, (Object*) ScreenTabListItem_new(name, screen));
  113. }
  114. static const char* const ScreenTabsFunctions[] = {" ", " ", " ", " ", "New ", " ", " ", " ", " ", "Done ", NULL};
  115. ScreenTabsPanel* ScreenTabsPanel_new(Settings* settings) {
  116. ScreenTabsPanel* this = AllocThis(ScreenTabsPanel);
  117. Panel* super = (Panel*) this;
  118. FunctionBar* fuBar = FunctionBar_new(ScreenTabsFunctions, NULL, NULL);
  119. Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
  120. this->settings = settings;
  121. this->names = ScreenNamesPanel_new(settings);
  122. super->cursorOn = false;
  123. this->cursor = 0;
  124. Panel_setHeader(super, "Screen tabs");
  125. assert(settings->dynamicScreens != NULL);
  126. Panel_add(super, (Object*) ScreenTabListItem_new("Processes", NULL));
  127. Hashtable_foreach(settings->dynamicScreens, addDynamicScreen, super);
  128. return this;
  129. }
  130. // -------------
  131. ObjectClass ScreenNameListItem_class = {
  132. .extends = Class(ListItem),
  133. .display = ListItem_display,
  134. .delete = ListItem_delete,
  135. .compare = ListItem_compare
  136. };
  137. ScreenNameListItem* ScreenNameListItem_new(const char* value, ScreenSettings* ss) {
  138. ScreenNameListItem* this = AllocThis(ScreenNameListItem);
  139. ListItem_init((ListItem*)this, value, 0);
  140. this->ss = ss;
  141. return this;
  142. }
  143. static const char* const ScreenNamesFunctions[] = {" ", " ", " ", " ", "New ", " ", " ", " ", " ", "Done ", NULL};
  144. static void ScreenNamesPanel_delete(Object* object) {
  145. Panel* super = (Panel*) object;
  146. ScreenNamesPanel* this = (ScreenNamesPanel*) object;
  147. /* do not delete screen settings still in use */
  148. int n = Panel_size(super);
  149. for (int i = 0; i < n; i++) {
  150. ScreenNameListItem* item = (ScreenNameListItem*) Panel_get(super, i);
  151. item->ss = NULL;
  152. }
  153. /* during renaming the ListItem's value points to our static buffer */
  154. if (this->renamingItem)
  155. this->renamingItem->value = this->saved;
  156. Panel_done(super);
  157. free(this);
  158. }
  159. static void renameScreenSettings(ScreenNamesPanel* this, const ListItem* item) {
  160. const ScreenNameListItem* nameItem = (const ScreenNameListItem*) item;
  161. ScreenSettings* ss = nameItem->ss;
  162. free_and_xStrdup(&ss->heading, item->value);
  163. Settings* settings = this->settings;
  164. settings->changed = true;
  165. settings->lastUpdate++;
  166. }
  167. static HandlerResult ScreenNamesPanel_eventHandlerRenaming(Panel* super, int ch) {
  168. ScreenNamesPanel* const this = (ScreenNamesPanel*) super;
  169. if (ch >= 32 && ch < 127 && ch != '=') {
  170. if (this->cursor < SCREEN_NAME_LEN - 1) {
  171. this->buffer[this->cursor] = (char)ch;
  172. this->cursor++;
  173. super->selectedLen = strlen(this->buffer);
  174. Panel_setCursorToSelection(super);
  175. }
  176. return HANDLED;
  177. }
  178. switch (ch) {
  179. case 127:
  180. case KEY_BACKSPACE:
  181. if (this->cursor > 0) {
  182. this->cursor--;
  183. this->buffer[this->cursor] = '\0';
  184. super->selectedLen = strlen(this->buffer);
  185. Panel_setCursorToSelection(super);
  186. }
  187. break;
  188. case '\n':
  189. case '\r':
  190. case KEY_ENTER: {
  191. ListItem* item = (ListItem*) Panel_getSelected(super);
  192. if (!item)
  193. break;
  194. assert(item == this->renamingItem);
  195. free(this->saved);
  196. item->value = xStrdup(this->buffer);
  197. this->renamingItem = NULL;
  198. super->cursorOn = false;
  199. Panel_setSelectionColor(super, PANEL_SELECTION_FOCUS);
  200. renameScreenSettings(this, item);
  201. break;
  202. }
  203. case 27: { // Esc
  204. ListItem* item = (ListItem*) Panel_getSelected(super);
  205. if (!item)
  206. break;
  207. assert(item == this->renamingItem);
  208. item->value = this->saved;
  209. this->renamingItem = NULL;
  210. super->cursorOn = false;
  211. Panel_setSelectionColor(super, PANEL_SELECTION_FOCUS);
  212. break;
  213. }
  214. }
  215. return HANDLED;
  216. }
  217. static void startRenaming(Panel* super) {
  218. ScreenNamesPanel* const this = (ScreenNamesPanel*) super;
  219. ListItem* item = (ListItem*) Panel_getSelected(super);
  220. if (item == NULL)
  221. return;
  222. this->renamingItem = item;
  223. super->cursorOn = true;
  224. char* name = item->value;
  225. this->saved = name;
  226. strncpy(this->buffer, name, SCREEN_NAME_LEN);
  227. this->buffer[SCREEN_NAME_LEN] = '\0';
  228. this->cursor = strlen(this->buffer);
  229. item->value = this->buffer;
  230. Panel_setSelectionColor(super, PANEL_EDIT);
  231. super->selectedLen = strlen(this->buffer);
  232. Panel_setCursorToSelection(super);
  233. }
  234. static void addNewScreen(Panel* super, DynamicScreen* ds) {
  235. ScreenNamesPanel* const this = (ScreenNamesPanel*) super;
  236. const char* name = "New";
  237. ScreenSettings* ss = (ds != NULL) ? Settings_newDynamicScreen(this->settings, name, ds, NULL) : Settings_newScreen(this->settings, &(const ScreenDefaults) { .name = name, .columns = "PID Command", .sortKey = "PID" });
  238. ScreenNameListItem* item = ScreenNameListItem_new(name, ss);
  239. int idx = Panel_getSelectedIndex(super);
  240. Panel_insert(super, idx + 1, (Object*) item);
  241. Panel_setSelected(super, idx + 1);
  242. }
  243. static HandlerResult ScreenNamesPanel_eventHandlerNormal(Panel* super, int ch) {
  244. ScreenNamesPanel* const this = (ScreenNamesPanel*) super;
  245. ScreenNameListItem* oldFocus = (ScreenNameListItem*) Panel_getSelected(super);
  246. HandlerResult result = IGNORED;
  247. switch (ch) {
  248. case '\n':
  249. case '\r':
  250. case KEY_ENTER:
  251. case KEY_MOUSE:
  252. case KEY_RECLICK:
  253. Panel_setSelectionColor(super, PANEL_SELECTION_FOCUS);
  254. result = HANDLED;
  255. break;
  256. case EVENT_SET_SELECTED:
  257. result = HANDLED;
  258. break;
  259. case KEY_NPAGE:
  260. case KEY_PPAGE:
  261. case KEY_HOME:
  262. case KEY_END:
  263. Panel_onKey(super, ch);
  264. break;
  265. case KEY_F(5):
  266. case KEY_CTRL('N'):
  267. addNewScreen(super, this->ds);
  268. startRenaming(super);
  269. result = HANDLED;
  270. break;
  271. default:
  272. if (ch < 255 && isalpha(ch))
  273. result = Panel_selectByTyping(super, ch);
  274. if (result == BREAK_LOOP)
  275. result = IGNORED;
  276. break;
  277. }
  278. ScreenNameListItem* newFocus = (ScreenNameListItem*) Panel_getSelected(super);
  279. if (newFocus && oldFocus != newFocus)
  280. result = HANDLED;
  281. return result;
  282. }
  283. static HandlerResult ScreenNamesPanel_eventHandler(Panel* super, int ch) {
  284. ScreenNamesPanel* const this = (ScreenNamesPanel*) super;
  285. if (!this->renamingItem)
  286. return ScreenNamesPanel_eventHandlerNormal(super, ch);
  287. return ScreenNamesPanel_eventHandlerRenaming(super, ch);
  288. }
  289. PanelClass ScreenNamesPanel_class = {
  290. .super = {
  291. .extends = Class(Panel),
  292. .delete = ScreenNamesPanel_delete
  293. },
  294. .eventHandler = ScreenNamesPanel_eventHandler
  295. };
  296. ScreenNamesPanel* ScreenNamesPanel_new(Settings* settings) {
  297. ScreenNamesPanel* this = AllocThis(ScreenNamesPanel);
  298. Panel* super = (Panel*) this;
  299. FunctionBar* fuBar = FunctionBar_new(ScreenNamesFunctions, NULL, NULL);
  300. Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
  301. this->settings = settings;
  302. this->renamingItem = NULL;
  303. memset(this->buffer, 0, sizeof(this->buffer));
  304. this->ds = NULL;
  305. this->saved = NULL;
  306. this->cursor = 0;
  307. super->cursorOn = false;
  308. Panel_setHeader(super, "Screens");
  309. for (unsigned int i = 0; i < settings->nScreens; i++) {
  310. ScreenSettings* ss = settings->screens[i];
  311. /* initially show only for Processes tabs (selected) */
  312. if (ss->dynamic)
  313. continue;
  314. Panel_add(super, (Object*) ScreenNameListItem_new(ss->heading, ss));
  315. }
  316. return this;
  317. }