Action.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. htop - Action.c
  3. (C) 2015 Hisham H. Muhammad
  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 "Action.h"
  9. #include <assert.h>
  10. #include <pwd.h>
  11. #include <stdbool.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "CRT.h"
  15. #include "CategoriesPanel.h"
  16. #include "CommandScreen.h"
  17. #include "DynamicColumn.h"
  18. #include "EnvScreen.h"
  19. #include "FunctionBar.h"
  20. #include "Hashtable.h"
  21. #include "IncSet.h"
  22. #include "InfoScreen.h"
  23. #include "ListItem.h"
  24. #include "Macros.h"
  25. #include "MainPanel.h"
  26. #include "OpenFilesScreen.h"
  27. #include "Process.h"
  28. #include "ProcessLocksScreen.h"
  29. #include "ProvideCurses.h"
  30. #include "Row.h"
  31. #include "RowField.h"
  32. #include "Scheduling.h"
  33. #include "ScreenManager.h"
  34. #include "SignalsPanel.h"
  35. #include "Table.h"
  36. #include "TraceScreen.h"
  37. #include "UsersTable.h"
  38. #include "Vector.h"
  39. #include "XUtils.h"
  40. #if (defined(HAVE_LIBHWLOC) || defined(HAVE_AFFINITY))
  41. #include "Affinity.h"
  42. #include "AffinityPanel.h"
  43. #endif
  44. Object* Action_pickFromVector(State* st, Panel* list, int x, bool follow) {
  45. MainPanel* mainPanel = st->mainPanel;
  46. Header* header = st->header;
  47. Machine* host = st->host;
  48. int y = ((Panel*)mainPanel)->y;
  49. ScreenManager* scr = ScreenManager_new(header, host, st, false);
  50. scr->allowFocusChange = false;
  51. ScreenManager_add(scr, list, x);
  52. ScreenManager_add(scr, (Panel*)mainPanel, -1);
  53. Panel* panelFocus;
  54. int ch;
  55. bool unfollow = false;
  56. int row = follow ? MainPanel_selectedRow(mainPanel) : -1;
  57. if (follow && host->activeTable->following == -1) {
  58. host->activeTable->following = row;
  59. unfollow = true;
  60. }
  61. ScreenManager_run(scr, &panelFocus, &ch, NULL);
  62. if (unfollow) {
  63. host->activeTable->following = -1;
  64. }
  65. ScreenManager_delete(scr);
  66. Panel_move((Panel*)mainPanel, 0, y);
  67. Panel_resize((Panel*)mainPanel, COLS, LINES - y - 1);
  68. if (panelFocus == list && ch == 13) {
  69. if (follow) {
  70. const Row* selected = (const Row*)Panel_getSelected((Panel*)mainPanel);
  71. if (selected && selected->id == row)
  72. return Panel_getSelected(list);
  73. beep();
  74. } else {
  75. return Panel_getSelected(list);
  76. }
  77. }
  78. return NULL;
  79. }
  80. // ----------------------------------------
  81. static void Action_runSetup(State* st) {
  82. const Settings* settings = st->host->settings;
  83. ScreenManager* scr = ScreenManager_new(st->header, st->host, st, true);
  84. CategoriesPanel_new(scr, st->header, st->host);
  85. ScreenManager_run(scr, NULL, NULL, "Setup");
  86. ScreenManager_delete(scr);
  87. if (settings->changed) {
  88. CRT_setMouse(settings->enableMouse);
  89. Header_writeBackToSettings(st->header);
  90. }
  91. }
  92. static bool changePriority(MainPanel* panel, int delta) {
  93. bool anyTagged;
  94. bool ok = MainPanel_foreachRow(panel, Process_rowChangePriorityBy, (Arg) { .i = delta }, &anyTagged);
  95. if (!ok)
  96. beep();
  97. return anyTagged;
  98. }
  99. static void addUserToVector(ht_key_t key, void* userCast, void* panelCast) {
  100. const char* user = userCast;
  101. Panel* panel = panelCast;
  102. Panel_add(panel, (Object*) ListItem_new(user, key));
  103. }
  104. bool Action_setUserOnly(const char* userName, uid_t* userId) {
  105. const struct passwd* user = getpwnam(userName);
  106. if (user) {
  107. *userId = user->pw_uid;
  108. return true;
  109. }
  110. *userId = (uid_t)-1;
  111. return false;
  112. }
  113. static void tagAllChildren(Panel* panel, Row* parent) {
  114. parent->tag = true;
  115. int parent_id = parent->id;
  116. for (int i = 0; i < Panel_size(panel); i++) {
  117. Row* row = (Row*) Panel_get(panel, i);
  118. if (!row->tag && Row_isChildOf(row, parent_id)) {
  119. tagAllChildren(panel, row);
  120. }
  121. }
  122. }
  123. static bool expandCollapse(Panel* panel) {
  124. Row* row = (Row*) Panel_getSelected(panel);
  125. if (!row)
  126. return false;
  127. row->showChildren = !row->showChildren;
  128. return true;
  129. }
  130. static bool collapseIntoParent(Panel* panel) {
  131. const Row* r = (Row*) Panel_getSelected(panel);
  132. if (!r)
  133. return false;
  134. int parent_id = Row_getGroupOrParent(r);
  135. for (int i = 0; i < Panel_size(panel); i++) {
  136. Row* row = (Row*) Panel_get(panel, i);
  137. if (row->id == parent_id) {
  138. row->showChildren = false;
  139. Panel_setSelected(panel, i);
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey) {
  146. ScreenSettings_setSortKey(settings->ss, (RowField) sortKey);
  147. return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_UPDATE_PANELHDR | HTOP_KEEP_FOLLOWING;
  148. }
  149. // ----------------------------------------
  150. static bool Action_writeableProcess(State* st) {
  151. const Settings* settings = st->host->settings;
  152. bool readonly = Settings_isReadonly() || settings->ss->dynamic;
  153. return !readonly;
  154. }
  155. static bool Action_readableProcess(State* st) {
  156. const Settings* settings = st->host->settings;
  157. return !settings->ss->dynamic;
  158. }
  159. static Htop_Reaction actionSetSortColumn(State* st) {
  160. Htop_Reaction reaction = HTOP_OK;
  161. Panel* sortPanel = Panel_new(0, 0, 0, 0, Class(ListItem), true, FunctionBar_newEnterEsc("Sort ", "Cancel "));
  162. Panel_setHeader(sortPanel, "Sort by");
  163. Machine* host = st->host;
  164. Settings* settings = host->settings;
  165. const RowField* fields = settings->ss->fields;
  166. Hashtable* dynamicColumns = settings->dynamicColumns;
  167. for (int i = 0; fields[i]; i++) {
  168. char* name = NULL;
  169. if (fields[i] >= ROW_DYNAMIC_FIELDS) {
  170. DynamicColumn* column = Hashtable_get(dynamicColumns, fields[i]);
  171. if (!column)
  172. continue;
  173. name = xStrdup(column->caption ? column->caption : column->name);
  174. } else {
  175. name = String_trim(Process_fields[fields[i]].name);
  176. }
  177. Panel_add(sortPanel, (Object*) ListItem_new(name, fields[i]));
  178. if (fields[i] == ScreenSettings_getActiveSortKey(settings->ss))
  179. Panel_setSelected(sortPanel, i);
  180. free(name);
  181. }
  182. const ListItem* field = (const ListItem*) Action_pickFromVector(st, sortPanel, 14, false);
  183. if (field) {
  184. reaction |= Action_setSortKey(settings, field->key);
  185. }
  186. Object_delete(sortPanel);
  187. host->activeTable->needsSort = true;
  188. return reaction | HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
  189. }
  190. static Htop_Reaction actionSortByPID(State* st) {
  191. return Action_setSortKey(st->host->settings, PID);
  192. }
  193. static Htop_Reaction actionSortByMemory(State* st) {
  194. return Action_setSortKey(st->host->settings, PERCENT_MEM);
  195. }
  196. static Htop_Reaction actionSortByCPU(State* st) {
  197. return Action_setSortKey(st->host->settings, PERCENT_CPU);
  198. }
  199. static Htop_Reaction actionSortByTime(State* st) {
  200. return Action_setSortKey(st->host->settings, TIME);
  201. }
  202. static Htop_Reaction actionToggleKernelThreads(State* st) {
  203. Settings* settings = st->host->settings;
  204. settings->hideKernelThreads = !settings->hideKernelThreads;
  205. settings->lastUpdate++;
  206. Machine_scanTables(st->host); // needed to not have a visible delay showing wrong data
  207. return HTOP_RECALCULATE | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING;
  208. }
  209. static Htop_Reaction actionToggleUserlandThreads(State* st) {
  210. Settings* settings = st->host->settings;
  211. settings->hideUserlandThreads = !settings->hideUserlandThreads;
  212. settings->lastUpdate++;
  213. Machine_scanTables(st->host); // needed to not have a visible delay showing wrong data
  214. return HTOP_RECALCULATE | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING;
  215. }
  216. static Htop_Reaction actionToggleRunningInContainer(State* st) {
  217. Settings* settings = st->host->settings;
  218. settings->hideRunningInContainer = !settings->hideRunningInContainer;
  219. settings->lastUpdate++;
  220. return HTOP_RECALCULATE | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING;
  221. }
  222. static Htop_Reaction actionToggleProgramPath(State* st) {
  223. Settings* settings = st->host->settings;
  224. settings->showProgramPath = !settings->showProgramPath;
  225. settings->lastUpdate++;
  226. return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING;
  227. }
  228. static Htop_Reaction actionToggleMergedCommand(State* st) {
  229. Settings* settings = st->host->settings;
  230. settings->showMergedCommand = !settings->showMergedCommand;
  231. settings->lastUpdate++;
  232. return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_UPDATE_PANELHDR;
  233. }
  234. static Htop_Reaction actionToggleTreeView(State* st) {
  235. Machine* host = st->host;
  236. ScreenSettings* ss = host->settings->ss;
  237. ss->treeView = !ss->treeView;
  238. if (!ss->allBranchesCollapsed)
  239. Table_expandTree(host->activeTable);
  240. host->activeTable->needsSort = true;
  241. return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
  242. }
  243. static Htop_Reaction actionToggleHideMeters(State* st) {
  244. st->hideMeters = !st->hideMeters;
  245. return HTOP_RESIZE | HTOP_KEEP_FOLLOWING;
  246. }
  247. static Htop_Reaction actionExpandOrCollapseAllBranches(State* st) {
  248. Machine* host = st->host;
  249. ScreenSettings* ss = host->settings->ss;
  250. if (!ss->treeView) {
  251. return HTOP_OK;
  252. }
  253. ss->allBranchesCollapsed = !ss->allBranchesCollapsed;
  254. if (ss->allBranchesCollapsed)
  255. Table_collapseAllBranches(host->activeTable);
  256. else
  257. Table_expandTree(host->activeTable);
  258. return HTOP_REFRESH | HTOP_SAVE_SETTINGS;
  259. }
  260. static Htop_Reaction actionIncFilter(State* st) {
  261. Machine* host = st->host;
  262. IncSet* inc = (st->mainPanel)->inc;
  263. IncSet_activate(inc, INC_FILTER, (Panel*)st->mainPanel);
  264. host->activeTable->incFilter = IncSet_filter(inc);
  265. return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
  266. }
  267. static Htop_Reaction actionIncSearch(State* st) {
  268. IncSet_reset(st->mainPanel->inc, INC_SEARCH);
  269. IncSet_activate(st->mainPanel->inc, INC_SEARCH, (Panel*)st->mainPanel);
  270. return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
  271. }
  272. static Htop_Reaction actionHigherPriority(State* st) {
  273. if (!Action_writeableProcess(st))
  274. return HTOP_OK;
  275. bool changed = changePriority(st->mainPanel, -1);
  276. return changed ? HTOP_REFRESH : HTOP_OK;
  277. }
  278. static Htop_Reaction actionLowerPriority(State* st) {
  279. if (!Action_writeableProcess(st))
  280. return HTOP_OK;
  281. bool changed = changePriority(st->mainPanel, 1);
  282. return changed ? HTOP_REFRESH : HTOP_OK;
  283. }
  284. static Htop_Reaction actionInvertSortOrder(State* st) {
  285. Machine* host = st->host;
  286. ScreenSettings_invertSortOrder(host->settings->ss);
  287. host->activeTable->needsSort = true;
  288. return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_UPDATE_PANELHDR;
  289. }
  290. static Htop_Reaction actionExpandOrCollapse(State* st) {
  291. if (!st->host->settings->ss->treeView)
  292. return HTOP_OK;
  293. bool changed = expandCollapse((Panel*)st->mainPanel);
  294. return changed ? HTOP_RECALCULATE : HTOP_OK;
  295. }
  296. static Htop_Reaction actionCollapseIntoParent(State* st) {
  297. if (!st->host->settings->ss->treeView) {
  298. return HTOP_OK;
  299. }
  300. bool changed = collapseIntoParent((Panel*)st->mainPanel);
  301. return changed ? HTOP_RECALCULATE : HTOP_OK;
  302. }
  303. static Htop_Reaction actionExpandCollapseOrSortColumn(State* st) {
  304. return st->host->settings->ss->treeView ? actionExpandOrCollapse(st) : actionSetSortColumn(st);
  305. }
  306. static inline void setActiveScreen(Settings* settings, State* st, unsigned int ssIdx) {
  307. assert(settings->ssIndex == ssIdx);
  308. Machine* host = st->host;
  309. settings->ss = settings->screens[ssIdx];
  310. if (!settings->ss->table)
  311. settings->ss->table = host->processTable;
  312. host->activeTable = settings->ss->table;
  313. // set correct functionBar - readonly if requested, and/or with non-process screens
  314. bool readonly = Settings_isReadonly() || (host->activeTable != host->processTable);
  315. MainPanel_setFunctionBar(st->mainPanel, readonly);
  316. }
  317. static Htop_Reaction actionNextScreen(State* st) {
  318. Settings* settings = st->host->settings;
  319. settings->ssIndex++;
  320. if (settings->ssIndex == settings->nScreens) {
  321. settings->ssIndex = 0;
  322. }
  323. setActiveScreen(settings, st, settings->ssIndex);
  324. return HTOP_UPDATE_PANELHDR | HTOP_REFRESH | HTOP_REDRAW_BAR;
  325. }
  326. static Htop_Reaction actionPrevScreen(State* st) {
  327. Settings* settings = st->host->settings;
  328. if (settings->ssIndex == 0) {
  329. settings->ssIndex = settings->nScreens - 1;
  330. } else {
  331. settings->ssIndex--;
  332. }
  333. setActiveScreen(settings, st, settings->ssIndex);
  334. return HTOP_UPDATE_PANELHDR | HTOP_REFRESH | HTOP_REDRAW_BAR;
  335. }
  336. Htop_Reaction Action_setScreenTab(State* st, int x) {
  337. Settings* settings = st->host->settings;
  338. int s = 2;
  339. for (unsigned int i = 0; i < settings->nScreens; i++) {
  340. if (x < s) {
  341. return 0;
  342. }
  343. const char* tab = settings->screens[i]->heading;
  344. int len = strlen(tab);
  345. if (x <= s + len + 1) {
  346. settings->ssIndex = i;
  347. setActiveScreen(settings, st, i);
  348. return HTOP_UPDATE_PANELHDR | HTOP_REFRESH | HTOP_REDRAW_BAR;
  349. }
  350. s += len + 3;
  351. }
  352. return 0;
  353. }
  354. static Htop_Reaction actionQuit(ATTR_UNUSED State* st) {
  355. return HTOP_QUIT;
  356. }
  357. static Htop_Reaction actionSetAffinity(State* st) {
  358. if (!Action_writeableProcess(st))
  359. return HTOP_OK;
  360. Machine* host = st->host;
  361. if (host->activeCPUs == 1)
  362. return HTOP_OK;
  363. #if (defined(HAVE_LIBHWLOC) || defined(HAVE_AFFINITY))
  364. const Row* row = (const Row*) Panel_getSelected((Panel*)st->mainPanel);
  365. if (!row)
  366. return HTOP_OK;
  367. Affinity* affinity1 = Affinity_rowGet(row, host);
  368. if (!affinity1)
  369. return HTOP_OK;
  370. int width;
  371. Panel* affinityPanel = AffinityPanel_new(host, affinity1, &width);
  372. Affinity_delete(affinity1);
  373. const void* set = Action_pickFromVector(st, affinityPanel, width, true);
  374. if (set) {
  375. Affinity* affinity2 = AffinityPanel_getAffinity(affinityPanel, host);
  376. bool ok = MainPanel_foreachRow(st->mainPanel, Affinity_rowSet, (Arg) { .v = affinity2 }, NULL);
  377. if (!ok)
  378. beep();
  379. Affinity_delete(affinity2);
  380. }
  381. Object_delete(affinityPanel);
  382. return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
  383. #else
  384. return HTOP_OK;
  385. #endif
  386. }
  387. #ifdef SCHEDULER_SUPPORT
  388. static Htop_Reaction actionSetSchedPolicy(State* st) {
  389. if (!Action_writeableProcess(st))
  390. return HTOP_KEEP_FOLLOWING;
  391. static int preSelectedPolicy = SCHEDULINGPANEL_INITSELECTEDPOLICY;
  392. static int preSelectedPriority = SCHEDULINGPANEL_INITSELECTEDPRIORITY;
  393. Panel* schedPanel = Scheduling_newPolicyPanel(preSelectedPolicy);
  394. const ListItem* policy;
  395. for (;;) {
  396. policy = (const ListItem*) Action_pickFromVector(st, schedPanel, 18, true);
  397. if (!policy || policy->key != -1)
  398. break;
  399. Scheduling_togglePolicyPanelResetOnFork(schedPanel);
  400. }
  401. if (policy) {
  402. preSelectedPolicy = policy->key;
  403. Panel* prioPanel = Scheduling_newPriorityPanel(policy->key, preSelectedPriority);
  404. if (prioPanel) {
  405. const ListItem* prio = (const ListItem*) Action_pickFromVector(st, prioPanel, 14, true);
  406. if (prio)
  407. preSelectedPriority = prio->key;
  408. Panel_delete((Object*) prioPanel);
  409. }
  410. SchedulingArg v = { .policy = preSelectedPolicy, .priority = preSelectedPriority };
  411. bool ok = MainPanel_foreachRow(st->mainPanel, Scheduling_rowSetPolicy, (Arg) { .v = &v }, NULL);
  412. if (!ok)
  413. beep();
  414. }
  415. Panel_delete((Object*)schedPanel);
  416. return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_KEEP_FOLLOWING;
  417. }
  418. #endif /* SCHEDULER_SUPPORT */
  419. static Htop_Reaction actionKill(State* st) {
  420. if (!Action_writeableProcess(st))
  421. return HTOP_OK;
  422. static int preSelectedSignal = SIGNALSPANEL_INITSELECTEDSIGNAL;
  423. Panel* signalsPanel = SignalsPanel_new(preSelectedSignal);
  424. const ListItem* sgn = (ListItem*) Action_pickFromVector(st, signalsPanel, 14, true);
  425. if (sgn && sgn->key != 0) {
  426. preSelectedSignal = sgn->key;
  427. Panel_setHeader((Panel*)st->mainPanel, "Sending...");
  428. Panel_draw((Panel*)st->mainPanel, false, true, true, State_hideFunctionBar(st));
  429. refresh();
  430. bool ok = MainPanel_foreachRow(st->mainPanel, Process_rowSendSignal, (Arg) { .i = sgn->key }, NULL);
  431. if (!ok) {
  432. beep();
  433. }
  434. napms(500);
  435. }
  436. Panel_delete((Object*)signalsPanel);
  437. return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
  438. }
  439. static Htop_Reaction actionFilterByUser(State* st) {
  440. Panel* usersPanel = Panel_new(0, 0, 0, 0, Class(ListItem), true, FunctionBar_newEnterEsc("Show ", "Cancel "));
  441. Panel_setHeader(usersPanel, "Show processes of:");
  442. Machine* host = st->host;
  443. UsersTable_foreach(host->usersTable, addUserToVector, usersPanel);
  444. Vector_insertionSort(usersPanel->items);
  445. ListItem* allUsers = ListItem_new("All users", -1);
  446. Panel_insert(usersPanel, 0, (Object*) allUsers);
  447. const ListItem* picked = (ListItem*) Action_pickFromVector(st, usersPanel, 19, false);
  448. if (picked) {
  449. if (picked == allUsers) {
  450. host->userId = (uid_t)-1;
  451. } else {
  452. Action_setUserOnly(ListItem_getRef(picked), &host->userId);
  453. }
  454. }
  455. Panel_delete((Object*)usersPanel);
  456. return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
  457. }
  458. Htop_Reaction Action_follow(State* st) {
  459. st->host->activeTable->following = MainPanel_selectedRow(st->mainPanel);
  460. Panel_setSelectionColor((Panel*)st->mainPanel, PANEL_SELECTION_FOLLOW);
  461. return HTOP_KEEP_FOLLOWING;
  462. }
  463. static Htop_Reaction actionSetup(State* st) {
  464. Action_runSetup(st);
  465. return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR | HTOP_RESIZE;
  466. }
  467. static Htop_Reaction actionLsof(State* st) {
  468. if (!Action_writeableProcess(st))
  469. return HTOP_OK;
  470. const Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
  471. if (!p)
  472. return HTOP_OK;
  473. assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
  474. OpenFilesScreen* ofs = OpenFilesScreen_new(p);
  475. InfoScreen_run((InfoScreen*)ofs);
  476. OpenFilesScreen_delete((Object*)ofs);
  477. clear();
  478. CRT_enableDelay();
  479. return HTOP_REFRESH | HTOP_REDRAW_BAR;
  480. }
  481. static Htop_Reaction actionShowLocks(State* st) {
  482. if (!Action_readableProcess(st))
  483. return HTOP_OK;
  484. const Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
  485. if (!p)
  486. return HTOP_OK;
  487. assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
  488. ProcessLocksScreen* pls = ProcessLocksScreen_new(p);
  489. InfoScreen_run((InfoScreen*)pls);
  490. ProcessLocksScreen_delete((Object*)pls);
  491. clear();
  492. CRT_enableDelay();
  493. return HTOP_REFRESH | HTOP_REDRAW_BAR;
  494. }
  495. static Htop_Reaction actionStrace(State* st) {
  496. if (!Action_writeableProcess(st))
  497. return HTOP_OK;
  498. const Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
  499. if (!p)
  500. return HTOP_OK;
  501. assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
  502. TraceScreen* ts = TraceScreen_new(p);
  503. bool ok = TraceScreen_forkTracer(ts);
  504. if (ok) {
  505. InfoScreen_run((InfoScreen*)ts);
  506. }
  507. TraceScreen_delete((Object*)ts);
  508. clear();
  509. CRT_enableDelay();
  510. return HTOP_REFRESH | HTOP_REDRAW_BAR;
  511. }
  512. static Htop_Reaction actionTag(State* st) {
  513. Row* r = (Row*) Panel_getSelected((Panel*)st->mainPanel);
  514. if (!r)
  515. return HTOP_OK;
  516. Row_toggleTag(r);
  517. Panel_onKey((Panel*)st->mainPanel, KEY_DOWN);
  518. return HTOP_OK;
  519. }
  520. static Htop_Reaction actionRedraw(ATTR_UNUSED State* st) {
  521. clear();
  522. // HTOP_RECALCULATE here to make Ctrl-L also refresh the data and not only redraw
  523. return HTOP_RECALCULATE | HTOP_REFRESH | HTOP_REDRAW_BAR;
  524. }
  525. static Htop_Reaction actionTogglePauseUpdate(State* st) {
  526. st->pauseUpdate = !st->pauseUpdate;
  527. return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_KEEP_FOLLOWING;
  528. }
  529. static const struct {
  530. const char* key;
  531. bool roInactive;
  532. const char* info;
  533. } helpLeft[] = {
  534. { .key = " #: ", .roInactive = false, .info = "hide/show header meters" },
  535. { .key = " Tab: ", .roInactive = false, .info = "switch to next screen tab" },
  536. { .key = " Arrows: ", .roInactive = false, .info = "scroll process list" },
  537. { .key = " Digits: ", .roInactive = false, .info = "incremental PID search" },
  538. { .key = " F3 /: ", .roInactive = false, .info = "incremental name search" },
  539. { .key = " F4 \\: ", .roInactive = false, .info = "incremental name filtering" },
  540. { .key = " F5 t: ", .roInactive = false, .info = "tree view" },
  541. { .key = " p: ", .roInactive = false, .info = "toggle program path" },
  542. { .key = " m: ", .roInactive = false, .info = "toggle merged command" },
  543. { .key = " Z: ", .roInactive = false, .info = "pause/resume process updates" },
  544. { .key = " u: ", .roInactive = false, .info = "show processes of a single user" },
  545. { .key = " H: ", .roInactive = false, .info = "hide/show user process threads" },
  546. { .key = " K: ", .roInactive = false, .info = "hide/show kernel threads" },
  547. { .key = " O: ", .roInactive = false, .info = "hide/show processes in containers" },
  548. { .key = " F: ", .roInactive = false, .info = "cursor follows process" },
  549. { .key = " + - *: ", .roInactive = false, .info = "expand/collapse tree/toggle all" },
  550. { .key = "N P M T: ", .roInactive = false, .info = "sort by PID, CPU%, MEM% or TIME" },
  551. { .key = " I: ", .roInactive = false, .info = "invert sort order" },
  552. { .key = " F6 > .: ", .roInactive = false, .info = "select sort column" },
  553. { .key = NULL, .info = NULL }
  554. };
  555. static const struct {
  556. const char* key;
  557. bool roInactive;
  558. const char* info;
  559. } helpRight[] = {
  560. { .key = " S-Tab: ", .roInactive = false, .info = "switch to previous screen tab" },
  561. { .key = " Space: ", .roInactive = false, .info = "tag process" },
  562. { .key = " c: ", .roInactive = false, .info = "tag process and its children" },
  563. { .key = " U: ", .roInactive = false, .info = "untag all processes" },
  564. { .key = " F9 k: ", .roInactive = true, .info = "kill process/tagged processes" },
  565. { .key = " F7 ]: ", .roInactive = true, .info = "higher priority (root only)" },
  566. { .key = " F8 [: ", .roInactive = true, .info = "lower priority (+ nice)" },
  567. #if (defined(HAVE_LIBHWLOC) || defined(HAVE_AFFINITY))
  568. { .key = " a: ", .roInactive = true, .info = "set CPU affinity" },
  569. #endif
  570. { .key = " e: ", .roInactive = false, .info = "show process environment" },
  571. { .key = " i: ", .roInactive = true, .info = "set IO priority" },
  572. { .key = " l: ", .roInactive = true, .info = "list open files with lsof" },
  573. { .key = " x: ", .roInactive = false, .info = "list file locks of process" },
  574. { .key = " s: ", .roInactive = true, .info = "trace syscalls with strace" },
  575. { .key = " w: ", .roInactive = false, .info = "wrap process command in multiple lines" },
  576. #ifdef SCHEDULER_SUPPORT
  577. { .key = " Y: ", .roInactive = true, .info = "set scheduling policy" },
  578. #endif
  579. { .key = " F2 C S: ", .roInactive = false, .info = "setup" },
  580. { .key = " F1 h ?: ", .roInactive = false, .info = "show this help screen" },
  581. { .key = " F10 q: ", .roInactive = false, .info = "quit" },
  582. { .key = NULL, .info = NULL }
  583. };
  584. static inline void addattrstr( int attr, const char* str) {
  585. attrset(attr);
  586. addstr(str);
  587. }
  588. static Htop_Reaction actionHelp(State* st) {
  589. clear();
  590. attrset(CRT_colors[HELP_BOLD]);
  591. for (int i = 0; i < LINES - 1; i++)
  592. mvhline(i, 0, ' ', COLS);
  593. int line = 0;
  594. mvaddstr(line++, 0, "htop " VERSION " - " COPYRIGHT);
  595. mvaddstr(line++, 0, "Released under the GNU GPLv2+. See 'man' page for more info.");
  596. attrset(CRT_colors[DEFAULT_COLOR]);
  597. line++;
  598. mvaddstr(line++, 0, "CPU usage bar: ");
  599. #define addbartext(attr, prefix, text) \
  600. do { \
  601. addattrstr(CRT_colors[DEFAULT_COLOR], prefix); \
  602. addattrstr(attr, text); \
  603. } while(0)
  604. addattrstr(CRT_colors[BAR_BORDER], "[");
  605. addbartext(CRT_colors[CPU_NICE_TEXT], "", "low");
  606. addbartext(CRT_colors[CPU_NORMAL], "/", "normal");
  607. addbartext(CRT_colors[CPU_SYSTEM], "/", "kernel");
  608. if (st->host->settings->detailedCPUTime) {
  609. addbartext(CRT_colors[CPU_IRQ], "/", "irq");
  610. addbartext(CRT_colors[CPU_SOFTIRQ], "/", "soft-irq");
  611. addbartext(CRT_colors[CPU_STEAL], "/", "steal");
  612. addbartext(CRT_colors[CPU_GUEST], "/", "guest");
  613. addbartext(CRT_colors[CPU_IOWAIT], "/", "io-wait");
  614. addbartext(CRT_colors[BAR_SHADOW], " ", "used%");
  615. } else {
  616. addbartext(CRT_colors[CPU_GUEST], "/", "guest");
  617. addbartext(CRT_colors[BAR_SHADOW], " ", "used%");
  618. }
  619. addattrstr(CRT_colors[BAR_BORDER], "]");
  620. attrset(CRT_colors[DEFAULT_COLOR]);
  621. mvaddstr(line++, 0, "Memory bar: ");
  622. addattrstr(CRT_colors[BAR_BORDER], "[");
  623. addbartext(CRT_colors[MEMORY_USED], "", "used");
  624. addbartext(CRT_colors[MEMORY_SHARED], "/", "shared");
  625. addbartext(CRT_colors[MEMORY_COMPRESSED], "/", "compressed");
  626. addbartext(CRT_colors[MEMORY_BUFFERS_TEXT], "/", "buffers");
  627. addbartext(CRT_colors[MEMORY_CACHE], "/", "cache");
  628. addbartext(CRT_colors[BAR_SHADOW], " ", "used");
  629. addbartext(CRT_colors[BAR_SHADOW], "/", "total");
  630. addattrstr(CRT_colors[BAR_BORDER], "]");
  631. attrset(CRT_colors[DEFAULT_COLOR]);
  632. mvaddstr(line++, 0, "Swap bar: ");
  633. addattrstr(CRT_colors[BAR_BORDER], "[");
  634. addbartext(CRT_colors[SWAP], "", "used");
  635. #ifdef HTOP_LINUX
  636. addbartext(CRT_colors[SWAP_CACHE], "/", "cache");
  637. addbartext(CRT_colors[SWAP_FRONTSWAP], "/", "frontswap");
  638. #else
  639. addbartext(CRT_colors[SWAP_CACHE], " ", "");
  640. #endif
  641. addbartext(CRT_colors[BAR_SHADOW], " ", "used");
  642. addbartext(CRT_colors[BAR_SHADOW], "/", "total");
  643. addattrstr(CRT_colors[BAR_BORDER], "]");
  644. line++;
  645. #undef addbartext
  646. attrset(CRT_colors[DEFAULT_COLOR]);
  647. mvaddstr(line++, 0, "Type and layout of header meters are configurable in the setup screen.");
  648. if (CRT_colorScheme == COLORSCHEME_MONOCHROME) {
  649. mvaddstr(line, 0, "In monochrome, meters display as different chars, in order: |#*@$%&.");
  650. }
  651. line++;
  652. #define addattrstatestr(attr, state, desc) \
  653. do { \
  654. addattrstr(attr, state); \
  655. addattrstr(CRT_colors[DEFAULT_COLOR], ": " desc); \
  656. } while(0)
  657. mvaddstr(line, 0, "Process state: ");
  658. addattrstatestr(CRT_colors[PROCESS_RUN_STATE], "R", "running; ");
  659. addattrstatestr(CRT_colors[PROCESS_SHADOW], "S", "sleeping; ");
  660. addattrstatestr(CRT_colors[PROCESS_RUN_STATE], "t", "traced/stopped; ");
  661. addattrstatestr(CRT_colors[PROCESS_D_STATE], "Z", "zombie; ");
  662. addattrstatestr(CRT_colors[PROCESS_D_STATE], "D", "disk sleep");
  663. attrset(CRT_colors[DEFAULT_COLOR]);
  664. #undef addattrstatestr
  665. line += 2;
  666. const bool readonly = Settings_isReadonly();
  667. int item;
  668. for (item = 0; helpLeft[item].key; item++) {
  669. attrset((helpLeft[item].roInactive && readonly) ? CRT_colors[HELP_SHADOW] : CRT_colors[DEFAULT_COLOR]);
  670. mvaddstr(line + item, 10, helpLeft[item].info);
  671. attrset((helpLeft[item].roInactive && readonly) ? CRT_colors[HELP_SHADOW] : CRT_colors[HELP_BOLD]);
  672. mvaddstr(line + item, 1, helpLeft[item].key);
  673. if (String_eq(helpLeft[item].key, " H: ")) {
  674. attrset((helpLeft[item].roInactive && readonly) ? CRT_colors[HELP_SHADOW] : CRT_colors[PROCESS_THREAD]);
  675. mvaddstr(line + item, 33, "threads");
  676. } else if (String_eq(helpLeft[item].key, " K: ")) {
  677. attrset((helpLeft[item].roInactive && readonly) ? CRT_colors[HELP_SHADOW] : CRT_colors[PROCESS_THREAD]);
  678. mvaddstr(line + item, 27, "threads");
  679. }
  680. }
  681. int leftHelpItems = item;
  682. for (item = 0; helpRight[item].key; item++) {
  683. attrset((helpRight[item].roInactive && readonly) ? CRT_colors[HELP_SHADOW] : CRT_colors[HELP_BOLD]);
  684. mvaddstr(line + item, 43, helpRight[item].key);
  685. attrset((helpRight[item].roInactive && readonly) ? CRT_colors[HELP_SHADOW] : CRT_colors[DEFAULT_COLOR]);
  686. mvaddstr(line + item, 52, helpRight[item].info);
  687. }
  688. line += MAXIMUM(leftHelpItems, item);
  689. line++;
  690. attrset(CRT_colors[HELP_BOLD]);
  691. mvaddstr(line++, 0, "Press any key to return.");
  692. attrset(CRT_colors[DEFAULT_COLOR]);
  693. refresh();
  694. CRT_readKey();
  695. clear();
  696. return HTOP_RECALCULATE | HTOP_REDRAW_BAR | HTOP_KEEP_FOLLOWING;
  697. }
  698. static Htop_Reaction actionUntagAll(State* st) {
  699. for (int i = 0; i < Panel_size((Panel*)st->mainPanel); i++) {
  700. Row* row = (Row*) Panel_get((Panel*)st->mainPanel, i);
  701. row->tag = false;
  702. }
  703. return HTOP_REFRESH;
  704. }
  705. static Htop_Reaction actionTagAllChildren(State* st) {
  706. Row* row = (Row*) Panel_getSelected((Panel*)st->mainPanel);
  707. if (!row)
  708. return HTOP_OK;
  709. tagAllChildren((Panel*)st->mainPanel, row);
  710. return HTOP_OK;
  711. }
  712. static Htop_Reaction actionShowEnvScreen(State* st) {
  713. if (!Action_readableProcess(st))
  714. return HTOP_OK;
  715. Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
  716. if (!p)
  717. return HTOP_OK;
  718. assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
  719. EnvScreen* es = EnvScreen_new(p);
  720. InfoScreen_run((InfoScreen*)es);
  721. EnvScreen_delete((Object*)es);
  722. clear();
  723. CRT_enableDelay();
  724. return HTOP_REFRESH | HTOP_REDRAW_BAR;
  725. }
  726. static Htop_Reaction actionShowCommandScreen(State* st) {
  727. if (!Action_readableProcess(st))
  728. return HTOP_OK;
  729. Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
  730. if (!p)
  731. return HTOP_OK;
  732. assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
  733. CommandScreen* cmdScr = CommandScreen_new(p);
  734. InfoScreen_run((InfoScreen*)cmdScr);
  735. CommandScreen_delete((Object*)cmdScr);
  736. clear();
  737. CRT_enableDelay();
  738. return HTOP_REFRESH | HTOP_REDRAW_BAR;
  739. }
  740. void Action_setBindings(Htop_Action* keys) {
  741. keys[' '] = actionTag;
  742. keys['#'] = actionToggleHideMeters;
  743. keys['*'] = actionExpandOrCollapseAllBranches;
  744. keys['+'] = actionExpandOrCollapse;
  745. keys[','] = actionSetSortColumn;
  746. keys['-'] = actionExpandOrCollapse;
  747. keys['.'] = actionSetSortColumn;
  748. keys['/'] = actionIncSearch;
  749. keys['<'] = actionSetSortColumn;
  750. keys['='] = actionExpandOrCollapse;
  751. keys['>'] = actionSetSortColumn;
  752. keys['?'] = actionHelp;
  753. keys['C'] = actionSetup;
  754. keys['F'] = Action_follow;
  755. keys['H'] = actionToggleUserlandThreads;
  756. keys['I'] = actionInvertSortOrder;
  757. keys['K'] = actionToggleKernelThreads;
  758. keys['M'] = actionSortByMemory;
  759. keys['N'] = actionSortByPID;
  760. keys['O'] = actionToggleRunningInContainer;
  761. keys['P'] = actionSortByCPU;
  762. keys['S'] = actionSetup;
  763. keys['T'] = actionSortByTime;
  764. keys['U'] = actionUntagAll;
  765. #ifdef SCHEDULER_SUPPORT
  766. keys['Y'] = actionSetSchedPolicy;
  767. #endif
  768. keys['Z'] = actionTogglePauseUpdate;
  769. keys['['] = actionLowerPriority;
  770. keys['\014'] = actionRedraw; // Ctrl+L
  771. keys['\177'] = actionCollapseIntoParent;
  772. keys['\\'] = actionIncFilter;
  773. keys[']'] = actionHigherPriority;
  774. keys['a'] = actionSetAffinity;
  775. keys['c'] = actionTagAllChildren;
  776. keys['e'] = actionShowEnvScreen;
  777. keys['h'] = actionHelp;
  778. keys['k'] = actionKill;
  779. keys['l'] = actionLsof;
  780. keys['m'] = actionToggleMergedCommand;
  781. keys['p'] = actionToggleProgramPath;
  782. keys['q'] = actionQuit;
  783. keys['s'] = actionStrace;
  784. keys['t'] = actionToggleTreeView;
  785. keys['u'] = actionFilterByUser;
  786. keys['w'] = actionShowCommandScreen;
  787. keys['x'] = actionShowLocks;
  788. keys[KEY_F(1)] = actionHelp;
  789. keys[KEY_F(2)] = actionSetup;
  790. keys[KEY_F(3)] = actionIncSearch;
  791. keys[KEY_F(4)] = actionIncFilter;
  792. keys[KEY_F(5)] = actionToggleTreeView;
  793. keys[KEY_F(6)] = actionSetSortColumn;
  794. keys[KEY_F(7)] = actionHigherPriority;
  795. keys[KEY_F(8)] = actionLowerPriority;
  796. keys[KEY_F(9)] = actionKill;
  797. keys[KEY_F(10)] = actionQuit;
  798. keys[KEY_F(18)] = actionExpandCollapseOrSortColumn;
  799. keys[KEY_RECLICK] = actionExpandOrCollapse;
  800. keys[KEY_SHIFT_TAB] = actionPrevScreen;
  801. keys['\t'] = actionNextScreen;
  802. }