13 #include <sys/ioctl.h>
17 #define PFX "oshide: "
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); \
29 FPTR(XCreateBitmapFromData);
30 FPTR(XCreatePixmapCursor);
35 FPTR(XCreateSimpleWindow);
36 FPTR(XChangeWindowAttributes);
40 FPTR(XCheckTypedEvent);
46 static Cursor transparent_cursor(struct xfuncs *xf, Display *display, Window win)
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);
61 static void *x11h_handler(void *arg)
64 unsigned int display_width, display_height;
65 XSetWindowAttributes attributes;
73 memset(&xf, 0, sizeof(xf));
74 x11lib = dlopen("libX11.so.6", RTLD_LAZY);
76 fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
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);
96 display = xf.pXOpenDisplay(NULL);
99 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
100 xf.pXDisplayName(NULL));
104 visual = DefaultVisual(display, 0);
105 if (visual->class != TrueColor)
106 fprintf(stderr, PFX "warning: non true color visual\n");
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));
112 screen = DefaultScreen(display);
114 display_width = DisplayWidth(display, screen);
115 display_height = DisplayHeight(display, screen);
116 printf(PFX "display is %dx%d\n", display_width, display_height);
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));
124 attributes.override_redirect = True;
125 attributes.cursor = transparent_cursor(&xf, display, win);
126 xf.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
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
135 xf.pXNextEvent(display, &report);
139 while (xf.pXCheckTypedEvent(display, Expose, &report))
145 // printf("focus out\n");
146 // xf.pXUnmapWindow(display, win);
150 // printf("press %d\n", report.xkey.keycode);
161 fprintf(stderr, "x11 handling disabled.\n");
165 static struct termios g_kbd_termios_saved;
168 static void hidecon_start(void)
170 struct termios kbd_termios;
173 g_kbdfd = open("/dev/tty", O_RDWR);
175 perror(PFX "open /dev/tty");
179 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
180 perror(PFX "(not hiding FB): KDGETMODE");
184 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
185 perror(PFX "tcgetattr");
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;
195 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
196 perror(PFX "tcsetattr");
200 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
201 perror(PFX "KDSETMODE KD_GRAPHICS");
202 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
213 static void hidecon_end(void)
218 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
219 perror(PFX "KDSETMODE KD_TEXT");
221 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
222 perror(PFX "tcsetattr");
228 int oshide_init(void)
233 ret = pthread_create(&tid, NULL, x11h_handler, NULL);
235 fprintf(stderr, PFX "failed to create thread: %d\n", ret);
245 void oshide_finish(void)
247 /* XXX: the X thread.. */