13 #include <sys/ioctl.h>
17 #define PFX "oshide: "
18 #define TERMIOS_DUMP_FILE "/tmp/pico_tios"
20 #define FPTR(f) typeof(f) * p##f
21 #define FPTR_LINK(xf, dl, f) { \
22 xf.p##f = dlsym(dl, #f); \
23 if (xf.p##f == NULL) { \
24 fprintf(stderr, "missing symbol: %s\n", #f); \
30 FPTR(XCreateBitmapFromData);
31 FPTR(XCreatePixmapCursor);
36 FPTR(XCreateSimpleWindow);
37 FPTR(XChangeWindowAttributes);
41 FPTR(XCheckTypedEvent);
47 static Cursor transparent_cursor(struct xfuncs *xf, Display *display, Window win)
54 memset(&dummy, 0, sizeof(dummy));
55 pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
56 cursor = xf->pXCreatePixmapCursor(display, pix, pix,
57 &dummy, &dummy, 0, 0);
58 xf->pXFreePixmap(display, pix);
62 static void *x11h_handler(void *arg)
65 unsigned int display_width, display_height;
66 XSetWindowAttributes attributes;
74 memset(&xf, 0, sizeof(xf));
75 x11lib = dlopen("libX11.so.6", RTLD_LAZY);
77 fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
80 FPTR_LINK(xf, x11lib, XCreateBitmapFromData);
81 FPTR_LINK(xf, x11lib, XCreatePixmapCursor);
82 FPTR_LINK(xf, x11lib, XFreePixmap);
83 FPTR_LINK(xf, x11lib, XOpenDisplay);
84 FPTR_LINK(xf, x11lib, XDisplayName);
85 FPTR_LINK(xf, x11lib, XCloseDisplay);
86 FPTR_LINK(xf, x11lib, XCreateSimpleWindow);
87 FPTR_LINK(xf, x11lib, XChangeWindowAttributes);
88 FPTR_LINK(xf, x11lib, XSelectInput);
89 FPTR_LINK(xf, x11lib, XMapWindow);
90 FPTR_LINK(xf, x11lib, XNextEvent);
91 FPTR_LINK(xf, x11lib, XCheckTypedEvent);
92 FPTR_LINK(xf, x11lib, XUnmapWindow);
93 FPTR_LINK(xf, x11lib, XGrabKeyboard);
97 display = xf.pXOpenDisplay(NULL);
100 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
101 xf.pXDisplayName(NULL));
105 visual = DefaultVisual(display, 0);
106 if (visual->class != TrueColor)
107 fprintf(stderr, PFX "warning: non true color visual\n");
109 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
110 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
111 ProtocolRevision(display));
113 screen = DefaultScreen(display);
115 display_width = DisplayWidth(display, screen);
116 display_height = DisplayHeight(display, screen);
117 printf(PFX "display is %dx%d\n", display_width, display_height);
119 win = xf.pXCreateSimpleWindow(display,
120 RootWindow(display, screen),
121 0, 0, display_width, display_height, 0,
122 BlackPixel(display, screen),
123 BlackPixel(display, screen));
125 attributes.override_redirect = True;
126 attributes.cursor = transparent_cursor(&xf, display, win);
127 xf.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
129 xf.pXSelectInput(display, win, ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask);
130 xf.pXMapWindow(display, win);
131 xf.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
132 // XSetIOErrorHandler
136 xf.pXNextEvent(display, &report);
140 while (xf.pXCheckTypedEvent(display, Expose, &report))
146 // printf("focus out\n");
147 // xf.pXUnmapWindow(display, win);
151 // printf("press %d\n", report.xkey.keycode);
162 fprintf(stderr, "x11 handling disabled.\n");
166 static struct termios g_kbd_termios_saved;
169 static void hidecon_start(void)
171 struct termios kbd_termios;
175 g_kbdfd = open("/dev/tty", O_RDWR);
177 perror(PFX "open /dev/tty");
181 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
182 perror(PFX "(not hiding FB): KDGETMODE");
186 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
187 perror(PFX "tcgetattr");
191 /* dump for picorestore */
192 g_kbd_termios_saved = kbd_termios;
193 tios_f = fopen(TERMIOS_DUMP_FILE, "wb");
195 fwrite(&kbd_termios, sizeof(kbd_termios), 1, tios_f);
199 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
200 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
201 kbd_termios.c_cc[VMIN] = 0;
202 kbd_termios.c_cc[VTIME] = 0;
204 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
205 perror(PFX "tcsetattr");
209 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
210 perror(PFX "KDSETMODE KD_GRAPHICS");
211 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
222 static void hidecon_end(void)
227 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
228 perror(PFX "KDSETMODE KD_TEXT");
230 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
231 perror(PFX "tcsetattr");
233 remove(TERMIOS_DUMP_FILE);
239 int oshide_init(void)
244 ret = pthread_create(&tid, NULL, x11h_handler, NULL);
246 fprintf(stderr, PFX "failed to create thread: %d\n", ret);
256 void oshide_finish(void)
258 /* XXX: the X thread.. */