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

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

David Hunt's avatar
David Hunt committed
11
#include "StringUtils.h"
12
#include "RichString.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13

14
#include <stdio.h>
15
#include <errno.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
16
17
#include <signal.h>
#include <stdlib.h>
18
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
19
#include <locale.h>
Christian Hesse's avatar
Christian Hesse committed
20
#include <langinfo.h>
21
22
23
24
#if HAVE_SETUID_ENABLED
#include <unistd.h>
#include <sys/types.h>
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
25

26
27
28
#define ColorIndex(i,j) ((7-i)*8+j)

#define ColorPair(i,j) COLOR_PAIR(ColorIndex(i,j))
Hisham Muhammad's avatar
Hisham Muhammad committed
29
30
31
32
33
34
35
36
37
38

#define Black COLOR_BLACK
#define Red COLOR_RED
#define Green COLOR_GREEN
#define Yellow COLOR_YELLOW
#define Blue COLOR_BLUE
#define Magenta COLOR_MAGENTA
#define Cyan COLOR_CYAN
#define White COLOR_WHITE

39
#define ColorPairGrayBlack ColorPair(Magenta,Magenta)
40
#define ColorIndexGrayBlack ColorIndex(Magenta,Magenta)
41

42
43
#define KEY_WHEELUP KEY_F(20)
#define KEY_WHEELDOWN KEY_F(21)
44
#define KEY_RECLICK KEY_F(22)
45
#define KEY_SHIFT_TAB KEY_F(23)
46

Hisham Muhammad's avatar
Hisham Muhammad committed
47
48
49
//#link curses

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
50
#include <stdbool.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
51

Hisham Muhammad's avatar
Hisham Muhammad committed
52
53
54
55
56
57
58
59
60
61
62
typedef enum TreeStr_ {
   TREE_STR_HORZ,
   TREE_STR_VERT,
   TREE_STR_RTEE,
   TREE_STR_BEND,
   TREE_STR_TEND,
   TREE_STR_OPEN,
   TREE_STR_SHUT,
   TREE_STR_COUNT
} TreeStr;

63
64
65
66
67
68
typedef enum ColorSchemes_ {
   COLORSCHEME_DEFAULT = 0,
   COLORSCHEME_MONOCHROME = 1,
   COLORSCHEME_BLACKONWHITE = 2,
   COLORSCHEME_LIGHTTERMINAL = 3,
   COLORSCHEME_MIDNIGHT = 4,
69
   COLORSCHEME_BLACKNIGHT = 5,
70
71
   COLORSCHEME_BROKENGRAY = 6,
   LAST_COLORSCHEME = 7,
72
73
} ColorSchemes;

Hisham Muhammad's avatar
Hisham Muhammad committed
74
75
76
77
78
79
80
81
typedef enum ColorElements_ {
   RESET_COLOR,
   DEFAULT_COLOR,
   FUNCTION_BAR,
   FUNCTION_KEY,
   FAILED_SEARCH,
   PANEL_HEADER_FOCUS,
   PANEL_HEADER_UNFOCUS,
82
83
84
   PANEL_SELECTION_FOCUS,
   PANEL_SELECTION_FOLLOW,
   PANEL_SELECTION_UNFOCUS,
Hisham Muhammad's avatar
Hisham Muhammad committed
85
86
87
88
89
   LARGE_NUMBER,
   METER_TEXT,
   METER_VALUE,
   LED_COLOR,
   UPTIME,
Hisham Muhammad's avatar
Hisham Muhammad committed
90
   BATTERY,
Hisham Muhammad's avatar
Hisham Muhammad committed
91
92
93
94
95
96
97
98
   TASKS_RUNNING,
   SWAP,
   PROCESS,
   PROCESS_SHADOW,
   PROCESS_TAG,
   PROCESS_MEGABYTES,
   PROCESS_TREE,
   PROCESS_R_STATE,
99
   PROCESS_D_STATE,
Hisham Muhammad's avatar
Hisham Muhammad committed
100
101
102
   PROCESS_BASENAME,
   PROCESS_HIGH_PRIORITY,
   PROCESS_LOW_PRIORITY,
103
104
   PROCESS_THREAD,
   PROCESS_THREAD_BASENAME,
Hisham Muhammad's avatar
Hisham Muhammad committed
105
106
107
108
109
110
   BAR_BORDER,
   BAR_SHADOW,
   GRAPH_1,
   GRAPH_2,
   MEMORY_USED,
   MEMORY_BUFFERS,
111
   MEMORY_BUFFERS_TEXT,
Hisham Muhammad's avatar
Hisham Muhammad committed
112
113
114
115
116
117
118
119
120
   MEMORY_CACHE,
   LOAD,
   LOAD_AVERAGE_FIFTEEN,
   LOAD_AVERAGE_FIVE,
   LOAD_AVERAGE_ONE,
   CHECK_BOX,
   CHECK_MARK,
   CHECK_TEXT,
   CLOCK,
121
122
   HELP_BOLD,
   HOSTNAME,
Hisham Muhammad's avatar
Hisham Muhammad committed
123
   CPU_NICE,
124
   CPU_NICE_TEXT,
Hisham Muhammad's avatar
Hisham Muhammad committed
125
126
   CPU_NORMAL,
   CPU_KERNEL,
127
128
129
   CPU_IOWAIT,
   CPU_IRQ,
   CPU_SOFTIRQ,
130
131
   CPU_STEAL,
   CPU_GUEST,
132
   PANEL_EDIT,
Hisham Muhammad's avatar
Hisham Muhammad committed
133
134
135
136
   SCREENS_OTH_BORDER,
   SCREENS_OTH_TEXT,
   SCREENS_CUR_BORDER,
   SCREENS_CUR_TEXT,
Hisham Muhammad's avatar
Hisham Muhammad committed
137
138
139
   LAST_COLORELEMENT
} ColorElements;

