-
Notifications
You must be signed in to change notification settings - Fork 3
/
linux-fb-ui.cpp
202 lines (178 loc) · 6.3 KB
/
linux-fb-ui.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Copyright (c) 2014 Harm Hanemaaijer <fgenfb@yahoo.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <errno.h>
#include <signal.h>
#include <linux/vt.h>
#include "sre.h"
#include "sreBackend.h"
#include "MouseEventQueue.h"
#include "demo.h"
#include "gui-common.h"
static MouseEventQueue *mouse_event_queue;
static int saved_kd_mode;
void LinuxFBRestoreConsoleState() {
fflush(stdout);
fflush(stderr);
int tty = open("/dev/tty0", O_RDWR);
// First check whether the console is already in the mode to
// be restored (this function may be called multiple times due
// to signals and atexit).
int current_kd_mode;
ioctl(tty, KDGETMODE, ¤t_kd_mode);
if (current_kd_mode == saved_kd_mode) {
close(tty);
return;
}
ioctl(tty, KDSETMODE, saved_kd_mode);
usleep(1000000);
// Switch to another VT and back to restore the text content.
struct vt_stat vtstat;
ioctl(tty, VT_GETSTATE, &vtstat);
int current_vt = vtstat.v_active;
int temp_vt;
if (current_vt == 1)
temp_vt = 2;
else
temp_vt = 1;
ioctl(tty, VT_ACTIVATE, temp_vt);
ioctl(tty, VT_WAITACTIVE, temp_vt);
ioctl(tty, VT_ACTIVATE, current_vt);
ioctl(tty, VT_WAITACTIVE, current_vt);
// fwrite("\033c\n", 1, 5, stdout);
fflush(stdout);
close(tty);
}
struct sigaction signal_quit_oldact, signal_segv_oldact, signal_int_oldact,
signal_abort_oldact;
void signal_quit(int num, siginfo_t *info, void *p) {
LinuxFBRestoreConsoleState();
if (signal_quit_oldact.sa_flags & SA_SIGINFO)
signal_quit_oldact.sa_sigaction(num, info, p);
else
signal_quit_oldact.sa_handler(num);
}
void signal_segv(int num, siginfo_t *info, void *p) {
LinuxFBRestoreConsoleState();
if (signal_segv_oldact.sa_flags & SA_SIGINFO)
signal_segv_oldact.sa_sigaction(num, info, p);
else
signal_segv_oldact.sa_handler(num);
}
void signal_int(int num, siginfo_t *info, void *p) {
LinuxFBRestoreConsoleState();
if (signal_int_oldact.sa_flags & SA_SIGINFO)
signal_int_oldact.sa_sigaction(num, info, p);
else
signal_int_oldact.sa_handler(num);
}
void signal_abort(int num, siginfo_t *info, void *p) {
LinuxFBRestoreConsoleState();
if (signal_abort_oldact.sa_flags & SA_SIGINFO)
signal_abort_oldact.sa_sigaction(num, info, p);
else
signal_abort_oldact.sa_handler(num);
}
void LinuxFBSetConsoleGraphics() {
fflush(stdout);
fflush(stderr);
// Set console graphics mode and install some handlers to restore console textmode.
int tty = open("/dev/tty0", O_RDWR);
if (tty < 0)
goto error_setting_graphics_mode;
if (ioctl(tty, KDGETMODE, &saved_kd_mode) < 0)
goto error_setting_graphics_mode;
if (ioctl(tty, KDSETMODE, KD_GRAPHICS) < 0)
goto error_setting_graphics_mode;
close(tty);
atexit(LinuxFBRestoreConsoleState);
struct sigaction act;
act.sa_sigaction = signal_quit;
__sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction(SIGQUIT, &act, &signal_quit_oldact);
act.sa_sigaction = signal_segv;
sigaction(SIGSEGV, &act, &signal_segv_oldact);
act.sa_sigaction = signal_int;
sigaction(SIGINT, &act, &signal_int_oldact);
// SIGABRT is raised by assert().
act.sa_sigaction = signal_abort;
sigaction(SIGABRT, &act, &signal_abort_oldact);
return;
error_setting_graphics_mode :
printf("Error setting console graphics mode.\n");
exit(1);
}
void LinuxFBWarpCursor(int x, int y) {
mouse_event_queue->setPosition(x, y);
}
void LinuxFBInitializeUI(int width, int height) {
mouse_event_queue = new MouseEventQueue;
mouse_event_queue->initialize();
mouse_event_queue->setScreenSize(width, height);
// Eat up preexisting mouse events.
while (mouse_event_queue->isEventAvailable()) {
MouseEvent event = mouse_event_queue->getEvent();
}
LinuxFBWarpCursor(width / 2, height / 2);
}
void LinuxFBDeinitializeUI() {
mouse_event_queue->terminate();
}
// Mouse interface for the Linux framebuffer console.
static const unsigned int linux_mouse_button_table[] = {
MouseEvent::LeftButton, SRE_MOUSE_BUTTON_LEFT,
MouseEvent::MiddleButton, SRE_MOUSE_BUTTON_MIDDLE,
MouseEvent::RightButton, SRE_MOUSE_BUTTON_RIGHT,
SRE_TRANSLATION_TABLE_END
};
static double left_pressed_date = 0;
static double right_pressed_date = 0;
void LinuxFBProcessGUIEvents() {
bool motion_occurred = false;
int motion_x, motion_y;
while (mouse_event_queue->isEventAvailable()) {
MouseEvent event = mouse_event_queue->getEvent();
if (event.type == MouseEvent::Passive || event.type == MouseEvent::Move) {
motion_x = event.x;
motion_y = event.y;
motion_occurred = true;
}
else if (event.type == MouseEvent::Press) {
unsigned int button = GUITranslateKeycode(event.button, linux_mouse_button_table);
if (button != 0)
GUIMouseButtonCallbackNoKeyboard(button, SRE_PRESS);
}
else if (event.type == MouseEvent::Release) {
unsigned int button = GUITranslateKeycode(event.button, linux_mouse_button_table);
if (button != 0)
GUIMouseButtonCallbackNoKeyboard(button, SRE_RELEASE);
}
}
if (motion_occurred)
GUIProcessMouseMotion(motion_x, motion_y);
}
double LinuxFBGetCurrentTime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
}