| Benny Baumann | 8e4ce18 | 2023-05-24 11:14:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | htop - InfoScreen.c |
| 3 | (C) 2016 Hisham H. Muhammad |
| 4 | (C) 2020,2022 htop dev team |
| 5 | Released under the GNU GPLv2+, see the COPYING file |
| 6 | in the source distribution for its full text. |
| 7 | */ |
| 8 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 9 | #include "config.h" // IWYU pragma: keep |
| 10 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 11 | #include "InfoScreen.h" |
| 12 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 13 | #include <stdarg.h> |
| 14 | #include <stdio.h> |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 17 | #include "CRT.h" |
| 18 | #include "IncSet.h" |
| 19 | #include "ListItem.h" |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 20 | #include "Object.h" |
| 21 | #include "ProvideCurses.h" |
| Benny Baumann | 872e542 | 2020-10-14 20:21:09 +0200 | [diff] [blame] | 22 | #include "XUtils.h" |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 23 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 24 | |
| Richard | d5faf64 | 2017-07-22 21:41:19 -0500 | [diff] [blame] | 25 | static const char* const InfoScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL}; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 26 | |
| Richard | d5faf64 | 2017-07-22 21:41:19 -0500 | [diff] [blame] | 27 | static const char* const InfoScreenKeys[] = {"F3", "F4", "F5", "Esc"}; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 28 | |
| Christian Göttsche | 7e7a53c | 2020-12-20 16:58:37 +0100 | [diff] [blame] | 29 | static const int InfoScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27}; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 30 | |
| Christian Göttsche | 41eea8a | 2020-10-07 19:02:15 +0200 | [diff] [blame] | 31 | InfoScreen* InfoScreen_init(InfoScreen* this, const Process* process, FunctionBar* bar, int height, const char* panelHeader) { |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 32 | this->process = process; |
| 33 | if (!bar) { |
| 34 | bar = FunctionBar_new(InfoScreenFunctions, InfoScreenKeys, InfoScreenEvents); |
| 35 | } |
| Christian Göttsche | ce9e7fd | 2021-01-02 23:51:53 +0100 | [diff] [blame] | 36 | this->display = Panel_new(0, 1, COLS, height, Class(ListItem), false, bar); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 37 | this->inc = IncSet_new(bar); |
| Christian Göttsche | d37d66b | 2021-03-12 16:46:55 +0100 | [diff] [blame] | 38 | this->lines = Vector_new(Vector_type(this->display->items), true, DEFAULT_SIZE); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 39 | Panel_setHeader(this->display, panelHeader); |
| 40 | return this; |
| 41 | } |
| 42 | |
| 43 | InfoScreen* InfoScreen_done(InfoScreen* this) { |
| 44 | Panel_delete((Object*)this->display); |
| 45 | IncSet_delete(this->inc); |
| 46 | Vector_delete(this->lines); |
| 47 | return this; |
| 48 | } |
| 49 | |
| Christian Göttsche | 11f558f | 2020-08-20 21:58:14 +0200 | [diff] [blame] | 50 | void InfoScreen_drawTitled(InfoScreen* this, const char* fmt, ...) { |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 51 | va_list ap; |
| 52 | va_start(ap, fmt); |
| ryenus | ff455b0 | 2020-10-03 00:12:31 +0800 | [diff] [blame] | 53 | |
| Christian Göttsche | e3862aa | 2020-12-16 21:46:11 +0100 | [diff] [blame] | 54 | char title[COLS + 1]; |
| 55 | int len = vsnprintf(title, sizeof(title), fmt, ap); |
| 56 | va_end(ap); |
| 57 | |
| ryenus | ff455b0 | 2020-10-03 00:12:31 +0800 | [diff] [blame] | 58 | if (len > COLS) { |
| 59 | memset(&title[COLS - 3], '.', 3); |
| 60 | } |
| 61 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 62 | attrset(CRT_colors[METER_TEXT]); |
| 63 | mvhline(0, 0, ' ', COLS); |
| V | bfcb8ca | 2021-01-08 05:11:45 +0100 | [diff] [blame] | 64 | mvaddstr(0, 0, title); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 65 | attrset(CRT_colors[DEFAULT_COLOR]); |
| Christian Göttsche | a3cced9 | 2020-12-28 23:26:14 +0100 | [diff] [blame] | 66 | Panel_draw(this->display, true, true, true, false); |
| Christian Göttsche | e3862aa | 2020-12-16 21:46:11 +0100 | [diff] [blame] | 67 | |
| Hisham Muhammad | 72ba20f | 2021-08-31 15:38:52 +1000 | [diff] [blame] | 68 | IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void InfoScreen_addLine(InfoScreen* this, const char* line) { |
| 72 | Vector_add(this->lines, (Object*) ListItem_new(line, 0)); |
| 73 | const char* incFilter = IncSet_filter(this->inc); |
| Daniel Lange | 7c43e02 | 2022-03-25 16:24:24 +0100 | [diff] [blame] | 74 | if (!incFilter || String_contains_i(line, incFilter, true)) { |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 75 | Panel_add(this->display, Vector_get(this->lines, Vector_size(this->lines) - 1)); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 76 | } |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void InfoScreen_appendLine(InfoScreen* this, const char* line) { |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 80 | ListItem* last = (ListItem*)Vector_get(this->lines, Vector_size(this->lines) - 1); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 81 | ListItem_append(last, line); |
| 82 | const char* incFilter = IncSet_filter(this->inc); |
| Daniel Lange | 7c43e02 | 2022-03-25 16:24:24 +0100 | [diff] [blame] | 83 | if (incFilter && Panel_get(this->display, Panel_size(this->display) - 1) != (Object*)last && String_contains_i(line, incFilter, true)) { |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 84 | Panel_add(this->display, (Object*)last); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 85 | } |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void InfoScreen_run(InfoScreen* this) { |
| 89 | Panel* panel = this->display; |
| 90 | |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 91 | if (As_InfoScreen(this)->scan) |
| 92 | InfoScreen_scan(this); |
| 93 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 94 | InfoScreen_draw(this); |
| 95 | |
| 96 | bool looping = true; |
| 97 | while (looping) { |
| 98 | |
| Christian Göttsche | a3cced9 | 2020-12-28 23:26:14 +0100 | [diff] [blame] | 99 | Panel_draw(panel, false, true, true, false); |
| Hisham Muhammad | 72ba20f | 2021-08-31 15:38:52 +1000 | [diff] [blame] | 100 | IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 101 | |
| Hisham Muhammad | 72ba20f | 2021-08-31 15:38:52 +1000 | [diff] [blame] | 102 | int ch = Panel_getCh(panel); |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 103 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 104 | if (ch == ERR) { |
| 105 | if (As_InfoScreen(this)->onErr) { |
| 106 | InfoScreen_onErr(this); |
| 107 | continue; |
| 108 | } |
| 109 | } |
| 110 | |
| nia | 2ab8fb8 | 2021-07-14 20:17:13 +0200 | [diff] [blame] | 111 | #ifdef HAVE_GETMOUSE |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 112 | if (ch == KEY_MOUSE) { |
| 113 | MEVENT mevent; |
| 114 | int ok = getmouse(&mevent); |
| Nathan Scott | 36ef4d4 | 2020-08-19 18:10:16 +1000 | [diff] [blame] | 115 | if (ok == OK) { |
| Youngjae Lee | 3c61813 | 2021-01-14 13:37:46 +0900 | [diff] [blame] | 116 | if (mevent.bstate & BUTTON1_RELEASED) { |
| 117 | if (mevent.y >= panel->y && mevent.y < LINES - 1) { |
| 118 | Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1); |
| 119 | ch = 0; |
| 120 | } else if (mevent.y == LINES - 1) { |
| 121 | ch = IncSet_synthesizeEvent(this->inc, mevent.x); |
| 122 | } |
| YJ Lee | 34da6fd | 2021-01-14 16:25:23 +0900 | [diff] [blame] | 123 | } |
| Youngjae Lee | 3c61813 | 2021-01-14 13:37:46 +0900 | [diff] [blame] | 124 | #if NCURSES_MOUSE_VERSION > 1 |
| YJ Lee | 34da6fd | 2021-01-14 16:25:23 +0900 | [diff] [blame] | 125 | else if (mevent.bstate & BUTTON4_PRESSED) { |
| Youngjae Lee | 3c61813 | 2021-01-14 13:37:46 +0900 | [diff] [blame] | 126 | ch = KEY_WHEELUP; |
| 127 | } else if (mevent.bstate & BUTTON5_PRESSED) { |
| 128 | ch = KEY_WHEELDOWN; |
| Jorge Pereira | 8de0498 | 2020-06-11 13:41:13 -0300 | [diff] [blame] | 129 | } |
| YJ Lee | 34da6fd | 2021-01-14 16:25:23 +0900 | [diff] [blame] | 130 | #endif |
| Christian Göttsche | f8208f2 | 2020-10-20 13:14:32 +0200 | [diff] [blame] | 131 | } |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 132 | } |
| nia | 2ab8fb8 | 2021-07-14 20:17:13 +0200 | [diff] [blame] | 133 | #endif |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 134 | |
| 135 | if (this->inc->active) { |
| 136 | IncSet_handleKey(this->inc, ch, panel, IncSet_getListItemValue, this->lines); |
| 137 | continue; |
| 138 | } |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 139 | |
| Sohaib Mohamed | 21cb1c4 | 2021-11-04 21:40:39 +0200 | [diff] [blame] | 140 | switch (ch) { |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 141 | case ERR: |
| 142 | continue; |
| 143 | case KEY_F(3): |
| 144 | case '/': |
| 145 | IncSet_activate(this->inc, INC_SEARCH, panel); |
| 146 | break; |
| 147 | case KEY_F(4): |
| 148 | case '\\': |
| 149 | IncSet_activate(this->inc, INC_FILTER, panel); |
| 150 | break; |
| 151 | case KEY_F(5): |
| 152 | clear(); |
| Øystein Hiåsen | d8d8303 | 2021-02-15 20:32:01 +0100 | [diff] [blame] | 153 | if (As_InfoScreen(this)->scan) { |
| 154 | Vector_prune(this->lines); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 155 | InfoScreen_scan(this); |
| Øystein Hiåsen | d8d8303 | 2021-02-15 20:32:01 +0100 | [diff] [blame] | 156 | } |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 157 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 158 | InfoScreen_draw(this); |
| 159 | break; |
| 160 | case '\014': // Ctrl+L |
| 161 | clear(); |
| 162 | InfoScreen_draw(this); |
| 163 | break; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 164 | case 27: |
| Benny Baumann | c49ca61 | 2020-11-21 16:59:38 +0100 | [diff] [blame] | 165 | case 'q': |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 166 | case KEY_F(10): |
| 167 | looping = false; |
| 168 | break; |
| 169 | case KEY_RESIZE: |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 170 | Panel_resize(panel, COLS, LINES - 2); |
| Øystein Hiåsen | d8d8303 | 2021-02-15 20:32:01 +0100 | [diff] [blame] | 171 | if (As_InfoScreen(this)->scan) { |
| 172 | Vector_prune(this->lines); |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 173 | InfoScreen_scan(this); |
| Øystein Hiåsen | d8d8303 | 2021-02-15 20:32:01 +0100 | [diff] [blame] | 174 | } |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 175 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 176 | InfoScreen_draw(this); |
| 177 | break; |
| 178 | default: |
| 179 | if (As_InfoScreen(this)->onKey && InfoScreen_onKey(this, ch)) { |
| 180 | continue; |
| 181 | } |
| 182 | Panel_onKey(panel, ch); |
| 183 | } |
| 184 | } |
| 185 | } |