-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputserver.c
More file actions
54 lines (41 loc) · 1.21 KB
/
inputserver.c
File metadata and controls
54 lines (41 loc) · 1.21 KB
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
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
char input;
bool active;
pthread_t thread;
pthread_mutex_t inputMutex, activeMutex;
void *inputServerThread(){
pthread_mutex_lock(&activeMutex);
while(active){
pthread_mutex_unlock(&activeMutex);
if(kbhit()){
char ch = getch();
if(ch == 27){ // Unix specific behavior (arrow keys are represented as escape characters)
ch = getchar();
if(ch == '[')
ch = getchar();
}
pthread_mutex_lock(&inputMutex);
input = ch;
pthread_mutex_unlock(&inputMutex);
pthread_mutex_lock(&activeMutex);
}
}
pthread_mutex_unlock(&activeMutex);
return NULL;
}
void inputServerInit(){
if(pthread_mutex_init(&inputMutex, NULL) != 0)
fprintf(stderr, "inputMutex init failed\n");
active = true;
pthread_create(&thread, NULL, &inputServerThread, NULL);
}
void inputServerStop(){
pthread_mutex_lock(&activeMutex);
active = false;
pthread_mutex_unlock(&activeMutex);
pthread_join(thread, NULL);
pthread_mutex_destroy(&inputMutex);
pthread_mutex_destroy(&activeMutex);
}