-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mouse.h
79 lines (67 loc) · 1.71 KB
/
Mouse.h
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
// Possible mouse modes
typedef enum
{
NO_MODE = 0,
NORMAL_MODE,
HILITE_MODE,
BUTTON_MODE,
ALL_MODE
} MouseMode;
// Control codes
// Normal control codes
#define UP_ARROW "\033[A"
#define DOWN_ARROW "\033[B"
// Control codes for application keypad mode
#define UP_ARROW_APP "\033OA"
#define DOWN_ARROW_APP "\033OB"
#define ARROW_LEN (sizeof(UP_ARROW) - 1)
// Mode control codes
#define TOGGLE_ON 'h'
#define TOGGLE_OFF 'l'
// Excludes mode and toggle flag
#define TOGGLE_MOUSE "\033[?100"
#define TOGGLE_MOUSE_LEN (sizeof(TOGGLE_MOUSE) - 1)
// Excludes toggle flag
#define TOGGLE_CURSOR_KEYS "\033[?1"
#define TOGGLE_CURSOR_KEYS_LEN (sizeof(TOGGLE_CURSOR_KEYS) - 1)
// X11 mouse button values
typedef enum
{
MOUSE_BUTTON1 = 0,
MOUSE_BUTTON2,
MOUSE_BUTTON3,
MOUSE_RELEASE,
MOUSE_WHEEL_DOWN,
MOUSE_WHEEL_UP
} MouseButton;
// X11 mouse reporting responses
#define MOUSE_RESPONSE "\033[M%c%c%c"
#define MOUSE_RESPONSE_LEN 6
// Returns a control code for a mouse movement (from iTerm)
inline NSData* mousePress(MouseButton button, unsigned int modflag,
unsigned int x, unsigned int y)
{
char buf[MOUSE_RESPONSE_LEN + 1];
char cb;
switch (button)
{
case MOUSE_WHEEL_DOWN:
cb = 65;
break;
case MOUSE_WHEEL_UP:
cb = 64;
break;
default:
cb = button % 3;
}
cb += 32;
if (modflag & NSShiftKeyMask)
cb |= 4;
if (modflag & NSAlternateKeyMask) // Alt/option
cb |= 8;
if (modflag & NSControlKeyMask)
cb |= 16;
snprintf(buf, sizeof(buf), MOUSE_RESPONSE, cb, 32 + x + 1,
32 + y + 1);
return [NSData dataWithBytes: buf length: MOUSE_RESPONSE_LEN];
}