2 * Copyright (c) 2010, 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>
34 #include <sys/types.h>
39 #include <sys/ioctl.h>
43 #define PFX "op_runfbapp: "
45 static struct termios g_kbd_termios_saved;
47 static pthread_cond_t g_start_cond = PTHREAD_COND_INITIALIZER;
48 static pthread_mutex_t g_start_mutex = PTHREAD_MUTEX_INITIALIZER;
50 static void signal_main_thread(void)
52 pthread_mutex_lock(&g_start_mutex);
53 pthread_cond_signal(&g_start_cond);
54 pthread_mutex_unlock(&g_start_mutex);
57 static Cursor transparent_cursor(Display *display, Window win)
64 memset(&dummy, 0, sizeof(dummy));
65 pix = XCreateBitmapFromData(display, win, &d, 1, 1);
66 cursor = XCreatePixmapCursor(display, pix, pix,
67 &dummy, &dummy, 0, 0);
68 XFreePixmap(display, pix);
72 static void *x11_handler(void *arg)
74 unsigned int display_width, display_height;
75 XSetWindowAttributes attributes;
81 display = XOpenDisplay(NULL);
84 fprintf(stderr, PFX "(not hiding X): Can't open display: %s\n",
90 screen = DefaultScreen(display);
92 display_width = DisplayWidth(display, screen);
93 display_height = DisplayHeight(display, screen);
95 win = XCreateSimpleWindow(display,
96 RootWindow(display, screen),
97 0, 0, display_width, display_height, 0,
98 BlackPixel(display, screen),
99 BlackPixel(display, screen));
101 attributes.override_redirect = True;
102 attributes.cursor = transparent_cursor(display, win);
103 XChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
105 XSelectInput(display, win, ExposureMask);
106 XMapWindow(display, win);
107 XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
108 XSync(display, False);
112 XNextEvent(display, &report);
114 if (report.type == Expose) {
115 signal_main_thread();
116 while (XCheckTypedEvent(display, Expose, &report))
124 static void hidecon_start(void)
126 struct termios kbd_termios;
129 g_kbdfd = open("/dev/tty", O_RDWR);
131 perror(PFX "open /dev/tty");
135 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
136 perror(PFX "(not hiding FB): KDGETMODE");
140 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
141 perror(PFX "tcgetattr");
145 g_kbd_termios_saved = kbd_termios;
146 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
147 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
148 kbd_termios.c_cc[VMIN] = 0;
149 kbd_termios.c_cc[VTIME] = 0;
151 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
152 perror(PFX "tcsetattr");
156 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
157 perror(PFX "KDSETMODE KD_GRAPHICS");
158 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
169 static void hidecon_end(void)
174 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
175 perror(PFX "KDSETMODE KD_TEXT");
177 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
178 perror(PFX "tcsetattr");
184 static void do_exec(char * const argv[])
197 execvp(argv[0], argv);
198 perror(PFX "execvp");
202 ret = waitpid(pid, &status, 0);
204 perror(PFX "waitpid");
207 int main(int argc, char *argv[])
212 pthread_mutex_lock(&g_start_mutex);
214 ret = pthread_create(&tid, NULL, x11_handler, NULL);
216 fprintf(stderr, PFX "pthread_create: %d\n", ret);
221 pthread_cond_wait(&g_start_cond, &g_start_mutex);
222 pthread_mutex_unlock(&g_start_mutex);
230 /* XXX: maybe stop the X thread nicely? */