frontend: fbdev buffer fix
[pcsx_rearmed.git] / frontend / linux / xenv.c
CommitLineData
698517be 1/*
0b49a8f7 2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2011
698517be 3 *
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.
8 * See the COPYING file in the top-level directory.
9 */
10
11#include <stdio.h>
12#include <string.h>
13#include <pthread.h>
14
15#include <dlfcn.h>
16#include <X11/Xlib.h>
17#include <X11/Xutil.h>
0b49a8f7 18#include <X11/XKBlib.h>
698517be 19
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
0b49a8f7 28#define PFX "xenv: "
698517be 29
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
0b49a8f7 39struct 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);
698517be 58};
59
0b49a8f7 60static struct xstuff g_xstuff;
698517be 61
0b49a8f7 62static Cursor transparent_cursor(struct xstuff *xf, Display *display, Window win)
698517be 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
0b49a8f7 77static int x11h_init(void)
698517be 78{
698517be 79 unsigned int display_width, display_height;
0b49a8f7 80 Display *display;
698517be 81 XSetWindowAttributes attributes;
82 Window win;
698517be 83 Visual *visual;
84 void *x11lib;
85 int screen;
86
0b49a8f7 87 memset(&g_xstuff, 0, sizeof(g_xstuff));
698517be 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 }
0b49a8f7 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);
698517be 110
111 //XInitThreads();
112
0b49a8f7 113 g_xstuff.display = display = g_xstuff.pXOpenDisplay(NULL);
698517be 114 if (display == NULL)
115 {
116 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
0b49a8f7 117 g_xstuff.pXDisplayName(NULL));
698517be 118 goto fail2;
119 }
120
121 visual = DefaultVisual(display, 0);
122 if (visual->class != TrueColor)
123 fprintf(stderr, PFX "warning: non true color visual\n");
124
125 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
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);
133 printf(PFX "display is %dx%d\n", display_width, display_height);
134
0b49a8f7 135 win = g_xstuff.pXCreateSimpleWindow(display,
698517be 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;
0b49a8f7 142 attributes.cursor = transparent_cursor(&g_xstuff, display, win);
143 g_xstuff.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
698517be 144
0b49a8f7 145 g_xstuff.pXSelectInput(display, win, ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask);
146 g_xstuff.pXMapWindow(display, win);
147 g_xstuff.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
148 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
698517be 149 // XSetIOErrorHandler
150
0b49a8f7 151 return 0;
152fail2:
153 dlclose(x11lib);
154fail:
155 g_xstuff.display = NULL;
156 fprintf(stderr, "x11 handling disabled.\n");
157 return -1;
158}
159
160static int x11h_update(int *is_down)
161{
162 XEvent evt;
163
164 while (g_xstuff.pXPending(g_xstuff.display))
698517be 165 {
0b49a8f7 166 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
167 switch (evt.type)
698517be 168 {
169 case Expose:
0b49a8f7 170 while (g_xstuff.pXCheckTypedEvent(g_xstuff.display, Expose, &evt))
698517be 171 ;
172 break;
173
698517be 174 case KeyPress:
0b49a8f7 175 *is_down = 1;
176 return g_xstuff.pXLookupKeysym(&evt.xkey, 0);
698517be 177
0b49a8f7 178 case KeyRelease:
179 *is_down = 0;
180 return g_xstuff.pXLookupKeysym(&evt.xkey, 0);
181 // printf("press %d\n", evt.xkey.keycode);
698517be 182 }
183 }
184
0b49a8f7 185 return NoSymbol;
698517be 186}
187
188static struct termios g_kbd_termios_saved;
0b49a8f7 189static int g_kbdfd = -1;
698517be 190
0b49a8f7 191static int tty_init(void)
698517be 192{
193 struct termios kbd_termios;
194 int mode;
195
196 g_kbdfd = open("/dev/tty", O_RDWR);
197 if (g_kbdfd == -1) {
198 perror(PFX "open /dev/tty");
0b49a8f7 199 return -1;
698517be 200 }
201
202 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
203 perror(PFX "(not hiding FB): KDGETMODE");
204 goto fail;
205 }
206
207 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
208 perror(PFX "tcgetattr");
209 goto fail;
210 }
211
212 g_kbd_termios_saved = kbd_termios;
213 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
214 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
215 kbd_termios.c_cc[VMIN] = 0;
216 kbd_termios.c_cc[VTIME] = 0;
217
218 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
219 perror(PFX "tcsetattr");
220 goto fail;
221 }
222
223 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
224 perror(PFX "KDSETMODE KD_GRAPHICS");
225 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
226 goto fail;
227 }
228
0b49a8f7 229 return 0;
698517be 230
231fail:
232 close(g_kbdfd);
233 g_kbdfd = -1;
0b49a8f7 234 return -1;
698517be 235}
236
0b49a8f7 237static void tty_end(void)
698517be 238{
239 if (g_kbdfd < 0)
240 return;
241
242 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
243 perror(PFX "KDSETMODE KD_TEXT");
244
245 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
246 perror(PFX "tcsetattr");
247
248 close(g_kbdfd);
249 g_kbdfd = -1;
250}
251
0b49a8f7 252int xenv_init(void)
698517be 253{
698517be 254 int ret;
255
0b49a8f7 256 ret = x11h_init();
257 if (ret == 0)
258 return 0;
698517be 259
0b49a8f7 260 ret = tty_init();
261 if (ret == 0)
262 return 0;
698517be 263
0b49a8f7 264 fprintf(stderr, PFX "error: both x11h_init and tty_init failed\n");
265 return -1;
698517be 266}
267
0b49a8f7 268int xenv_update(int *is_down)
698517be 269{
0b49a8f7 270 if (g_xstuff.display)
271 return x11h_update(is_down);
698517be 272
0b49a8f7 273 // TODO: read tty?
274 return -1;
698517be 275}
276
0b49a8f7 277void xenv_finish(void)
698517be 278{
0b49a8f7 279 // TODO: cleanup X?
280 tty_end();
698517be 281}