2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
14 #include <X11/Xutil.h>
16 #include <sys/types.h>
20 #include <sys/ioctl.h>
24 #define PFX "oshide: "
26 #define FPTR(f) typeof(f) * p##f
27 #define FPTR_LINK(xf, dl, f) { \
28 xf.p##f = dlsym(dl, #f); \
29 if (xf.p##f == NULL) { \
30 fprintf(stderr, "missing symbol: %s\n", #f); \
36 FPTR(XCreateBitmapFromData);
37 FPTR(XCreatePixmapCursor);
42 FPTR(XCreateSimpleWindow);
43 FPTR(XChangeWindowAttributes);
47 FPTR(XCheckTypedEvent);
53 static Cursor transparent_cursor(struct xfuncs *xf, Display *display, Window win)
60 memset(&dummy, 0, sizeof(dummy));
61 pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
62 cursor = xf->pXCreatePixmapCursor(display, pix, pix,
63 &dummy, &dummy, 0, 0);
64 xf->pXFreePixmap(display, pix);
68 static void *x11h_handler(void *arg)
71 unsigned int display_width, display_height;
72 XSetWindowAttributes attributes;
80 memset(&xf, 0, sizeof(xf));
81 x11lib = dlopen("libX11.so.6", RTLD_LAZY);
83 fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
86 FPTR_LINK(xf, x11lib, XCreateBitmapFromData);
87 FPTR_LINK(xf, x11lib, XCreatePixmapCursor);
88 FPTR_LINK(xf, x11lib, XFreePixmap);
89 FPTR_LINK(xf, x11lib, XOpenDisplay);
90 FPTR_LINK(xf, x11lib, XDisplayName);
91 FPTR_LINK(xf, x11lib, XCloseDisplay);
92 FPTR_LINK(xf, x11lib, XCreateSimpleWindow);
93 FPTR_LINK(xf, x11lib, XChangeWindowAttributes);
94 FPTR_LINK(xf, x11lib, XSelectInput);
95 FPTR_LINK(xf, x11lib, XMapWindow);
96 FPTR_LINK(xf, x11lib, XNextEvent);
97 FPTR_LINK(xf, x11lib, XCheckTypedEvent);
98 FPTR_LINK(xf, x11lib, XUnmapWindow);
99 FPTR_LINK(xf, x11lib, XGrabKeyboard);
103 display = xf.pXOpenDisplay(NULL);
106 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
107 xf.pXDisplayName(NULL));
111 visual = DefaultVisual(display, 0);
112 if (visual->class != TrueColor)
113 fprintf(stderr, PFX "warning: non true color visual\n");
115 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
116 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
117 ProtocolRevision(display));
119 screen = DefaultScreen(display);
121 display_width = DisplayWidth(display, screen);
122 display_height = DisplayHeight(display, screen);
123 printf(PFX "display is %dx%d\n", display_width, display_height);
125 win = xf.pXCreateSimpleWindow(display,
126 RootWindow(display, screen),
127 0, 0, display_width, display_height, 0,
128 BlackPixel(display, screen),
129 BlackPixel(display, screen));
131 attributes.override_redirect = True;
132 attributes.cursor = transparent_cursor(&xf, display, win);
133 xf.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
135 xf.pXSelectInput(display, win, ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask);
136 xf.pXMapWindow(display, win);
137 xf.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
138 // XSetIOErrorHandler
142 xf.pXNextEvent(display, &report);
146 while (xf.pXCheckTypedEvent(display, Expose, &report))
152 // printf("focus out\n");
153 // xf.pXUnmapWindow(display, win);
157 // printf("press %d\n", report.xkey.keycode);
168 fprintf(stderr, "x11 handling disabled.\n");
172 static struct termios g_kbd_termios_saved;
175 static void hidecon_start(void)
177 struct termios kbd_termios;
180 g_kbdfd = open("/dev/tty", O_RDWR);
182 perror(PFX "open /dev/tty");
186 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
187 perror(PFX "(not hiding FB): KDGETMODE");
191 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
192 perror(PFX "tcgetattr");
196 g_kbd_termios_saved = kbd_termios;
197 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
198 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
199 kbd_termios.c_cc[VMIN] = 0;
200 kbd_termios.c_cc[VTIME] = 0;
202 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
203 perror(PFX "tcsetattr");
207 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
208 perror(PFX "KDSETMODE KD_GRAPHICS");
209 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
220 static void hidecon_end(void)
225 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
226 perror(PFX "KDSETMODE KD_TEXT");
228 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
229 perror(PFX "tcsetattr");
235 int oshide_init(void)
240 ret = pthread_create(&tid, NULL, x11h_handler, NULL);
242 fprintf(stderr, PFX "failed to create thread: %d\n", ret);
252 void oshide_finish(void)
254 /* XXX: the X thread.. */