2 * Copyright (c) 2010,2012 GraÅžvydas Ignotas
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the organization nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <X11/Xutil.h>
33 #include <X11/XKBlib.h>
34 #include <X11/XF86keysym.h>
36 #include <sys/types.h>
41 #include <sys/ioctl.h>
46 #define PFX "op_runfbapp: "
48 static struct termios g_kbd_termios_saved;
49 static int g_kbdfd = -1;
51 static pid_t g_child_pid;
52 static pthread_cond_t g_start_cond = PTHREAD_COND_INITIALIZER;
53 static pthread_mutex_t g_start_mutex = PTHREAD_MUTEX_INITIALIZER;
55 static void signal_main_thread(void)
57 pthread_mutex_lock(&g_start_mutex);
58 pthread_cond_signal(&g_start_cond);
59 pthread_mutex_unlock(&g_start_mutex);
62 static Cursor transparent_cursor(Display *display, Window win)
69 memset(&dummy, 0, sizeof(dummy));
70 pix = XCreateBitmapFromData(display, win, &d, 1, 1);
71 cursor = XCreatePixmapCursor(display, pix, pix,
72 &dummy, &dummy, 0, 0);
73 XFreePixmap(display, pix);
77 static void x11h_wait_vmstate(Display *display)
79 Atom wm_state = XInternAtom(display, "WM_STATE", False);
83 for (i = 0; i < 20; i++) {
84 while (XPending(display)) {
85 XNextEvent(display, &evt);
86 if (evt.type == PropertyNotify && evt.xproperty.atom == wm_state)
92 fprintf(stderr, "timeout waiting for wm_state change\n");
95 static int x11h_minimize(Display *display, Window window)
97 XSetWindowAttributes attributes;
98 int screen = DefaultScreen(display);
99 int display_width, display_height;
103 XWithdrawWindow(display, window, screen);
105 attributes.override_redirect = False;
106 XChangeWindowAttributes(display, window,
107 CWOverrideRedirect, &attributes);
109 wm_hints.flags = StateHint;
110 wm_hints.initial_state = IconicState;
111 XSetWMHints(display, window, &wm_hints);
113 XMapWindow(display, window);
115 while (XNextEvent(display, &evt) == 0)
117 // printf("m event %d\n", evt.type);
128 XWithdrawWindow(display, window, screen);
130 // must wait for some magic vmstate property change before setting override_redirect
131 x11h_wait_vmstate(display);
133 attributes.override_redirect = True;
134 XChangeWindowAttributes(display, window,
135 CWOverrideRedirect, &attributes);
137 // fixup window after resize on override_redirect loss
138 display_width = DisplayWidth(display, screen);
139 display_height = DisplayHeight(display, screen);
140 XMoveResizeWindow(display, window, 0, 0, display_width, display_height);
142 XMapWindow(display, window);
143 XGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
144 XkbSetDetectableAutoRepeat(display, 1, NULL);
146 // we don't know when event dispatch will be called, so sync now
147 XSync(display, False);
152 static void *x11_handler(void *arg)
154 unsigned int display_width, display_height;
155 XSetWindowAttributes attributes;
156 const char *window_title = arg;
162 display = XOpenDisplay(NULL);
165 fprintf(stderr, PFX "(not hiding X): Can't open display: %s\n",
167 signal_main_thread();
172 screen = DefaultScreen(display);
174 display_width = DisplayWidth(display, screen);
175 display_height = DisplayHeight(display, screen);
177 win = XCreateSimpleWindow(display,
178 RootWindow(display, screen),
179 0, 0, display_width, display_height, 0,
180 BlackPixel(display, screen),
181 BlackPixel(display, screen));
183 attributes.override_redirect = True;
184 attributes.cursor = transparent_cursor(display, win);
185 XChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
187 XStoreName(display, win, window_title);
188 XSelectInput(display, win, ExposureMask | FocusChangeMask
189 | KeyPressMask | KeyReleaseMask | PropertyChangeMask);
190 XMapWindow(display, win);
191 XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
192 XkbSetDetectableAutoRepeat(display, 1, NULL);
193 XSync(display, False);
197 XNextEvent(display, &evt);
201 signal_main_thread();
202 while (XCheckTypedEvent(display, Expose, &evt))
207 if (XLookupKeysym(&evt.xkey, 0) == XF86XK_MenuKB
208 && g_child_pid != 0) {
209 kill(g_child_pid, SIGSTOP);
210 x11h_minimize(display, win);
211 kill(g_child_pid, SIGCONT);
220 static void hidecon_start(void)
222 struct termios kbd_termios;
225 g_kbdfd = open("/dev/tty", O_RDWR);
227 perror(PFX "open /dev/tty");
231 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
232 perror(PFX "(not hiding FB): KDGETMODE");
236 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
237 perror(PFX "tcgetattr");
241 g_kbd_termios_saved = kbd_termios;
242 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
243 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
244 kbd_termios.c_cc[VMIN] = 0;
245 kbd_termios.c_cc[VTIME] = 0;
247 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
248 perror(PFX "tcsetattr");
252 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
253 perror(PFX "KDSETMODE KD_GRAPHICS");
254 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
265 static void hidecon_end(void)
270 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
271 perror(PFX "KDSETMODE KD_TEXT");
273 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
274 perror(PFX "tcsetattr");
280 static void do_exec(char * const argv[])
293 execvp(argv[0], argv);
294 perror(PFX "execvp");
300 ret = waitpid(pid, &status, 0);
302 perror(PFX "waitpid");
305 int main(int argc, char *argv[])
312 printf("usage:\n%s <app> [arg]..\n", argv[0]);
316 pthread_mutex_lock(&g_start_mutex);
318 name = strrchr(argv[1], '/');
324 ret = pthread_create(&tid, NULL, x11_handler, name);
326 fprintf(stderr, PFX "pthread_create: %d\n", ret);
331 pthread_cond_wait(&g_start_cond, &g_start_mutex);
332 pthread_mutex_unlock(&g_start_mutex);
342 /* XXX: maybe stop the X thread nicely? */