some pandora tweaks
[libpicofe.git] / linux / oshide.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <pthread.h>
4
5 #include <dlfcn.h>
6 #include <X11/Xlib.h>
7 #include <X11/Xutil.h>
8
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
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
28 struct xfuncs {
29 FPTR(XCreateBitmapFromData);
30 FPTR(XCreatePixmapCursor);
31 FPTR(XFreePixmap);
32 FPTR(XOpenDisplay);
33 FPTR(XDisplayName);
34 FPTR(XCloseDisplay);
35 FPTR(XCreateSimpleWindow);
36 FPTR(XChangeWindowAttributes);
37 FPTR(XSelectInput);
38 FPTR(XMapWindow);
39 FPTR(XNextEvent);
40 FPTR(XCheckTypedEvent);
41 FPTR(XUnmapWindow);
42 FPTR(XGrabKeyboard);
43 };
44
45
46 static 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
61 static 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)
106                 fprintf(stderr, PFX "warning: non true color visual\n");
107
108         printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
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);
116         printf(PFX "display is %dx%d\n", display_width, display_height);
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
158 fail2:
159         dlclose(x11lib);
160 fail:
161         fprintf(stderr, "x11 handling disabled.\n");
162         return NULL;
163 }
164
165 static struct termios g_kbd_termios_saved;
166 static int g_kbdfd;
167
168 static void hidecon_start(void)
169 {
170         struct termios kbd_termios;
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
189         g_kbd_termios_saved = kbd_termios;
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
208 fail:
209         close(g_kbdfd);
210         g_kbdfd = -1;
211 }
212
213 static 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
224         close(g_kbdfd);
225         g_kbdfd = -1;
226 }
227
228 int oshide_init(void)
229 {
230         pthread_t tid;
231         int ret;
232
233         ret = pthread_create(&tid, NULL, x11h_handler, NULL);
234         if (ret != 0) {
235                 fprintf(stderr, PFX "failed to create thread: %d\n", ret);
236                 return ret;
237         }
238         pthread_detach(tid);
239
240         hidecon_start();
241
242         return 0;
243 }
244
245 void oshide_finish(void)
246 {
247         /* XXX: the X thread.. */
248
249         hidecon_end();
250 }
251
252 #if 0
253 int main()
254 {
255         x11h_init();
256         sleep(5);
257 }
258 #endif