AvailableMetersPanel.c 3.03 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1

2
#include "AvailableMetersPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
3
4
5
6
#include "Settings.h"
#include "Header.h"
#include "ScreenManager.h"

7
#include "Panel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
8
9
10
11
12
13

#include "debug.h"
#include <assert.h>

/*{

14
15
typedef struct AvailableMetersPanel_ {
   Panel super;
Hisham Muhammad's avatar
Hisham Muhammad committed
16
17

   Settings* settings;
18
19
   Panel* leftBox;
   Panel* rightBox;
Hisham Muhammad's avatar
Hisham Muhammad committed
20
   ScreenManager* scr;
21
} AvailableMetersPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
22
23
24

}*/

25
26
27
28
29
AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Panel* leftMeters, Panel* rightMeters, ScreenManager* scr) {
   AvailableMetersPanel* this = (AvailableMetersPanel*) malloc(sizeof(AvailableMetersPanel));
   Panel* super = (Panel*) this;
   Panel_init(super, 1, 1, 1, 1, LISTITEM_CLASS, true);
   ((Object*)this)->delete = AvailableMetersPanel_delete;
Hisham Muhammad's avatar
Hisham Muhammad committed
30
31
32
33
34
   
   this->settings = settings;
   this->leftBox = leftMeters;
   this->rightBox = rightMeters;
   this->scr = scr;
35
   super->eventHandler = AvailableMetersPanel_EventHandler;
Hisham Muhammad's avatar
Hisham Muhammad committed
36

37
   Panel_setHeader(super, "Available meters");
38
39
   for (int i = 1; Meter_types[i]; i++) {
      MeterType* type = Meter_types[i];
Hisham Muhammad's avatar
Hisham Muhammad committed
40
      if (type != &CPUMeter) {
41
         Panel_add(super, (Object*) ListItem_new(type->uiName, i << 16));
42
43
44
45
46
      }
   }
   MeterType* type = &CPUMeter;
   int processors = settings->pl->processorCount;
   if (processors > 1) {
47
      Panel_add(super, (Object*) ListItem_new("CPU average", 0));
48
49
50
      for (int i = 1; i <= processors; i++) {
         char buffer[50];
         sprintf(buffer, "%s %d", type->uiName, i);
51
         Panel_add(super, (Object*) ListItem_new(buffer, i));
52
53
      }
   } else {
54
      Panel_add(super, (Object*) ListItem_new("CPU", 1));
Hisham Muhammad's avatar
Hisham Muhammad committed
55
56
57
58
   }
   return this;
}

59
60
61
62
void AvailableMetersPanel_delete(Object* object) {
   Panel* super = (Panel*) object;
   AvailableMetersPanel* this = (AvailableMetersPanel*) object;
   Panel_done(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
63
64
65
66
   free(this);
}

/* private */
67
inline void AvailableMetersPanel_addHeader(Header* header, Panel* lb, MeterType* type, int param, HeaderSide side) {
68
   Meter* meter = (Meter*) Header_addMeter(header, type, param, side);
69
   Panel_add(lb, (Object*) Meter_toListItem(meter));
Hisham Muhammad's avatar
Hisham Muhammad committed
70
71
}

72
73
HandlerResult AvailableMetersPanel_EventHandler(Panel* super, int ch) {
   AvailableMetersPanel* this = (AvailableMetersPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
74
75
   Header* header = this->settings->header;
   
76
   ListItem* selected = (ListItem*) Panel_getSelected(super);
77
78
   int param = selected->key & 0xff;
   int type = selected->key >> 16;
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
81
82
83
84
85
   HandlerResult result = IGNORED;

   switch(ch) {
      case KEY_F(5):
      case 'l':
      case 'L':
      {
86
         AvailableMetersPanel_addHeader(header, this->leftBox, Meter_types[type], param, LEFT_HEADER);
Hisham Muhammad's avatar
Hisham Muhammad committed
87
88
89
90
91
92
93
         result = HANDLED;
         break;
      }
      case KEY_F(6):
      case 'r':
      case 'R':
      {
94
         AvailableMetersPanel_addHeader(header, this->rightBox, Meter_types[type], param, RIGHT_HEADER);
Hisham Muhammad's avatar
Hisham Muhammad committed
95
96
97
98
99
100
101
102
103
104
105
106
         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;
}