2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
17 #include <X11/Xutil.h>
19 #include <sys/types.h>
23 #include <sys/ioctl.h>
27 #define PFX "oshide: "
29 #define FPTR(f) typeof(f) * p##f
30 #define FPTR_LINK(xf, dl, f) { \
31 xf.p##f = dlsym(dl, #f); \
32 if (xf.p##f == NULL) { \
33 fprintf(stderr, "missing symbol: %s\n", #f); \
39 FPTR(XCreateBitmapFromData);
40 FPTR(XCreatePixmapCursor);
45 FPTR(XCreateSimpleWindow);
46 FPTR(XChangeWindowAttributes);
50 FPTR(XCheckTypedEvent);
56 static Cursor transparent_cursor(struct xfuncs *xf, Display *display, Window win)
63 memset(&dummy, 0, sizeof(dummy));
64 pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
65 cursor = xf->pXCreatePixmapCursor(display, pix, pix,
66 &dummy, &dummy, 0, 0);
67 xf->pXFreePixmap(display, pix);
71 static void *x11h_handler(void *arg)
74 unsigned int display_width, display_height;
75 XSetWindowAttributes attributes;
83 memset(&xf, 0, sizeof(xf));
84 x11lib = dlopen("libX11.so.6", RTLD_LAZY);
86 fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
89 FPTR_LINK(xf, x11lib, XCreateBitmapFromData);
90 FPTR_LINK(xf, x11lib, XCreatePixmapCursor);
91 FPTR_LINK(xf, x11lib, XFreePixmap);
92 FPTR_LINK(xf, x11lib, XOpenDisplay);
93 FPTR_LINK(xf, x11lib, XDisplayName);
94 FPTR_LINK(xf, x11lib, XCloseDisplay);
95 FPTR_LINK(xf, x11lib, XCreateSimpleWindow);
96 FPTR_LINK(xf, x11lib, XChangeWindowAttributes);
97 FPTR_LINK(xf, x11lib, XSelectInput);
98 FPTR_LINK(xf, x11lib, XMapWindow);
99 FPTR_LINK(xf, x11lib, XNextEvent);
100 FPTR_LINK(xf, x11lib, XCheckTypedEvent);
101 FPTR_LINK(xf, x11lib, XUnmapWindow);
102 FPTR_LINK(xf, x11lib, XGrabKeyboard);
106 display = xf.pXOpenDisplay(NULL);
109 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
110 xf.pXDisplayName(NULL));
114 visual = DefaultVisual(display, 0);
115 if (visual->class != TrueColor)
116 fprintf(stderr, PFX "warning: non true color visual\n");
118 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
119 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
120 ProtocolRevision(display));
122 screen = DefaultScreen(display);
124 display_width = DisplayWidth(display, screen);
125 display_height = DisplayHeight(display, screen);
126 printf(PFX "display is %dx%d\n", display_width, display_height);
128 win = xf.pXCreateSimpleWindow(display,
129 RootWindow(display, screen),
130 0, 0, display_width, display_height, 0,
131 BlackPixel(display, screen),
132 BlackPixel(display, screen));
134 attributes.override_redirect = True;
135 attributes.cursor = transparent_cursor(&xf, display, win);
136 xf.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
138 xf.pXSelectInput(display, win, ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask);
139 xf.pXMapWindow(display, win);
140 xf.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
141 // XSetIOErrorHandler
145 xf.pXNextEvent(display, &report);
149 while (xf.pXCheckTypedEvent(display, Expose, &report))
155 // printf("focus out\n");
156 // xf.pXUnmapWindow(display, win);
160 // printf("press %d\n", report.xkey.keycode);
171 fprintf(stderr, "x11 handling disabled.\n");
175 static struct termios g_kbd_termios_saved;
178 static void hidecon_start(void)
180 struct termios kbd_termios;
183 g_kbdfd = open("/dev/tty", O_RDWR);
185 perror(PFX "open /dev/tty");
189 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
190 perror(PFX "(not hiding FB): KDGETMODE");
194 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
195 perror(PFX "tcgetattr");
199 g_kbd_termios_saved = kbd_termios;
200 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
201 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
202 kbd_termios.c_cc[VMIN] = 0;
203 kbd_termios.c_cc[VTIME] = 0;
205 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
206 perror(PFX "tcsetattr");
210 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
211 perror(PFX "KDSETMODE KD_GRAPHICS");
212 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
223 static void hidecon_end(void)
228 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
229 perror(PFX "KDSETMODE KD_TEXT");
231 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
232 perror(PFX "tcsetattr");
238 int oshide_init(void)
243 ret = pthread_create(&tid, NULL, x11h_handler, NULL);
245 fprintf(stderr, PFX "failed to create thread: %d\n", ret);
255 void oshide_finish(void)
257 /* XXX: the X thread.. */