/
userinterface.cpp
114 lines (106 loc) · 2.68 KB
/
userinterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "userinterface.h"
#include <QtCore>
#include <stdio.h>
volatile bool hasInterrupt = false;
QMutex hIMutex;
QWaitCondition hICondition;
char buffer[10];
void intSignalHandler(int sig)
{
Q_UNUSED(sig)
hIMutex.lock();
hasInterrupt = true;
hICondition.wakeOne();
hIMutex.unlock();
}
UserInterface::UserInterface(QObject *parent) : QThread(parent), usingUI(false), isAborting(false)
{
struct sigaction newSig;
newSig.sa_handler = intSignalHandler;
sigemptyset(&newSig.sa_mask);
sigaction(SIGINT, &newSig, &oldSig);
}
UserInterface::~UserInterface()
{
sigaction(SIGINT, &oldSig, NULL);
hasInterrupt = false;
}
void UserInterface::abort()
{
if (isRunning())
{
hIMutex.lock();
isAborting = true;
hasInterrupt = true;
hICondition.wakeOne();
hIMutex.unlock();
}
}
void UserInterface::run()
{
while (true)
{
hIMutex.lock();
while (!hasInterrupt)
hICondition.wait(&hIMutex);
hIMutex.unlock();
if (isAborting)
return;
usingUI = true;
printf("\n");
printf(" 0: Cancel\n");
printf(" 1: Activate\n");
printf(" 2: Deactivate\n");
printf(" 3: Set my name\n");
printf(" 4: Set follow name\n");
printf(" 5: Set target name\n");
printf(" 9: Exit\n");
int action = -1;
while (action)
{
printf("Action: ");
fflush(stdout);
scanf("%d", &action);
switch (action)
{
case 0:
break;
case 1:
emit setActivated(true);
action = 0;
break;
case 2:
emit setActivated(false);
action = 0;
break;
case 3:
printf("Name: ");
fflush(stdout);
fgets(buffer, 10, stdin);
emit setMyName(QString(buffer));
action = 0;
break;
case 4:
printf("Name: ");
fflush(stdout);
fgets(buffer, 10, stdin);
emit setFollowName(QString(buffer));
action = 0;
break;
case 5:
printf("Name: ");
fflush(stdout);
fgets(buffer, 10, stdin);
emit setTargetName(QString(buffer));
action = 0;
break;
case 9:
return;
default:
printf("Error: Unknown action.\n");
}
}
usingUI = false;
hasInterrupt = false;
}
}