| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2009-2010 |
| 3 | * |
| 4 | * This work is licensed under the terms of any of these licenses |
| 5 | * (at your option): |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <pthread.h> |
| 14 | |
| 15 | #include <dlfcn.h> |
| 16 | #include <X11/Xlib.h> |
| 17 | #include <X11/Xutil.h> |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <termios.h> |
| 25 | #include <linux/kd.h> |
| 26 | |
| 27 | #define PFX "oshide: " |
| 28 | |
| 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); \ |
| 34 | goto fail; \ |
| 35 | } \ |
| 36 | } |
| 37 | |
| 38 | struct xfuncs { |
| 39 | FPTR(XCreateBitmapFromData); |
| 40 | FPTR(XCreatePixmapCursor); |
| 41 | FPTR(XFreePixmap); |
| 42 | FPTR(XOpenDisplay); |
| 43 | FPTR(XDisplayName); |
| 44 | FPTR(XCloseDisplay); |
| 45 | FPTR(XCreateSimpleWindow); |
| 46 | FPTR(XChangeWindowAttributes); |
| 47 | FPTR(XSelectInput); |
| 48 | FPTR(XMapWindow); |
| 49 | FPTR(XNextEvent); |
| 50 | FPTR(XCheckTypedEvent); |
| 51 | FPTR(XUnmapWindow); |
| 52 | FPTR(XGrabKeyboard); |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | static Cursor transparent_cursor(struct xfuncs *xf, Display *display, Window win) |
| 57 | { |
| 58 | Cursor cursor; |
| 59 | Pixmap pix; |
| 60 | XColor dummy; |
| 61 | char d = 0; |
| 62 | |
| 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); |
| 68 | return cursor; |
| 69 | } |
| 70 | |
| 71 | static void *x11h_handler(void *arg) |
| 72 | { |
| 73 | struct xfuncs xf; |
| 74 | unsigned int display_width, display_height; |
| 75 | XSetWindowAttributes attributes; |
| 76 | Window win; |
| 77 | XEvent report; |
| 78 | Display *display; |
| 79 | Visual *visual; |
| 80 | void *x11lib; |
| 81 | int screen; |
| 82 | |
| 83 | memset(&xf, 0, sizeof(xf)); |
| 84 | x11lib = dlopen("libX11.so.6", RTLD_LAZY); |
| 85 | if (x11lib == NULL) { |
| 86 | fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror()); |
| 87 | goto fail; |
| 88 | } |
| 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); |
| 103 | |
| 104 | //XInitThreads(); |
| 105 | |
| 106 | display = xf.pXOpenDisplay(NULL); |
| 107 | if (display == NULL) |
| 108 | { |
| 109 | fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n", |
| 110 | xf.pXDisplayName(NULL)); |
| 111 | goto fail2; |
| 112 | } |
| 113 | |
| 114 | visual = DefaultVisual(display, 0); |
| 115 | if (visual->class != TrueColor) |
| 116 | fprintf(stderr, PFX "warning: non true color visual\n"); |
| 117 | |
| 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)); |
| 121 | |
| 122 | screen = DefaultScreen(display); |
| 123 | |
| 124 | display_width = DisplayWidth(display, screen); |
| 125 | display_height = DisplayHeight(display, screen); |
| 126 | printf(PFX "display is %dx%d\n", display_width, display_height); |
| 127 | |
| 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)); |
| 133 | |
| 134 | attributes.override_redirect = True; |
| 135 | attributes.cursor = transparent_cursor(&xf, display, win); |
| 136 | xf.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes); |
| 137 | |
| 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 |
| 142 | |
| 143 | while (1) |
| 144 | { |
| 145 | xf.pXNextEvent(display, &report); |
| 146 | switch (report.type) |
| 147 | { |
| 148 | case Expose: |
| 149 | while (xf.pXCheckTypedEvent(display, Expose, &report)) |
| 150 | ; |
| 151 | break; |
| 152 | |
| 153 | case FocusOut: |
| 154 | // XFocusChangeEvent |
| 155 | // printf("focus out\n"); |
| 156 | // xf.pXUnmapWindow(display, win); |
| 157 | break; |
| 158 | |
| 159 | case KeyPress: |
| 160 | // printf("press %d\n", report.xkey.keycode); |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | fail2: |
| 169 | dlclose(x11lib); |
| 170 | fail: |
| 171 | fprintf(stderr, "x11 handling disabled.\n"); |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | static struct termios g_kbd_termios_saved; |
| 176 | static int g_kbdfd; |
| 177 | |
| 178 | static void hidecon_start(void) |
| 179 | { |
| 180 | struct termios kbd_termios; |
| 181 | int mode; |
| 182 | |
| 183 | g_kbdfd = open("/dev/tty", O_RDWR); |
| 184 | if (g_kbdfd == -1) { |
| 185 | perror(PFX "open /dev/tty"); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) { |
| 190 | perror(PFX "(not hiding FB): KDGETMODE"); |
| 191 | goto fail; |
| 192 | } |
| 193 | |
| 194 | if (tcgetattr(g_kbdfd, &kbd_termios) == -1) { |
| 195 | perror(PFX "tcgetattr"); |
| 196 | goto fail; |
| 197 | } |
| 198 | |
| 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; |
| 204 | |
| 205 | if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) { |
| 206 | perror(PFX "tcsetattr"); |
| 207 | goto fail; |
| 208 | } |
| 209 | |
| 210 | if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) { |
| 211 | perror(PFX "KDSETMODE KD_GRAPHICS"); |
| 212 | tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved); |
| 213 | goto fail; |
| 214 | } |
| 215 | |
| 216 | return; |
| 217 | |
| 218 | fail: |
| 219 | close(g_kbdfd); |
| 220 | g_kbdfd = -1; |
| 221 | } |
| 222 | |
| 223 | static void hidecon_end(void) |
| 224 | { |
| 225 | if (g_kbdfd < 0) |
| 226 | return; |
| 227 | |
| 228 | if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1) |
| 229 | perror(PFX "KDSETMODE KD_TEXT"); |
| 230 | |
| 231 | if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1) |
| 232 | perror(PFX "tcsetattr"); |
| 233 | |
| 234 | close(g_kbdfd); |
| 235 | g_kbdfd = -1; |
| 236 | } |
| 237 | |
| 238 | int oshide_init(void) |
| 239 | { |
| 240 | pthread_t tid; |
| 241 | int ret; |
| 242 | |
| 243 | ret = pthread_create(&tid, NULL, x11h_handler, NULL); |
| 244 | if (ret != 0) { |
| 245 | fprintf(stderr, PFX "failed to create thread: %d\n", ret); |
| 246 | return ret; |
| 247 | } |
| 248 | pthread_detach(tid); |
| 249 | |
| 250 | hidecon_start(); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | void oshide_finish(void) |
| 256 | { |
| 257 | /* XXX: the X thread.. */ |
| 258 | |
| 259 | hidecon_end(); |
| 260 | } |
| 261 | |
| 262 | #if 0 |
| 263 | int main() |
| 264 | { |
| 265 | x11h_init(); |
| 266 | sleep(5); |
| 267 | } |
| 268 | #endif |