AvailableMetersPanel.c 3.79 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"
9
#include "MetersPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10

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

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

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

25
26
typedef struct AvailableMetersPanel_ {
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
27
   ScreenManager* scr;
Hisham Muhammad's avatar
Hisham Muhammad committed
28
29

   Settings* settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
30
   Header* header;
Hisham Muhammad's avatar
Hisham Muhammad committed
31
32
   Panel* leftPanel;
   Panel* rightPanel;
33
} AvailableMetersPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
36

}*/

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

Hisham Muhammad's avatar
Hisham Muhammad committed
44
45
static inline void AvailableMetersPanel_addMeter(Header* header, Panel* panel, MeterClass* type, int param, int column) {
   Meter* meter = (Meter*) Header_addMeterByClass(header, type, param, column);
Hisham Muhammad's avatar
Hisham Muhammad committed
46
   Panel_add(panel, (Object*) Meter_toListItem(meter));
47
48
49
   Panel_setSelected(panel, Panel_size(panel) - 1);
   ((MetersPanel*)panel)->moving = true;
   ((ListItem*)Panel_getSelected(panel))->moving = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
}

52
static HandlerResult AvailableMetersPanel_eventHandler(Panel* super, int ch) {
53
   AvailableMetersPanel* this = (AvailableMetersPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
54
   Header* header = this->header;
Hisham Muhammad's avatar
Hisham Muhammad committed
55
   
56
   ListItem* selected = (ListItem*) Panel_getSelected(super);
57
58
   int param = selected->key & 0xff;
   int type = selected->key >> 16;
Hisham Muhammad's avatar
Hisham Muhammad committed
59
   HandlerResult result = IGNORED;
60
   bool update = false;
Hisham Muhammad's avatar
Hisham Muhammad committed
61
62
63
64
65
66

   switch(ch) {
      case KEY_F(5):
      case 'l':
      case 'L':
      {
Hisham Muhammad's avatar
Hisham Muhammad committed
67
         AvailableMetersPanel_addMeter(header, this->leftPanel, Platform_meterTypes[type], param, 0);
Hisham Muhammad's avatar
Hisham Muhammad committed
68
         result = HANDLED;
69
         update = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
70
71
         break;
      }
72
73
74
      case 0x0a:
      case 0x0d:
      case KEY_ENTER:
Hisham Muhammad's avatar
Hisham Muhammad committed
75
76
77
78
      case KEY_F(6):
      case 'r':
      case 'R':
      {
Hisham Muhammad's avatar
Hisham Muhammad committed
79
         AvailableMetersPanel_addMeter(header, this->rightPanel, Platform_meterTypes[type], param, 1);
80
81
         result = (KEY_LEFT << 16) | SYNTH_KEY;
         update = true;
Hisham Muhammad's avatar
Hisham Muhammad committed
82
83
84
         break;
      }
   }
85
   if (update) {
Hisham Muhammad's avatar
Hisham Muhammad committed
86
87
88
89
90
91
92
      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;
}
93

94
95
96
97
98
99
100
101
PanelClass AvailableMetersPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = AvailableMetersPanel_delete
   },
   .eventHandler = AvailableMetersPanel_eventHandler
};

Hisham Muhammad's avatar
Hisham Muhammad committed
102
AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Header* header, Panel* leftMeters, Panel* rightMeters, ScreenManager* scr, ProcessList* pl) {
103
   AvailableMetersPanel* this = AllocThis(AvailableMetersPanel);
104
   Panel* super = (Panel*) this;
105
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
106
107
   
   this->settings = settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
108
   this->header = header;
109
110
111
112
113
   this->leftPanel = leftMeters;
   this->rightPanel = rightMeters;
   this->scr = scr;

   Panel_setHeader(super, "Available meters");
114
115
   for (int i = 1; Platform_meterTypes[i]; i++) {
      MeterClass* type = Platform_meterTypes[i];
116
      if (type != &CPUMeter_class) {
117
118
         const char* label = type->description ? type->description : type->uiName;
         Panel_add(super, (Object*) ListItem_new(label, i << 16));
119
120
      }
   }
121
   MeterClass* type = &CPUMeter_class;
Hisham Muhammad's avatar
Hisham Muhammad committed
122
   int cpus = pl->cpuCount;
123
   if (cpus > 1) {
124
      Panel_add(super, (Object*) ListItem_new("CPU average", 0));
125
      for (int i = 1; i <= cpus; i++) {
126
127
128
129
130
131
132
133
134
         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;
}