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;
48 static Cursor transparent_cursor(Display *display, Window win)
55 memset(&dummy, 0, sizeof(dummy));
56 pix = XCreateBitmapFromData(display, win, &d, 1, 1);
57 cursor = XCreatePixmapCursor(display, pix, pix,
58 &dummy, &dummy, 0, 0);
59 XFreePixmap(display, pix);
63 static void *x11_handler(void *arg)
65 unsigned int display_width, display_height;
66 XSetWindowAttributes attributes;
72 display = XOpenDisplay(NULL);
75 fprintf(stderr, PFX "(not hiding X): Can't open display: %s\n",
80 screen = DefaultScreen(display);
82 display_width = DisplayWidth(display, screen);
83 display_height = DisplayHeight(display, screen);
85 win = XCreateSimpleWindow(display,
86 RootWindow(display, screen),
87 0, 0, display_width, display_height, 0,
88 BlackPixel(display, screen),
89 BlackPixel(display, screen));
91 attributes.override_redirect = True;
92 attributes.cursor = transparent_cursor(display, win);
93 XChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
95 XSelectInput(display, win, ExposureMask);
96 XMapWindow(display, win);
97 XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
101 XNextEvent(display, &report);
103 if (report.type == Expose)
104 while (XCheckTypedEvent(display, Expose, &report))
111 static void hidecon_start(void)
113 struct termios kbd_termios;
116 g_kbdfd = open("/dev/tty", O_RDWR);
118 perror(PFX "open /dev/tty");
122 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
123 perror(PFX "(not hiding FB): KDGETMODE");
127 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
128 perror(PFX "tcgetattr");
132 g_kbd_termios_saved = kbd_termios;
133 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
134 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
135 kbd_termios.c_cc[VMIN] = 0;
136 kbd_termios.c_cc[VTIME] = 0;
138 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
139 perror(PFX "tcsetattr");
143 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
144 perror(PFX "KDSETMODE KD_GRAPHICS");
145 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
156 static void hidecon_end(void)
161 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
162 perror(PFX "KDSETMODE KD_TEXT");
164 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
165 perror(PFX "tcsetattr");
171 static void do_exec(char * const argv[])
184 execvp(argv[0], argv);
185 perror(PFX "execvp");
189 ret = waitpid(pid, &status, 0);
191 perror(PFX "waitpid");
194 int main(int argc, char *argv[])
199 ret = pthread_create(&tid, NULL, x11_handler, NULL);
201 fprintf(stderr, PFX "pthread_create: %d\n", ret);
212 /* XXX: maybe stop the X thread nicely? */