392b07b2 |
1 | /* |
c7b78b94 |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2009-2012 |
392b07b2 |
3 | * |
957a9eac |
4 | * This work is licensed under the terms of any of these licenses |
5 | * (at your option): |
6 | * - GNU GPL, version 2 or later. |
7 | * - GNU LGPL, version 2.1 or later. |
392b07b2 |
8 | * See the COPYING file in the top-level directory. |
9 | */ |
10 | |
b2c5ee47 |
11 | #include <stdio.h> |
12 | #include <string.h> |
13 | #include <pthread.h> |
f6eaae4f |
14 | |
b2c5ee47 |
15 | #include <dlfcn.h> |
16 | #include <X11/Xlib.h> |
17 | #include <X11/Xutil.h> |
838a76d4 |
18 | #include <X11/XKBlib.h> |
b2c5ee47 |
19 | |
f6eaae4f |
20 | #include <sys/types.h> |
21 | #include <sys/stat.h> |
22 | #include <fcntl.h> |
23 | #include <unistd.h> |
24 | #include <sys/ioctl.h> |
25 | #include <termios.h> |
26 | #include <linux/kd.h> |
27 | |
838a76d4 |
28 | #define PFX "xenv: " |
f6eaae4f |
29 | |
b2c5ee47 |
30 | #define FPTR(f) typeof(f) * p##f |
31 | #define FPTR_LINK(xf, dl, f) { \ |
32 | xf.p##f = dlsym(dl, #f); \ |
33 | if (xf.p##f == NULL) { \ |
34 | fprintf(stderr, "missing symbol: %s\n", #f); \ |
35 | goto fail; \ |
36 | } \ |
37 | } |
38 | |
838a76d4 |
39 | struct xstuff { |
40 | Display *display; |
41 | FPTR(XCreateBitmapFromData); |
42 | FPTR(XCreatePixmapCursor); |
43 | FPTR(XFreePixmap); |
44 | FPTR(XOpenDisplay); |
45 | FPTR(XDisplayName); |
46 | FPTR(XCloseDisplay); |
47 | FPTR(XCreateSimpleWindow); |
48 | FPTR(XChangeWindowAttributes); |
49 | FPTR(XSelectInput); |
50 | FPTR(XMapWindow); |
51 | FPTR(XNextEvent); |
52 | FPTR(XCheckTypedEvent); |
53 | FPTR(XUnmapWindow); |
54 | FPTR(XGrabKeyboard); |
55 | FPTR(XPending); |
56 | FPTR(XLookupKeysym); |
57 | FPTR(XkbSetDetectableAutoRepeat); |
b2c5ee47 |
58 | }; |
59 | |
838a76d4 |
60 | static struct xstuff g_xstuff; |
b2c5ee47 |
61 | |
838a76d4 |
62 | static Cursor transparent_cursor(struct xstuff *xf, Display *display, Window win) |
b2c5ee47 |
63 | { |
64 | Cursor cursor; |
65 | Pixmap pix; |
66 | XColor dummy; |
67 | char d = 0; |
68 | |
69 | memset(&dummy, 0, sizeof(dummy)); |
70 | pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1); |
71 | cursor = xf->pXCreatePixmapCursor(display, pix, pix, |
72 | &dummy, &dummy, 0, 0); |
73 | xf->pXFreePixmap(display, pix); |
74 | return cursor; |
75 | } |
76 | |
838a76d4 |
77 | static int x11h_init(void) |
b2c5ee47 |
78 | { |
b2c5ee47 |
79 | unsigned int display_width, display_height; |
838a76d4 |
80 | Display *display; |
b2c5ee47 |
81 | XSetWindowAttributes attributes; |
82 | Window win; |
b2c5ee47 |
83 | Visual *visual; |
84 | void *x11lib; |
85 | int screen; |
86 | |
838a76d4 |
87 | memset(&g_xstuff, 0, sizeof(g_xstuff)); |
b2c5ee47 |
88 | x11lib = dlopen("libX11.so.6", RTLD_LAZY); |
89 | if (x11lib == NULL) { |
90 | fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror()); |
91 | goto fail; |
92 | } |
838a76d4 |
93 | FPTR_LINK(g_xstuff, x11lib, XCreateBitmapFromData); |
94 | FPTR_LINK(g_xstuff, x11lib, XCreatePixmapCursor); |
95 | FPTR_LINK(g_xstuff, x11lib, XFreePixmap); |
96 | FPTR_LINK(g_xstuff, x11lib, XOpenDisplay); |
97 | FPTR_LINK(g_xstuff, x11lib, XDisplayName); |
98 | FPTR_LINK(g_xstuff, x11lib, XCloseDisplay); |
99 | FPTR_LINK(g_xstuff, x11lib, XCreateSimpleWindow); |
100 | FPTR_LINK(g_xstuff, x11lib, XChangeWindowAttributes); |
101 | FPTR_LINK(g_xstuff, x11lib, XSelectInput); |
102 | FPTR_LINK(g_xstuff, x11lib, XMapWindow); |
103 | FPTR_LINK(g_xstuff, x11lib, XNextEvent); |
104 | FPTR_LINK(g_xstuff, x11lib, XCheckTypedEvent); |
105 | FPTR_LINK(g_xstuff, x11lib, XUnmapWindow); |
106 | FPTR_LINK(g_xstuff, x11lib, XGrabKeyboard); |
107 | FPTR_LINK(g_xstuff, x11lib, XPending); |
108 | FPTR_LINK(g_xstuff, x11lib, XLookupKeysym); |
109 | FPTR_LINK(g_xstuff, x11lib, XkbSetDetectableAutoRepeat); |
b2c5ee47 |
110 | |
111 | //XInitThreads(); |
112 | |
838a76d4 |
113 | g_xstuff.display = display = g_xstuff.pXOpenDisplay(NULL); |
b2c5ee47 |
114 | if (display == NULL) |
115 | { |
116 | fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n", |
838a76d4 |
117 | g_xstuff.pXDisplayName(NULL)); |
b2c5ee47 |
118 | goto fail2; |
119 | } |
120 | |
121 | visual = DefaultVisual(display, 0); |
122 | if (visual->class != TrueColor) |
f6eaae4f |
123 | fprintf(stderr, PFX "warning: non true color visual\n"); |
b2c5ee47 |
124 | |
f6eaae4f |
125 | printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display), |
b2c5ee47 |
126 | VendorRelease(display), DisplayString(display), ProtocolVersion(display), |
127 | ProtocolRevision(display)); |
128 | |
129 | screen = DefaultScreen(display); |
130 | |
131 | display_width = DisplayWidth(display, screen); |
132 | display_height = DisplayHeight(display, screen); |
f6eaae4f |
133 | printf(PFX "display is %dx%d\n", display_width, display_height); |
b2c5ee47 |
134 | |
838a76d4 |
135 | win = g_xstuff.pXCreateSimpleWindow(display, |
b2c5ee47 |
136 | RootWindow(display, screen), |
137 | 0, 0, display_width, display_height, 0, |
138 | BlackPixel(display, screen), |
139 | BlackPixel(display, screen)); |
140 | |
141 | attributes.override_redirect = True; |
838a76d4 |
142 | attributes.cursor = transparent_cursor(&g_xstuff, display, win); |
143 | g_xstuff.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes); |
b2c5ee47 |
144 | |
c7b78b94 |
145 | g_xstuff.pXSelectInput(display, win, ExposureMask | FocusChangeMask | KeyPressMask |
146 | | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); |
838a76d4 |
147 | g_xstuff.pXMapWindow(display, win); |
148 | g_xstuff.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime); |
149 | g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL); |
b2c5ee47 |
150 | // XSetIOErrorHandler |
151 | |
838a76d4 |
152 | return 0; |
153 | fail2: |
154 | dlclose(x11lib); |
155 | fail: |
156 | g_xstuff.display = NULL; |
157 | fprintf(stderr, "x11 handling disabled.\n"); |
158 | return -1; |
159 | } |
160 | |
c7b78b94 |
161 | static void x11h_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed), |
162 | int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed), |
163 | int (*mousem_cb)(void *cb_arg, int x, int y), |
164 | void *cb_arg) |
838a76d4 |
165 | { |
166 | XEvent evt; |
c7b78b94 |
167 | int keysym; |
838a76d4 |
168 | |
169 | while (g_xstuff.pXPending(g_xstuff.display)) |
b2c5ee47 |
170 | { |
838a76d4 |
171 | g_xstuff.pXNextEvent(g_xstuff.display, &evt); |
172 | switch (evt.type) |
b2c5ee47 |
173 | { |
c7b78b94 |
174 | case Expose: |
175 | while (g_xstuff.pXCheckTypedEvent(g_xstuff.display, Expose, &evt)) |
176 | ; |
177 | break; |
178 | |
179 | case KeyPress: |
180 | keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0); |
181 | if (key_cb != NULL) |
182 | key_cb(cb_arg, keysym, 1); |
183 | break; |
184 | |
185 | case KeyRelease: |
186 | keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0); |
187 | if (key_cb != NULL) |
188 | key_cb(cb_arg, keysym, 0); |
189 | break; |
190 | |
191 | case ButtonPress: |
192 | if (mouseb_cb != NULL) |
193 | mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y, |
194 | evt.xbutton.button, 1); |
195 | break; |
196 | |
197 | case ButtonRelease: |
198 | if (mouseb_cb != NULL) |
199 | mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y, |
200 | evt.xbutton.button, 0); |
201 | break; |
202 | |
203 | case MotionNotify: |
204 | if (mousem_cb != NULL) |
205 | mousem_cb(cb_arg, evt.xmotion.x, evt.xmotion.y); |
206 | break; |
b2c5ee47 |
207 | } |
208 | } |
b2c5ee47 |
209 | } |
210 | |
f6eaae4f |
211 | static struct termios g_kbd_termios_saved; |
3797b7de |
212 | static int g_kbdfd = -1; |
f6eaae4f |
213 | |
838a76d4 |
214 | static int tty_init(void) |
f6eaae4f |
215 | { |
216 | struct termios kbd_termios; |
f6eaae4f |
217 | int mode; |
218 | |
219 | g_kbdfd = open("/dev/tty", O_RDWR); |
220 | if (g_kbdfd == -1) { |
221 | perror(PFX "open /dev/tty"); |
838a76d4 |
222 | return -1; |
f6eaae4f |
223 | } |
224 | |
225 | if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) { |
226 | perror(PFX "(not hiding FB): KDGETMODE"); |
227 | goto fail; |
228 | } |
229 | |
230 | if (tcgetattr(g_kbdfd, &kbd_termios) == -1) { |
231 | perror(PFX "tcgetattr"); |
232 | goto fail; |
233 | } |
234 | |
209a7eff |
235 | g_kbd_termios_saved = kbd_termios; |
f6eaae4f |
236 | kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG); |
237 | kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON); |
238 | kbd_termios.c_cc[VMIN] = 0; |
239 | kbd_termios.c_cc[VTIME] = 0; |
240 | |
241 | if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) { |
242 | perror(PFX "tcsetattr"); |
243 | goto fail; |
244 | } |
245 | |
246 | if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) { |
247 | perror(PFX "KDSETMODE KD_GRAPHICS"); |
248 | tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved); |
249 | goto fail; |
250 | } |
251 | |
838a76d4 |
252 | return 0; |
f6eaae4f |
253 | |
254 | fail: |
255 | close(g_kbdfd); |
256 | g_kbdfd = -1; |
838a76d4 |
257 | return -1; |
f6eaae4f |
258 | } |
259 | |
838a76d4 |
260 | static void tty_end(void) |
f6eaae4f |
261 | { |
262 | if (g_kbdfd < 0) |
263 | return; |
264 | |
265 | if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1) |
266 | perror(PFX "KDSETMODE KD_TEXT"); |
267 | |
268 | if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1) |
269 | perror(PFX "tcsetattr"); |
270 | |
f6eaae4f |
271 | close(g_kbdfd); |
272 | g_kbdfd = -1; |
273 | } |
274 | |
c7b78b94 |
275 | int xenv_init(int *have_mouse_events) |
b2c5ee47 |
276 | { |
c7b78b94 |
277 | int have_mouse = 0; |
b2c5ee47 |
278 | int ret; |
279 | |
838a76d4 |
280 | ret = x11h_init(); |
c7b78b94 |
281 | if (ret == 0) { |
282 | have_mouse = 1; |
283 | goto out; |
284 | } |
b2c5ee47 |
285 | |
838a76d4 |
286 | ret = tty_init(); |
287 | if (ret == 0) |
c7b78b94 |
288 | goto out; |
f6eaae4f |
289 | |
838a76d4 |
290 | fprintf(stderr, PFX "error: both x11h_init and tty_init failed\n"); |
c7b78b94 |
291 | ret = -1; |
292 | out: |
293 | if (have_mouse_events != NULL) |
294 | *have_mouse_events = have_mouse; |
295 | return ret; |
b2c5ee47 |
296 | } |
297 | |
c7b78b94 |
298 | int xenv_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed), |
299 | int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed), |
300 | int (*mousem_cb)(void *cb_arg, int x, int y), |
301 | void *cb_arg) |
f6eaae4f |
302 | { |
c7b78b94 |
303 | if (g_xstuff.display) { |
304 | x11h_update(key_cb, mouseb_cb, mousem_cb, cb_arg); |
305 | return 0; |
306 | } |
f6eaae4f |
307 | |
838a76d4 |
308 | // TODO: read tty? |
309 | return -1; |
f6eaae4f |
310 | } |
311 | |
838a76d4 |
312 | void xenv_finish(void) |
b2c5ee47 |
313 | { |
838a76d4 |
314 | // TODO: cleanup X? |
315 | tty_end(); |
b2c5ee47 |
316 | } |