Header.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. htop - Header.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 "Header.h"
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "CRT.h"
  17. #include "CPUMeter.h"
  18. #include "DynamicMeter.h"
  19. #include "Macros.h"
  20. #include "Object.h"
  21. #include "Platform.h"
  22. #include "ProvideCurses.h"
  23. #include "Settings.h"
  24. #include "XUtils.h"
  25. Header* Header_new(Machine* host, HeaderLayout hLayout) {
  26. Header* this = xCalloc(1, sizeof(Header));
  27. this->columns = xMallocArray(HeaderLayout_getColumns(hLayout), sizeof(Vector*));
  28. this->headerLayout = hLayout;
  29. this->host = host;
  30. Header_forEachColumn(this, i) {
  31. this->columns[i] = Vector_new(Class(Meter), true, DEFAULT_SIZE);
  32. }
  33. return this;
  34. }
  35. void Header_delete(Header* this) {
  36. Header_forEachColumn(this, i) {
  37. Vector_delete(this->columns[i]);
  38. }
  39. free(this->columns);
  40. free(this);
  41. }
  42. void Header_setLayout(Header* this, HeaderLayout hLayout) {
  43. size_t oldColumns = HeaderLayout_getColumns(this->headerLayout);
  44. size_t newColumns = HeaderLayout_getColumns(hLayout);
  45. this->headerLayout = hLayout;
  46. if (newColumns == oldColumns)
  47. return;
  48. if (newColumns > oldColumns) {
  49. this->columns = xReallocArray(this->columns, newColumns, sizeof(Vector*));
  50. for (size_t i = oldColumns; i < newColumns; i++)
  51. this->columns[i] = Vector_new(Class(Meter), true, DEFAULT_SIZE);
  52. } else {
  53. // move meters from to-be-deleted columns into last one
  54. for (size_t i = newColumns; i < oldColumns; i++) {
  55. for (int j = this->columns[i]->items - 1; j >= 0; j--) {
  56. Vector_add(this->columns[newColumns - 1], Vector_take(this->columns[i], j));
  57. }
  58. Vector_delete(this->columns[i]);
  59. }
  60. this->columns = xReallocArray(this->columns, newColumns, sizeof(Vector*));
  61. }
  62. Header_calculateHeight(this);
  63. }
  64. static void Header_addMeterByName(Header* this, const char* name, MeterModeId mode, unsigned int column) {
  65. assert(column < HeaderLayout_getColumns(this->headerLayout));
  66. Vector* meters = this->columns[column];
  67. const char* paren = strchr(name, '(');
  68. unsigned int param = 0;
  69. size_t nameLen;
  70. if (paren) {
  71. if (sscanf(paren, "(%10u)", &param) != 1) { // not CPUMeter
  72. char dynamic[32] = {0};
  73. if (sscanf(paren, "(%30s)", dynamic) == 1) { // DynamicMeter
  74. char* end;
  75. if ((end = strrchr(dynamic, ')')) == NULL)
  76. return; // htoprc parse failure
  77. *end = '\0';
  78. const Settings* settings = this->host->settings;
  79. if (!DynamicMeter_search(settings->dynamicMeters, dynamic, &param))
  80. return; // name lookup failure
  81. } else {
  82. param = 0;
  83. }
  84. }
  85. nameLen = paren - name;
  86. } else {
  87. nameLen = strlen(name);
  88. }
  89. for (const MeterClass* const* type = Platform_meterTypes; *type; type++) {
  90. if (0 == strncmp(name, (*type)->name, nameLen) && (*type)->name[nameLen] == '\0') {
  91. Meter* meter = Meter_new(this->host, param, *type);
  92. if (mode != 0) {
  93. Meter_setMode(meter, mode);
  94. }
  95. Vector_add(meters, meter);
  96. break;
  97. }
  98. }
  99. }
  100. void Header_populateFromSettings(Header* this) {
  101. const Settings* settings = this->host->settings;
  102. Header_setLayout(this, settings->hLayout);
  103. Header_forEachColumn(this, col) {
  104. const MeterColumnSetting* colSettings = &settings->hColumns[col];
  105. Vector_prune(this->columns[col]);
  106. for (size_t i = 0; i < colSettings->len; i++) {
  107. Header_addMeterByName(this, colSettings->names[i], colSettings->modes[i], col);
  108. }
  109. }
  110. Header_calculateHeight(this);
  111. }
  112. void Header_writeBackToSettings(const Header* this) {
  113. Settings* settings = this->host->settings;
  114. Settings_setHeaderLayout(settings, this->headerLayout);
  115. Header_forEachColumn(this, col) {
  116. MeterColumnSetting* colSettings = &settings->hColumns[col];
  117. if (colSettings->names) {
  118. for (size_t j = 0; j < colSettings->len; j++)
  119. free(colSettings->names[j]);
  120. free(colSettings->names);
  121. }
  122. free(colSettings->modes);
  123. const Vector* vec = this->columns[col];
  124. int len = Vector_size(vec);
  125. colSettings->names = len ? xCalloc(len + 1, sizeof(*colSettings->names)) : NULL;
  126. colSettings->modes = len ? xCalloc(len, sizeof(*colSettings->modes)) : NULL;
  127. colSettings->len = len;
  128. for (int i = 0; i < len; i++) {
  129. const Meter* meter = (Meter*) Vector_get(vec, i);
  130. char* name;
  131. if (meter->param && As_Meter(meter) == &DynamicMeter_class) {
  132. const char* dynamic = DynamicMeter_lookup(settings->dynamicMeters, meter->param);
  133. xAsprintf(&name, "%s(%s)", As_Meter(meter)->name, dynamic);
  134. } else if (meter->param && As_Meter(meter) == &CPUMeter_class) {
  135. xAsprintf(&name, "%s(%u)", As_Meter(meter)->name, meter->param);
  136. } else {
  137. xAsprintf(&name, "%s", As_Meter(meter)->name);
  138. }
  139. colSettings->names[i] = name;
  140. colSettings->modes[i] = meter->mode;
  141. }
  142. }
  143. }
  144. Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, unsigned int column) {
  145. assert(column < HeaderLayout_getColumns(this->headerLayout));
  146. Vector* meters = this->columns[column];
  147. Meter* meter = Meter_new(this->host, param, type);
  148. Vector_add(meters, meter);
  149. return meter;
  150. }
  151. void Header_reinit(Header* this) {
  152. Header_forEachColumn(this, col) {
  153. for (int i = 0; i < Vector_size(this->columns[col]); i++) {
  154. Meter* meter = (Meter*) Vector_get(this->columns[col], i);
  155. if (Meter_initFn(meter)) {
  156. Meter_init(meter);
  157. }
  158. }
  159. }
  160. }
  161. void Header_draw(const Header* this) {
  162. const int height = this->height;
  163. const int pad = this->pad;
  164. attrset(CRT_colors[RESET_COLOR]);
  165. for (int y = 0; y < height; y++) {
  166. mvhline(y, 0, ' ', COLS);
  167. }
  168. const int numCols = HeaderLayout_getColumns(this->headerLayout);
  169. const int width = COLS - 2 * pad - (numCols - 1);
  170. int x = pad;
  171. float roundingLoss = 0.0F;
  172. Header_forEachColumn(this, col) {
  173. Vector* meters = this->columns[col];
  174. float colWidth = (float)width * HeaderLayout_layouts[this->headerLayout].widths[col] / 100.0F;
  175. roundingLoss += colWidth - floorf(colWidth);
  176. if (roundingLoss >= 1.0F) {
  177. colWidth += 1.0F;
  178. roundingLoss -= 1.0F;
  179. }
  180. for (int y = (pad / 2), i = 0; i < Vector_size(meters); i++) {
  181. Meter* meter = (Meter*) Vector_get(meters, i);
  182. float actualWidth = colWidth;
  183. /* Let meters in text mode expand to the right on empty neighbors;
  184. except for multi column meters. */
  185. if (meter->mode == TEXT_METERMODE && !Meter_isMultiColumn(meter)) {
  186. for (int j = 1; j < meter->columnWidthCount; j++) {
  187. actualWidth++; /* separator column */
  188. actualWidth += (float)width * HeaderLayout_layouts[this->headerLayout].widths[col + j] / 100.0F;
  189. }
  190. }
  191. assert(meter->draw);
  192. meter->draw(meter, x, y, floorf(actualWidth));
  193. y += meter->h;
  194. }
  195. x += floorf(colWidth);
  196. x++; /* separator column */
  197. }
  198. }
  199. void Header_updateData(Header* this) {
  200. Header_forEachColumn(this, col) {
  201. Vector* meters = this->columns[col];
  202. int items = Vector_size(meters);
  203. for (int i = 0; i < items; i++) {
  204. Meter* meter = (Meter*) Vector_get(meters, i);
  205. Meter_updateValues(meter);
  206. }
  207. }
  208. }
  209. /*
  210. * Calculate how many columns the current meter is allowed to span,
  211. * by counting how many columns to the right are empty or contain a BlankMeter.
  212. * Returns the number of columns to span, i.e. if the direct neighbor is occupied 1.
  213. */
  214. static int calcColumnWidthCount(const Header* this, const Meter* curMeter, const int pad, const unsigned int curColumn, const int curHeight) {
  215. for (size_t i = curColumn + 1; i < HeaderLayout_getColumns(this->headerLayout); i++) {
  216. const Vector* meters = this->columns[i];
  217. int height = pad;
  218. for (int j = 0; j < Vector_size(meters); j++) {
  219. const Meter* meter = (const Meter*) Vector_get(meters, j);
  220. if (height >= curHeight + curMeter->h)
  221. break;
  222. height += meter->h;
  223. if (height <= curHeight)
  224. continue;
  225. if (!Object_isA((const Object*) meter, (const ObjectClass*) &BlankMeter_class))
  226. return i - curColumn;
  227. }
  228. }
  229. return HeaderLayout_getColumns(this->headerLayout) - curColumn;
  230. }
  231. int Header_calculateHeight(Header* this) {
  232. const Settings* settings = this->host->settings;
  233. const int pad = settings->headerMargin ? 2 : 0;
  234. int maxHeight = pad;
  235. Header_forEachColumn(this, col) {
  236. const Vector* meters = this->columns[col];
  237. int height = pad;
  238. for (int i = 0; i < Vector_size(meters); i++) {
  239. Meter* meter = (Meter*) Vector_get(meters, i);
  240. meter->columnWidthCount = calcColumnWidthCount(this, meter, pad, col, height);
  241. height += meter->h;
  242. }
  243. maxHeight = MAXIMUM(maxHeight, height);
  244. }
  245. if (maxHeight == pad) {
  246. maxHeight = 0;
  247. this->pad = 0;
  248. } else {
  249. this->pad = pad;
  250. }
  251. if (settings->screenTabs) {
  252. maxHeight++;
  253. }
  254. this->height = maxHeight;
  255. return maxHeight;
  256. }