flasher: use nanddump to avoid dep on mtdblock
[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
48 static Cursor transparent_cursor(Display *display, Window win)
49 {
50         Cursor cursor;
51         Pixmap pix;
52         XColor dummy;
53         char d = 0;
54
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);
60         return cursor;
61 }
62
63 static void *x11_handler(void *arg)
64 {
65         unsigned int display_width, display_height;
66         XSetWindowAttributes attributes;
67         Window win;
68         XEvent report;
69         Display *display;
70         int screen;
71
72         display = XOpenDisplay(NULL);
73         if (display == NULL)
74         {
75                 fprintf(stderr, PFX "(not hiding X): Can't open display: %s\n",
76                         XDisplayName(NULL));
77                 return NULL;
78         }
79
80         screen = DefaultScreen(display);
81
82         display_width = DisplayWidth(display, screen);
83         display_height = DisplayHeight(display, screen);
84
85         win = XCreateSimpleWindow(display,
86                         RootWindow(display, screen),
87                         0, 0, display_width, display_height, 0,
88                         BlackPixel(display, screen),
89                         BlackPixel(display, screen));
90
91         attributes.override_redirect = True;
92         attributes.cursor = transparent_cursor(display, win);
93         XChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
94
95         XSelectInput(display, win, ExposureMask);
96         XMapWindow(display, win);
97         XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
98
99         while (1)
100         {
101                 XNextEvent(display, &report);
102
103                 if (report.type == Expose)
104                         while (XCheckTypedEvent(display, Expose, &report))
105                                 ;
106         }
107
108         return NULL;
109 }
110
111 static void hidecon_start(void)
112 {
113         struct termios kbd_termios;
114         int mode;
115
116         g_kbdfd = open("/dev/tty", O_RDWR);
117         if (g_kbdfd == -1) {
118                 perror(PFX "open /dev/tty");
119                 return;
120         }
121
122         if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
123                 perror(PFX "(not hiding FB): KDGETMODE");
124                 goto fail;
125         }
126
127         if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
128                 perror(PFX "tcgetattr");
129                 goto fail;
130         }
131
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;
137
138         if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
139                 perror(PFX "tcsetattr");
140                 goto fail;
141         }
142
143         if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
144                 perror(PFX "KDSETMODE KD_GRAPHICS");
145                 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
146                 goto fail;
147         }
148
149         return;
150
151 fail:
152         close(g_kbdfd);
153         g_kbdfd = -1;
154 }
155
156 static void hidecon_end(void)
157 {
158         if (g_kbdfd < 0)
159                 return;
160
161         if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
162                 perror(PFX "KDSETMODE KD_TEXT");
163
164         if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
165                 perror(PFX "tcsetattr");
166
167         close(g_kbdfd);
168         g_kbdfd = -1;
169 }
170
171 static void do_exec(char * const argv[])
172 {
173         int ret, status;
174         pid_t pid;
175
176         pid = fork();
177         if (pid == -1) {
178                 perror(PFX "fork");
179                 return;
180         }
181
182         if (pid == 0) {
183                 /* child */
184                 execvp(argv[0], argv);
185                 perror(PFX "execvp");
186                 exit(1);
187         }
188
189         ret = waitpid(pid, &status, 0);
190         if (ret < 0)
191                 perror(PFX "waitpid");
192 }
193
194 int main(int argc, char *argv[])
195 {
196         pthread_t tid;
197         int ret;
198
199         ret = pthread_create(&tid, NULL, x11_handler, NULL);
200         if (ret != 0) {
201                 fprintf(stderr, PFX "pthread_create: %d\n", ret);
202                 return 1;
203         }
204         pthread_detach(tid);
205
206         hidecon_start();
207
208         do_exec(argv + 1);
209
210         hidecon_end();
211
212         /* XXX: maybe stop the X thread nicely? */
213
214         return 0;
215 }
216