tweaking pandora frontend
[libpicofe.git] / linux / oshide.c
CommitLineData
b2c5ee47 1#include <stdio.h>
2#include <string.h>
3#include <pthread.h>
f6eaae4f 4
b2c5ee47 5#include <dlfcn.h>
6#include <X11/Xlib.h>
7#include <X11/Xutil.h>
8
f6eaae4f 9#include <sys/types.h>
10#include <sys/stat.h>
11#include <fcntl.h>
12#include <unistd.h>
13#include <sys/ioctl.h>
14#include <termios.h>
15#include <linux/kd.h>
16
17#define PFX "oshide: "
18#define TERMIOS_DUMP_FILE "/tmp/pico_tios"
19
b2c5ee47 20#define FPTR(f) typeof(f) * p##f
21#define FPTR_LINK(xf, dl, f) { \
22 xf.p##f = dlsym(dl, #f); \
23 if (xf.p##f == NULL) { \
24 fprintf(stderr, "missing symbol: %s\n", #f); \
25 goto fail; \
26 } \
27}
28
29struct xfuncs {
30FPTR(XCreateBitmapFromData);
31FPTR(XCreatePixmapCursor);
32FPTR(XFreePixmap);
33FPTR(XOpenDisplay);
34FPTR(XDisplayName);
35FPTR(XCloseDisplay);
36FPTR(XCreateSimpleWindow);
37FPTR(XChangeWindowAttributes);
38FPTR(XSelectInput);
39FPTR(XMapWindow);
40FPTR(XNextEvent);
41FPTR(XCheckTypedEvent);
42FPTR(XUnmapWindow);
43FPTR(XGrabKeyboard);
44};
45
46
47static Cursor transparent_cursor(struct xfuncs *xf, Display *display, Window win)
48{
49 Cursor cursor;
50 Pixmap pix;
51 XColor dummy;
52 char d = 0;
53
54 memset(&dummy, 0, sizeof(dummy));
55 pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
56 cursor = xf->pXCreatePixmapCursor(display, pix, pix,
57 &dummy, &dummy, 0, 0);
58 xf->pXFreePixmap(display, pix);
59 return cursor;
60}
61
62static void *x11h_handler(void *arg)
63{
64 struct xfuncs xf;
65 unsigned int display_width, display_height;
66 XSetWindowAttributes attributes;
67 Window win;
68 XEvent report;
69 Display *display;
70 Visual *visual;
71 void *x11lib;
72 int screen;
73
74 memset(&xf, 0, sizeof(xf));
75 x11lib = dlopen("libX11.so.6", RTLD_LAZY);
76 if (x11lib == NULL) {
77 fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
78 goto fail;
79 }
80 FPTR_LINK(xf, x11lib, XCreateBitmapFromData);
81 FPTR_LINK(xf, x11lib, XCreatePixmapCursor);
82 FPTR_LINK(xf, x11lib, XFreePixmap);
83 FPTR_LINK(xf, x11lib, XOpenDisplay);
84 FPTR_LINK(xf, x11lib, XDisplayName);
85 FPTR_LINK(xf, x11lib, XCloseDisplay);
86 FPTR_LINK(xf, x11lib, XCreateSimpleWindow);
87 FPTR_LINK(xf, x11lib, XChangeWindowAttributes);
88 FPTR_LINK(xf, x11lib, XSelectInput);
89 FPTR_LINK(xf, x11lib, XMapWindow);
90 FPTR_LINK(xf, x11lib, XNextEvent);
91 FPTR_LINK(xf, x11lib, XCheckTypedEvent);
92 FPTR_LINK(xf, x11lib, XUnmapWindow);
93 FPTR_LINK(xf, x11lib, XGrabKeyboard);
94
95 //XInitThreads();
96
97 display = xf.pXOpenDisplay(NULL);
98 if (display == NULL)
99 {
100 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
101 xf.pXDisplayName(NULL));
102 goto fail2;
103 }
104
105 visual = DefaultVisual(display, 0);
106 if (visual->class != TrueColor)
f6eaae4f 107 fprintf(stderr, PFX "warning: non true color visual\n");
b2c5ee47 108
f6eaae4f 109 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
b2c5ee47 110 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
111 ProtocolRevision(display));
112
113 screen = DefaultScreen(display);
114
115 display_width = DisplayWidth(display, screen);
116 display_height = DisplayHeight(display, screen);
f6eaae4f 117 printf(PFX "display is %dx%d\n", display_width, display_height);
b2c5ee47 118
119 win = xf.pXCreateSimpleWindow(display,
120 RootWindow(display, screen),
121 0, 0, display_width, display_height, 0,
122 BlackPixel(display, screen),
123 BlackPixel(display, screen));
124
125 attributes.override_redirect = True;
126 attributes.cursor = transparent_cursor(&xf, display, win);
127 xf.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
128
129 xf.pXSelectInput(display, win, ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask);
130 xf.pXMapWindow(display, win);
131 xf.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
132 // XSetIOErrorHandler
133
134 while (1)
135 {
136 xf.pXNextEvent(display, &report);
137 switch (report.type)
138 {
139 case Expose:
140 while (xf.pXCheckTypedEvent(display, Expose, &report))
141 ;
142 break;
143
144 case FocusOut:
145 // XFocusChangeEvent
146 // printf("focus out\n");
147 // xf.pXUnmapWindow(display, win);
148 break;
149
150 case KeyPress:
151 // printf("press %d\n", report.xkey.keycode);
152 break;
153
154 default:
155 break;
156 }
157 }
158
159fail2:
160 dlclose(x11lib);
161fail:
162 fprintf(stderr, "x11 handling disabled.\n");
163 return NULL;
164}
165
f6eaae4f 166static struct termios g_kbd_termios_saved;
167static int g_kbdfd;
168
169static void hidecon_start(void)
170{
171 struct termios kbd_termios;
172 FILE *tios_f;
173 int mode;
174
175 g_kbdfd = open("/dev/tty", O_RDWR);
176 if (g_kbdfd == -1) {
177 perror(PFX "open /dev/tty");
178 return;
179 }
180
181 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
182 perror(PFX "(not hiding FB): KDGETMODE");
183 goto fail;
184 }
185
186 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
187 perror(PFX "tcgetattr");
188 goto fail;
189 }
190
191 /* dump for picorestore */
192 g_kbd_termios_saved = kbd_termios;
193 tios_f = fopen(TERMIOS_DUMP_FILE, "wb");
194 if (tios_f) {
195 fwrite(&kbd_termios, sizeof(kbd_termios), 1, tios_f);
196 fclose(tios_f);
197 }
198
199 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
200 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
201 kbd_termios.c_cc[VMIN] = 0;
202 kbd_termios.c_cc[VTIME] = 0;
203
204 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
205 perror(PFX "tcsetattr");
206 goto fail;
207 }
208
209 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
210 perror(PFX "KDSETMODE KD_GRAPHICS");
211 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
212 goto fail;
213 }
214
215 return;
216
217fail:
218 close(g_kbdfd);
219 g_kbdfd = -1;
220}
221
222static void hidecon_end(void)
223{
224 if (g_kbdfd < 0)
225 return;
226
227 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
228 perror(PFX "KDSETMODE KD_TEXT");
229
230 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
231 perror(PFX "tcsetattr");
232
233 remove(TERMIOS_DUMP_FILE);
234
235 close(g_kbdfd);
236 g_kbdfd = -1;
237}
238
239int oshide_init(void)
b2c5ee47 240{
241 pthread_t tid;
242 int ret;
243
244 ret = pthread_create(&tid, NULL, x11h_handler, NULL);
245 if (ret != 0) {
f6eaae4f 246 fprintf(stderr, PFX "failed to create thread: %d\n", ret);
b2c5ee47 247 return ret;
248 }
249 pthread_detach(tid);
250
f6eaae4f 251 hidecon_start();
252
b2c5ee47 253 return 0;
254}
255
f6eaae4f 256void oshide_finish(void)
257{
258 /* XXX: the X thread.. */
259
260 hidecon_end();
261}
262
b2c5ee47 263#if 0
264int main()
265{
266 x11h_init();
267 sleep(5);
268}
269#endif