-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
61 lines (48 loc) · 1.53 KB
/
util.c
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
/*
* path: /home/klassiker/.local/share/repos/xkeymou/util.c
* author: klassiker [mrdotx]
* github: https://github.com/mrdotx/xkeymou
* date: 2022-07-16T19:35:53+0200
*/
#include <stdio.h>
#include <time.h>
#include "util.h"
void sleep_ms(long ms) {
struct timespec remaining, request = {
SECS_TO_SLEEP,
NSEC_TO_SLEEP * ms
};
nanosleep(&request, &remaining);
}
const char *get_direction(int x, int y) {
static char string[16];
if (0 == x && 0 == y) sprintf(string, "STOPPED ");
if (0 == x && 0 > y) sprintf(string, "UP ");
if (0 == x && 0 < y) sprintf(string, "DOWN ");
if (0 > x && 0 == y) sprintf(string, "LEFT ");
if (0 < x && 0 == y) sprintf(string, "RIGHT ");
if (0 > x && 0 > y) sprintf(string, "UP-LEFT ");
if (0 < x && 0 > y) sprintf(string, "UP-RIGHT ");
if (0 > x && 0 < y) sprintf(string, "DOWN-LEFT ");
if (0 < x && 0 < y) sprintf(string, "DOWN-RIGHT ");
return string;
}
const char *get_button(int button, int is_press) {
static char string[16];
char *status = is_press ? "pressed " : "released";
switch(button) {
case 0:
sprintf(string, "DUMMY %s ", status);
break;
case 1:
sprintf(string, "LEFT %s ", status);
break;
case 2:
sprintf(string, "MIDDLE %s", status);
break;
case 3:
sprintf(string, "RIGHT %s ", status);
break;
}
return string;
}