AffinityPanel.c 1.48 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
13
static HandlerResult AffinityPanel_eventHandler(Panel* this, int ch) {
   HandlerResult result = IGNORED;
   CheckItem* selected = (CheckItem*) Panel_getSelected(this);
   switch(ch) {
14
   case KEY_MOUSE:
15
16
17
18
19
20
21
22
23
24
25
26
27
   case ' ':
      CheckItem_set(selected, ! (CheckItem_get(selected)) );
      result = HANDLED;
      break;
   case 0x0a:
   case 0x0d:
   case KEY_ENTER:
      result = BREAK_LOOP;
      break;
   }
   return result;
}

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

   Panel_setHeader(this, "Use CPUs:");
33
   int curCpu = 0;
34
   for (int i = 0; i < pl->cpuCount; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
35
      char number[10];
36
      snprintf(number, 9, "%d", ProcessList_cpuId(pl, i));
37
38
39
40
41
42
43
44
      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
45
46
47
48
   }
   return this;
}

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