op_runfbapp: fix thread and fbapp race
[pandora-misc.git] / op_runfbapp.c
1 /*
2  * Copyright (c) 2010, GraÅžvydas Ignotas
3  * All rights reserved.
4  *
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.
15  *
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.
26  */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <pthread.h>
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <sys/ioctl.h>
40 #include <termios.h>
41 #include <linux/kd.h>
42
43 #define PFX "op_runfbapp: "
44
45 static struct termios g_kbd_termios_saved;
46 static int g_kbdfd;
47 static pthread_cond_t g_start_cond = PTHREAD_COND_INITIALIZER;
48 static pthread_mutex_t g_start_mutex = PTHREAD_MUTEX_INITIALIZER;
49
50 static void signal_main_thread(void)
51 {
52         pthread_mutex_lock(&g_start_mutex);
53         pthread_cond_signal(&g_start_cond);
54         pthread_mutex_unlock(&g_start_mutex);
55 }
56
57 static Cursor transparent_cursor(Display *display, Window win)
58 {
59         Cursor cursor;
60         Pixmap pix;
61         XColor dummy;
62         char d = 0;
63
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);
69         return cursor;
70 }
71
72 static void *x11_handler(void *arg)
73 {
74         unsigned int display_width, display_height;
75         XSetWindowAttributes attributes;
76         Window win;
77         XEvent report;
78         Display *display;
79         int screen;
80
81         display = XOpenDisplay(NULL);
82         if (display == NULL)
83         {
84                 fprintf(stderr, PFX "(not hiding X): Can't open display: %s\n",
85                         XDisplayName(NULL));
86                 signal_main_thread();
87                 return NULL;
88         }
89
90         screen = DefaultScreen(display);
91
92         display_width = DisplayWidth(display, screen);
93         display_height = DisplayHeight(display, screen);
94
95         win = XCreateSimpleWindow(display,
96                         RootWindow(display, screen),
97                         0, 0, display_width, display_height, 0,
98                         BlackPixel(display, screen),
99                         BlackPixel(display, screen));
100
101         attributes.override_redirect = True;
102         attributes.cursor = transparent_cursor(display, win);
103         XChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
104
105         XSelectInput(display, win, ExposureMask);
106         XMapWindow(display, win);
107         XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
108         XSync(display, False);
109
110         while (1)
111         {
112                 XNextEvent(display, &report);
113
114                 if (report.type == Expose) {
115                         signal_main_thread();
116                         while (XCheckTypedEvent(display, Expose, &report))
117                                 ;
118                 }
119         }
120
121         return NULL;
122 }
123
124 static void hidecon_start(void)
125 {
126         struct termios kbd_termios;
127         int mode;
128
129         g_kbdfd = open("/dev/tty", O_RDWR);
130         if (g_kbdfd == -1) {
131                 perror(PFX "open /dev/tty");
132                 return;
133         }
134
135         if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
136                 perror(PFX "(not hiding FB): KDGETMODE");
137                 goto fail;
138         }
139
140         if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
141                 perror(PFX "tcgetattr");
142                 goto fail;
143         }
144
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;
150
151         if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
152                 perror(PFX "tcsetattr");
153                 goto fail;
154         }
155
156         if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
157                 perror(PFX "KDSETMODE KD_GRAPHICS");
158                 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
159                 goto fail;
160         }
161
162         return;
163
164 fail:
165         close(g_kbdfd);
166         g_kbdfd = -1;
167 }
168
169 static void hidecon_end(void)
170 {
171         if (g_kbdfd < 0)
172                 return;
173
174         if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
175                 perror(PFX "KDSETMODE KD_TEXT");
176
177         if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
178                 perror(PFX "tcsetattr");
179
180         close(g_kbdfd);
181         g_kbdfd = -1;
182 }
183
184 static void do_exec(char * const argv[])
185 {
186         int ret, status;
187         pid_t pid;
188
189         pid = fork();
190         if (pid == -1) {
191                 perror(PFX "fork");
192                 return;
193         }
194
195         if (pid == 0) {
196                 /* child */
197                 execvp(argv[0], argv);
198                 perror(PFX "execvp");
199                 exit(1);
200         }
201
202         ret = waitpid(pid, &status, 0);
203         if (ret < 0)
204                 perror(PFX "waitpid");
205 }
206
207 int main(int argc, char *argv[])
208 {
209         pthread_t tid;
210         int ret;
211
212         pthread_mutex_lock(&g_start_mutex);
213
214         ret = pthread_create(&tid, NULL, x11_handler, NULL);
215         if (ret != 0) {
216                 fprintf(stderr, PFX "pthread_create: %d\n", ret);
217                 return 1;
218         }
219         pthread_detach(tid);
220
221         pthread_cond_wait(&g_start_cond, &g_start_mutex);
222         pthread_mutex_unlock(&g_start_mutex);
223
224         hidecon_start();
225
226         do_exec(argv + 1);
227
228         hidecon_end();
229
230         /* XXX: maybe stop the X thread nicely? */
231
232         return 0;
233 }
234