Action.c 21.1 KB
Newer Older
1
2
/*
htop - Action.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2015 Hisham H. Muhammad
4
5
6
7
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

Hisham Muhammad's avatar
Hisham Muhammad committed
8
9
#include "config.h"

10
#include "Action.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11
12
13
14
#include "Affinity.h"
#include "AffinityPanel.h"
#include "CategoriesPanel.h"
#include "CRT.h"
15
#include "EnvScreen.h"
16
#include "MainPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
17
18
#include "OpenFilesScreen.h"
#include "Process.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
19
#include "ScreenManager.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
20
#include "SignalsPanel.h"
David Hunt's avatar
David Hunt committed
21
#include "StringUtils.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
22
#include "TraceScreen.h"
23
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
24
25
26
27
28

#include <ctype.h>
#include <math.h>
#include <pwd.h>
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
29
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
30
31
32
#include <stdbool.h>
#include <sys/param.h>
#include <sys/time.h>
33
34
35
36
37

/*{

#include "IncSet.h"
#include "Settings.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
38
#include "Header.h"
39
#include "UsersTable.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
40
41
#include "ProcessList.h"
#include "Panel.h"
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

typedef enum {
   HTOP_OK = 0x00,
   HTOP_REFRESH = 0x01,
   HTOP_RECALCULATE = 0x03, // implies HTOP_REFRESH
   HTOP_SAVE_SETTINGS = 0x04,
   HTOP_KEEP_FOLLOWING = 0x08,
   HTOP_QUIT = 0x10,
   HTOP_REDRAW_BAR = 0x20,
   HTOP_UPDATE_PANELHDR = 0x41, // implies HTOP_REFRESH
} Htop_Reaction;

typedef Htop_Reaction (*Htop_Action)();

typedef struct State_ {
   Settings* settings;
   UsersTable* ut;
Hisham Muhammad's avatar
Hisham Muhammad committed
59
60
61
   ProcessList* pl;
   Panel* panel;
   Header* header;
62
63
64
65
} State;

}*/