140
141
void CRT_fatalError(const char* note) __attribute__ ((noreturn));

142
143
void CRT_handleSIGSEGV(int sgn);

144
#define KEY_ALT(x) (KEY_F(64 - 26) + (x - 'A'))
145

Hisham Muhammad's avatar
Hisham Muhammad committed
146
147
}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
148
149
150
151
152
153
154
155
156
157
const char *CRT_treeStrAscii[TREE_STR_COUNT] = {
   "-", // TREE_STR_HORZ
   "|", // TREE_STR_VERT
   "`", // TREE_STR_RTEE
   "`", // TREE_STR_BEND
   ",", // TREE_STR_TEND
   "+", // TREE_STR_OPEN
   "-", // TREE_STR_SHUT
};

158
159
#ifdef HAVE_LIBNCURSESW

Hisham Muhammad's avatar
Hisham Muhammad committed
160
161
162
163
164
165
166
167
168
169
const char *CRT_treeStrUtf8[TREE_STR_COUNT] = {
   "\xe2\x94\x80", // TREE_STR_HORZ ─
   "\xe2\x94\x82", // TREE_STR_VERT │
   "\xe2\x94\x9c", // TREE_STR_RTEE ├
   "\xe2\x94\x94", // TREE_STR_BEND └
   "\xe2\x94\x8c", // TREE_STR_TEND ┌
   "+",            // TREE_STR_OPEN +
   "\xe2\x94\x80", // TREE_STR_SHUT ─
};

170
171
172
173
bool CRT_utf8 = false;

#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
174
const char **CRT_treeStr = CRT_treeStrAscii;
Hisham Muhammad's avatar
Hisham Muhammad committed
175

Hisham Muhammad's avatar
Hisham Muhammad committed
176
177
static bool CRT_hasColors;

178
int CRT_delay = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
179

180
181
182
183
184
185
186
187
188
189
int* CRT_colors;

