update to new nub names
[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 <fcntl.h>
37 #include <unistd.h>
38 #include <sys/ioctl.h>
39 #include <termios.h>
40 #include <linux/kd.h>
41
42 #define PFX "op_runfbapp: "
43
44 static struct termios g_kbd_termios_saved;
45 static int g_kbdfd;
46
47 static Cursor transparent_cursor(Display *display, Window win)
48 {
49         Cursor cursor;
50         Pixmap pix;
51         XColor dummy;
52         char d = 0;
53
54         memset(&dummy, 0, sizeof(dummy));
55         pix = XCreateBitmapFromData(display, win, &d, 1, 1);
56         cursor = XCreatePixmapCursor(display, pix, pix,
57                         &dummy, &dummy, 0, 0);
58         XFreePixmap(display, pix);
59         return cursor;
60 }
61
62 static void *x11_handler(void *arg)
63 {
64         unsigned int display_width, display_height;
65         XSetWindowAttributes attributes;
66         Window win;
67         XEvent report;
68         Display *display;
69         int screen;
70
71         display = XOpenDisplay(NULL);
72         if (display == NULL)
73         {
74                 fprintf(stderr, PFX "(not hiding X): Can't open display: %s\n",
75                         XDisplayName(NULL));
76                 return NULL;
77         }
78
79         screen = DefaultScreen(display);
80
81         display_width = DisplayWidth(display, screen);
82         display_height = DisplayHeight(display, screen);
83
84         win = XCreateSimpleWindow(display,
85                         RootWindow(display, screen),
86                         0, 0, display_width, display_height, 0,
87                         BlackPixel(display, screen),
88                         BlackPixel(display, screen));
89
90         attributes.override_redirect = True;
91         attributes.cursor = transparent_cursor(display, win);
92         XChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
93
94         XSelectInput(display, win, ExposureMask);
95         XMapWindow(display, win);
96         XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
97
98         while (1)
99         {
100                 XNextEvent(display, &report);
101
102                 if (report.type == Expose)
103                         while (XCheckTypedEvent(display, Expose, &report))
104                                 ;
105         }
106
107         return NULL;
108 }
109
110 static void hidecon_start(void)
111 {
112         struct termios kbd_termios;
113         int mode;
114
115         g_kbdfd = open("/dev/tty", O_RDWR);
116         if (g_kbdfd == -1) {
117                 perror(PFX "open /dev/tty");
118                 return;
119         }
120
121         if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
122                 perror(PFX "(not hiding FB): KDGETMODE");
123                 goto fail;
124         }
125
126         if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
127                 perror(PFX "tcgetattr");
128                 goto fail;
129         }
130
131         g_kbd_termios_saved = kbd_termios;
132         kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
133         kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
134         kbd_termios.c_cc[VMIN] = 0;
135         kbd_termios.c_cc[VTIME] = 0;
136
137         if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
138                 perror(PFX "tcsetattr");
139                 goto fail;
140         }
141
142         if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
143                 perror(PFX "KDSETMODE KD_GRAPHICS");
144                 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
145                 goto fail;
146         }
147
148         return;
149
150 fail:
151         close(g_kbdfd);
152         g_kbdfd = -1;
153 }
154
155 static void hidecon_end(void)
156 {
157         if (g_kbdfd < 0)
158                 return;
159
160         if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
161                 perror(PFX "KDSETMODE KD_TEXT");
162
163         if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
164                 perror(PFX "tcsetattr");
165
166         close(g_kbdfd);
167         g_kbdfd = -1;
168 }
169
170 int main(int argc, char *argv[])
171 {
172         pthread_t tid;
173         char *cmd, *p;
174         int len = 0;
175         int ret;
176         int i;
177
178         for (i = 1; i < argc; i++)
179                 len += strlen(argv[i]) + 1;
180
181         cmd = malloc(len);
182         if (cmd == NULL)
183         {
184                 fprintf(stderr, PFX "OOM\n");
185                 return 1;
186         }
187
188         /* rebuild command line for program we launch */
189         for (p = cmd, i = 1; i < argc; i++) {
190                 strcpy(p, argv[i]);
191                 p += strlen(p);
192
193                 if (i < argc - 1)
194                         *p++ = ' ';
195         }
196
197         ret = pthread_create(&tid, NULL, x11_handler, NULL);
198         if (ret != 0) {
199                 fprintf(stderr, PFX "pthread_create: %d\n", ret);
200                 return 1;
201         }
202         pthread_detach(tid);
203
204         hidecon_start();
205
206         ret = system(cmd);
207         if (ret == -1)
208                 perror(PFX "system");
209         free(cmd);
210
211         hidecon_end();
212
213         /* XXX: maybe stop the X thread nicely? */
214
215         return 0;
216 }
217