AffinityPanel.c 1.63 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
/*
htop - AffinityPanel.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
9

#include "AffinityPanel.h"

Hisham Muhammad's avatar
Hisham Muhammad committed
10
#include "CheckItem.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include "debug.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12

Hisham Muhammad's avatar
Hisham Muhammad committed
13
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
14
15
16
17
18
19
20
21
#include <string.h>

/*{
#include "Panel.h"
#include "Affinity.h"
#include "ProcessList.h"
#include "ListItem.h"
}*/
Hisham Muhammad's avatar
Hisham Muhammad committed
22

23
24
25
static HandlerResult AffinityPanel_eventHandler(Panel* this, int ch) {
   CheckItem* selected = (CheckItem*) Panel_getSelected(this);
   switch(ch) {
26
   case KEY_MOUSE:
27
28
   case ' ':
      CheckItem_set(selected, ! (CheckItem_get(selected)) );
Hisham Muhammad's avatar
Hisham Muhammad committed
29
      return HANDLED;
30
31
32
   case 0x0a:
   case 0x0d:
   case KEY_ENTER:
Hisham Muhammad's avatar
Hisham Muhammad committed
33
      return BREAK_LOOP;
34
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
35
   return IGNORED;
36
37
}

38
Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity) {
Hisham Muhammad's avatar
Hisham Muhammad committed
39
40
41
42
   Panel* this = Panel_new(1, 1, 1, 1, CHECKITEM_CLASS, true, ListItem_compare);
   this->eventHandler = AffinityPanel_eventHandler;

   Panel_setHeader(this, "Use CPUs:");
43
   int curCpu = 0;
44
   for (int i = 0; i < pl->cpuCount; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
45
      char number[10];
46
      snprintf(number, 9, "%d", ProcessList_cpuId(pl, i));
47
48
49
50
51
52
53
      bool mode;
      if (curCpu < affinity->used && affinity->cpus[curCpu] == i) {
         mode = true;
         curCpu++;
      } else {
         mode = false;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
54
      Panel_add(this, (Object*) CheckItem_new(strdup(number), NULL, mode));         
Hisham Muhammad's avatar
Hisham Muhammad committed
55
56
57
58
   }
   return this;
}

59
60
Affinity* AffinityPanel_getAffinity(Panel* this) {
   Affinity* affinity = Affinity_new();
Hisham Muhammad's avatar
Hisham Muhammad committed
61
   int size = Panel_size(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
   for (int i = 0; i < size; i++) {
      if (CheckItem_get((CheckItem*)Panel_get(this, i)))
64
         Affinity_add(affinity, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
65
   }
66
   return affinity;
Hisham Muhammad's avatar
Hisham Muhammad committed
67
}