SignalsPanel.c 1.79 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1

2
3
#include "SignalsPanel.h"
#include "Panel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
9
10
11
12
13
#include "SignalItem.h"
#include "RichString.h"

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

#include <ctype.h>

/*{

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

   int state;
   Signal** signals;
19
} SignalsPanel;
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
22

}*/

23
static HandlerResult SignalsPanel_eventHandler(Panel* super, int ch) {
24
   SignalsPanel* this = (SignalsPanel*) super;
Hisham Muhammad's avatar
Hisham Muhammad committed
25

Hisham Muhammad's avatar
Hisham Muhammad committed
26
   int size = Panel_size(super);
Hisham Muhammad's avatar
Hisham Muhammad committed
27
28
   
   if (ch <= 255 && isdigit(ch)) {
Hisham Muhammad's avatar
Hisham Muhammad committed
29
      int sgn = ch-48 + this->state;
Hisham Muhammad's avatar
Hisham Muhammad committed
30
      for (int i = 0; i < size; i++)
Hisham Muhammad's avatar
Hisham Muhammad committed
31
         if (((Signal*) Panel_get(super, i))->number == sgn) {
32
            Panel_setSelected(super, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
33
34
            break;
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
35
      this->state = sgn * 10;
Hisham Muhammad's avatar
Hisham Muhammad committed
36
37
38
39
40
41
42
43
44
45
46
      if (this->state > 100)
         this->state = 0;
      return HANDLED;
   } else {
      this->state = 0;
   }
   if (ch == 13) {
      return BREAK_LOOP;
   }
   return IGNORED;
}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

static void SignalsPanel_delete(Object* object) {
   Panel* super = (Panel*) object;
   SignalsPanel* this = (SignalsPanel*) object;
   Panel_done(super);
   free(this->signals);
   free(this);
}

SignalsPanel* SignalsPanel_new(int x, int y, int w, int h) {
   SignalsPanel* this = (SignalsPanel*) malloc(sizeof(SignalsPanel));
   Panel* super = (Panel*) this;
   Panel_init(super, x, y, w, h, SIGNAL_CLASS, true);
   ((Object*)this)->delete = SignalsPanel_delete;

   this->signals = Signal_getSignalTable();
   super->eventHandler = SignalsPanel_eventHandler;
   int sigCount = Signal_getSignalCount();
   for(int i = 0; i < sigCount; i++)
      Panel_set(super, i, (Object*) this->signals[i]);
   SignalsPanel_reset(this);
   return this;
}

void SignalsPanel_reset(SignalsPanel* this) {
   Panel* super = (Panel*) this;

   Panel_setHeader(super, "Send signal:");
   Panel_setSelected(super, 16); // 16th item is SIGTERM
   this->state = 0;
}