AffinityPanel.c 1.41 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1

2
#include "ProcessList.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
3
4
5
6
7
8
9
#include "AffinityPanel.h"
#include "Panel.h"
#include "CheckItem.h"

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

10
11
12
static HandlerResult AffinityPanel_eventHandler(Panel* this, int ch) {
   CheckItem* selected = (CheckItem*) Panel_getSelected(this);
   switch(ch) {
13
   case KEY_MOUSE:
14
15
   case ' ':
      CheckItem_set(selected, ! (CheckItem_get(selected)) );
Hisham Muhammad's avatar
Hisham Muhammad committed
16
      return HANDLED;
17
18
19
   case 0x0a:
   case 0x0d:
   case KEY_ENTER:
Hisham Muhammad's avatar
Hisham Muhammad committed
20
      return BREAK_LOOP;
21
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
22
   return IGNORED;
23
24
}

25
Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity) {
Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28
29
   Panel* this = Panel_new(1, 1, 1, 1, CHECKITEM_CLASS, true, ListItem_compare);
   this->eventHandler = AffinityPanel_eventHandler;

   Panel_setHeader(this, "Use CPUs:");
30
   int curCpu = 0;
31
   for (int i = 0; i < pl->cpuCount; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
32
      char number[10];
33
      snprintf(number, 9, "%d", ProcessList_cpuId(pl, i));
34
35
36
37
38
39
40
41
      bool mode;
      if (curCpu < affinity->used && affinity->cpus[curCpu] == i) {
         mode = true;
         curCpu++;
      } else {
         mode = false;
      }
      Panel_add(this, (Object*) CheckItem_new(String_copy(number), NULL, mode));         
Hisham Muhammad's avatar
Hisham Muhammad committed
42
43
44
45
   }
   return this;
}

46
47
Affinity* AffinityPanel_getAffinity(Panel* this) {
   Affinity* affinity = Affinity_new();
Hisham Muhammad's avatar
Hisham Muhammad committed
48
   int size = Panel_size(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
49
50
   for (int i = 0; i < size; i++) {
      if (CheckItem_get((CheckItem*)Panel_get(this, i)))
51
         Affinity_add(affinity, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
52
   }
53
   return affinity;
Hisham Muhammad's avatar
Hisham Muhammad committed
54
}