int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
   [COLORSCHEME_DEFAULT] = {
      [RESET_COLOR] = ColorPair(White,Black),
      [DEFAULT_COLOR] = ColorPair(White,Black),
      [FUNCTION_BAR] = ColorPair(Black,Cyan),
      [FUNCTION_KEY] = ColorPair(White,Black),
      [PANEL_HEADER_FOCUS] = ColorPair(Black,Green),
      [PANEL_HEADER_UNFOCUS] = ColorPair(Black,Green),
190
191
192
      [PANEL_SELECTION_FOCUS] = ColorPair(Black,Cyan),
      [PANEL_SELECTION_FOLLOW] = ColorPair(Black,Yellow),
      [PANEL_SELECTION_UNFOCUS] = ColorPair(Black,White),
193
194
195
196
197
198
199
200
201
      [FAILED_SEARCH] = ColorPair(Red,Cyan),
      [UPTIME] = A_BOLD | ColorPair(Cyan,Black),
      [BATTERY] = A_BOLD | ColorPair(Cyan,Black),
      [LARGE_NUMBER] = A_BOLD | ColorPair(Red,Black),
      [METER_TEXT] = ColorPair(Cyan,Black),
      [METER_VALUE] = A_BOLD | ColorPair(Cyan,Black),
      [LED_COLOR] = ColorPair(Green,Black),
      [TASKS_RUNNING] = A_BOLD | ColorPair(Green,Black),
      [PROCESS] = A_NORMAL,
202
      [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
203
204
205
206
207
208
209
      [PROCESS_TAG] = A_BOLD | ColorPair(Yellow,Black),
      [PROCESS_MEGABYTES] = ColorPair(Cyan,Black),
      [PROCESS_BASENAME] = A_BOLD | ColorPair(Cyan,Black),
      [PROCESS_TREE] = ColorPair(Cyan,Black),
      [PROCESS_R_STATE] = ColorPair(Green,Black),
      [PROCESS_D_STATE] = A_BOLD | ColorPair(Red,Black),
      [PROCESS_HIGH_PRIORITY] = ColorPair(Red,Black),
210
      [PROCESS_LOW_PRIORITY] = ColorPair(Green,Black),
211
212
213
      [PROCESS_THREAD] = ColorPair(Green,Black),
      [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Green,Black),
      [BAR_BORDER] = A_BOLD,
214
      [BAR_SHADOW] = A_BOLD | ColorPairGrayBlack,
215
216
217
218
219
220
221
      [SWAP] = ColorPair(Red,Black),
      [GRAPH_1] = A_BOLD | ColorPair(Cyan,Black),
      [GRAPH_2] = ColorPair(Cyan,Black),
      [MEMORY_USED] = ColorPair(Green,Black),
      [MEMORY_BUFFERS] = ColorPair(Blue,Black),
      [MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Blue,Black),
      [MEMORY_CACHE] = ColorPair(Yellow,Black),
222
223
224
      [LOAD_AVERAGE_FIFTEEN] = ColorPair(Cyan,Black),
      [LOAD_AVERAGE_FIVE] = A_BOLD | ColorPair(Cyan,Black),
      [LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(White,Black),
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
      [LOAD] = A_BOLD,
      [HELP_BOLD] = A_BOLD | ColorPair(Cyan,Black),
      [CLOCK] = A_BOLD,
      [CHECK_BOX] = ColorPair(Cyan,Black),
      [CHECK_MARK] = A_BOLD,
      [CHECK_TEXT] = A_NORMAL,
      [HOSTNAME] = A_BOLD,
      [CPU_NICE] = ColorPair(Blue,Black),
      [CPU_NICE_TEXT] = A_BOLD | ColorPair(Blue,Black),
      [CPU_NORMAL] = ColorPair(Green,Black),
      [CPU_KERNEL] = ColorPair(Red,Black),
      [CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black),
      [CPU_IRQ] = ColorPair(Yellow,Black),
      [CPU_SOFTIRQ] = ColorPair(Magenta,Black),
      [CPU_STEAL] = ColorPair(Cyan,Black),
      [CPU_GUEST] = ColorPair(Cyan,Black),
241
      [PANEL_EDIT] = ColorPair(White,Blue),
Hisham Muhammad's avatar
Hisham Muhammad committed
242
243
244
245
      [SCREENS_OTH_BORDER] = ColorPair(Blue,Blue),
      [SCREENS_OTH_TEXT] = ColorPair(Black,Blue),
      [SCREENS_CUR_BORDER] = ColorPair(Green,Green),
      [SCREENS_CUR_TEXT] = ColorPair(Black,Green),
246
247
248
249
250
251
252
253
   },
   [COLORSCHEME_MONOCHROME] = {
      [RESET_COLOR] = A_NORMAL,
      [DEFAULT_COLOR] = A_NORMAL,
      [FUNCTION_BAR] = A_REVERSE,
      [FUNCTION_KEY] = A_NORMAL,
      [PANEL_HEADER_FOCUS] = A_REVERSE,
      [PANEL_HEADER_UNFOCUS] = A_REVERSE,
254
255
256
      [PANEL_SELECTION_FOCUS] = A_REVERSE,
      [PANEL_SELECTION_FOLLOW] = A_REVERSE,
      [PANEL_SELECTION_UNFOCUS] = A_BOLD,
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
      [FAILED_SEARCH] = A_REVERSE | A_BOLD,
      [UPTIME] = A_BOLD,
      [BATTERY] = A_BOLD,
      [LARGE_NUMBER] = A_BOLD,
      [METER_TEXT] = A_NORMAL,
      [METER_VALUE] = A_BOLD,
      [LED_COLOR] = A_NORMAL,
      [TASKS_RUNNING] = A_BOLD,
      [PROCESS] = A_NORMAL,
      [PROCESS_SHADOW] = A_DIM,
      [PROCESS_TAG] = A_BOLD,
      [PROCESS_MEGABYTES] = A_BOLD,
      [PROCESS_BASENAME] = A_BOLD,
      [PROCESS_TREE] = A_BOLD,
      [PROCESS_R_STATE] = A_BOLD,
      [PROCESS_D_STATE] = A_BOLD,
      [PROCESS_HIGH_PRIORITY] = A_BOLD,
      [PROCESS_LOW_PRIORITY] = A_DIM,
      [PROCESS_THREAD] = A_BOLD,
      [PROCESS_THREAD_BASENAME] = A_REVERSE,
      [BAR_BORDER] = A_BOLD,
      [BAR_SHADOW] = A_DIM,
      [SWAP] = A_BOLD,
      [GRAPH_1] = A_BOLD,
      [GRAPH_2] = A_NORMAL,
      [MEMORY_USED] = A_BOLD,
      [MEMORY_BUFFERS] = A_NORMAL,
      [MEMORY_BUFFERS_TEXT] = A_NORMAL,
      [MEMORY_CACHE] = A_NORMAL,
      [LOAD_AVERAGE_FIFTEEN] = A_DIM,
      [LOAD_AVERAGE_FIVE] = A_NORMAL,
      [LOAD_AVERAGE_ONE] = A_BOLD,
      [LOAD] = A_BOLD,
      [HELP_BOLD] = A_BOLD,
      [CLOCK] = A_BOLD,
      [CHECK_BOX] = A_BOLD,
      [CHECK_MARK] = A_NORMAL,
      [CHECK_TEXT] = A_NORMAL,
      [HOSTNAME] = A_BOLD,
      [CPU_NICE] = A_NORMAL,
      [CPU_NICE_TEXT] = A_NORMAL,
      [CPU_NORMAL] = A_BOLD,
      [CPU_KERNEL] = A_BOLD,
      [CPU_IOWAIT] = A_NORMAL,
      [CPU_IRQ] = A_BOLD,
      [CPU_SOFTIRQ] = A_BOLD,
      [CPU_STEAL] = A_REVERSE,
      [CPU_GUEST] = A_REVERSE,
305
      [PANEL_EDIT] = A_BOLD,
Hisham Muhammad's avatar
Hisham Muhammad committed
306
307
308
309
      [SCREENS_OTH_BORDER] = A_DIM,
      [SCREENS_OTH_TEXT] = A_DIM,
      [SCREENS_CUR_BORDER] = A_REVERSE,
      [SCREENS_CUR_TEXT] = A_REVERSE,
310
311
312
313
314
315
316
317
   },
   [COLORSCHEME_BLACKONWHITE] = {
      [RESET_COLOR] = ColorPair(Black,White),
      [DEFAULT_COLOR] = ColorPair(Black,White),
      [FUNCTION_BAR] = ColorPair(Black,Cyan),
      [FUNCTION_KEY] = ColorPair(Black,White),
      [PANEL_HEADER_FOCUS] = ColorPair(Black,Green),
      [PANEL_HEADER_UNFOCUS] = ColorPair(Black,Green),
318
319
320
      [PANEL_SELECTION_FOCUS] = ColorPair(Black,Cyan),
      [PANEL_SELECTION_FOLLOW] = ColorPair(Black,Yellow),
      [PANEL_SELECTION_UNFOCUS] = ColorPair(Blue,White),
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
      [FAILED_SEARCH] = ColorPair(Red,Cyan),
      [UPTIME] = ColorPair(Yellow,White),
      [BATTERY] = ColorPair(Yellow,White),
      [LARGE_NUMBER] = ColorPair(Red,White),
      [METER_TEXT] = ColorPair(Blue,White),
      [METER_VALUE] = ColorPair(Black,White),
      [LED_COLOR] = ColorPair(Green,White),
      [TASKS_RUNNING] = ColorPair(Green,White),
      [PROCESS] = ColorPair(Black,White),
      [PROCESS_SHADOW] = A_BOLD | ColorPair(Black,White),
      [PROCESS_TAG] = ColorPair(White,Blue),
      [PROCESS_MEGABYTES] = ColorPair(Blue,White),
      [PROCESS_BASENAME] = ColorPair(Blue,White),
      [PROCESS_TREE] = ColorPair(Green,White),
      [PROCESS_R_STATE] = ColorPair(Green,White),
      [PROCESS_D_STATE] = A_BOLD | ColorPair(Red,White),
      [PROCESS_HIGH_PRIORITY] = ColorPair(Red,White),
338
      [PROCESS_LOW_PRIORITY] = ColorPair(Green,White),
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
      [PROCESS_THREAD] = ColorPair(Blue,White),
      [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Blue,White),
      [BAR_BORDER] = ColorPair(Blue,White),
      [BAR_SHADOW] = ColorPair(Black,White),
      [SWAP] = ColorPair(Red,White),
      [GRAPH_1] = A_BOLD | ColorPair(Blue,White),
      [GRAPH_2] = ColorPair(Blue,White),
      [MEMORY_USED] = ColorPair(Green,White),
      [MEMORY_BUFFERS] = ColorPair(Cyan,White),
      [MEMORY_BUFFERS_TEXT] = ColorPair(Cyan,White),
      [MEMORY_CACHE] = ColorPair(Yellow,White),
      [LOAD_AVERAGE_FIFTEEN] = ColorPair(Black,White),
      [LOAD_AVERAGE_FIVE] = ColorPair(Black,White),
      [LOAD_AVERAGE_ONE] = ColorPair(Black,White),
      [LOAD] = ColorPair(Black,White),
      [HELP_BOLD] = ColorPair(Blue,White),
      [CLOCK] = ColorPair(Black,White),
      [CHECK_BOX] = ColorPair(Blue,White),
      [CHECK_MARK] = ColorPair(Black,White),
      [CHECK_TEXT] = ColorPair(Black,White),
      [HOSTNAME] = ColorPair(Black,White),
      [CPU_NICE] = ColorPair(Cyan,White),
      [CPU_NICE_TEXT] = ColorPair(Cyan,White),
      [CPU_NORMAL] = ColorPair(Green,White),
      [CPU_KERNEL] = ColorPair(Red,White),
      [CPU_IOWAIT] = A_BOLD | ColorPair(Black, White),
      [CPU_IRQ] = ColorPair(Blue,White),
      [CPU_SOFTIRQ] = ColorPair(Blue,White),
      [CPU_STEAL] = ColorPair(Cyan,White),
      [CPU_GUEST] = ColorPair(Cyan,White),
369
      [PANEL_EDIT] = ColorPair(White,Blue),
Hisham Muhammad's avatar
Hisham Muhammad committed
370
371
372
373
      [SCREENS_OTH_BORDER] = A_BOLD | ColorPair(Black,White),
      [SCREENS_OTH_TEXT] = A_BOLD | ColorPair(Black,White),
      [SCREENS_CUR_BORDER] = ColorPair(Green,Green),
      [SCREENS_CUR_TEXT] = ColorPair(Black,Green),
374
375
376
377
378
379
380
381
   },
   [COLORSCHEME_LIGHTTERMINAL] = {
      [RESET_COLOR] = ColorPair(Black,Black),
      [DEFAULT_COLOR] = ColorPair(Black,Black),
      [FUNCTION_BAR] = ColorPair(Black,Cyan),
      [FUNCTION_KEY] = ColorPair(Black,Black),
      [PANEL_HEADER_FOCUS] = ColorPair(Black,Green),
      [PANEL_HEADER_UNFOCUS] = ColorPair(Black,Green),
382
383
384
      [PANEL_SELECTION_FOCUS] = ColorPair(Black,Cyan),
      [PANEL_SELECTION_FOLLOW] = ColorPair(Black,Yellow),
      [PANEL_SELECTION_UNFOCUS] = ColorPair(Blue,Black),
385
386
387
388
389
390
391
392
393
      [FAILED_SEARCH] = ColorPair(Red,Cyan),
      [UPTIME] = ColorPair(Yellow,Black),
      [BATTERY] = ColorPair(Yellow,Black),
      [LARGE_NUMBER] = ColorPair(Red,Black),
      [METER_TEXT] = ColorPair(Blue,Black),
      [METER_VALUE] = ColorPair(Black,Black),
      [LED_COLOR] = ColorPair(Green,Black),
      [TASKS_RUNNING] = ColorPair(Green,Black),
      [PROCESS] = ColorPair(Black,Black),
394
      [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
395
396
397
398
399
400
401
      [PROCESS_TAG] = ColorPair(White,Blue),
      [PROCESS_MEGABYTES] = ColorPair(Blue,Black),
      [PROCESS_BASENAME] = ColorPair(Green,Black),
      [PROCESS_TREE] = ColorPair(Blue,Black),
      [PROCESS_R_STATE] = ColorPair(Green,Black),
      [PROCESS_D_STATE] = A_BOLD | ColorPair(Red,Black),
      [PROCESS_HIGH_PRIORITY] = ColorPair(Red,Black),
402
      [PROCESS_LOW_PRIORITY] = ColorPair(Green,Black),
403
404
405
      [PROCESS_THREAD] = ColorPair(Blue,Black),
      [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Blue,Black),
      [BAR_BORDER] = ColorPair(Blue,Black),
406
      [BAR_SHADOW] = ColorPairGrayBlack,
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
      [SWAP] = ColorPair(Red,Black),
      [GRAPH_1] = A_BOLD | ColorPair(Cyan,Black),
      [GRAPH_2] = ColorPair(Cyan,Black),
      [MEMORY_USED] = ColorPair(Green,Black),
      [MEMORY_BUFFERS] = ColorPair(Cyan,Black),
      [MEMORY_BUFFERS_TEXT] = ColorPair(Cyan,Black),
      [MEMORY_CACHE] = ColorPair(Yellow,Black),
      [LOAD_AVERAGE_FIFTEEN] = ColorPair(Black,Black),
      [LOAD_AVERAGE_FIVE] = ColorPair(Black,Black),
      [LOAD_AVERAGE_ONE] = ColorPair(Black,Black),
      [LOAD] = ColorPair(White,Black),
      [HELP_BOLD] = ColorPair(Blue,Black),
      [CLOCK] = ColorPair(White,Black),
      [CHECK_BOX] = ColorPair(Blue,Black),
      [CHECK_MARK] = ColorPair(Black,Black),
      [CHECK_TEXT] = ColorPair(Black,Black),
      [HOSTNAME] = ColorPair(White,Black),
      [CPU_NICE] = ColorPair(Cyan,Black),
      [CPU_NICE_TEXT] = ColorPair(Cyan,Black),
      [CPU_NORMAL] = ColorPair(Green,Black),
      [CPU_KERNEL] = ColorPair(Red,Black),
      [CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black),
      [CPU_IRQ] = A_BOLD | ColorPair(Blue,Black),
      [CPU_SOFTIRQ] = ColorPair(Blue,Black),
      [CPU_STEAL] = ColorPair(Black,Black),
      [CPU_GUEST] = ColorPair(Black,Black),
433
      [PANEL_EDIT] = ColorPair(White,Blue),
Hisham Muhammad's avatar
Hisham Muhammad committed
434
435
436
437
      [SCREENS_OTH_BORDER] = ColorPair(Blue,Black),
      [SCREENS_OTH_TEXT] = ColorPair(Blue,Black),
      [SCREENS_CUR_BORDER] = ColorPair(Green,Green),
      [SCREENS_CUR_TEXT] = ColorPair(Black,Green),
438
439
440
441
442
443
444
445
   },
   [COLORSCHEME_MIDNIGHT] = {
      [RESET_COLOR] = ColorPair(White,Blue),
      [DEFAULT_COLOR] = ColorPair(White,Blue),
      [FUNCTION_BAR] = ColorPair(Black,Cyan),
      [FUNCTION_KEY] = A_NORMAL,
      [PANEL_HEADER_FOCUS] = ColorPair(Black,Cyan),
      [PANEL_HEADER_UNFOCUS] = ColorPair(Black,Cyan),
446
447
448
      [PANEL_SELECTION_FOCUS] = ColorPair(Black,White),
      [PANEL_SELECTION_FOLLOW] = ColorPair(Black,Yellow),
      [PANEL_SELECTION_UNFOCUS] = A_BOLD | ColorPair(Yellow,Blue),
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
      [FAILED_SEARCH] = ColorPair(Red,Cyan),
      [UPTIME] = A_BOLD | ColorPair(Yellow,Blue),
      [BATTERY] = A_BOLD | ColorPair(Yellow,Blue),
      [LARGE_NUMBER] = A_BOLD | ColorPair(Red,Blue),
      [METER_TEXT] = ColorPair(Cyan,Blue),
      [METER_VALUE] = A_BOLD | ColorPair(Cyan,Blue),
      [LED_COLOR] = ColorPair(Green,Blue),
      [TASKS_RUNNING] = A_BOLD | ColorPair(Green,Blue),
      [PROCESS] = ColorPair(White,Blue),
      [PROCESS_SHADOW] = A_BOLD | ColorPair(Black,Blue),
      [PROCESS_TAG] = A_BOLD | ColorPair(Yellow,Blue),
      [PROCESS_MEGABYTES] = ColorPair(Cyan,Blue),
      [PROCESS_BASENAME] = A_BOLD | ColorPair(Cyan,Blue),
      [PROCESS_TREE] = ColorPair(Cyan,Blue),
      [PROCESS_R_STATE] = ColorPair(Green,Blue),
      [PROCESS_D_STATE] = A_BOLD | ColorPair(Red,Blue),
      [PROCESS_HIGH_PRIORITY] = ColorPair(Red,Blue),
466
      [PROCESS_LOW_PRIORITY] = ColorPair(Green,Blue),
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
      [PROCESS_THREAD] = ColorPair(Green,Blue),
      [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Green,Blue),
      [BAR_BORDER] = A_BOLD | ColorPair(Yellow,Blue),
      [BAR_SHADOW] = ColorPair(Cyan,Blue),
      [SWAP] = ColorPair(Red,Blue),
      [GRAPH_1] = A_BOLD | ColorPair(Cyan,Blue),
      [GRAPH_2] = ColorPair(Cyan,Blue),
      [MEMORY_USED] = A_BOLD | ColorPair(Green,Blue),
      [MEMORY_BUFFERS] = A_BOLD | ColorPair(Cyan,Blue),
      [MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Cyan,Blue),
      [MEMORY_CACHE] = A_BOLD | ColorPair(Yellow,Blue),
      [LOAD_AVERAGE_FIFTEEN] = A_BOLD | ColorPair(Black,Blue),
      [LOAD_AVERAGE_FIVE] = A_NORMAL | ColorPair(White,Blue),
      [LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(White,Blue),
      [LOAD] = A_BOLD | ColorPair(White,Blue),
      [HELP_BOLD] = A_BOLD | ColorPair(Cyan,Blue),
      [CLOCK] = ColorPair(White,Blue),
      [CHECK_BOX] = ColorPair(Cyan,Blue),
      [CHECK_MARK] = A_BOLD | ColorPair(White,Blue),
      [CHECK_TEXT] = A_NORMAL | ColorPair(White,Blue),
      [HOSTNAME] = ColorPair(White,Blue),
      [CPU_NICE] = A_BOLD | ColorPair(Cyan,Blue),
      [CPU_NICE_TEXT] = A_BOLD | ColorPair(Cyan,Blue),
      [CPU_NORMAL] = A_BOLD | ColorPair(Green,Blue),
      [CPU_KERNEL] = A_BOLD | ColorPair(Red,Blue),
      [CPU_IOWAIT] = A_BOLD | ColorPair(Blue,Blue),
      [CPU_IRQ] = A_BOLD | ColorPair(Black,Blue),
      [CPU_SOFTIRQ] = ColorPair(Black,Blue),
      [CPU_STEAL] = ColorPair(White,Blue),
      [CPU_GUEST] = ColorPair(White,Blue),
497
      [PANEL_EDIT] = ColorPair(White,Blue),
Hisham Muhammad's avatar
Hisham Muhammad committed
498
499
500
501
      [SCREENS_OTH_BORDER] = A_BOLD | ColorPair(Yellow,Blue),
      [SCREENS_OTH_TEXT] = ColorPair(Cyan,Blue),
      [SCREENS_CUR_BORDER] = ColorPair(Cyan,Cyan),
      [SCREENS_CUR_TEXT] = ColorPair(Black,Cyan),
502
503
504
505
506
507
508
509
   },
   [COLORSCHEME_BLACKNIGHT] = {
      [RESET_COLOR] = ColorPair(Cyan,Black),
      [DEFAULT_COLOR] = ColorPair(Cyan,Black),
      [FUNCTION_BAR] = ColorPair(Black,Green),
      [FUNCTION_KEY] = ColorPair(Cyan,Black),
      [PANEL_HEADER_FOCUS] = ColorPair(Black,Green),
      [PANEL_HEADER_UNFOCUS] = ColorPair(Black,Green),
510
511
512
      [PANEL_SELECTION_FOCUS] = ColorPair(Black,Cyan),
      [PANEL_SELECTION_FOLLOW] = ColorPair(Black,Yellow),
      [PANEL_SELECTION_UNFOCUS] = ColorPair(Black,White),
513
514
515
516
517
518
519
520
521
      [FAILED_SEARCH] = ColorPair(Red,Cyan),
      [UPTIME] = ColorPair(Green,Black),
      [BATTERY] = ColorPair(Green,Black),
      [LARGE_NUMBER] = A_BOLD | ColorPair(Red,Black),
      [METER_TEXT] = ColorPair(Cyan,Black),
      [METER_VALUE] = ColorPair(Green,Black),
      [LED_COLOR] = ColorPair(Green,Black),
      [TASKS_RUNNING] = A_BOLD | ColorPair(Green,Black),
      [PROCESS] = ColorPair(Cyan,Black),
522
      [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
523
524
525
526
527
528
529
530
531
      [PROCESS_TAG] = A_BOLD | ColorPair(Yellow,Black),
      [PROCESS_MEGABYTES] = A_BOLD | ColorPair(Green,Black),
      [PROCESS_BASENAME] = A_BOLD | ColorPair(Green,Black),
      [PROCESS_TREE] = ColorPair(Cyan,Black),
      [PROCESS_THREAD] = ColorPair(Green,Black),
      [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Blue,Black),
      [PROCESS_R_STATE] = ColorPair(Green,Black),
      [PROCESS_D_STATE] = A_BOLD | ColorPair(Red,Black),
      [PROCESS_HIGH_PRIORITY] = ColorPair(Red,Black),
532
      [PROCESS_LOW_PRIORITY] = ColorPair(Green,Black),
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
      [BAR_BORDER] = A_BOLD | ColorPair(Green,Black),
      [BAR_SHADOW] = ColorPair(Cyan,Black),
      [SWAP] = ColorPair(Red,Black),
      [GRAPH_1] = A_BOLD | ColorPair(Green,Black),
      [GRAPH_2] = ColorPair(Green,Black),
      [MEMORY_USED] = ColorPair(Green,Black),
      [MEMORY_BUFFERS] = ColorPair(Blue,Black),
      [MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Blue,Black),
      [MEMORY_CACHE] = ColorPair(Yellow,Black),
      [LOAD_AVERAGE_FIFTEEN] = ColorPair(Green,Black),
      [LOAD_AVERAGE_FIVE] = ColorPair(Green,Black),
      [LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(Green,Black),
      [LOAD] = A_BOLD,
      [HELP_BOLD] = A_BOLD | ColorPair(Cyan,Black),
      [CLOCK] = ColorPair(Green,Black),
      [CHECK_BOX] = ColorPair(Green,Black),
      [CHECK_MARK] = A_BOLD | ColorPair(Green,Black),
      [CHECK_TEXT] = ColorPair(Cyan,Black),
      [HOSTNAME] = ColorPair(Green,Black),
      [CPU_NICE] = ColorPair(Blue,Black),
      [CPU_NICE_TEXT] = A_BOLD | ColorPair(Blue,Black),
      [CPU_NORMAL] = ColorPair(Green,Black),
      [CPU_KERNEL] = ColorPair(Red,Black),
      [CPU_IOWAIT] = ColorPair(Yellow,Black),
      [CPU_IRQ] = A_BOLD | ColorPair(Blue,Black),
      [CPU_SOFTIRQ] = ColorPair(Blue,Black),
      [CPU_STEAL] = ColorPair(Cyan,Black),
      [CPU_GUEST] = ColorPair(Cyan,Black),
561
      [PANEL_EDIT] = ColorPair(White,Cyan),
Hisham Muhammad's avatar
Hisham Muhammad committed
562
563
564
565
      [SCREENS_OTH_BORDER] = ColorPair(White,Black),
      [SCREENS_OTH_TEXT] = ColorPair(Cyan,Black),
      [SCREENS_CUR_BORDER] = A_BOLD | ColorPair(White,Black),
      [SCREENS_CUR_TEXT] = A_BOLD | ColorPair(Green,Black),
566
567
   },
   [COLORSCHEME_BROKENGRAY] = { 0 } // dynamically generated.
568
};
Hisham Muhammad's avatar
Hisham Muhammad committed
569

Hisham Muhammad's avatar
Hisham Muhammad committed
570
571
int CRT_scrollHAmount = 5;

572
573
int CRT_scrollWheelVAmount = 10;

574
575
char* CRT_termType;

Hisham Muhammad's avatar
Hisham Muhammad committed
576
577
578
579
// TODO move color scheme to Settings, perhaps?

int CRT_colorScheme = 0;

580
581
void *backtraceArray[128];

Hisham Muhammad's avatar
Hisham Muhammad committed
582
583
static void CRT_handleSIGTERM(int sgn) {
   (void) sgn;
584
585
586
587
   CRT_done();
   exit(0);
}

588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#if HAVE_SETUID_ENABLED

static int CRT_euid = -1;

static int CRT_egid = -1;

#define DIE(msg) do { CRT_done(); fprintf(stderr, msg); exit(1); } while(0)

void CRT_dropPrivileges() {
   CRT_egid = getegid();
   CRT_euid = geteuid();
   if (setegid(getgid()) == -1) {
      DIE("Fatal error: failed dropping group privileges.\n");
   }
   if (seteuid(getuid()) == -1) {
      DIE("Fatal error: failed dropping user privileges.\n");
   }
}

void CRT_restorePrivileges() {
   if (CRT_egid == -1 || CRT_euid == -1) {
      DIE("Fatal error: internal inconsistency.\n");
   }
   if (setegid(CRT_egid) == -1) {
      DIE("Fatal error: failed restoring group privileges.\n");
   }
   if (seteuid(CRT_euid) == -1) {
      DIE("Fatal error: failed restoring user privileges.\n");
   }
}

#else

/* Turn setuid operations into NOPs */

#ifndef CRT_dropPrivileges
#define CRT_dropPrivileges()
#define CRT_restorePrivileges()
#endif

#endif

Hisham Muhammad's avatar
Hisham Muhammad committed
630
631
632
633
634
// TODO: pass an instance of Settings instead.

void CRT_init(int delay, int colorScheme) {
   initscr();
   noecho();
635
   CRT_delay = delay;
636
637
638
   if (CRT_delay == 0) {
      CRT_delay = 1;
   }
639
   CRT_colors = CRT_colorSchemes[colorScheme];
Hisham Muhammad's avatar
Hisham Muhammad committed
640
   CRT_colorScheme = colorScheme;
641
642
   
   for (int i = 0; i < LAST_COLORELEMENT; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
643
      unsigned int color = CRT_colorSchemes[COLORSCHEME_DEFAULT][i];
644
      CRT_colorSchemes[COLORSCHEME_BROKENGRAY][i] = color == (A_BOLD | ColorPairGrayBlack) ? ColorPair(White,Black) : color;
645
646
   }
   
647
   halfdelay(CRT_delay);
Hisham Muhammad's avatar
Hisham Muhammad committed
648
649
650
   nonl();
   intrflush(stdscr, false);
   keypad(stdscr, true);
651
   mouseinterval(0);
Hisham Muhammad's avatar
Hisham Muhammad committed
652
653
654
655
656
657
658
   curs_set(0);
   if (has_colors()) {
      start_color();
      CRT_hasColors = true;
   } else {
      CRT_hasColors = false;
   }
659
   CRT_termType = getenv("TERM");
Hisham Muhammad's avatar
Hisham Muhammad committed
660
661
662
663
   if (String_eq(CRT_termType, "linux"))
      CRT_scrollHAmount = 20;
   else
      CRT_scrollHAmount = 5;
664
   if (String_startsWith(CRT_termType, "xterm") || String_eq(CRT_termType, "vt220")) {
Hisham Muhammad's avatar
Hisham Muhammad committed
665
666
      define_key("\033[H", KEY_HOME);
      define_key("\033[F", KEY_END);
667
668
      define_key("\033[7~", KEY_HOME);
      define_key("\033[8~", KEY_END);
Hisham Muhammad's avatar
Hisham Muhammad committed
669
670
671
672
673
674
675
676
677
      define_key("\033OP", KEY_F(1));
      define_key("\033OQ", KEY_F(2));
      define_key("\033OR", KEY_F(3));
      define_key("\033OS", KEY_F(4));
      define_key("\033[11~", KEY_F(1));
      define_key("\033[12~", KEY_F(2));
      define_key("\033[13~", KEY_F(3));
      define_key("\033[14~", KEY_F(4));
      define_key("\033[17;2~", KEY_F(18));
678
      define_key("\033[Z", KEY_SHIFT_TAB);
679
680
681
682
683
      char sequence[3] = "\033a";
      for (char c = 'a'; c <= 'z'; c++) {
         sequence[1] = c;
         define_key(sequence, KEY_ALT('A' + (c - 'a')));
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
684
   }
685
686
687
   if (String_startsWith(CRT_termType, "rxvt")) {
      define_key("\033[Z", KEY_SHIFT_TAB);
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
688
689
690
691
#ifndef DEBUG
   signal(11, CRT_handleSIGSEGV);
#endif
   signal(SIGTERM, CRT_handleSIGTERM);
Hisham's avatar
Hisham committed
692
   signal(SIGQUIT, CRT_handleSIGTERM);
Hisham Muhammad's avatar
Hisham Muhammad committed
693
694
695
696
697
   use_default_colors();
   if (!has_colors())
      CRT_colorScheme = 1;
   CRT_setColors(CRT_colorScheme);

Christian Hesse's avatar
Christian Hesse committed
698
   /* initialize locale */
699
   setlocale(LC_CTYPE, "");
Christian Hesse's avatar
Christian Hesse committed
700

Hisham Muhammad's avatar
Hisham Muhammad committed
701
#ifdef HAVE_LIBNCURSESW
Christian Hesse's avatar
Christian Hesse committed
702
   if(strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
Hisham Muhammad's avatar
Hisham Muhammad committed
703
704
705
706
707
      CRT_utf8 = true;
   else
      CRT_utf8 = false;
#endif

708
709
   CRT_treeStr =
#ifdef HAVE_LIBNCURSESW
710
      CRT_utf8 ? CRT_treeStrUtf8 :
711
#endif
712
      CRT_treeStrAscii;
Hisham Muhammad's avatar
Hisham Muhammad committed
713

714
715
716
717
718
#if NCURSES_MOUSE_VERSION > 1
   mousemask(BUTTON1_RELEASED | BUTTON4_PRESSED | BUTTON5_PRESSED, NULL);
#else
   mousemask(BUTTON1_RELEASED, NULL);
#endif
719

Hisham Muhammad's avatar
Hisham Muhammad committed
720
721
722
723
724
725
726
}

void CRT_done() {
   curs_set(1);
   endwin();
}

727
728
729
730
731
732
733
void CRT_fatalError(const char* note) {
   char* sysMsg = strerror(errno);
   CRT_done();
   fprintf(stderr, "%s: %s\n", note, sysMsg);
   exit(2);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
734
735
736
int CRT_readKey() {
   nocbreak();
   cbreak();
Hisham Muhammad's avatar
Hisham Muhammad committed
737
   nodelay(stdscr, FALSE);
Hisham Muhammad's avatar
Hisham Muhammad committed
738
   int ret = getch();
739
   halfdelay(CRT_delay);
Hisham Muhammad's avatar
Hisham Muhammad committed
740
741
742
743
744
745
746
747
748
749
   return ret;
}

void CRT_disableDelay() {
   nocbreak();
   cbreak();
   nodelay(stdscr, TRUE);
}

void CRT_enableDelay() {
750
   halfdelay(CRT_delay);
Hisham Muhammad's avatar
Hisham Muhammad committed
751
752
753
754
}

void CRT_setColors(int colorScheme) {
   CRT_colorScheme = colorScheme;
755
756
757
758
759
760
761
762

   for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
         if (ColorIndex(i,j) != ColorPairGrayBlack) {
            int bg = (colorScheme != COLORSCHEME_BLACKNIGHT)
                     ? (j==0 ? -1 : j)
                     : j;
            init_pair(ColorIndex(i,j), i, bg);
763
         }
764
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
765
   }
766
767
768
769
770
771
772

   int grayBlackFg = COLORS > 8 ? 8 : 0;
   int grayBlackBg = (colorScheme != COLORSCHEME_BLACKNIGHT)
                     ? -1
                     : 0;
   init_pair(ColorIndexGrayBlack, grayBlackFg, grayBlackBg);

773
   CRT_colors = CRT_colorSchemes[colorScheme];
Hisham Muhammad's avatar
Hisham Muhammad committed
774
}