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

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

Hisham Muhammad's avatar
Hisham Muhammad committed
10
11
12
#include "CPUMeter.h"
#include "Header.h"
#include "ListItem.h"
13
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
14

Hisham Muhammad's avatar
Hisham Muhammad committed
15
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
16
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
17
18

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
19
20
21
#include "Settings.h"
#include "Panel.h"
#include "ScreenManager.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
22

23
24
typedef struct AvailableMetersPanel_ {
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
25
26

   Settings* settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
27
28
   Panel* leftPanel;
   Panel* rightPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
29
   ScreenManager* scr;
30
} AvailableMetersPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
31
32
33

}*/

34
static void AvailableMetersPanel_delete(Object* object) {
35
36
37
   Panel* super = (Panel*) object;
   AvailableMetersPanel* this = (AvailableMetersPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
38
39
40
   free(this);
}

41
static inline void AvailableMetersPanel_addHeader(Header* header, Panel* panel, MeterClass* type, int param, HeaderSide side) {
42
   Meter* meter = (Meter*) Header_addMeter(header, type, param, side);
Hisham Muhammad's avatar
Hisham Muhammad committed
43
   Panel_add(panel, (Object*) Meter_toListItem(meter));
Hisham Muhammad's avatar
Hisham Muhammad committed
44
45
}

46
static HandlerResult AvailableMetersPanel_eventHandler(Panel* super, int ch) {
47
   AvailableMetersPanel* this = (AvailableMetersPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
48
49
   Header* header = this->settings->header;
   
50
   ListItem* selected = (ListItem*) Panel_getSelected(super);
51
52
   int param = selected->key & 0xff;
   int type = selected->key >> 16;
Hisham Muhammad's avatar
Hisham Muhammad committed
53
54
55
56
57
58
59
   HandlerResult result = IGNORED;

   switch(ch) {
      case KEY_F(5):
      case 'l':
      case 'L':
      {
60
         AvailableMetersPanel_addHeader(header, this->leftPanel, Platform_meterTypes[type], param, LEFT_HEADER);
Hisham Muhammad's avatar
Hisham Muhammad committed
61
62
63
64
65
66
67
         result = HANDLED;
         break;
      }
      case KEY_F(6):
      case 'r':
      case 'R':
      {
68
         AvailableMetersPanel_addHeader(header, this->rightPanel, Platform_meterTypes[type], param, RIGHT_HEADER);
Hisham Muhammad's avatar
Hisham Muhammad committed
69
70
71
72
73
74
75
76
77
78
79
80
         result = HANDLED;
         break;
      }
   }
   if (result == HANDLED) {
      this->settings->changed = true;
      Header_calculateHeight(header);
      Header_draw(header);
      ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
   }
   return result;
}
81

82
83
84
85
86
87
88
89
PanelClass AvailableMetersPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = AvailableMetersPanel_delete
   },
   .eventHandler = AvailableMetersPanel_eventHandler
};

90
AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Panel* leftMeters, Panel* rightMeters, ScreenManager* scr) {
91
   AvailableMetersPanel* this = AllocThis(AvailableMetersPanel);
92
   Panel* super = (Panel*) this;
93
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
94
95
96
97
98
99
100
   
   this->settings = settings;
   this->leftPanel = leftMeters;
   this->rightPanel = rightMeters;
   this->scr = scr;

   Panel_setHeader(super, "Available meters");
101
102
   for (int i = 1; Platform_meterTypes[i]; i++) {
      MeterClass* type = Platform_meterTypes[i];
103
      if (type != &CPUMeter_class) {
104
105
106
         Panel_add(super, (Object*) ListItem_new(type->uiName, i << 16));
      }
   }
107
   MeterClass* type = &CPUMeter_class;
108
109
   int cpus = settings->pl->cpuCount;
   if (cpus > 1) {
110
      Panel_add(super, (Object*) ListItem_new("CPU average", 0));
111
      for (int i = 1; i <= cpus; i++) {
112
113
114
115
116
117
118
119
120
         char buffer[50];
         sprintf(buffer, "%s %d", type->uiName, i);
         Panel_add(super, (Object*) ListItem_new(buffer, i));
      }
   } else {
      Panel_add(super, (Object*) ListItem_new("CPU", 1));
   }
   return this;
}