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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
* (C) Gražvydas "notaz" Ignotas, 2009-2010
*
* This work is licensed under the terms of any of these licenses
* (at your option):
* - GNU GPL, version 2 or later.
* - GNU LGPL, version 2.1 or later.
* See the COPYING file in the top-level directory.
*/
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <dlfcn.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <linux/kd.h>
#define PFX "oshide: "
#define FPTR(f) typeof(f) * p##f
#define FPTR_LINK(xf, dl, f) { \
xf.p##f = dlsym(dl, #f); \
if (xf.p##f == NULL) { \
fprintf(stderr, "missing symbol: %s\n", #f); \
goto fail; \
} \
}
struct xfuncs {
FPTR(XCreateBitmapFromData);
FPTR(XCreatePixmapCursor);
FPTR(XFreePixmap);
FPTR(XOpenDisplay);
FPTR(XDisplayName);
FPTR(XCloseDisplay);
FPTR(XCreateSimpleWindow);
FPTR(XChangeWindowAttributes);
FPTR(XSelectInput);
FPTR(XMapWindow);
FPTR(XNextEvent);
FPTR(XCheckTypedEvent);
FPTR(XUnmapWindow);
FPTR(XGrabKeyboard);
};
static Cursor transparent_cursor(struct xfuncs *xf, Display *display, Window win)
{
Cursor cursor;
Pixmap pix;
XColor dummy;
char d = 0;
memset(&dummy, 0, sizeof(dummy));
pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
cursor = xf->pXCreatePixmapCursor(display, pix, pix,
&dummy, &dummy, 0, 0);
xf->pXFreePixmap(display, pix);
return cursor;
}
static void *x11h_handler(void *arg)
{
struct xfuncs xf;
unsigned int display_width, display_height;
XSetWindowAttributes attributes;
Window win;
XEvent report;
Display *display;
Visual *visual;
void *x11lib;
int screen;
memset(&xf, 0, sizeof(xf));
x11lib = dlopen("libX11.so.6", RTLD_LAZY);
if (x11lib == NULL) {
fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
goto fail;
}
FPTR_LINK(xf, x11lib, XCreateBitmapFromData);
FPTR_LINK(xf, x11lib, XCreatePixmapCursor);
FPTR_LINK(xf, x11lib, XFreePixmap);
FPTR_LINK(xf, x11lib, XOpenDisplay);
FPTR_LINK(xf, x11lib, XDisplayName);
FPTR_LINK(xf, x11lib, XCloseDisplay);
FPTR_LINK(xf, x11lib, XCreateSimpleWindow);
FPTR_LINK(xf, x11lib, XChangeWindowAttributes);
FPTR_LINK(xf, x11lib, XSelectInput);
FPTR_LINK(xf, x11lib, XMapWindow);
FPTR_LINK(xf, x11lib, XNextEvent);
FPTR_LINK(xf, x11lib, XCheckTypedEvent);
FPTR_LINK(xf, x11lib, XUnmapWindow);
FPTR_LINK(xf, x11lib, XGrabKeyboard);
//XInitThreads();
display = xf.pXOpenDisplay(NULL);
if (display == NULL)
{
fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
xf.pXDisplayName(NULL));
goto fail2;
}
visual = DefaultVisual(display, 0);
if (visual->class != TrueColor)
fprintf(stderr, PFX "warning: non true color visual\n");
printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
VendorRelease(display), DisplayString(display), ProtocolVersion(display),
ProtocolRevision(display));
screen = DefaultScreen(display);
display_width = DisplayWidth(display, screen);
display_height = DisplayHeight(display, screen);
printf(PFX "display is %dx%d\n", display_width, display_height);
win = xf.pXCreateSimpleWindow(display,
RootWindow(display, screen),
0, 0, display_width, display_height, 0,
BlackPixel(display, screen),
BlackPixel(display, screen));
attributes.override_redirect = True;
attributes.cursor = transparent_cursor(&xf, display, win);
xf.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
xf.pXSelectInput(display, win, ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask);
xf.pXMapWindow(display, win);
xf.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
// XSetIOErrorHandler
while (1)
{
xf.pXNextEvent(display, &report);
switch (report.type)
{
case Expose:
while (xf.pXCheckTypedEvent(display, Expose, &report))
;
break;
case FocusOut:
// XFocusChangeEvent
// printf("focus out\n");
// xf.pXUnmapWindow(display, win);
break;
case KeyPress:
// printf("press %d\n", report.xkey.keycode);
break;
default:
break;
}
}
fail2:
dlclose(x11lib);
fail:
fprintf(stderr, "x11 handling disabled.\n");
return NULL;
}
static struct termios g_kbd_termios_saved;
static int g_kbdfd;
static void hidecon_start(void)
{
struct termios kbd_termios;
int mode;
g_kbdfd = open("/dev/tty", O_RDWR);
if (g_kbdfd == -1) {
perror(PFX "open /dev/tty");
return;
}
if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
perror(PFX "(not hiding FB): KDGETMODE");
goto fail;
}
if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
perror(PFX "tcgetattr");
goto fail;
}
g_kbd_termios_saved = kbd_termios;
kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
kbd_termios.c_cc[VMIN] = 0;
kbd_termios.c_cc[VTIME] = 0;
if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
perror(PFX "tcsetattr");
goto fail;
}
if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
perror(PFX "KDSETMODE KD_GRAPHICS");
tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
goto fail;
}
return;
fail:
close(g_kbdfd);
g_kbdfd = -1;
}
static void hidecon_end(void)
{
if (g_kbdfd < 0)
return;
if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
perror(PFX "KDSETMODE KD_TEXT");
if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
perror(PFX "tcsetattr");
close(g_kbdfd);
g_kbdfd = -1;
}
int oshide_init(void)
{
pthread_t tid;
int ret;
ret = pthread_create(&tid, NULL, x11h_handler, NULL);
if (ret != 0) {
fprintf(stderr, PFX "failed to create thread: %d\n", ret);
return ret;
}
pthread_detach(tid);
hidecon_start();
return 0;
}
void oshide_finish(void)
{
/* XXX: the X thread.. */
hidecon_end();
}
#if 0
int main()
{
x11h_init();
sleep(5);
}
#endif
|