66
Object* Action_pickFromVector(State* st, Panel* list, int x) {
Hisham Muhammad's avatar
Hisham Muhammad committed
67
68
69
70
   Panel* panel = st->panel;
   Header* header = st->header;
   Settings* settings = st->settings;
   
Hisham Muhammad's avatar
Hisham Muhammad committed
71
   int y = panel->y;
72
   ScreenManager* scr = ScreenManager_new(0, header->height, 0, -1, HORIZONTAL, header, settings, false);
Hisham Muhammad's avatar
Hisham Muhammad committed
73
   scr->allowFocusChange = false;
74
75
   ScreenManager_add(scr, list, x - 1);
   ScreenManager_add(scr, panel, -1);
Hisham Muhammad's avatar
Hisham Muhammad committed
76
77
78
   Panel* panelFocus;
   int ch;
   bool unfollow = false;
79
   int pid = MainPanel_selectedPid((MainPanel*)panel);
Hisham Muhammad's avatar
Hisham Muhammad committed
80
81
82
83
   if (header->pl->following == -1) {
      header->pl->following = pid;
      unfollow = true;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
84
   ScreenManager_run(scr, &panelFocus, &ch, NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
   if (unfollow) {
      header->pl->following = -1;
   }
   ScreenManager_delete(scr);
   Panel_move(panel, 0, y);
   Panel_resize(panel, COLS, LINES-y-1);
   if (panelFocus == list && ch == 13) {
      Process* selected = (Process*)Panel_getSelected(panel);
      if (selected && selected->pid == pid)
         return Panel_getSelected(list);
      else
         beep();
   }
   return NULL;
}
Hisham Muhammad's avatar
Hisham Muhammad committed
100
101
102

// ----------------------------------------

103
static void Action_runSetup(Settings* settings, const Header* header, ProcessList* pl) {
Hisham Muhammad's avatar
Hisham Muhammad committed
104
105
   ScreenManager* scr = ScreenManager_new(0, header->height, 0, -1, HORIZONTAL, header, settings, true);
   CategoriesPanel* panelCategories = CategoriesPanel_new(scr, settings, (Header*) header, pl);
106
   ScreenManager_add(scr, (Panel*) panelCategories, 16);
Hisham Muhammad's avatar
Hisham Muhammad committed
107
108
109
   CategoriesPanel_makeMetersPage(panelCategories);
   Panel* panelFocus;
   int ch;
Hisham Muhammad's avatar
Hisham Muhammad committed
110
   ScreenManager_run(scr, &panelFocus, &ch, "Setup");
Hisham Muhammad's avatar
Hisham Muhammad committed
111
   ScreenManager_delete(scr);
112
113
114
   if (settings->changed) {
      Header_writeBackToSettings(header);
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
115
116
}

117
static bool changePriority(MainPanel* panel, int delta) {
Hisham Muhammad's avatar
Hisham Muhammad committed
118
   bool anyTagged;
119
   bool ok = MainPanel_foreachProcess(panel, (MainPanel_ForeachProcessFn) Process_changePriorityBy, (Arg){ .i = delta }, &anyTagged);
Hisham Muhammad's avatar
Hisham Muhammad committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
   if (!ok)
      beep();
   return anyTagged;
}

static void addUserToVector(int key, void* userCast, void* panelCast) {
   char* user = (char*) userCast;
   Panel* panel = (Panel*) panelCast;
   Panel_add(panel, (Object*) ListItem_new(user, key));
}

bool Action_setUserOnly(const char* userName, uid_t* userId) {
   struct passwd* user = getpwnam(userName);
   if (user) {
      *userId = user->pw_uid;
      return true;
   }
   *userId = -1;
   return false;
}

static void tagAllChildren(Panel* panel, Process* parent) {
   parent->tag = true;
   pid_t ppid = parent->pid;
   for (int i = 0; i < Panel_size(panel); i++) {
      Process* p = (Process*) Panel_get(panel, i);
146
      if (!p->tag && Process_isChildOf(p, ppid)) {
Hisham Muhammad's avatar
Hisham Muhammad committed
147
148
149
150
151
152
153
154
155
156
157
158
         tagAllChildren(panel, p);
      }
   }
}

static bool expandCollapse(Panel* panel) {
   Process* p = (Process*) Panel_getSelected(panel);
   if (!p) return false;
   p->showChildren = !p->showChildren;
   return true;
}

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
static bool collapseIntoParent(Panel* panel) {
   Process* p = (Process*) Panel_getSelected(panel);
   if (!p) return false;
   pid_t ppid = Process_getParentPid(p);
   for (int i = 0; i < Panel_size(panel); i++) {
      Process* q = (Process*) Panel_get(panel, i);
      if (q->pid == ppid) {
         q->showChildren = false;
         Panel_setSelected(panel, i);
         return true;
      }
   }
   return false;
}

174
Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey) {
175
176
177
178
   ScreenSettings* ss = settings->ss;
   ss->sortKey = sortKey;
   ss->direction = 1;
   ss->treeView = false;
179
   return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_UPDATE_PANELHDR | HTOP_KEEP_FOLLOWING;
Hisham Muhammad's avatar
Hisham Muhammad committed
180
181
182
183
}

static Htop_Reaction sortBy(State* st) {
   Htop_Reaction reaction = HTOP_OK;
Hisham Muhammad's avatar
Hisham Muhammad committed
184
   Panel* sortPanel = Panel_new(0, 0, 0, 0, true, Class(ListItem), FunctionBar_newEnterEsc("Sort   ", "Cancel "));
Hisham Muhammad's avatar
Hisham Muhammad committed
185
   Panel_setHeader(sortPanel, "Sort by");
186
187
   ScreenSettings* ss = st->settings->ss;
   ProcessField* fields = ss->fields;
Hisham Muhammad's avatar
Hisham Muhammad committed
188
189
190
   for (int i = 0; fields[i]; i++) {
      char* name = String_trim(Process_fields[fields[i]].name);
      Panel_add(sortPanel, (Object*) ListItem_new(name, fields[i]));
191
      if (fields[i] == ss->sortKey)
Hisham Muhammad's avatar
Hisham Muhammad committed
192
193
194
         Panel_setSelected(sortPanel, i);
      free(name);
   }
195
   ListItem* field = (ListItem*) Action_pickFromVector(st, sortPanel, 15);
Hisham Muhammad's avatar
Hisham Muhammad committed
196
   if (field) {
197
      reaction |= Action_setSortKey(st->settings, field->key);
Hisham Muhammad's avatar
Hisham Muhammad committed
198
199
200
201
202
203
204
205
   }
   Object_delete(sortPanel);
   return reaction | HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
}

// ----------------------------------------

static Htop_Reaction actionResize(State* st) {
206
   clear();
Hisham Muhammad's avatar
Hisham Muhammad committed
207
208
209
210
211
   Panel_resize(st->panel, COLS, LINES-(st->panel->y)-1);
   return HTOP_REDRAW_BAR;
}

static Htop_Reaction actionSortByMemory(State* st) {
212
   return Action_setSortKey(st->settings, PERCENT_MEM);
Hisham Muhammad's avatar
Hisham Muhammad committed
213
214
215
}

static Htop_Reaction actionSortByCPU(State* st) {
216
   return Action_setSortKey(st->settings, PERCENT_CPU);
Hisham Muhammad's avatar
Hisham Muhammad committed
217
218
219
}

static Htop_Reaction actionSortByTime(State* st) {
220
   return Action_setSortKey(st->settings, TIME);
Hisham Muhammad's avatar
Hisham Muhammad committed
221
222
223
224
225
226
227
228
229
230
231
232
233
}

static Htop_Reaction actionToggleKernelThreads(State* st) {
   st->settings->hideKernelThreads = !st->settings->hideKernelThreads;
   return HTOP_RECALCULATE | HTOP_SAVE_SETTINGS;
}

static Htop_Reaction actionToggleUserlandThreads(State* st) {
   st->settings->hideUserlandThreads = !st->settings->hideUserlandThreads;
   st->settings->hideThreads = st->settings->hideUserlandThreads;
   return HTOP_RECALCULATE | HTOP_SAVE_SETTINGS;
}

234
235
236
237
238
static Htop_Reaction actionToggleProgramPath(State* st) {
   st->settings->showProgramPath = !st->settings->showProgramPath;
   return HTOP_REFRESH | HTOP_SAVE_SETTINGS;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
239
static Htop_Reaction actionToggleTreeView(State* st) {
240
241
242
   ScreenSettings* ss = st->settings->ss;
   ss->treeView = !ss->treeView;
   if (ss->treeView) ss->direction = 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
243
244
245
246
247
   ProcessList_expandTree(st->pl);
   return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
}

static Htop_Reaction actionIncFilter(State* st) {
248
   IncSet* inc = ((MainPanel*)st->panel)->inc;
249
   IncSet_activate(inc, INC_FILTER, st->panel);
250
   st->pl->incFilter = IncSet_filter(inc);
Hisham Muhammad's avatar
Hisham Muhammad committed
251
252
253
254
   return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
}

static Htop_Reaction actionIncSearch(State* st) {
255
   IncSet_activate(((MainPanel*)st->panel)->inc, INC_SEARCH, st->panel);
Hisham Muhammad's avatar
Hisham Muhammad committed
256
257
258
259
   return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
}

static Htop_Reaction actionHigherPriority(State* st) {
260
   bool changed = changePriority((MainPanel*)st->panel, -1);
Hisham Muhammad's avatar
Hisham Muhammad committed
261
262
263
264
   return changed ? HTOP_REFRESH : HTOP_OK;
}

static Htop_Reaction actionLowerPriority(State* st) {
265
   bool changed = changePriority((MainPanel*)st->panel, 1);
Hisham Muhammad's avatar
Hisham Muhammad committed
266
267
268
269
   return changed ? HTOP_REFRESH : HTOP_OK;
}

static Htop_Reaction actionInvertSortOrder(State* st) {
270
   ScreenSettings_invertSortOrder(st->settings->ss);
Hisham Muhammad's avatar
Hisham Muhammad committed
271
272
273
274
275
276
277
278
279
280
281
282
   return HTOP_REFRESH | HTOP_SAVE_SETTINGS;
}

static Htop_Reaction actionSetSortColumn(State* st) {
   return sortBy(st);
}

static Htop_Reaction actionExpandOrCollapse(State* st) {
   bool changed = expandCollapse(st->panel);
   return changed ? HTOP_RECALCULATE : HTOP_OK;
}

283
static Htop_Reaction actionCollapseIntoParent(State* st) {
284
   if (!st->settings->ss->treeView) {
285
286
287
288
289
290
      return HTOP_OK;
   }
   bool changed = collapseIntoParent(st->panel);
   return changed ? HTOP_RECALCULATE : HTOP_OK;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
291
static Htop_Reaction actionExpandCollapseOrSortColumn(State* st) {
292
   return st->settings->ss->treeView ? actionExpandOrCollapse(st) : actionSetSortColumn(st);
Hisham Muhammad's avatar
Hisham Muhammad committed
293
294
295
296
297
298
}

static Htop_Reaction actionQuit() {
   return HTOP_QUIT;
}

299
300
301
302
303
304
305
306
307
308
static Htop_Reaction actionNextScreen(State* st) {
   Settings* settings = st->settings;
   settings->ssIndex++;
   if (settings->ssIndex == settings->nScreens) {
      settings->ssIndex = 0;
   }
   settings->ss = settings->screens[settings->ssIndex];
   return HTOP_REFRESH;
}

309
310
static Htop_Reaction actionPrevScreen(State* st) {
   Settings* settings = st->settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
311
   if (settings->ssIndex == 0) {
312
      settings->ssIndex = settings->nScreens - 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
313
314
   } else {
      settings->ssIndex--;
315
316
317
318
319
   }
   settings->ss = settings->screens[settings->ssIndex];
   return HTOP_REFRESH;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
Htop_Reaction Action_setScreenTab(Settings* settings, int x) {
   int s = 2;
   for (unsigned int i = 0; i < settings->nScreens; i++) {
      if (x < s) {
         return 0;
      }
      const char* name = settings->screens[i]->name;
      int len = strlen(name);
      if (x <= s + len + 1) {
         settings->ssIndex = i;
         settings->ss = settings->screens[i];
         return HTOP_REFRESH;
      }
      s += len + 3;
   }
   return 0;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
338
339
340
static Htop_Reaction actionSetAffinity(State* st) {
   if (st->pl->cpuCount == 1)
      return HTOP_OK;
Hisham's avatar
Hisham committed
341
#if (HAVE_LIBHWLOC || HAVE_LINUX_AFFINITY)
Hisham Muhammad's avatar
Hisham Muhammad committed
342
343
344
345
346
347
348
349
350
   Panel* panel = st->panel;
   
   Process* p = (Process*) Panel_getSelected(panel);
   if (!p) return HTOP_OK;
   Affinity* affinity = Affinity_get(p, st->pl);
   if (!affinity) return HTOP_OK;
   Panel* affinityPanel = AffinityPanel_new(st->pl, affinity);
   Affinity_delete(affinity);

351
   void* set = Action_pickFromVector(st, affinityPanel, 15);
Hisham Muhammad's avatar
Hisham Muhammad committed
352
353
   if (set) {
      Affinity* affinity = AffinityPanel_getAffinity(affinityPanel, st->pl);
354
      bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) Affinity_set, (Arg){ .v = affinity }, NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
355
356
357
358
359
360
361
362
363
364
      if (!ok) beep();
      Affinity_delete(affinity);
   }
   Panel_delete((Object*)affinityPanel);
#endif
   return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
}

static Htop_Reaction actionKill(State* st) {
   Panel* signalsPanel = (Panel*) SignalsPanel_new();
365
   ListItem* sgn = (ListItem*) Action_pickFromVector(st, signalsPanel, 15);
Hisham Muhammad's avatar
Hisham Muhammad committed
366
367
368
369
370
   if (sgn) {
      if (sgn->key != 0) {
         Panel_setHeader(st->panel, "Sending...");
         Panel_draw(st->panel, true);
         refresh();
371
         MainPanel_foreachProcess((MainPanel*)st->panel, (MainPanel_ForeachProcessFn) Process_sendSignal, (Arg){ .i = sgn->key }, NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
372
373
374
375
376
377
378
379
         napms(500);
      }
   }
   Panel_delete((Object*)signalsPanel);
   return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
}

static Htop_Reaction actionFilterByUser(State* st) {
Hisham Muhammad's avatar
Hisham Muhammad committed
380
   Panel* usersPanel = Panel_new(0, 0, 0, 0, true, Class(ListItem), FunctionBar_newEnterEsc("Show   ", "Cancel "));
Hisham Muhammad's avatar
Hisham Muhammad committed
381
382
383
384
385
   Panel_setHeader(usersPanel, "Show processes of:");
   UsersTable_foreach(st->ut, addUserToVector, usersPanel);
   Vector_insertionSort(usersPanel->items);
   ListItem* allUsers = ListItem_new("All users", -1);
   Panel_insert(usersPanel, 0, (Object*) allUsers);
386
   ListItem* picked = (ListItem*) Action_pickFromVector(st, usersPanel, 20);
Hisham Muhammad's avatar
Hisham Muhammad committed
387
388
389
390
391
392
393
394
395
396
397
   if (picked) {
      if (picked == allUsers) {
         st->pl->userId = -1;
      } else {
         Action_setUserOnly(ListItem_getRef(picked), &(st->pl->userId));
      }
   }
   Panel_delete((Object*)usersPanel);
   return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
}

Hisham's avatar
Hisham committed
398
Htop_Reaction Action_follow(State* st) {
399
   st->pl->following = MainPanel_selectedPid((MainPanel*)st->panel);
400
   Panel_setSelectionColor(st->panel, CRT_colors[PANEL_SELECTION_FOLLOW]);
Hisham Muhammad's avatar
Hisham Muhammad committed
401
402
403
404
   return HTOP_KEEP_FOLLOWING;
}

static Htop_Reaction actionSetup(State* st) {
405
   Action_runSetup(st->settings, st->header, st->pl);
Hisham Muhammad's avatar
Hisham Muhammad committed
406
407
408
409
410
411
412
413
414
415
   // TODO: shouldn't need this, colors should be dynamic
   int headerHeight = Header_calculateHeight(st->header);
   Panel_move(st->panel, 0, headerHeight);
   Panel_resize(st->panel, COLS, LINES-headerHeight-1);
   return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
}

static Htop_Reaction actionLsof(State* st) {
   Process* p = (Process*) Panel_getSelected(st->panel);
   if (!p) return HTOP_OK;
416
417
418
   OpenFilesScreen* ofs = OpenFilesScreen_new(p);
   InfoScreen_run((InfoScreen*)ofs);
   OpenFilesScreen_delete((Object*)ofs);
Hisham Muhammad's avatar
Hisham Muhammad committed
419
420
421
422
423
424
425
426
427
   clear();
   CRT_enableDelay();
   return HTOP_REFRESH | HTOP_REDRAW_BAR;
}

static Htop_Reaction actionStrace(State* st) {
   Process* p = (Process*) Panel_getSelected(st->panel);
   if (!p) return HTOP_OK;
   TraceScreen* ts = TraceScreen_new(p);
428
429
430
431
432
   bool ok = TraceScreen_forkTracer(ts);
   if (ok) {
      InfoScreen_run((InfoScreen*)ts);
   }
   TraceScreen_delete((Object*)ts);
Hisham Muhammad's avatar
Hisham Muhammad committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
   clear();
   CRT_enableDelay();
   return HTOP_REFRESH | HTOP_REDRAW_BAR;
}

static Htop_Reaction actionTag(State* st) {
   Process* p = (Process*) Panel_getSelected(st->panel);
   if (!p) return HTOP_OK;
   Process_toggleTag(p);
   Panel_onKey(st->panel, KEY_DOWN);
   return HTOP_OK;
}

static Htop_Reaction actionRedraw() {
   clear();
   return HTOP_REFRESH | HTOP_REDRAW_BAR;
}

Richard's avatar
Richard committed
451
static const struct { const char* key; const char* info; } helpLeft[] = {
Hisham Muhammad's avatar
Hisham Muhammad committed
452
453
454
455
456
   { .key = " Arrows: ", .info = "scroll process list" },
   { .key = " Digits: ", .info = "incremental PID search" },
   { .key = "   F3 /: ", .info = "incremental name search" },
   { .key = "   F4 \\: ",.info = "incremental name filtering" },
   { .key = "   F5 t: ", .info = "tree view" },
Marco Hinz's avatar
Marco Hinz committed
457
   { .key = "      p: ", .info = "toggle program path" },
Hisham Muhammad's avatar
Hisham Muhammad committed
458
   { .key = "      u: ", .info = "show processes of a single user" },
Hisham's avatar
Hisham committed
459
   { .key = "      H: ", .info = "hide/show user process threads" },
Hisham Muhammad's avatar
Hisham Muhammad committed
460
461
462
463
464
   { .key = "      K: ", .info = "hide/show kernel threads" },
   { .key = "      F: ", .info = "cursor follows process" },
   { .key = " F6 + -: ", .info = "expand/collapse tree" },
   { .key = "  P M T: ", .info = "sort by CPU%, MEM% or TIME" },
   { .key = "      I: ", .info = "invert sort order" },
465
   { .key = " F6 > .: ", .info = "select sort column" },
Hisham Muhammad's avatar
Hisham Muhammad committed
466
467
468
   { .key = NULL, .info = NULL }
};

Richard's avatar
Richard committed
469
static const struct { const char* key; const char* info; } helpRight[] = {
Hisham Muhammad's avatar
Hisham Muhammad committed
470
471
472
473
474
475
   { .key = "  Space: ", .info = "tag process" },
   { .key = "      c: ", .info = "tag process and its children" },
   { .key = "      U: ", .info = "untag all processes" },
   { .key = "   F9 k: ", .info = "kill process/tagged processes" },
   { .key = "   F7 ]: ", .info = "higher priority (root only)" },
   { .key = "   F8 [: ", .info = "lower priority (+ nice)" },
Hisham's avatar
Hisham committed
476
#if (HAVE_LIBHWLOC || HAVE_LINUX_AFFINITY)
Hisham Muhammad's avatar
Hisham Muhammad committed
477
478
   { .key = "      a: ", .info = "set CPU affinity" },
#endif
479
   { .key = "      e: ", .info = "show process environment" },
Vasyl Boroviak's avatar
Vasyl Boroviak committed
480
   { .key = "      i: ", .info = "set IO priority" },
Hisham Muhammad's avatar
Hisham Muhammad committed
481
482
483
   { .key = "      l: ", .info = "list open files with lsof" },
   { .key = "      s: ", .info = "trace syscalls with strace" },
   { .key = "         ", .info = "" },
ryenus's avatar
ryenus committed
484
   { .key = " F2 C S: ", .info = "setup" },
Hisham Muhammad's avatar
Hisham Muhammad committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
   { .key = "   F1 h: ", .info = "show this help screen" },
   { .key = "  F10 q: ", .info = "quit" },
   { .key = NULL, .info = NULL }
};

static Htop_Reaction actionHelp(State* st) {
   Settings* settings = st->settings;

   clear();
   attrset(CRT_colors[HELP_BOLD]);

   for (int i = 0; i < LINES-1; i++)
      mvhline(i, 0, ' ', COLS);

   mvaddstr(0, 0, "htop " VERSION " - " COPYRIGHT);
   mvaddstr(1, 0, "Released under the GNU GPL. See 'man' page for more info.");

   attrset(CRT_colors[DEFAULT_COLOR]);
   mvaddstr(3, 0, "CPU usage bar: ");
   #define addattrstr(a,s) attrset(a);addstr(s)
   addattrstr(CRT_colors[BAR_BORDER], "[");
   if (settings->detailedCPUTime) {
      addattrstr(CRT_colors[CPU_NICE_TEXT], "low"); addstr("/");
      addattrstr(CRT_colors[CPU_NORMAL], "normal"); addstr("/");
      addattrstr(CRT_colors[CPU_KERNEL], "kernel"); addstr("/");
      addattrstr(CRT_colors[CPU_IRQ], "irq"); addstr("/");
      addattrstr(CRT_colors[CPU_SOFTIRQ], "soft-irq"); addstr("/");
      addattrstr(CRT_colors[CPU_STEAL], "steal"); addstr("/");
      addattrstr(CRT_colors[CPU_GUEST], "guest"); addstr("/");
      addattrstr(CRT_colors[CPU_IOWAIT], "io-wait");
      addattrstr(CRT_colors[BAR_SHADOW], " used%");
   } else {
      addattrstr(CRT_colors[CPU_NICE_TEXT], "low-priority"); addstr("/");
      addattrstr(CRT_colors[CPU_NORMAL], "normal"); addstr("/");
      addattrstr(CRT_colors[CPU_KERNEL], "kernel"); addstr("/");
520
      addattrstr(CRT_colors[CPU_GUEST], "virtualiz");
Hisham Muhammad's avatar
Hisham Muhammad committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
      addattrstr(CRT_colors[BAR_SHADOW], "               used%");
   }
   addattrstr(CRT_colors[BAR_BORDER], "]");
   attrset(CRT_colors[DEFAULT_COLOR]);
   mvaddstr(4, 0, "Memory bar:    ");
   addattrstr(CRT_colors[BAR_BORDER], "[");
   addattrstr(CRT_colors[MEMORY_USED], "used"); addstr("/");
   addattrstr(CRT_colors[MEMORY_BUFFERS_TEXT], "buffers"); addstr("/");
   addattrstr(CRT_colors[MEMORY_CACHE], "cache");
   addattrstr(CRT_colors[BAR_SHADOW], "                            used/total");
   addattrstr(CRT_colors[BAR_BORDER], "]");
   attrset(CRT_colors[DEFAULT_COLOR]);
   mvaddstr(5, 0, "Swap bar:      ");
   addattrstr(CRT_colors[BAR_BORDER], "[");
   addattrstr(CRT_colors[SWAP], "used");
   addattrstr(CRT_colors[BAR_SHADOW], "                                          used/total");
   addattrstr(CRT_colors[BAR_BORDER], "]");
   attrset(CRT_colors[DEFAULT_COLOR]);
   mvaddstr(6,0, "Type and layout of header meters are configurable in the setup screen.");
   if (CRT_colorScheme == COLORSCHEME_MONOCHROME) {
541
      mvaddstr(7, 0, "In monochrome, meters display as different chars, in order: |#*@$%&.");
Hisham Muhammad's avatar
Hisham Muhammad committed
542
543
544
545
546
547
548
   }
   mvaddstr( 8, 0, " Status: R: running; S: sleeping; T: traced/stopped; Z: zombie; D: disk sleep");
   for (int i = 0; helpLeft[i].info; i++) { mvaddstr(9+i, 9,  helpLeft[i].info); }
   for (int i = 0; helpRight[i].info; i++) { mvaddstr(9+i, 49, helpRight[i].info); }
   attrset(CRT_colors[HELP_BOLD]);
   for (int i = 0; helpLeft[i].key;  i++) { mvaddstr(9+i, 0,  helpLeft[i].key); }
   for (int i = 0; helpRight[i].key; i++) { mvaddstr(9+i, 40, helpRight[i].key); }
Hisham's avatar
Hisham committed
549
   attrset(CRT_colors[PROCESS_THREAD]);
Marco Hinz's avatar
Marco Hinz committed
550
551
   mvaddstr(16, 32, "threads");
   mvaddstr(17, 26, "threads");
Hisham's avatar
Hisham committed
552
   attrset(CRT_colors[DEFAULT_COLOR]);
Hisham Muhammad's avatar
Hisham Muhammad committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

   attrset(CRT_colors[HELP_BOLD]);
   mvaddstr(23,0, "Press any key to return.");
   attrset(CRT_colors[DEFAULT_COLOR]);
   refresh();
   CRT_readKey();
   clear();

   return HTOP_RECALCULATE | HTOP_REDRAW_BAR;
}

static Htop_Reaction actionUntagAll(State* st) {
   for (int i = 0; i < Panel_size(st->panel); i++) {
      Process* p = (Process*) Panel_get(st->panel, i);
      p->tag = false;
   }
   return HTOP_REFRESH;
}

static Htop_Reaction actionTagAllChildren(State* st) {
   Process* p = (Process*) Panel_getSelected(st->panel);
   if (!p) return HTOP_OK;
   tagAllChildren(st->panel, p);
   return HTOP_OK;
}

579
580
581
static Htop_Reaction actionShowEnvScreen(State* st) {
   Process* p = (Process*) Panel_getSelected(st->panel);
   if (!p) return HTOP_OK;
582
583
584
   EnvScreen* es = EnvScreen_new(p);
   InfoScreen_run((InfoScreen*)es);
   EnvScreen_delete((Object*)es);
585
586
587
588
589
590
   clear();
   CRT_enableDelay();
   return HTOP_REFRESH | HTOP_REDRAW_BAR;
}


Hisham Muhammad's avatar
Hisham Muhammad committed
591
592
593
594
595
596
597
void Action_setBindings(Htop_Action* keys) {
   keys[KEY_RESIZE] = actionResize;
   keys['M'] = actionSortByMemory;
   keys['T'] = actionSortByTime;
   keys['P'] = actionSortByCPU;
   keys['H'] = actionToggleUserlandThreads;
   keys['K'] = actionToggleKernelThreads;
598
   keys['p'] = actionToggleProgramPath;
Hisham Muhammad's avatar
Hisham Muhammad committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
   keys['t'] = actionToggleTreeView;
   keys[KEY_F(5)] = actionToggleTreeView;
   keys[KEY_F(4)] = actionIncFilter;
   keys['\\'] = actionIncFilter;
   keys[KEY_F(3)] = actionIncSearch;
   keys['/'] = actionIncSearch;

   keys[']'] = actionHigherPriority;
   keys[KEY_F(7)] = actionHigherPriority;
   keys['['] = actionLowerPriority;
   keys[KEY_F(8)] = actionLowerPriority;
   keys['I'] = actionInvertSortOrder;
   keys[KEY_F(6)] = actionExpandCollapseOrSortColumn;
   keys[KEY_F(18)] = actionExpandCollapseOrSortColumn;
   keys['<'] = actionSetSortColumn;
   keys[','] = actionSetSortColumn;
   keys['>'] = actionSetSortColumn;
   keys['.'] = actionSetSortColumn;
   keys[KEY_F(10)] = actionQuit;
   keys['q'] = actionQuit;
   keys['a'] = actionSetAffinity;
   keys[KEY_F(9)] = actionKill;
   keys['k'] = actionKill;
622
   keys[KEY_RECLICK] = actionExpandOrCollapse;
Hisham Muhammad's avatar
Hisham Muhammad committed
623
624
625
   keys['+'] = actionExpandOrCollapse;
   keys['='] = actionExpandOrCollapse;
   keys['-'] = actionExpandOrCollapse;
626
   keys['\177'] = actionCollapseIntoParent;
Hisham Muhammad's avatar
Hisham Muhammad committed
627
   keys['u'] = actionFilterByUser;
Hisham's avatar
Hisham committed
628
   keys['F'] = Action_follow;
Hisham Muhammad's avatar
Hisham Muhammad committed
629
630
631
632
633
634
635
636
637
638
639
640
   keys['S'] = actionSetup;
   keys['C'] = actionSetup;
   keys[KEY_F(2)] = actionSetup;
   keys['l'] = actionLsof;
   keys['s'] = actionStrace;
   keys[' '] = actionTag;
   keys['\014'] = actionRedraw; // Ctrl+L
   keys[KEY_F(1)] = actionHelp;
   keys['h'] = actionHelp;
   keys['?'] = actionHelp;
   keys['U'] = actionUntagAll;
   keys['c'] = actionTagAllChildren;
641
   keys['e'] = actionShowEnvScreen;
642
   keys['\t'] = actionNextScreen;
643
   keys[KEY_SHIFT_TAB] = actionPrevScreen;
Hisham Muhammad's avatar
Hisham Muhammad committed
644
645
}