881fcb77898f58d45ab4f771c309118a2f470623
[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         kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
190         kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
191         kbd_termios.c_cc[VMIN] = 0;
192         kbd_termios.c_cc[VTIME] = 0;
193
194         if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
195                 perror(PFX "tcsetattr");
196                 goto fail;
197         }
198
199         if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
200                 perror(PFX "KDSETMODE KD_GRAPHICS");
201                 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
202                 goto fail;
203         }
204
205         return;
206
207 fail:
208         close(g_kbdfd);
209         g_kbdfd = -1;
210 }
211
212 static void hidecon_end(void)
213 {
214         if (g_kbdfd < 0)
215                 return;
216
217         if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
218                 perror(PFX "KDSETMODE KD_TEXT");
219
220         if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
221                 perror(PFX "tcsetattr");
222
223         close(g_kbdfd);
224         g_kbdfd = -1;
225 }
226
227 int oshide_init(void)
228 {
229         pthread_t tid;
230         int ret;
231
232         ret = pthread_create(&tid, NULL, x11h_handler, NULL);
233         if (ret != 0) {
234                 fprintf(stderr, PFX "failed to create thread: %d\n", ret);
235                 return ret;
236         }
237         pthread_detach(tid);
238
239         hidecon_start();
240
241         return 0;
242 }
243
244 void oshide_finish(void)
245 {
246         /* XXX: the X thread.. */
247
248         hidecon_end();
249 }
250
251 #if 0
252 int main()
253 {
254         x11h_init();
255         sleep(5);
256 }
257 #endif