AffinityPanel.c 1.28 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5

#include "AffinityPanel.h"

#include "Panel.h"
#include "CheckItem.h"
6
#include "ProcessList.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
7
8
9
10

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

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

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

   Panel_setHeader(this, "Use CPUs:");
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) + 1);
Hisham Muhammad's avatar
Hisham Muhammad committed
37
38
39
40
41
42
      Panel_add(this, (Object*) CheckItem_new(String_copy(number), NULL, mask & (1 << i)));
   }
   return this;
}

unsigned long AffinityPanel_getAffinity(Panel* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
43
   int size = Panel_size(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
44
45
46
47
48
49
50
   unsigned long mask = 0;
   for (int i = 0; i < size; i++) {
      if (CheckItem_get((CheckItem*)Panel_get(this, i)))
         mask = mask | (1 << i);
   }
   return mask;
}