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