CRT.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /*
  2. htop - CRT.c
  3. (C) 2004-2011 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 "CRT.h"
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <langinfo.h>
  12. #include <signal.h>
  13. #include <stdarg.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "CommandLine.h"
  19. #include "ProvideCurses.h"
  20. #include "ProvideTerm.h"
  21. #include "XUtils.h"
  22. #if !defined(NDEBUG) && defined(HAVE_MEMFD_CREATE)
  23. #include <sys/mman.h>
  24. #endif
  25. #if defined(HAVE_LIBUNWIND_H) && defined(HAVE_LIBUNWIND)
  26. # define PRINT_BACKTRACE
  27. # define UNW_LOCAL_ONLY
  28. # include <libunwind.h>
  29. # if defined(HAVE_DLADDR)
  30. # include <dlfcn.h>
  31. # endif
  32. #elif defined(HAVE_EXECINFO_H)
  33. # define PRINT_BACKTRACE
  34. # include <execinfo.h>
  35. #endif
  36. #define ColorIndex(i,j) ((7-(i))*8+(j))
  37. #define ColorPair(i,j) COLOR_PAIR(ColorIndex(i,j))
  38. #define Black COLOR_BLACK
  39. #define Red COLOR_RED
  40. #define Green COLOR_GREEN
  41. #define Yellow COLOR_YELLOW
  42. #define Blue COLOR_BLUE
  43. #define Magenta COLOR_MAGENTA
  44. #define Cyan COLOR_CYAN
  45. #define White COLOR_WHITE
  46. #define ColorPairGrayBlack ColorPair(Magenta,Magenta)
  47. #define ColorIndexGrayBlack ColorIndex(Magenta,Magenta)
  48. #define ColorPairWhiteDefault ColorPair(Red, Red)
  49. #define ColorIndexWhiteDefault ColorIndex(Red, Red)
  50. static const char* const CRT_treeStrAscii[LAST_TREE_STR] = {
  51. [TREE_STR_VERT] = "|",
  52. [TREE_STR_RTEE] = "`",
  53. [TREE_STR_BEND] = "`",
  54. [TREE_STR_TEND] = ",",
  55. [TREE_STR_OPEN] = "+",
  56. [TREE_STR_SHUT] = "-",
  57. [TREE_STR_ASC] = "+",
  58. [TREE_STR_DESC] = "-",
  59. };
  60. #ifdef HAVE_LIBNCURSESW
  61. static const char* const CRT_treeStrUtf8[LAST_TREE_STR] = {
  62. [TREE_STR_VERT] = "\xe2\x94\x82", // │
  63. [TREE_STR_RTEE] = "\xe2\x94\x9c", // ├
  64. [TREE_STR_BEND] = "\xe2\x94\x94", // └
  65. [TREE_STR_TEND] = "\xe2\x94\x8c", // ┌
  66. [TREE_STR_OPEN] = "+", // +, TODO use 🮯 'BOX DRAWINGS LIGHT HORIZONTAL
  67. // WITH VERTICAL STROKE' (U+1FBAF, "\xf0\x9f\xae\xaf") when
  68. // Unicode 13 is common
  69. [TREE_STR_SHUT] = "\xe2\x94\x80", // ─
  70. [TREE_STR_ASC] = "\xe2\x96\xb3", // △
  71. [TREE_STR_DESC] = "\xe2\x96\xbd", // ▽
  72. };
  73. bool CRT_utf8 = false;
  74. #endif
  75. const char* const* CRT_treeStr = CRT_treeStrAscii;
  76. static const Settings* CRT_settings;
  77. const char* CRT_degreeSign;
  78. static const char* initDegreeSign(void) {
  79. #ifdef HAVE_LIBNCURSESW
  80. if (CRT_utf8)
  81. return "\xc2\xb0";
  82. static char buffer[4];
  83. // this might fail if the current locale does not support wide characters
  84. int r = snprintf(buffer, sizeof(buffer), "%lc", 176);
  85. if (r > 0)
  86. return buffer;
  87. #endif
  88. return "";
  89. }
  90. const int* CRT_colors;
  91. static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
  92. [COLORSCHEME_DEFAULT] = {
  93. [RESET_COLOR] = ColorPair(White, Black),
  94. [DEFAULT_COLOR] = ColorPair(White, Black),
  95. [FUNCTION_BAR] = ColorPair(Black, Cyan),
  96. [FUNCTION_KEY] = ColorPair(White, Black),
  97. [PANEL_HEADER_FOCUS] = ColorPair(Black, Green),
  98. [PANEL_HEADER_UNFOCUS] = ColorPair(Black, Green),
  99. [PANEL_SELECTION_FOCUS] = ColorPair(Black, Cyan),
  100. [PANEL_SELECTION_FOLLOW] = ColorPair(Black, Yellow),
  101. [PANEL_SELECTION_UNFOCUS] = ColorPair(Black, White),
  102. [FAILED_SEARCH] = ColorPair(Red, Cyan),
  103. [FAILED_READ] = A_BOLD | ColorPair(Red, Black),
  104. [PAUSED] = A_BOLD | ColorPair(Yellow, Cyan),
  105. [UPTIME] = A_BOLD | ColorPair(Cyan, Black),
  106. [BATTERY] = A_BOLD | ColorPair(Cyan, Black),
  107. [LARGE_NUMBER] = A_BOLD | ColorPair(Red, Black),
  108. [METER_SHADOW] = A_BOLD | ColorPairGrayBlack,
  109. [METER_TEXT] = ColorPair(Cyan, Black),
  110. [METER_VALUE] = A_BOLD | ColorPair(Cyan, Black),
  111. [METER_VALUE_ERROR] = A_BOLD | ColorPair(Red, Black),
  112. [METER_VALUE_IOREAD] = ColorPair(Green, Black),
  113. [METER_VALUE_IOWRITE] = A_BOLD | ColorPair(Blue, Black),
  114. [METER_VALUE_NOTICE] = A_BOLD | ColorPair(White, Black),
  115. [METER_VALUE_OK] = ColorPair(Green, Black),
  116. [METER_VALUE_WARN] = A_BOLD | ColorPair(Yellow, Black),
  117. [LED_COLOR] = ColorPair(Green, Black),
  118. [TASKS_RUNNING] = A_BOLD | ColorPair(Green, Black),
  119. [PROCESS] = A_NORMAL,
  120. [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
  121. [PROCESS_TAG] = A_BOLD | ColorPair(Yellow, Black),
  122. [PROCESS_MEGABYTES] = ColorPair(Cyan, Black),
  123. [PROCESS_GIGABYTES] = ColorPair(Green, Black),
  124. [PROCESS_BASENAME] = A_BOLD | ColorPair(Cyan, Black),
  125. [PROCESS_TREE] = ColorPair(Cyan, Black),
  126. [PROCESS_RUN_STATE] = ColorPair(Green, Black),
  127. [PROCESS_D_STATE] = A_BOLD | ColorPair(Red, Black),
  128. [PROCESS_HIGH_PRIORITY] = ColorPair(Red, Black),
  129. [PROCESS_LOW_PRIORITY] = ColorPair(Green, Black),
  130. [PROCESS_NEW] = ColorPair(Black, Green),
  131. [PROCESS_TOMB] = ColorPair(Black, Red),
  132. [PROCESS_THREAD] = ColorPair(Green, Black),
  133. [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Green, Black),
  134. [PROCESS_COMM] = ColorPair(Magenta, Black),
  135. [PROCESS_THREAD_COMM] = A_BOLD | ColorPair(Blue, Black),
  136. [PROCESS_PRIV] = ColorPair(Magenta, Black),
  137. [BAR_BORDER] = A_BOLD,
  138. [BAR_SHADOW] = A_BOLD | ColorPairGrayBlack,
  139. [SWAP] = ColorPair(Red, Black),
  140. [SWAP_CACHE] = ColorPair(Yellow, Black),
  141. [SWAP_FRONTSWAP] = A_BOLD | ColorPairGrayBlack,
  142. [GRAPH_1] = A_BOLD | ColorPair(Cyan, Black),
  143. [GRAPH_2] = ColorPair(Cyan, Black),
  144. [MEMORY_USED] = ColorPair(Green, Black),
  145. [MEMORY_BUFFERS] = A_BOLD | ColorPair(Blue, Black),
  146. [MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Blue, Black),
  147. [MEMORY_CACHE] = ColorPair(Yellow, Black),
  148. [MEMORY_SHARED] = ColorPair(Magenta, Black),
  149. [MEMORY_COMPRESSED] = A_BOLD | ColorPairGrayBlack,
  150. [HUGEPAGE_1] = ColorPair(Green, Black),
  151. [HUGEPAGE_2] = ColorPair(Yellow, Black),
  152. [HUGEPAGE_3] = ColorPair(Red, Black),
  153. [HUGEPAGE_4] = A_BOLD | ColorPair(Blue, Black),
  154. [LOAD_AVERAGE_FIFTEEN] = ColorPair(Cyan, Black),
  155. [LOAD_AVERAGE_FIVE] = A_BOLD | ColorPair(Cyan, Black),
  156. [LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(White, Black),
  157. [LOAD] = A_BOLD,
  158. [HELP_BOLD] = A_BOLD | ColorPair(Cyan, Black),
  159. [HELP_SHADOW] = A_BOLD | ColorPairGrayBlack,
  160. [CLOCK] = A_BOLD,
  161. [DATE] = A_BOLD,
  162. [DATETIME] = A_BOLD,
  163. [CHECK_BOX] = ColorPair(Cyan, Black),
  164. [CHECK_MARK] = A_BOLD,
  165. [CHECK_TEXT] = A_NORMAL,
  166. [HOSTNAME] = A_BOLD,
  167. [CPU_NICE] = A_BOLD | ColorPair(Blue, Black),
  168. [CPU_NICE_TEXT] = A_BOLD | ColorPair(Blue, Black),
  169. [CPU_NORMAL] = ColorPair(Green, Black),
  170. [CPU_SYSTEM] = ColorPair(Red, Black),
  171. [CPU_IOWAIT] = A_BOLD | ColorPairGrayBlack,
  172. [CPU_IRQ] = ColorPair(Yellow, Black),
  173. [CPU_SOFTIRQ] = ColorPair(Magenta, Black),
  174. [CPU_STEAL] = ColorPair(Cyan, Black),
  175. [CPU_GUEST] = ColorPair(Cyan, Black),
  176. [GPU_ENGINE_1] = ColorPair(Green, Black),
  177. [GPU_ENGINE_2] = ColorPair(Yellow, Black),
  178. [GPU_ENGINE_3] = ColorPair(Red, Black),
  179. [GPU_ENGINE_4] = A_BOLD | ColorPair(Blue, Black),
  180. [GPU_RESIDUE] = ColorPair(Magenta, Black),
  181. [PANEL_EDIT] = ColorPair(White, Blue),
  182. [SCREENS_OTH_BORDER] = ColorPair(Blue, Blue),
  183. [SCREENS_OTH_TEXT] = ColorPair(Black, Blue),
  184. [SCREENS_CUR_BORDER] = ColorPair(Green, Green),
  185. [SCREENS_CUR_TEXT] = ColorPair(Black, Green),
  186. [PRESSURE_STALL_THREEHUNDRED] = ColorPair(Cyan, Black),
  187. [PRESSURE_STALL_SIXTY] = A_BOLD | ColorPair(Cyan, Black),
  188. [PRESSURE_STALL_TEN] = A_BOLD | ColorPair(White, Black),
  189. [FILE_DESCRIPTOR_USED] = ColorPair(Green, Black),
  190. [FILE_DESCRIPTOR_MAX] = A_BOLD | ColorPair(Blue, Black),
  191. [ZFS_MFU] = A_BOLD | ColorPair(Blue, Black),
  192. [ZFS_MRU] = ColorPair(Yellow, Black),
  193. [ZFS_ANON] = ColorPair(Magenta, Black),
  194. [ZFS_HEADER] = ColorPair(Cyan, Black),
  195. [ZFS_OTHER] = ColorPair(Magenta, Black),
  196. [ZFS_COMPRESSED] = A_BOLD | ColorPair(Blue, Black),
  197. [ZFS_RATIO] = ColorPair(Magenta, Black),
  198. [ZRAM_COMPRESSED] = A_BOLD | ColorPair(Blue, Black),
  199. [ZRAM_UNCOMPRESSED] = ColorPair(Yellow, Black),
  200. [DYNAMIC_GRAY] = ColorPairGrayBlack,
  201. [DYNAMIC_DARKGRAY] = A_BOLD | ColorPairGrayBlack,
  202. [DYNAMIC_RED] = ColorPair(Red, Black),
  203. [DYNAMIC_GREEN] = ColorPair(Green, Black),
  204. [DYNAMIC_BLUE] = A_BOLD | ColorPair(Blue, Black),
  205. [DYNAMIC_CYAN] = ColorPair(Cyan, Black),
  206. [DYNAMIC_MAGENTA] = ColorPair(Magenta, Black),
  207. [DYNAMIC_YELLOW] = ColorPair(Yellow, Black),
  208. [DYNAMIC_WHITE] = ColorPair(White, Black),
  209. },
  210. [COLORSCHEME_MONOCHROME] = {
  211. [RESET_COLOR] = A_NORMAL,
  212. [DEFAULT_COLOR] = A_NORMAL,
  213. [FUNCTION_BAR] = A_REVERSE,
  214. [FUNCTION_KEY] = A_NORMAL,
  215. [PANEL_HEADER_FOCUS] = A_REVERSE,
  216. [PANEL_HEADER_UNFOCUS] = A_REVERSE,
  217. [PANEL_SELECTION_FOCUS] = A_REVERSE,
  218. [PANEL_SELECTION_FOLLOW] = A_REVERSE,
  219. [PANEL_SELECTION_UNFOCUS] = A_BOLD,
  220. [FAILED_SEARCH] = A_REVERSE | A_BOLD,
  221. [FAILED_READ] = A_BOLD,
  222. [PAUSED] = A_BOLD | A_REVERSE,
  223. [UPTIME] = A_BOLD,
  224. [BATTERY] = A_BOLD,
  225. [LARGE_NUMBER] = A_BOLD,
  226. [METER_SHADOW] = A_DIM,
  227. [METER_TEXT] = A_NORMAL,
  228. [METER_VALUE] = A_BOLD,
  229. [METER_VALUE_ERROR] = A_BOLD,
  230. [METER_VALUE_IOREAD] = A_NORMAL,
  231. [METER_VALUE_IOWRITE] = A_NORMAL,
  232. [METER_VALUE_NOTICE] = A_BOLD,
  233. [METER_VALUE_OK] = A_NORMAL,
  234. [METER_VALUE_WARN] = A_BOLD,
  235. [LED_COLOR] = A_NORMAL,
  236. [TASKS_RUNNING] = A_BOLD,
  237. [PROCESS] = A_NORMAL,
  238. [PROCESS_SHADOW] = A_DIM,
  239. [PROCESS_TAG] = A_BOLD,
  240. [PROCESS_MEGABYTES] = A_BOLD,
  241. [PROCESS_GIGABYTES] = A_BOLD,
  242. [PROCESS_BASENAME] = A_BOLD,
  243. [PROCESS_TREE] = A_BOLD,
  244. [PROCESS_RUN_STATE] = A_BOLD,
  245. [PROCESS_D_STATE] = A_BOLD,
  246. [PROCESS_HIGH_PRIORITY] = A_BOLD,
  247. [PROCESS_LOW_PRIORITY] = A_DIM,
  248. [PROCESS_NEW] = A_BOLD,
  249. [PROCESS_TOMB] = A_DIM,
  250. [PROCESS_THREAD] = A_BOLD,
  251. [PROCESS_THREAD_BASENAME] = A_REVERSE,
  252. [PROCESS_COMM] = A_BOLD,
  253. [PROCESS_THREAD_COMM] = A_REVERSE,
  254. [PROCESS_PRIV] = A_BOLD,
  255. [BAR_BORDER] = A_BOLD,
  256. [BAR_SHADOW] = A_DIM,
  257. [SWAP] = A_BOLD,
  258. [SWAP_CACHE] = A_NORMAL,
  259. [SWAP_FRONTSWAP] = A_DIM,
  260. [GRAPH_1] = A_BOLD,
  261. [GRAPH_2] = A_NORMAL,
  262. [MEMORY_USED] = A_BOLD,
  263. [MEMORY_BUFFERS] = A_NORMAL,
  264. [MEMORY_BUFFERS_TEXT] = A_NORMAL,
  265. [MEMORY_CACHE] = A_NORMAL,
  266. [MEMORY_SHARED] = A_NORMAL,
  267. [MEMORY_COMPRESSED] = A_DIM,
  268. [HUGEPAGE_1] = A_BOLD,
  269. [HUGEPAGE_2] = A_NORMAL,
  270. [HUGEPAGE_3] = A_REVERSE | A_BOLD,
  271. [HUGEPAGE_4] = A_REVERSE,
  272. [LOAD_AVERAGE_FIFTEEN] = A_DIM,
  273. [LOAD_AVERAGE_FIVE] = A_NORMAL,
  274. [LOAD_AVERAGE_ONE] = A_BOLD,
  275. [LOAD] = A_BOLD,
  276. [HELP_BOLD] = A_BOLD,
  277. [HELP_SHADOW] = A_DIM,
  278. [CLOCK] = A_BOLD,
  279. [DATE] = A_BOLD,
  280. [DATETIME] = A_BOLD,
  281. [CHECK_BOX] = A_BOLD,
  282. [CHECK_MARK] = A_NORMAL,
  283. [CHECK_TEXT] = A_NORMAL,
  284. [HOSTNAME] = A_BOLD,
  285. [CPU_NICE] = A_NORMAL,
  286. [CPU_NICE_TEXT] = A_NORMAL,
  287. [CPU_NORMAL] = A_BOLD,
  288. [CPU_SYSTEM] = A_BOLD,
  289. [CPU_IOWAIT] = A_NORMAL,
  290. [CPU_IRQ] = A_BOLD,
  291. [CPU_SOFTIRQ] = A_BOLD,
  292. [CPU_STEAL] = A_DIM,
  293. [CPU_GUEST] = A_DIM,
  294. [GPU_ENGINE_1] = A_BOLD,
  295. [GPU_ENGINE_2] = A_NORMAL,
  296. [GPU_ENGINE_3] = A_REVERSE | A_BOLD,
  297. [GPU_ENGINE_4] = A_REVERSE,
  298. [GPU_RESIDUE] = A_BOLD,
  299. [PANEL_EDIT] = A_BOLD,
  300. [SCREENS_OTH_BORDER] = A_DIM,
  301. [SCREENS_OTH_TEXT] = A_DIM,
  302. [SCREENS_CUR_BORDER] = A_REVERSE,
  303. [SCREENS_CUR_TEXT] = A_REVERSE,
  304. [PRESSURE_STALL_THREEHUNDRED] = A_DIM,
  305. [PRESSURE_STALL_SIXTY] = A_NORMAL,
  306. [PRESSURE_STALL_TEN] = A_BOLD,
  307. [FILE_DESCRIPTOR_USED] = A_BOLD,
  308. [FILE_DESCRIPTOR_MAX] = A_BOLD,
  309. [ZFS_MFU] = A_NORMAL,
  310. [ZFS_MRU] = A_NORMAL,
  311. [ZFS_ANON] = A_DIM,
  312. [ZFS_HEADER] = A_BOLD,
  313. [ZFS_OTHER] = A_DIM,
  314. [ZFS_COMPRESSED] = A_BOLD,
  315. [ZFS_RATIO] = A_BOLD,
  316. [ZRAM_COMPRESSED] = A_NORMAL,
  317. [ZRAM_UNCOMPRESSED] = A_NORMAL,
  318. [DYNAMIC_GRAY] = A_DIM,
  319. [DYNAMIC_DARKGRAY] = A_DIM,
  320. [DYNAMIC_RED] = A_BOLD,
  321. [DYNAMIC_GREEN] = A_NORMAL,
  322. [DYNAMIC_BLUE] = A_NORMAL,
  323. [DYNAMIC_CYAN] = A_BOLD,
  324. [DYNAMIC_MAGENTA] = A_NORMAL,
  325. [DYNAMIC_YELLOW] = A_NORMAL,
  326. [DYNAMIC_WHITE] = A_BOLD,
  327. },
  328. [COLORSCHEME_BLACKONWHITE] = {
  329. [RESET_COLOR] = ColorPair(Black, White),
  330. [DEFAULT_COLOR] = ColorPair(Black, White),
  331. [FUNCTION_BAR] = ColorPair(Black, Cyan),
  332. [FUNCTION_KEY] = ColorPair(Black, White),
  333. [PANEL_HEADER_FOCUS] = ColorPair(Black, Green),
  334. [PANEL_HEADER_UNFOCUS] = ColorPair(Black, Green),
  335. [PANEL_SELECTION_FOCUS] = ColorPair(Black, Cyan),
  336. [PANEL_SELECTION_FOLLOW] = ColorPair(Black, Yellow),
  337. [PANEL_SELECTION_UNFOCUS] = ColorPair(Blue, White),
  338. [FAILED_SEARCH] = ColorPair(Red, Cyan),
  339. [FAILED_READ] = ColorPair(Red, White),
  340. [PAUSED] = A_BOLD | ColorPair(Yellow, Cyan),
  341. [UPTIME] = ColorPair(Yellow, White),
  342. [BATTERY] = ColorPair(Yellow, White),
  343. [LARGE_NUMBER] = ColorPair(Red, White),
  344. [METER_SHADOW] = ColorPair(Blue, White),
  345. [METER_TEXT] = ColorPair(Blue, White),
  346. [METER_VALUE] = ColorPair(Black, White),
  347. [METER_VALUE_ERROR] = A_BOLD | ColorPair(Red, White),
  348. [METER_VALUE_IOREAD] = ColorPair(Green, White),
  349. [METER_VALUE_IOWRITE] = ColorPair(Yellow, White),
  350. [METER_VALUE_NOTICE] = A_BOLD | ColorPair(Yellow, White),
  351. [METER_VALUE_OK] = ColorPair(Green, White),
  352. [METER_VALUE_WARN] = A_BOLD | ColorPair(Yellow, White),
  353. [LED_COLOR] = ColorPair(Green, White),
  354. [TASKS_RUNNING] = ColorPair(Green, White),
  355. [PROCESS] = ColorPair(Black, White),
  356. [PROCESS_SHADOW] = A_BOLD | ColorPair(Black, White),
  357. [PROCESS_TAG] = ColorPair(White, Blue),
  358. [PROCESS_MEGABYTES] = ColorPair(Blue, White),
  359. [PROCESS_GIGABYTES] = ColorPair(Green, White),
  360. [PROCESS_BASENAME] = ColorPair(Blue, White),
  361. [PROCESS_TREE] = ColorPair(Green, White),
  362. [PROCESS_RUN_STATE] = ColorPair(Green, White),
  363. [PROCESS_D_STATE] = A_BOLD | ColorPair(Red, White),
  364. [PROCESS_HIGH_PRIORITY] = ColorPair(Red, White),
  365. [PROCESS_LOW_PRIORITY] = ColorPair(Green, White),
  366. [PROCESS_NEW] = ColorPair(White, Green),
  367. [PROCESS_TOMB] = ColorPair(White, Red),
  368. [PROCESS_THREAD] = ColorPair(Blue, White),
  369. [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Blue, White),
  370. [PROCESS_COMM] = ColorPair(Magenta, White),
  371. [PROCESS_THREAD_COMM] = ColorPair(Green, White),
  372. [PROCESS_PRIV] = ColorPair(Magenta, White),
  373. [BAR_BORDER] = ColorPair(Blue, White),
  374. [BAR_SHADOW] = ColorPair(Black, White),
  375. [SWAP] = ColorPair(Red, White),
  376. [SWAP_CACHE] = ColorPair(Yellow, White),
  377. [SWAP_FRONTSWAP] = A_BOLD | ColorPair(Black, White),
  378. [GRAPH_1] = A_BOLD | ColorPair(Blue, White),
  379. [GRAPH_2] = ColorPair(Blue, White),
  380. [MEMORY_USED] = ColorPair(Green, White),
  381. [MEMORY_BUFFERS] = ColorPair(Cyan, White),
  382. [MEMORY_BUFFERS_TEXT] = ColorPair(Cyan, White),
  383. [MEMORY_CACHE] = ColorPair(Yellow, White),
  384. [MEMORY_SHARED] = ColorPair(Magenta, White),
  385. [MEMORY_COMPRESSED] = A_BOLD | ColorPair(Black, White),
  386. [HUGEPAGE_1] = ColorPair(Green, White),
  387. [HUGEPAGE_2] = ColorPair(Yellow, White),
  388. [HUGEPAGE_3] = ColorPair(Red, White),
  389. [HUGEPAGE_4] = ColorPair(Blue, White),
  390. [LOAD_AVERAGE_FIFTEEN] = ColorPair(Black, White),
  391. [LOAD_AVERAGE_FIVE] = ColorPair(Black, White),
  392. [LOAD_AVERAGE_ONE] = ColorPair(Black, White),
  393. [LOAD] = ColorPair(Black, White),
  394. [HELP_BOLD] = ColorPair(Blue, White),
  395. [HELP_SHADOW] = A_BOLD | ColorPair(Black, White),
  396. [CLOCK] = ColorPair(Black, White),
  397. [DATE] = ColorPair(Black, White),
  398. [DATETIME] = ColorPair(Black, White),
  399. [CHECK_BOX] = ColorPair(Blue, White),
  400. [CHECK_MARK] = ColorPair(Black, White),
  401. [CHECK_TEXT] = ColorPair(Black, White),
  402. [HOSTNAME] = ColorPair(Black, White),
  403. [CPU_NICE] = ColorPair(Cyan, White),
  404. [CPU_NICE_TEXT] = ColorPair(Cyan, White),
  405. [CPU_NORMAL] = ColorPair(Green, White),
  406. [CPU_SYSTEM] = ColorPair(Red, White),
  407. [CPU_IOWAIT] = A_BOLD | ColorPair(Black, White),
  408. [CPU_IRQ] = ColorPair(Blue, White),
  409. [CPU_SOFTIRQ] = ColorPair(Blue, White),
  410. [CPU_STEAL] = ColorPair(Cyan, White),
  411. [CPU_GUEST] = ColorPair(Cyan, White),
  412. [GPU_ENGINE_1] = ColorPair(Green, White),
  413. [GPU_ENGINE_2] = ColorPair(Yellow, White),
  414. [GPU_ENGINE_3] = ColorPair(Red, White),
  415. [GPU_ENGINE_4] = ColorPair(Blue, White),
  416. [GPU_RESIDUE] = ColorPair(Magenta, White),
  417. [PANEL_EDIT] = ColorPair(White, Blue),
  418. [SCREENS_OTH_BORDER] = A_BOLD | ColorPair(Black, White),
  419. [SCREENS_OTH_TEXT] = A_BOLD | ColorPair(Black, White),
  420. [SCREENS_CUR_BORDER] = ColorPair(Green, Green),
  421. [SCREENS_CUR_TEXT] = ColorPair(Black, Green),
  422. [PRESSURE_STALL_THREEHUNDRED] = ColorPair(Black, White),
  423. [PRESSURE_STALL_SIXTY] = ColorPair(Black, White),
  424. [PRESSURE_STALL_TEN] = ColorPair(Black, White),
  425. [FILE_DESCRIPTOR_USED] = ColorPair(Green, White),
  426. [FILE_DESCRIPTOR_MAX] = ColorPair(Blue, White),
  427. [ZFS_MFU] = ColorPair(Cyan, White),
  428. [ZFS_MRU] = ColorPair(Yellow, White),
  429. [ZFS_ANON] = ColorPair(Magenta, White),
  430. [ZFS_HEADER] = ColorPair(Yellow, White),
  431. [ZFS_OTHER] = ColorPair(Magenta, White),
  432. [ZFS_COMPRESSED] = ColorPair(Cyan, White),
  433. [ZFS_RATIO] = ColorPair(Magenta, White),
  434. [ZRAM_COMPRESSED] = ColorPair(Cyan, White),
  435. [ZRAM_UNCOMPRESSED] = ColorPair(Yellow, White),
  436. [DYNAMIC_GRAY] = ColorPair(Black, White),
  437. [DYNAMIC_DARKGRAY] = A_BOLD | ColorPair(Black, White),
  438. [DYNAMIC_RED] = ColorPair(Red, White),
  439. [DYNAMIC_GREEN] = ColorPair(Green, White),
  440. [DYNAMIC_BLUE] = ColorPair(Blue, White),
  441. [DYNAMIC_CYAN] = ColorPair(Yellow, White),
  442. [DYNAMIC_MAGENTA] = ColorPair(Magenta, White),
  443. [DYNAMIC_YELLOW] = ColorPair(Yellow, White),
  444. [DYNAMIC_WHITE] = A_BOLD | ColorPair(Black, White),
  445. },
  446. [COLORSCHEME_LIGHTTERMINAL] = {
  447. [RESET_COLOR] = ColorPair(Black, Black),
  448. [DEFAULT_COLOR] = ColorPair(Black, Black),
  449. [FUNCTION_BAR] = ColorPair(Black, Cyan),
  450. [FUNCTION_KEY] = ColorPair(Black, Black),
  451. [PANEL_HEADER_FOCUS] = ColorPair(Black, Green),
  452. [PANEL_HEADER_UNFOCUS] = ColorPair(Black, Green),
  453. [PANEL_SELECTION_FOCUS] = ColorPair(Black, Cyan),
  454. [PANEL_SELECTION_FOLLOW] = ColorPair(Black, Yellow),
  455. [PANEL_SELECTION_UNFOCUS] = ColorPair(Blue, Black),
  456. [FAILED_SEARCH] = ColorPair(Red, Cyan),
  457. [FAILED_READ] = ColorPair(Red, Black),
  458. [PAUSED] = A_BOLD | ColorPair(Yellow, Cyan),
  459. [UPTIME] = ColorPair(Yellow, Black),
  460. [BATTERY] = ColorPair(Yellow, Black),
  461. [LARGE_NUMBER] = ColorPair(Red, Black),
  462. [METER_SHADOW] = A_BOLD | ColorPairGrayBlack,
  463. [METER_TEXT] = ColorPair(Blue, Black),
  464. [METER_VALUE] = ColorPair(Black, Black),
  465. [METER_VALUE_ERROR] = A_BOLD | ColorPair(Red, Black),
  466. [METER_VALUE_IOREAD] = ColorPair(Green, Black),
  467. [METER_VALUE_IOWRITE] = ColorPair(Yellow, Black),
  468. [METER_VALUE_NOTICE] = A_BOLD | ColorPairWhiteDefault,
  469. [METER_VALUE_OK] = ColorPair(Green, Black),
  470. [METER_VALUE_WARN] = A_BOLD | ColorPair(Yellow, Black),
  471. [LED_COLOR] = ColorPair(Green, Black),
  472. [TASKS_RUNNING] = ColorPair(Green, Black),
  473. [PROCESS] = ColorPair(Black, Black),
  474. [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
  475. [PROCESS_TAG] = ColorPair(White, Blue),
  476. [PROCESS_MEGABYTES] = ColorPair(Blue, Black),
  477. [PROCESS_GIGABYTES] = ColorPair(Green, Black),
  478. [PROCESS_BASENAME] = ColorPair(Green, Black),
  479. [PROCESS_TREE] = ColorPair(Blue, Black),
  480. [PROCESS_RUN_STATE] = ColorPair(Green, Black),
  481. [PROCESS_D_STATE] = A_BOLD | ColorPair(Red, Black),
  482. [PROCESS_HIGH_PRIORITY] = ColorPair(Red, Black),
  483. [PROCESS_LOW_PRIORITY] = ColorPair(Green, Black),
  484. [PROCESS_NEW] = ColorPair(Black, Green),
  485. [PROCESS_TOMB] = ColorPair(Black, Red),
  486. [PROCESS_THREAD] = ColorPair(Blue, Black),
  487. [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Blue, Black),
  488. [PROCESS_COMM] = ColorPair(Magenta, Black),
  489. [PROCESS_THREAD_COMM] = ColorPair(Yellow, Black),
  490. [PROCESS_PRIV] = ColorPair(Magenta, Black),
  491. [BAR_BORDER] = ColorPair(Blue, Black),
  492. [BAR_SHADOW] = ColorPairGrayBlack,
  493. [SWAP] = ColorPair(Red, Black),
  494. [SWAP_CACHE] = ColorPair(Yellow, Black),
  495. [SWAP_FRONTSWAP] = ColorPairGrayBlack,
  496. [GRAPH_1] = A_BOLD | ColorPair(Cyan, Black),
  497. [GRAPH_2] = ColorPair(Cyan, Black),
  498. [MEMORY_USED] = ColorPair(Green, Black),
  499. [MEMORY_BUFFERS] = ColorPair(Cyan, Black),
  500. [MEMORY_BUFFERS_TEXT] = ColorPair(Cyan, Black),
  501. [MEMORY_CACHE] = ColorPair(Yellow, Black),
  502. [MEMORY_SHARED] = ColorPair(Magenta, Black),
  503. [MEMORY_COMPRESSED] = ColorPairGrayBlack,
  504. [HUGEPAGE_1] = ColorPair(Green, Black),
  505. [HUGEPAGE_2] = ColorPair(Yellow, Black),
  506. [HUGEPAGE_3] = ColorPair(Red, Black),
  507. [HUGEPAGE_4] = ColorPair(Blue, Black),
  508. [LOAD_AVERAGE_FIFTEEN] = ColorPair(Black, Black),
  509. [LOAD_AVERAGE_FIVE] = ColorPair(Black, Black),
  510. [LOAD_AVERAGE_ONE] = ColorPair(Black, Black),
  511. [LOAD] = ColorPairWhiteDefault,
  512. [HELP_BOLD] = ColorPair(Blue, Black),
  513. [HELP_SHADOW] = A_BOLD | ColorPairGrayBlack,
  514. [CLOCK] = ColorPairWhiteDefault,
  515. [DATE] = ColorPairWhiteDefault,
  516. [DATETIME] = ColorPairWhiteDefault,
  517. [CHECK_BOX] = ColorPair(Blue, Black),
  518. [CHECK_MARK] = ColorPair(Black, Black),
  519. [CHECK_TEXT] = ColorPair(Black, Black),
  520. [HOSTNAME] = ColorPairWhiteDefault,
  521. [CPU_NICE] = ColorPair(Cyan, Black),
  522. [CPU_NICE_TEXT] = ColorPair(Cyan, Black),
  523. [CPU_NORMAL] = ColorPair(Green, Black),
  524. [CPU_SYSTEM] = ColorPair(Red, Black),
  525. [CPU_IOWAIT] = A_BOLD | ColorPair(Black, Black),
  526. [CPU_IRQ] = A_BOLD | ColorPair(Blue, Black),
  527. [CPU_SOFTIRQ] = ColorPair(Blue, Black),
  528. [CPU_STEAL] = ColorPair(Black, Black),
  529. [CPU_GUEST] = ColorPair(Black, Black),
  530. [GPU_ENGINE_1] = ColorPair(Green, Black),
  531. [GPU_ENGINE_2] = ColorPair(Yellow, Black),
  532. [GPU_ENGINE_3] = ColorPair(Red, Black),
  533. [GPU_ENGINE_4] = ColorPair(Blue, Black),
  534. [GPU_RESIDUE] = ColorPair(Magenta, Black),
  535. [PANEL_EDIT] = ColorPair(White, Blue),
  536. [SCREENS_OTH_BORDER] = ColorPair(Blue, Black),
  537. [SCREENS_OTH_TEXT] = ColorPair(Blue, Black),
  538. [SCREENS_CUR_BORDER] = ColorPair(Green, Green),
  539. [SCREENS_CUR_TEXT] = ColorPair(Black, Green),
  540. [PRESSURE_STALL_THREEHUNDRED] = ColorPair(Black, Black),
  541. [PRESSURE_STALL_SIXTY] = ColorPair(Black, Black),
  542. [PRESSURE_STALL_TEN] = ColorPair(Black, Black),
  543. [FILE_DESCRIPTOR_USED] = ColorPair(Green, Black),
  544. [FILE_DESCRIPTOR_MAX] = A_BOLD | ColorPair(Blue, Black),
  545. [ZFS_MFU] = ColorPair(Cyan, Black),
  546. [ZFS_MRU] = ColorPair(Yellow, Black),
  547. [ZFS_ANON] = A_BOLD | ColorPair(Magenta, Black),
  548. [ZFS_HEADER] = ColorPair(Black, Black),
  549. [ZFS_OTHER] = A_BOLD | ColorPair(Magenta, Black),
  550. [ZFS_COMPRESSED] = ColorPair(Cyan, Black),
  551. [ZFS_RATIO] = A_BOLD | ColorPair(Magenta, Black),
  552. [ZRAM_COMPRESSED] = ColorPair(Cyan, Black),
  553. [ZRAM_UNCOMPRESSED] = ColorPair(Yellow, Black),
  554. [DYNAMIC_GRAY] = ColorPairGrayBlack,
  555. [DYNAMIC_DARKGRAY] = A_BOLD | ColorPairGrayBlack,
  556. [DYNAMIC_RED] = ColorPair(Red, Black),
  557. [DYNAMIC_GREEN] = ColorPair(Green, Black),
  558. [DYNAMIC_BLUE] = ColorPair(Blue, Black),
  559. [DYNAMIC_CYAN] = ColorPair(Cyan, Black),
  560. [DYNAMIC_MAGENTA] = ColorPair(Magenta, Black),
  561. [DYNAMIC_YELLOW] = ColorPair(Yellow, Black),
  562. [DYNAMIC_WHITE] = ColorPairWhiteDefault,
  563. },
  564. [COLORSCHEME_MIDNIGHT] = {
  565. [RESET_COLOR] = ColorPair(White, Blue),
  566. [DEFAULT_COLOR] = ColorPair(White, Blue),
  567. [FUNCTION_BAR] = ColorPair(Black, Cyan),
  568. [FUNCTION_KEY] = A_NORMAL,
  569. [PANEL_HEADER_FOCUS] = ColorPair(Black, Cyan),
  570. [PANEL_HEADER_UNFOCUS] = ColorPair(Black, Cyan),
  571. [PANEL_SELECTION_FOCUS] = ColorPair(Black, White),
  572. [PANEL_SELECTION_FOLLOW] = ColorPair(Black, Yellow),
  573. [PANEL_SELECTION_UNFOCUS] = A_BOLD | ColorPair(Yellow, Blue),
  574. [FAILED_SEARCH] = ColorPair(Red, Cyan),
  575. [FAILED_READ] = A_BOLD | ColorPair(Red, Blue),
  576. [PAUSED] = A_BOLD | ColorPair(Yellow, Cyan),
  577. [UPTIME] = A_BOLD | ColorPair(Yellow, Blue),
  578. [BATTERY] = A_BOLD | ColorPair(Yellow, Blue),
  579. [LARGE_NUMBER] = A_BOLD | ColorPair(Red, Blue),
  580. [METER_SHADOW] = ColorPair(Cyan, Blue),
  581. [METER_TEXT] = ColorPair(Cyan, Blue),
  582. [METER_VALUE] = A_BOLD | ColorPair(Cyan, Blue),
  583. [METER_VALUE_ERROR] = A_BOLD | ColorPair(Red, Blue),
  584. [METER_VALUE_IOREAD] = ColorPair(Green, Blue),
  585. [METER_VALUE_IOWRITE] = ColorPair(Black, Blue),
  586. [METER_VALUE_NOTICE] = A_BOLD | ColorPair(White, Blue),
  587. [METER_VALUE_OK] = ColorPair(Green, Blue),
  588. [METER_VALUE_WARN] = A_BOLD | ColorPair(Yellow, Black),
  589. [LED_COLOR] = ColorPair(Green, Blue),
  590. [TASKS_RUNNING] = A_BOLD | ColorPair(Green, Blue),
  591. [PROCESS] = ColorPair(White, Blue),
  592. [PROCESS_SHADOW] = A_BOLD | ColorPair(Black, Blue),
  593. [PROCESS_TAG] = A_BOLD | ColorPair(Yellow, Blue),
  594. [PROCESS_MEGABYTES] = ColorPair(Cyan, Blue),
  595. [PROCESS_GIGABYTES] = ColorPair(Green, Blue),
  596. [PROCESS_BASENAME] = A_BOLD | ColorPair(Cyan, Blue),
  597. [PROCESS_TREE] = ColorPair(Cyan, Blue),
  598. [PROCESS_RUN_STATE] = ColorPair(Green, Blue),
  599. [PROCESS_D_STATE] = A_BOLD | ColorPair(Red, Blue),
  600. [PROCESS_HIGH_PRIORITY] = ColorPair(Red, Blue),
  601. [PROCESS_LOW_PRIORITY] = ColorPair(Green, Blue),
  602. [PROCESS_NEW] = ColorPair(Blue, Green),
  603. [PROCESS_TOMB] = ColorPair(Blue, Red),
  604. [PROCESS_THREAD] = ColorPair(Green, Blue),
  605. [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Green, Blue),
  606. [PROCESS_COMM] = ColorPair(Magenta, Blue),
  607. [PROCESS_THREAD_COMM] = ColorPair(Black, Blue),
  608. [PROCESS_PRIV] = ColorPair(Magenta, Blue),
  609. [BAR_BORDER] = A_BOLD | ColorPair(Yellow, Blue),
  610. [BAR_SHADOW] = ColorPair(Cyan, Blue),
  611. [SWAP] = ColorPair(Red, Blue),
  612. [SWAP_CACHE] = A_BOLD | ColorPair(Yellow, Blue),
  613. [SWAP_FRONTSWAP] = A_BOLD | ColorPair(Black, Blue),
  614. [GRAPH_1] = A_BOLD | ColorPair(Cyan, Blue),
  615. [GRAPH_2] = ColorPair(Cyan, Blue),
  616. [MEMORY_USED] = A_BOLD | ColorPair(Green, Blue),
  617. [MEMORY_BUFFERS] = A_BOLD | ColorPair(Cyan, Blue),
  618. [MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Cyan, Blue),
  619. [MEMORY_CACHE] = A_BOLD | ColorPair(Yellow, Blue),
  620. [MEMORY_SHARED] = A_BOLD | ColorPair(Magenta, Blue),
  621. [MEMORY_COMPRESSED] = A_BOLD | ColorPair(Black, Blue),
  622. [HUGEPAGE_1] = A_BOLD | ColorPair(Green, Blue),
  623. [HUGEPAGE_2] = A_BOLD | ColorPair(Yellow, Blue),
  624. [HUGEPAGE_3] = A_BOLD | ColorPair(Red, Blue),
  625. [HUGEPAGE_4] = A_BOLD | ColorPair(White, Blue),
  626. [LOAD_AVERAGE_FIFTEEN] = A_BOLD | ColorPair(Black, Blue),
  627. [LOAD_AVERAGE_FIVE] = A_NORMAL | ColorPair(White, Blue),
  628. [LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(White, Blue),
  629. [LOAD] = A_BOLD | ColorPair(White, Blue),
  630. [HELP_BOLD] = A_BOLD | ColorPair(Cyan, Blue),
  631. [HELP_SHADOW] = A_BOLD | ColorPair(Black, Blue),
  632. [CLOCK] = ColorPair(White, Blue),
  633. [DATE] = ColorPair(White, Blue),
  634. [DATETIME] = ColorPair(White, Blue),
  635. [CHECK_BOX] = ColorPair(Cyan, Blue),
  636. [CHECK_MARK] = A_BOLD | ColorPair(White, Blue),
  637. [CHECK_TEXT] = A_NORMAL | ColorPair(White, Blue),
  638. [HOSTNAME] = ColorPair(White, Blue),
  639. [CPU_NICE] = A_BOLD | ColorPair(Cyan, Blue),
  640. [CPU_NICE_TEXT] = A_BOLD | ColorPair(Cyan, Blue),
  641. [CPU_NORMAL] = A_BOLD | ColorPair(Green, Blue),
  642. [CPU_SYSTEM] = A_BOLD | ColorPair(Red, Blue),
  643. [CPU_IOWAIT] = A_BOLD | ColorPair(Black, Blue),
  644. [CPU_IRQ] = A_BOLD | ColorPair(Black, Blue),
  645. [CPU_SOFTIRQ] = ColorPair(Black, Blue),
  646. [CPU_STEAL] = ColorPair(White, Blue),
  647. [CPU_GUEST] = ColorPair(White, Blue),
  648. [GPU_ENGINE_1] = A_BOLD | ColorPair(Green, Blue),
  649. [GPU_ENGINE_2] = A_BOLD | ColorPair(Yellow, Blue),
  650. [GPU_ENGINE_3] = A_BOLD | ColorPair(Red, Blue),
  651. [GPU_ENGINE_4] = A_BOLD | ColorPair(White, Blue),
  652. [GPU_RESIDUE] = A_BOLD | ColorPair(Magenta, Blue),
  653. [PANEL_EDIT] = ColorPair(White, Blue),
  654. [SCREENS_OTH_BORDER] = A_BOLD | ColorPair(Yellow, Blue),
  655. [SCREENS_OTH_TEXT] = ColorPair(Cyan, Blue),
  656. [SCREENS_CUR_BORDER] = ColorPair(Cyan, Cyan),
  657. [SCREENS_CUR_TEXT] = ColorPair(Black, Cyan),
  658. [PRESSURE_STALL_THREEHUNDRED] = A_BOLD | ColorPair(Black, Blue),
  659. [PRESSURE_STALL_SIXTY] = A_NORMAL | ColorPair(White, Blue),
  660. [PRESSURE_STALL_TEN] = A_BOLD | ColorPair(White, Blue),
  661. [FILE_DESCRIPTOR_USED] = A_BOLD | ColorPair(Green, Blue),
  662. [FILE_DESCRIPTOR_MAX] = A_BOLD | ColorPair(Red, Blue),
  663. [ZFS_MFU] = A_BOLD | ColorPair(White, Blue),
  664. [ZFS_MRU] = A_BOLD | ColorPair(Yellow, Blue),
  665. [ZFS_ANON] = A_BOLD | ColorPair(Magenta, Blue),
  666. [ZFS_HEADER] = A_BOLD | ColorPair(Yellow, Blue),
  667. [ZFS_OTHER] = A_BOLD | ColorPair(Magenta, Blue),
  668. [ZFS_COMPRESSED] = A_BOLD | ColorPair(White, Blue),
  669. [ZFS_RATIO] = A_BOLD | ColorPair(Magenta, Blue),
  670. [ZRAM_COMPRESSED] = ColorPair(Cyan, Blue),
  671. [ZRAM_UNCOMPRESSED] = ColorPair(Yellow, Blue),
  672. [DYNAMIC_GRAY] = ColorPairGrayBlack,
  673. [DYNAMIC_DARKGRAY] = A_BOLD | ColorPairGrayBlack,
  674. [DYNAMIC_RED] = ColorPair(Red, Blue),
  675. [DYNAMIC_GREEN] = ColorPair(Green, Blue),
  676. [DYNAMIC_BLUE] = ColorPair(Black, Blue),
  677. [DYNAMIC_CYAN] = ColorPair(Cyan, Blue),
  678. [DYNAMIC_MAGENTA] = ColorPair(Magenta, Blue),
  679. [DYNAMIC_YELLOW] = ColorPair(Yellow, Blue),
  680. [DYNAMIC_WHITE] = ColorPair(White, Blue),
  681. },
  682. [COLORSCHEME_BLACKNIGHT] = {
  683. [RESET_COLOR] = ColorPair(Cyan, Black),
  684. [DEFAULT_COLOR] = ColorPair(Cyan, Black),
  685. [FUNCTION_BAR] = ColorPair(Black, Green),
  686. [FUNCTION_KEY] = ColorPair(Cyan, Black),
  687. [PANEL_HEADER_FOCUS] = ColorPair(Black, Green),
  688. [PANEL_HEADER_UNFOCUS] = ColorPair(Black, Green),
  689. [PANEL_SELECTION_FOCUS] = ColorPair(Black, Cyan),
  690. [PANEL_SELECTION_FOLLOW] = ColorPair(Black, Yellow),
  691. [PANEL_SELECTION_UNFOCUS] = ColorPair(Black, White),
  692. [FAILED_SEARCH] = ColorPair(Red, Green),
  693. [FAILED_READ] = A_BOLD | ColorPair(Red, Black),
  694. [PAUSED] = A_BOLD | ColorPair(Yellow, Green),
  695. [UPTIME] = ColorPair(Green, Black),
  696. [BATTERY] = ColorPair(Green, Black),
  697. [LARGE_NUMBER] = A_BOLD | ColorPair(Red, Black),
  698. [METER_SHADOW] = A_BOLD | ColorPairGrayBlack,
  699. [METER_TEXT] = ColorPair(Cyan, Black),
  700. [METER_VALUE] = ColorPair(Green, Black),
  701. [METER_VALUE_ERROR] = A_BOLD | ColorPair(Red, Black),
  702. [METER_VALUE_IOREAD] = ColorPair(Green, Black),
  703. [METER_VALUE_IOWRITE] = ColorPair(Blue, Black),
  704. [METER_VALUE_NOTICE] = A_BOLD | ColorPair(White, Black),
  705. [METER_VALUE_OK] = ColorPair(Green, Black),
  706. [METER_VALUE_WARN] = A_BOLD | ColorPair(Yellow, Black),
  707. [LED_COLOR] = ColorPair(Green, Black),
  708. [TASKS_RUNNING] = A_BOLD | ColorPair(Green, Black),
  709. [PROCESS] = ColorPair(Cyan, Black),
  710. [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
  711. [PROCESS_TAG] = A_BOLD | ColorPair(Yellow, Black),
  712. [PROCESS_MEGABYTES] = A_BOLD | ColorPair(Green, Black),
  713. [PROCESS_GIGABYTES] = A_BOLD | ColorPair(Yellow, Black),
  714. [PROCESS_BASENAME] = A_BOLD | ColorPair(Green, Black),
  715. [PROCESS_TREE] = ColorPair(Cyan, Black),
  716. [PROCESS_THREAD] = ColorPair(Green, Black),
  717. [PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(Blue, Black),
  718. [PROCESS_COMM] = ColorPair(Magenta, Black),
  719. [PROCESS_THREAD_COMM] = ColorPair(Yellow, Black),
  720. [PROCESS_RUN_STATE] = ColorPair(Green, Black),
  721. [PROCESS_D_STATE] = A_BOLD | ColorPair(Red, Black),
  722. [PROCESS_HIGH_PRIORITY] = ColorPair(Red, Black),
  723. [PROCESS_LOW_PRIORITY] = ColorPair(Green, Black),
  724. [PROCESS_NEW] = ColorPair(Black, Green),
  725. [PROCESS_TOMB] = ColorPair(Black, Red),
  726. [PROCESS_PRIV] = ColorPair(Magenta, Black),
  727. [BAR_BORDER] = A_BOLD | ColorPair(Green, Black),
  728. [BAR_SHADOW] = ColorPair(Cyan, Black),
  729. [SWAP] = ColorPair(Red, Black),
  730. [SWAP_CACHE] = ColorPair(Yellow, Black),
  731. [SWAP_FRONTSWAP] = ColorPair(Yellow, Black),
  732. [GRAPH_1] = A_BOLD | ColorPair(Green, Black),
  733. [GRAPH_2] = ColorPair(Green, Black),
  734. [MEMORY_USED] = ColorPair(Green, Black),
  735. [MEMORY_BUFFERS] = ColorPair(Blue, Black),
  736. [MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Blue, Black),
  737. [MEMORY_CACHE] = ColorPair(Yellow, Black),
  738. [MEMORY_SHARED] = ColorPair(Magenta, Black),
  739. [MEMORY_COMPRESSED] = ColorPair(Yellow, Black),
  740. [HUGEPAGE_1] = ColorPair(Green, Black),
  741. [HUGEPAGE_2] = ColorPair(Yellow, Black),
  742. [HUGEPAGE_3] = ColorPair(Red, Black),
  743. [HUGEPAGE_4] = ColorPair(Blue, Black),
  744. [LOAD_AVERAGE_FIFTEEN] = ColorPair(Green, Black),
  745. [LOAD_AVERAGE_FIVE] = ColorPair(Green, Black),
  746. [LOAD_AVERAGE_ONE] = A_BOLD | ColorPair(Green, Black),
  747. [LOAD] = A_BOLD,
  748. [HELP_BOLD] = A_BOLD | ColorPair(Cyan, Black),
  749. [HELP_SHADOW] = A_BOLD | ColorPairGrayBlack,
  750. [CLOCK] = ColorPair(Green, Black),
  751. [CHECK_BOX] = ColorPair(Green, Black),
  752. [CHECK_MARK] = A_BOLD | ColorPair(Green, Black),
  753. [CHECK_TEXT] = ColorPair(Cyan, Black),
  754. [HOSTNAME] = ColorPair(Green, Black),
  755. [CPU_NICE] = ColorPair(Blue, Black),
  756. [CPU_NICE_TEXT] = A_BOLD | ColorPair(Blue, Black),
  757. [CPU_NORMAL] = ColorPair(Green, Black),
  758. [CPU_SYSTEM] = ColorPair(Red, Black),
  759. [CPU_IOWAIT] = ColorPair(Yellow, Black),
  760. [CPU_IRQ] = A_BOLD | ColorPair(Blue, Black),
  761. [CPU_SOFTIRQ] = ColorPair(Blue, Black),
  762. [CPU_STEAL] = ColorPair(Cyan, Black),
  763. [CPU_GUEST] = ColorPair(Cyan, Black),
  764. [GPU_ENGINE_1] = ColorPair(Green, Black),
  765. [GPU_ENGINE_2] = ColorPair(Yellow, Black),
  766. [GPU_ENGINE_3] = ColorPair(Red, Black),
  767. [GPU_ENGINE_4] = ColorPair(Blue, Black),
  768. [GPU_RESIDUE] = ColorPair(Magenta, Black),
  769. [PANEL_EDIT] = ColorPair(White, Cyan),
  770. [SCREENS_OTH_BORDER] = ColorPair(White, Black),
  771. [SCREENS_OTH_TEXT] = ColorPair(Cyan, Black),
  772. [SCREENS_CUR_BORDER] = A_BOLD | ColorPair(White, Black),
  773. [SCREENS_CUR_TEXT] = A_BOLD | ColorPair(Green, Black),
  774. [PRESSURE_STALL_THREEHUNDRED] = ColorPair(Green, Black),
  775. [PRESSURE_STALL_SIXTY] = ColorPair(Green, Black),
  776. [PRESSURE_STALL_TEN] = A_BOLD | ColorPair(Green, Black),
  777. [FILE_DESCRIPTOR_USED] = ColorPair(Green, Black),
  778. [FILE_DESCRIPTOR_MAX] = A_BOLD | ColorPair(Blue, Black),
  779. [ZFS_MFU] = ColorPair(Blue, Black),
  780. [ZFS_MRU] = ColorPair(Yellow, Black),
  781. [ZFS_ANON] = ColorPair(Magenta, Black),
  782. [ZFS_HEADER] = ColorPair(Yellow, Black),
  783. [ZFS_OTHER] = ColorPair(Magenta, Black),
  784. [ZFS_COMPRESSED] = ColorPair(Blue, Black),
  785. [ZFS_RATIO] = ColorPair(Magenta, Black),
  786. [ZRAM_COMPRESSED] = ColorPair(Blue, Black),
  787. [ZRAM_UNCOMPRESSED] = ColorPair(Yellow, Black),
  788. [DYNAMIC_GRAY] = ColorPairGrayBlack,
  789. [DYNAMIC_DARKGRAY] = A_BOLD | ColorPairGrayBlack,
  790. [DYNAMIC_RED] = ColorPair(Red, Black),
  791. [DYNAMIC_GREEN] = ColorPair(Green, Black),
  792. [DYNAMIC_BLUE] = ColorPair(Blue, Black),
  793. [DYNAMIC_CYAN] = ColorPair(Cyan, Black),
  794. [DYNAMIC_MAGENTA] = ColorPair(Magenta, Black),
  795. [DYNAMIC_YELLOW] = ColorPair(Yellow, Black),
  796. [DYNAMIC_WHITE] = ColorPair(White, Black),
  797. },
  798. [COLORSCHEME_BROKENGRAY] = { 0 } // dynamically generated.
  799. };
  800. static bool CRT_retainScreenOnExit = false;
  801. int CRT_scrollHAmount = 5;
  802. int CRT_scrollWheelVAmount = 10;
  803. ColorScheme CRT_colorScheme = COLORSCHEME_DEFAULT;
  804. ATTR_NORETURN
  805. static void CRT_handleSIGTERM(int sgn) {
  806. CRT_done();
  807. if (!CRT_settings->changed)
  808. _exit(0);
  809. const char* signal_str = strsignal(sgn);
  810. if (!signal_str)
  811. signal_str = "unknown reason";
  812. char err_buf[512];
  813. snprintf(err_buf, sizeof(err_buf),
  814. "A signal %d (%s) was received, exiting without persisting settings to htoprc.\n",
  815. sgn, signal_str);
  816. full_write_str(STDERR_FILENO, err_buf);
  817. _exit(0);
  818. }
  819. #ifndef NDEBUG
  820. static int stderrRedirectNewFd = -1;
  821. static int stderrRedirectBackupFd = -1;
  822. static int createStderrCacheFile(void) {
  823. #if defined(HAVE_MEMFD_CREATE)
  824. return memfd_create("htop.stderr-redirect", 0);
  825. #elif defined(O_TMPFILE)
  826. return open("/tmp", O_TMPFILE | O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
  827. #else
  828. char tmpName[] = "htop.stderr-redirectXXXXXX";
  829. mode_t curUmask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
  830. int r = mkstemp(tmpName);
  831. umask(curUmask);
  832. if (r < 0)
  833. return r;
  834. (void) unlink(tmpName);
  835. return r;
  836. #endif /* HAVE_MEMFD_CREATE */
  837. }
  838. static void redirectStderr(void) {
  839. stderrRedirectNewFd = createStderrCacheFile();
  840. if (stderrRedirectNewFd < 0) {
  841. /* ignore failure */
  842. return;
  843. }
  844. stderrRedirectBackupFd = dup(STDERR_FILENO);
  845. dup2(stderrRedirectNewFd, STDERR_FILENO);
  846. }
  847. static void dumpStderr(void) {
  848. if (stderrRedirectNewFd < 0)
  849. return;
  850. fsync(STDERR_FILENO);
  851. dup2(stderrRedirectBackupFd, STDERR_FILENO);
  852. close(stderrRedirectBackupFd);
  853. stderrRedirectBackupFd = -1;
  854. lseek(stderrRedirectNewFd, 0, SEEK_SET);
  855. bool header = false;
  856. char buffer[8192];
  857. for (;;) {
  858. errno = 0;
  859. ssize_t res = read(stderrRedirectNewFd, buffer, sizeof(buffer));
  860. if (res < 0) {
  861. if (errno == EINTR)
  862. continue;
  863. break;
  864. }
  865. if (res == 0) {
  866. break;
  867. }
  868. if (res > 0) {
  869. if (!header) {
  870. full_write_str(STDERR_FILENO, ">>>>>>>>>> stderr output >>>>>>>>>>\n");
  871. header = true;
  872. }
  873. full_write(STDERR_FILENO, buffer, res);
  874. }
  875. }
  876. if (header)
  877. full_write_str(STDERR_FILENO, "\n<<<<<<<<<< stderr output <<<<<<<<<<\n");
  878. close(stderrRedirectNewFd);
  879. stderrRedirectNewFd = -1;
  880. }
  881. void CRT_debug_impl(const char* file, size_t lineno, const char* func, const char* fmt, ...) {
  882. va_list args;
  883. fprintf(stderr, "[%s:%zu (%s)]: ", file, lineno, func);
  884. va_start(args, fmt);
  885. vfprintf(stderr, fmt, args);
  886. va_end(args);
  887. fprintf(stderr, "\n");
  888. }
  889. #else /* !NDEBUG */
  890. static void redirectStderr(void) {
  891. }
  892. static void dumpStderr(void) {
  893. }
  894. #endif /* !NDEBUG */
  895. static struct sigaction old_sig_handler[32];
  896. static void CRT_installSignalHandlers(void) {
  897. struct sigaction act;
  898. sigemptyset(&act.sa_mask);
  899. act.sa_flags = (int)SA_RESETHAND | SA_NODEFER;
  900. act.sa_handler = CRT_handleSIGSEGV;
  901. sigaction(SIGSEGV, &act, &old_sig_handler[SIGSEGV]);
  902. sigaction(SIGFPE, &act, &old_sig_handler[SIGFPE]);
  903. sigaction(SIGILL, &act, &old_sig_handler[SIGILL]);
  904. sigaction(SIGBUS, &act, &old_sig_handler[SIGBUS]);
  905. sigaction(SIGPIPE, &act, &old_sig_handler[SIGPIPE]);
  906. sigaction(SIGSYS, &act, &old_sig_handler[SIGSYS]);
  907. sigaction(SIGABRT, &act, &old_sig_handler[SIGABRT]);
  908. signal(SIGCHLD, SIG_DFL);
  909. signal(SIGINT, CRT_handleSIGTERM);
  910. signal(SIGTERM, CRT_handleSIGTERM);
  911. signal(SIGQUIT, CRT_handleSIGTERM);
  912. signal(SIGUSR1, SIG_IGN);
  913. signal(SIGUSR2, SIG_IGN);
  914. }
  915. void CRT_resetSignalHandlers(void) {
  916. sigaction(SIGSEGV, &old_sig_handler[SIGSEGV], NULL);
  917. sigaction(SIGFPE, &old_sig_handler[SIGFPE], NULL);
  918. sigaction(SIGILL, &old_sig_handler[SIGILL], NULL);
  919. sigaction(SIGBUS, &old_sig_handler[SIGBUS], NULL);
  920. sigaction(SIGPIPE, &old_sig_handler[SIGPIPE], NULL);
  921. sigaction(SIGSYS, &old_sig_handler[SIGSYS], NULL);
  922. sigaction(SIGABRT, &old_sig_handler[SIGABRT], NULL);
  923. signal(SIGINT, SIG_DFL);
  924. signal(SIGTERM, SIG_DFL);
  925. signal(SIGQUIT, SIG_DFL);
  926. signal(SIGUSR1, SIG_DFL);
  927. signal(SIGUSR2, SIG_DFL);
  928. }
  929. #ifdef HAVE_GETMOUSE
  930. void CRT_setMouse(bool enabled) {
  931. if (enabled) {
  932. #if NCURSES_MOUSE_VERSION > 1
  933. mousemask(BUTTON1_RELEASED | BUTTON4_PRESSED | BUTTON5_PRESSED, NULL);
  934. #else
  935. mousemask(BUTTON1_RELEASED, NULL);
  936. #endif
  937. } else {
  938. mousemask(0, NULL);
  939. }
  940. }
  941. #endif
  942. static bool terminalSupportsDefinedKeys(const char* termType) {
  943. if (!termType) {
  944. return false;
  945. }
  946. switch (termType[0]) {
  947. case 'a':
  948. if (String_eq(termType, "alacritty")) {
  949. return true;
  950. }
  951. break;
  952. case 's':
  953. if (termType[1] == 't' && (termType[2] == '-' || !termType[2])) {
  954. return true;
  955. }
  956. if (String_eq(termType, "screen")) {
  957. return true;
  958. }
  959. break;
  960. case 't':
  961. if (String_eq(termType, "tmux")) {
  962. return true;
  963. }
  964. break;
  965. case 'v':
  966. if (String_eq(termType, "vt220")) {
  967. return true;
  968. }
  969. break;
  970. case 'x':
  971. if (String_eq(termType, "xterm")) {
  972. return true;
  973. }
  974. break;
  975. }
  976. return false;
  977. }
  978. void CRT_init(const Settings* settings, bool allowUnicode, bool retainScreenOnExit) {
  979. initscr();
  980. if (retainScreenOnExit) {
  981. CRT_retainScreenOnExit = true;
  982. refresh();
  983. tputs(exit_ca_mode, 0, putchar);
  984. tputs(clear_screen, 0, putchar);
  985. fflush(stdout);
  986. enter_ca_mode = 0;
  987. exit_ca_mode = 0;
  988. }
  989. redirectStderr();
  990. noecho();
  991. CRT_settings = settings;
  992. CRT_colors = CRT_colorSchemes[settings->colorScheme];
  993. CRT_colorScheme = settings->colorScheme;
  994. for (int i = 0; i < LAST_COLORELEMENT; i++) {
  995. unsigned int color = CRT_colorSchemes[COLORSCHEME_DEFAULT][i];
  996. CRT_colorSchemes[COLORSCHEME_BROKENGRAY][i] = color == (A_BOLD | ColorPairGrayBlack) ? ColorPair(White, Black) : color;
  997. }
  998. halfdelay(settings->delay);
  999. nonl();
  1000. intrflush(stdscr, false);
  1001. keypad(stdscr, true);
  1002. #ifdef HAVE_GETMOUSE
  1003. mouseinterval(0);
  1004. #endif
  1005. curs_set(0);
  1006. if (has_colors()) {
  1007. start_color();
  1008. }
  1009. const char* termType = getenv("TERM");
  1010. if (termType && String_eq(termType, "linux")) {
  1011. CRT_scrollHAmount = 20;
  1012. } else {
  1013. CRT_scrollHAmount = 5;
  1014. }
  1015. if (terminalSupportsDefinedKeys(termType)) {
  1016. #ifdef HTOP_NETBSD
  1017. #define define_key(s_, k_) define_key((char*)s_, k_)
  1018. IGNORE_WCASTQUAL_BEGIN
  1019. #endif
  1020. define_key("\033[H", KEY_HOME);
  1021. define_key("\033[F", KEY_END);
  1022. define_key("\033[7~", KEY_HOME);
  1023. define_key("\033[8~", KEY_END);
  1024. define_key("\033OP", KEY_F(1));
  1025. define_key("\033OQ", KEY_F(2));
  1026. define_key("\033OR", KEY_F(3));
  1027. define_key("\033OS", KEY_F(4));
  1028. define_key("\033O2R", KEY_F(15));
  1029. define_key("\033[11~", KEY_F(1));
  1030. define_key("\033[12~", KEY_F(2));
  1031. define_key("\033[13~", KEY_F(3));
  1032. define_key("\033[14~", KEY_F(4));
  1033. define_key("\033[14;2~", KEY_F(15));
  1034. define_key("\033[17;2~", KEY_F(18));
  1035. define_key("\033[Z", KEY_SHIFT_TAB);
  1036. char sequence[3] = "\033a";
  1037. for (char c = 'a'; c <= 'z'; c++) {
  1038. sequence[1] = c;
  1039. define_key(sequence, KEY_ALT('A' + (c - 'a')));
  1040. }
  1041. define_key("\033[I", KEY_FOCUS_IN);
  1042. define_key("\033[O", KEY_FOCUS_OUT);
  1043. #ifdef HTOP_NETBSD
  1044. IGNORE_WCASTQUAL_END
  1045. #undef define_key
  1046. #endif
  1047. }
  1048. if (termType && (String_startsWith(termType, "rxvt"))) {
  1049. define_key("\033[Z", KEY_SHIFT_TAB);
  1050. }
  1051. CRT_installSignalHandlers();
  1052. use_default_colors();
  1053. if (!has_colors())
  1054. CRT_colorScheme = COLORSCHEME_MONOCHROME;
  1055. CRT_setColors(CRT_colorScheme);
  1056. #ifdef HAVE_LIBNCURSESW
  1057. if (allowUnicode && String_eq(nl_langinfo(CODESET), "UTF-8")) {
  1058. CRT_utf8 = true;
  1059. } else {
  1060. CRT_utf8 = false;
  1061. }
  1062. #else
  1063. (void) allowUnicode;
  1064. #endif
  1065. CRT_treeStr =
  1066. #ifdef HAVE_LIBNCURSESW
  1067. CRT_utf8 ? CRT_treeStrUtf8 :
  1068. #endif
  1069. CRT_treeStrAscii;
  1070. CRT_setMouse(settings->enableMouse);
  1071. CRT_degreeSign = initDegreeSign();
  1072. }
  1073. void CRT_done(void) {
  1074. int resetColor = CRT_colors ? CRT_colors[RESET_COLOR] : CRT_colorSchemes[COLORSCHEME_DEFAULT][RESET_COLOR];
  1075. attron(resetColor);
  1076. mvhline(LINES - 1, 0, ' ', COLS);
  1077. attroff(resetColor);
  1078. refresh();
  1079. if (CRT_retainScreenOnExit) {
  1080. mvcur(-1, -1, LINES - 1, 0);
  1081. }
  1082. curs_set(1);
  1083. endwin();
  1084. dumpStderr();
  1085. }
  1086. void CRT_fatalError(const char* note) {
  1087. const char* sysMsg = strerror(errno);
  1088. CRT_done();
  1089. fprintf(stderr, "%s: %s\n", note, sysMsg);
  1090. exit(2);
  1091. }
  1092. int CRT_readKey(void) {
  1093. nocbreak();
  1094. cbreak();
  1095. nodelay(stdscr, FALSE);
  1096. int ret = getch();
  1097. halfdelay(CRT_settings->delay);
  1098. return ret;
  1099. }
  1100. void CRT_disableDelay(void) {
  1101. nocbreak();
  1102. cbreak();
  1103. nodelay(stdscr, TRUE);
  1104. }
  1105. void CRT_enableDelay(void) {
  1106. halfdelay(CRT_settings->delay);
  1107. }
  1108. void CRT_setColors(int colorScheme) {
  1109. CRT_colorScheme = colorScheme;
  1110. for (short int i = 0; i < 8; i++) {
  1111. for (short int j = 0; j < 8; j++) {
  1112. if (ColorIndex(i, j) != ColorIndexGrayBlack && ColorIndex(i, j) != ColorIndexWhiteDefault) {
  1113. short int bg = (colorScheme != COLORSCHEME_BLACKNIGHT) && (j == 0) ? -1 : j;
  1114. init_pair(ColorIndex(i, j), i, bg);
  1115. }
  1116. }
  1117. }
  1118. short int grayBlackFg = COLORS > 8 ? 8 : 0;
  1119. short int grayBlackBg = (colorScheme != COLORSCHEME_BLACKNIGHT) ? -1 : 0;
  1120. init_pair(ColorIndexGrayBlack, grayBlackFg, grayBlackBg);
  1121. init_pair(ColorIndexWhiteDefault, White, -1);
  1122. CRT_colors = CRT_colorSchemes[colorScheme];
  1123. }
  1124. #ifdef PRINT_BACKTRACE
  1125. static void print_backtrace(void) {
  1126. #if defined(HAVE_LIBUNWIND_H) && defined(HAVE_LIBUNWIND)
  1127. unw_context_t context;
  1128. unw_getcontext(&context);
  1129. unw_cursor_t cursor;
  1130. unw_init_local(&cursor, &context);
  1131. unsigned int item = 0;
  1132. char err_buf[1024];
  1133. while (unw_step(&cursor) > 0) {
  1134. unw_word_t pc;
  1135. unw_get_reg(&cursor, UNW_REG_IP, &pc);
  1136. if (pc == 0)
  1137. break;
  1138. char symbolName[256] = "?";
  1139. unw_word_t offset = 0;
  1140. unw_get_proc_name(&cursor, symbolName, sizeof(symbolName), &offset);
  1141. unw_proc_info_t pip;
  1142. pip.unwind_info = 0;
  1143. const char* fname = "?";
  1144. const void* ptr = 0;
  1145. if (unw_get_proc_info(&cursor, &pip) == 0) {
  1146. ptr = (const void*)(pip.start_ip + offset);
  1147. #ifdef HAVE_DLADDR
  1148. Dl_info dlinfo;
  1149. if (dladdr(ptr, &dlinfo) && dlinfo.dli_fname && *dlinfo.dli_fname)
  1150. fname = dlinfo.dli_fname;
  1151. #endif
  1152. }
  1153. const bool is_signal_frame = unw_is_signal_frame(&cursor) > 0;
  1154. const char* frame = is_signal_frame ? " {signal frame}" : "";
  1155. snprintf(err_buf, sizeof(err_buf), "%2u: %#14lx %s (%s+%#lx) [%p]%s\n", item++, pc, fname, symbolName, offset, ptr, frame);
  1156. full_write_str(STDERR_FILENO, err_buf);
  1157. }
  1158. #elif defined(HAVE_EXECINFO_H)
  1159. void* backtraceArray[256];
  1160. int nptrs = backtrace(backtraceArray, ARRAYSIZE(backtraceArray));
  1161. if (nptrs > 0) {
  1162. backtrace_symbols_fd(backtraceArray, nptrs, STDERR_FILENO);
  1163. } else {
  1164. full_write_str(STDERR_FILENO,
  1165. "[No backtrace information available from libc]\n"
  1166. );
  1167. }
  1168. #else
  1169. #error No implementation for print_backtrace()!
  1170. #endif
  1171. }
  1172. #endif
  1173. void CRT_handleSIGSEGV(int signal) {
  1174. CRT_done();
  1175. char err_buf[512];
  1176. snprintf(err_buf, sizeof(err_buf), "\n\n"
  1177. "FATAL PROGRAM ERROR DETECTED\n"
  1178. "============================\n"
  1179. "Please check at https://htop.dev/issues whether this issue has already been reported.\n"
  1180. "If no similar issue has been reported before, please create a new issue with the following information:\n"
  1181. " - Your %s version: '"VERSION"'\n"
  1182. " - Your OS and kernel version (uname -a)\n"
  1183. " - Your distribution and release (lsb_release -a)\n"
  1184. " - Likely steps to reproduce (How did it happen?)\n",
  1185. program
  1186. );
  1187. full_write_str(STDERR_FILENO, err_buf);
  1188. #ifdef PRINT_BACKTRACE
  1189. full_write_str(STDERR_FILENO, " - Backtrace of the issue (see below)\n");
  1190. #endif
  1191. full_write_str(STDERR_FILENO,
  1192. "\n"
  1193. );
  1194. const char* signal_str = strsignal(signal);
  1195. if (!signal_str) {
  1196. signal_str = "unknown reason";
  1197. }
  1198. snprintf(err_buf, sizeof(err_buf),
  1199. "Error information:\n"
  1200. "------------------\n"
  1201. "A signal %d (%s) was received.\n"
  1202. "\n",
  1203. signal, signal_str
  1204. );
  1205. full_write_str(STDERR_FILENO, err_buf);
  1206. full_write_str(STDERR_FILENO,
  1207. "Setting information:\n"
  1208. "--------------------\n");
  1209. Settings_write(CRT_settings, true);
  1210. full_write_str(STDERR_FILENO, "\n\n");
  1211. #ifdef PRINT_BACKTRACE
  1212. full_write_str(STDERR_FILENO,
  1213. "Backtrace information:\n"
  1214. "----------------------\n"
  1215. );
  1216. print_backtrace();
  1217. snprintf(err_buf, sizeof(err_buf),
  1218. "\n"
  1219. "To make the above information more practical to work with, "
  1220. "please also provide a disassembly of your %s binary. "
  1221. "This can usually be done by running the following command:\n"
  1222. "\n",
  1223. program
  1224. );
  1225. full_write_str(STDERR_FILENO, err_buf);
  1226. #ifdef HTOP_DARWIN
  1227. snprintf(err_buf, sizeof(err_buf), " otool -tvV `which %s` > ~/%s.otool\n", program, program);
  1228. #else
  1229. snprintf(err_buf, sizeof(err_buf), " objdump -d -S -w `which %s` > ~/%s.objdump\n", program, program);
  1230. #endif
  1231. full_write_str(STDERR_FILENO, err_buf);
  1232. full_write_str(STDERR_FILENO,
  1233. "\n"
  1234. "Please include the generated file in your report.\n"
  1235. );
  1236. #endif
  1237. snprintf(err_buf, sizeof(err_buf),
  1238. "Running this program with debug symbols or inside a debugger may provide further insights.\n"
  1239. "\n"
  1240. "Thank you for helping to improve %s!\n"
  1241. "\n",
  1242. program
  1243. );
  1244. full_write_str(STDERR_FILENO, err_buf);
  1245. /* Call old sigsegv handler; may be default exit or third party one (e.g. ASAN) */
  1246. if (sigaction(signal, &old_sig_handler[signal], NULL) < 0) {
  1247. /* This avoids an infinite loop in case the handler could not be reset. */
  1248. full_write_str(STDERR_FILENO,
  1249. "!!! Chained handler could not be restored. Forcing exit.\n"
  1250. );
  1251. _exit(1);
  1252. }
  1253. /* Trigger the previous signal handler. */
  1254. raise(signal);
  1255. // Always terminate, even if installed handler returns
  1256. full_write_str(STDERR_FILENO,
  1257. "!!! Chained handler did not exit. Forcing exit.\n"
  1258. );
  1259. _exit(1);
  1260. }