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>
49 #define PFX "op_runfbapp: "
51 static struct termios g_kbd_termios_saved;
52 static int g_kbdfd = -1;
54 static pid_t g_child_pid;
55 static pthread_cond_t g_start_cond = PTHREAD_COND_INITIALIZER;
56 static pthread_mutex_t g_start_mutex = PTHREAD_MUTEX_INITIALIZER;
58 static void signal_main_thread(void)
60 pthread_mutex_lock(&g_start_mutex);
61 pthread_cond_signal(&g_start_cond);
62 pthread_mutex_unlock(&g_start_mutex);
65 static Cursor transparent_cursor(Display *display, Window win)
72 memset(&dummy, 0, sizeof(dummy));
73 pix = XCreateBitmapFromData(display, win, &d, 1, 1);
74 cursor = XCreatePixmapCursor(display, pix, pix,
75 &dummy, &dummy, 0, 0);
76 XFreePixmap(display, pix);
80 static void x11h_wait_vmstate(Display *display)
82 Atom wm_state = XInternAtom(display, "WM_STATE", False);
86 for (i = 0; i < 20; i++) {
87 while (XPending(display)) {
88 XNextEvent(display, &evt);
89 if (evt.type == PropertyNotify && evt.xproperty.atom == wm_state)
95 fprintf(stderr, "timeout waiting for wm_state change\n");
98 static struct omapfb_plane_info omap_pi;
100 static int hide_omap_layer(void)
106 fd = open("/dev/fb1", O_RDWR);
108 perror("open(/dev/fb1)");
112 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &omap_pi);
114 perror("QUERY_PLANE");
118 if (omap_pi.enabled) {
120 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &omap_pi);
122 perror("SETUP_PLANE");
136 static void show_omap_layer(void)
141 fd = open("/dev/fb1", O_RDWR);
143 perror("open(/dev/fb1)");
148 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &omap_pi);
150 perror("SETUP_PLANE");
155 static int x11h_minimize(Display *display, Window window)
157 XSetWindowAttributes attributes;
158 int screen = DefaultScreen(display);
159 int display_width, display_height;
163 XWithdrawWindow(display, window, screen);
165 attributes.override_redirect = False;
166 XChangeWindowAttributes(display, window,
167 CWOverrideRedirect, &attributes);
169 wm_hints.flags = StateHint;
170 wm_hints.initial_state = IconicState;
171 XSetWMHints(display, window, &wm_hints);
173 XMapWindow(display, window);
175 while (XNextEvent(display, &evt) == 0)
177 // printf("m event %d\n", evt.type);
188 XWithdrawWindow(display, window, screen);
190 // must wait for some magic vmstate property change before setting override_redirect
191 x11h_wait_vmstate(display);
193 attributes.override_redirect = True;
194 XChangeWindowAttributes(display, window,
195 CWOverrideRedirect, &attributes);
197 // fixup window after resize on override_redirect loss
198 display_width = DisplayWidth(display, screen);
199 display_height = DisplayHeight(display, screen);
200 XMoveResizeWindow(display, window, 0, 0, display_width, display_height);
202 XMapWindow(display, window);
203 XGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
204 XkbSetDetectableAutoRepeat(display, 1, NULL);
206 // we don't know when event dispatch will be called, so sync now
207 XSync(display, False);
212 static void *x11_handler(void *arg)
214 unsigned int display_width, display_height;
215 XSetWindowAttributes attributes;
216 const char *window_title = arg;
223 display = XOpenDisplay(NULL);
226 fprintf(stderr, PFX "(not hiding X): Can't open display: %s\n",
228 signal_main_thread();
233 screen = DefaultScreen(display);
235 display_width = DisplayWidth(display, screen);
236 display_height = DisplayHeight(display, screen);
238 win = XCreateSimpleWindow(display,
239 RootWindow(display, screen),
240 0, 0, display_width, display_height, 0,
241 BlackPixel(display, screen),
242 BlackPixel(display, screen));
244 attributes.override_redirect = True;
245 attributes.cursor = transparent_cursor(display, win);
246 XChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
248 XStoreName(display, win, window_title);
249 XSelectInput(display, win, ExposureMask | FocusChangeMask
250 | KeyPressMask | KeyReleaseMask | PropertyChangeMask);
251 XMapWindow(display, win);
252 XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
253 XkbSetDetectableAutoRepeat(display, 1, NULL);
254 XSync(display, False);
258 XNextEvent(display, &evt);
262 signal_main_thread();
263 while (XCheckTypedEvent(display, Expose, &evt))
268 if (XLookupKeysym(&evt.xkey, 0) == XF86XK_MenuKB
269 && g_child_pid != 0) {
270 kill(g_child_pid, SIGSTOP);
272 ret = hide_omap_layer();
273 x11h_minimize(display, win);
277 kill(g_child_pid, SIGCONT);
286 static void hidecon_start(void)
288 struct termios kbd_termios;
291 g_kbdfd = open("/dev/tty", O_RDWR);
293 perror(PFX "open /dev/tty");
297 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
298 perror(PFX "(not hiding FB): KDGETMODE");
302 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
303 perror(PFX "tcgetattr");
307 g_kbd_termios_saved = kbd_termios;
308 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
309 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
310 kbd_termios.c_cc[VMIN] = 0;
311 kbd_termios.c_cc[VTIME] = 0;
313 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
314 perror(PFX "tcsetattr");
318 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
319 perror(PFX "KDSETMODE KD_GRAPHICS");
320 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
331 static void hidecon_end(void)
336 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
337 perror(PFX "KDSETMODE KD_TEXT");
339 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
340 perror(PFX "tcsetattr");
346 static void do_exec(char * const argv[])
359 execvp(argv[0], argv);
360 perror(PFX "execvp");
366 ret = waitpid(pid, &status, 0);
368 perror(PFX "waitpid");
371 int main(int argc, char *argv[])
378 printf("usage:\n%s <app> [arg]..\n", argv[0]);
382 pthread_mutex_lock(&g_start_mutex);
384 name = strrchr(argv[1], '/');
390 ret = pthread_create(&tid, NULL, x11_handler, name);
392 fprintf(stderr, PFX "pthread_create: %d\n", ret);
397 pthread_cond_wait(&g_start_cond, &g_start_mutex);
398 pthread_mutex_unlock(&g_start_mutex);
408 /* XXX: maybe stop the X thread nicely? */