AvailableMetersPanel.c 3.42 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
#include "ProcessList.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
23

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

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

}*/

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

Hisham Muhammad's avatar
Hisham Muhammad committed
43
44
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
45
   Panel_add(panel, (Object*) Meter_toListItem(meter));
Hisham Muhammad's avatar
Hisham Muhammad committed
46
47
}

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

   switch(ch) {
      case KEY_F(5):
      case 'l':
      case 'L':
      {
Hisham Muhammad's avatar
Hisham Muhammad committed
62
         AvailableMetersPanel_addMeter(header, this->leftPanel, Platform_meterTypes[type], param, 0);
Hisham Muhammad's avatar
Hisham Muhammad committed
63
64
65
66
67
68
69
         result = HANDLED;
         break;
      }
      case KEY_F(6):
      case 'r':
      case 'R':
      {
Hisham Muhammad's avatar
Hisham Muhammad committed
70
         AvailableMetersPanel_addMeter(header, this->rightPanel, Platform_meterTypes[type], param, 1);
Hisham Muhammad's avatar
Hisham Muhammad committed
71
72
73
74
75
76
77
78
79
80
81
82
         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;
}
83

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

Hisham Muhammad's avatar
Hisham Muhammad committed
92
AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Header* header, Panel* leftMeters, Panel* rightMeters, ScreenManager* scr, ProcessList* pl) {
93
   AvailableMetersPanel* this = AllocThis(AvailableMetersPanel);
94
   Panel* super = (Panel*) this;
95
   Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);
96
97
   
   this->settings = settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
98
   this->header = header;
99
100
101
102
103
   this->leftPanel = leftMeters;
   this->rightPanel = rightMeters;
   this->scr = scr;

   Panel_setHeader(super, "Available meters");
104
105
   for (int i = 1; Platform_meterTypes[i]; i++) {
      MeterClass* type = Platform_meterTypes[i];
106
      if (type != &CPUMeter_class) {
107
108
109
         Panel_add(super, (Object*) ListItem_new(type->uiName, i << 16));
      }
   }
110
   MeterClass* type = &CPUMeter_class;
Hisham Muhammad's avatar
Hisham Muhammad committed
111
   int cpus = pl->cpuCount;
112
   if (cpus > 1) {
113
      Panel_add(super, (Object*) ListItem_new("CPU average", 0));
114
      for (int i = 1; i <= cpus; i++) {
115
116
117
118
119
120
121
122
123
         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;
}