AvailableMetersPanel.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. htop - AvailableMetersPanel.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 "AvailableMetersPanel.h"
  9. #include <assert.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include "CPUMeter.h"
  13. #include "DynamicMeter.h"
  14. #include "FunctionBar.h"
  15. #include "Hashtable.h"
  16. #include "Header.h"
  17. #include "ListItem.h"
  18. #include "Macros.h"
  19. #include "Meter.h"
  20. #include "MetersPanel.h"
  21. #include "Object.h"
  22. #include "Platform.h"
  23. #include "ProvideCurses.h"
  24. #include "Settings.h"
  25. #include "XUtils.h"
  26. static void AvailableMetersPanel_delete(Object* object) {
  27. Panel* super = (Panel*) object;
  28. AvailableMetersPanel* this = (AvailableMetersPanel*) object;
  29. Panel_done(super);
  30. free(this->meterPanels);
  31. free(this);
  32. }
  33. static inline void AvailableMetersPanel_addMeter(Header* header, MetersPanel* panel, const MeterClass* type, unsigned int param, size_t column) {
  34. const Meter* meter = Header_addMeterByClass(header, type, param, column);
  35. Panel_add((Panel*)panel, (Object*) Meter_toListItem(meter, false));
  36. Panel_setSelected((Panel*)panel, Panel_size((Panel*)panel) - 1);
  37. MetersPanel_setMoving(panel, true);
  38. }
  39. static HandlerResult AvailableMetersPanel_eventHandler(Panel* super, int ch) {
  40. AvailableMetersPanel* this = (AvailableMetersPanel*) super;
  41. Header* header = this->header;
  42. const ListItem* selected = (ListItem*) Panel_getSelected(super);
  43. if (!selected)
  44. return IGNORED;
  45. unsigned int param = selected->key & 0xffff;
  46. int type = selected->key >> 16;
  47. HandlerResult result = IGNORED;
  48. bool update = false;
  49. switch (ch) {
  50. case KEY_F(5):
  51. case 'l':
  52. case 'L':
  53. AvailableMetersPanel_addMeter(header, this->meterPanels[0], Platform_meterTypes[type], param, 0);
  54. result = HANDLED;
  55. update = true;
  56. break;
  57. case 0x0a:
  58. case 0x0d:
  59. case KEY_ENTER:
  60. case KEY_F(6):
  61. case 'r':
  62. case 'R':
  63. AvailableMetersPanel_addMeter(header, this->meterPanels[this->columns - 1], Platform_meterTypes[type], param, this->columns - 1);
  64. result = (KEY_LEFT << 16) | SYNTH_KEY;
  65. update = true;
  66. break;
  67. }
  68. if (update) {
  69. Settings* settings = this->host->settings;
  70. settings->changed = true;
  71. settings->lastUpdate++;
  72. Header_calculateHeight(header);
  73. Header_updateData(header);
  74. Header_draw(header);
  75. ScreenManager_resize(this->scr);
  76. }
  77. return result;
  78. }
  79. const PanelClass AvailableMetersPanel_class = {
  80. .super = {
  81. .extends = Class(Panel),
  82. .delete = AvailableMetersPanel_delete
  83. },
  84. .eventHandler = AvailableMetersPanel_eventHandler
  85. };
  86. // Handle (&CPUMeter_class) entries in the AvailableMetersPanel
  87. static void AvailableMetersPanel_addCPUMeters(Panel* super, const MeterClass* type, const Machine* host) {
  88. if (host->existingCPUs > 1) {
  89. Panel_add(super, (Object*) ListItem_new("CPU average", 0));
  90. for (unsigned int i = 1; i <= host->existingCPUs; i++) {
  91. char buffer[50];
  92. xSnprintf(buffer, sizeof(buffer), "%s %d", type->uiName, Settings_cpuId(host->settings, i - 1));
  93. Panel_add(super, (Object*) ListItem_new(buffer, i));
  94. }
  95. } else {
  96. Panel_add(super, (Object*) ListItem_new(type->uiName, 1));
  97. }
  98. }
  99. typedef struct {
  100. Panel* super;
  101. unsigned int id;
  102. unsigned int offset;
  103. } DynamicIterator;
  104. static void AvailableMetersPanel_addDynamicMeter(ATTR_UNUSED ht_key_t key, void* value, void* data) {
  105. const DynamicMeter* meter = (const DynamicMeter*)value;
  106. DynamicIterator* iter = (DynamicIterator*)data;
  107. unsigned int identifier = (iter->offset << 16) | iter->id;
  108. const char* label = meter->description ? meter->description : meter->caption;
  109. if (!label)
  110. label = meter->name; /* last fallback to name, guaranteed set */
  111. Panel_add(iter->super, (Object*) ListItem_new(label, identifier));
  112. iter->id++;
  113. }
  114. // Handle (&DynamicMeter_class) entries in the AvailableMetersPanel
  115. static void AvailableMetersPanel_addDynamicMeters(Panel* super, const Settings* settings, unsigned int offset) {
  116. DynamicIterator iter = { .super = super, .id = 1, .offset = offset };
  117. Hashtable* dynamicMeters = settings->dynamicMeters;
  118. assert(dynamicMeters != NULL);
  119. Hashtable_foreach(dynamicMeters, AvailableMetersPanel_addDynamicMeter, &iter);
  120. }
  121. // Handle remaining Platform Meter entries in the AvailableMetersPanel
  122. static void AvailableMetersPanel_addPlatformMeter(Panel* super, const MeterClass* type, unsigned int offset) {
  123. const char* label = type->description ? type->description : type->uiName;
  124. Panel_add(super, (Object*) ListItem_new(label, offset << 16));
  125. }
  126. AvailableMetersPanel* AvailableMetersPanel_new(Machine* host, Header* header, size_t columns, MetersPanel** meterPanels, ScreenManager* scr) {
  127. AvailableMetersPanel* this = AllocThis(AvailableMetersPanel);
  128. Panel* super = (Panel*) this;
  129. FunctionBar* fuBar = FunctionBar_newEnterEsc("Add ", "Done ");
  130. Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);
  131. this->host = host;
  132. this->header = header;
  133. this->columns = columns;
  134. this->meterPanels = meterPanels;
  135. this->scr = scr;
  136. Panel_setHeader(super, "Available meters");
  137. // Platform_meterTypes[0] should be always (&CPUMeter_class) which we will
  138. // handle separately in the code below. Likewise, identifiers for Dynamic
  139. // Meters are handled separately - similar to CPUs, this allows generation
  140. // of multiple different Meters (also using 'param' to distinguish them).
  141. for (unsigned int i = 1; Platform_meterTypes[i]; i++) {
  142. const MeterClass* type = Platform_meterTypes[i];
  143. assert(type != &CPUMeter_class);
  144. if (type == &DynamicMeter_class)
  145. AvailableMetersPanel_addDynamicMeters(super, host->settings, i);
  146. else
  147. AvailableMetersPanel_addPlatformMeter(super, type, i);
  148. }
  149. AvailableMetersPanel_addCPUMeters(super, &CPUMeter_class, host);
  150. return this;
  151. }