App inst fixed on invalid image file; App zinstall for ZDE works - we've got GUI...
[ZeXOS.git] / apps / zde / handler.c
blob2a70bdb4e65709dd2164d30b7a9949b79b6b0934
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <pthread.h>
27 #include <libx/base.h>
28 #include <libx/object.h>
29 #include <libx/image.h>
30 #include <libx/cursor.h>
31 #include <libx/text.h>
33 #include "handler.h"
34 #include "window.h"
35 #include "cursor.h"
36 #include "icon.h"
37 #include "dialog.h"
39 extern zde_win_t *zde_win_last;
40 extern zde_cursor_t *zde_cursor;
42 typedef struct {
43 char *name;
44 unsigned attrib;
45 unsigned char next;
46 unsigned char unused;
47 } __attribute__ ((__packed__)) dirent_t;
49 dirent_t *dirent;
52 static void getdir ()
54 dirent = 0;
56 asm volatile (
57 "movl $25, %%eax;"
58 "int $0x80;"
59 "movl %%eax, %0;"
60 : "=g" (dirent) :: "%eax");
63 void *handler_icon_reader (void *r)
65 if (!r)
66 return (0);
68 char desc[64];
69 sprintf (desc, "Reader - %s", r);
71 /* create test window */
72 zde_win_t *window = create_window (desc, 1);
74 if (!window)
75 return (0);
77 int fd = open (r, O_RDONLY);
79 if (!fd)
80 return 0;
82 int l = lseek (fd, 0, SEEK_END);
84 lseek (fd, 0, SEEK_SET);
86 char *buf = (char *) malloc (l+1);
88 if (!buf)
89 return 0;
91 read (fd, buf, l);
93 zde_dobj_text_t dobj_text;
95 dobj_text.data = (char *) buf;
96 dobj_text.len = l;
97 dobj_text.sizex = 0;
98 dobj_text.sizey = 0;
100 create_dialog_object (ZDE_DOBJ_TYPE_TEXT, &dobj_text, 0, 0, window);
102 while (!(window->state & WINDOW_STATE_CLOSE)) {
103 schedule ();
105 if (window != zde_win_last)
106 continue;
108 if (zde_cursor->state == XCURSOR_STATE_LBUTTON) {
113 free (buf);
114 free (window);
116 return (0);
119 void *handler_icon_terminal (void *r)
121 if (!r)
122 return (0);
124 char desc[64];
125 sprintf (desc, "Terminal");
127 /* create test window */
128 zde_win_t *window = create_window (desc, 1);
130 if (!window)
131 return (0);
133 window->sizex = 6 * 80;
134 window->sizey = 9 * 65;
136 int fd = open ("/dev/tty1", O_RDWR);
138 if (!fd)
139 return 0;
141 if (fcntl (fd, F_SETFL, 0x2000) == -1) {
142 printf ("Cant set fd to terminal mode\n");
143 return 0;
146 int l = lseek (fd, 0, SEEK_END);
148 lseek (fd, 0, SEEK_SET);
150 char *buf = (char *) malloc (l+1);
152 if (!buf)
153 return 0;
155 char *buf2 = (char *) malloc (l+1);
157 if (!buf2) {
158 free (buf);
159 return 0;
162 zde_dobj_text_t dobj_text;
164 dobj_text.sizex = 0;
165 dobj_text.sizey = 0;
166 dobj_text.data = (char *) "tty";
167 dobj_text.len = 3;
169 zde_dobj_t *dobj = create_dialog_object (ZDE_DOBJ_TYPE_TEXT, &dobj_text, 0, 0, window);
171 if (!dobj)
172 return 0;
174 zde_dobj_text_t *text = (zde_dobj_text_t *) dobj->arg;
176 if (!text)
177 return 0;
179 while (!(window->state & WINDOW_STATE_CLOSE)) {
180 schedule ();
182 if (window != zde_win_last)
183 continue;
185 read (fd, buf, l);
187 lseek (fd, 0, SEEK_SET);
189 unsigned i = 0;
190 unsigned j = 0;
191 unsigned k = 0;
192 unsigned m = 80 * 65;
193 unsigned pos = 0;
195 while (i < m) {
196 if (buf[i*2])
197 buf2[pos ++] = buf[i*2];
198 else
199 buf2[pos ++] = ' ';
201 j ++;
203 if (j >= 80) {
204 j = 0;
205 k ++;
206 buf2[pos ++] = '\n';
209 i ++;
212 text->data = (char *) buf2;
213 text->len = pos;
215 char c[2];
216 c[0] = kbd_getkey ();
218 if (c[0])
219 write (fd, c, 1);
221 if (zde_cursor->state == XCURSOR_STATE_LBUTTON) {
226 free (buf2);
227 free (buf);
228 free (window);
230 return (0);
233 void *handler_icon_exec (void *r)
235 if (!r)
236 return (0);
238 char desc[64];
239 sprintf (desc, "ZDE app - %s", r);
241 /* create test window */
242 zde_win_t *window = create_window (desc, 1);
244 if (!window)
245 return (0);
247 zde_dobj_appcl_t dobj_appcl;
249 dobj_appcl.x = window->x;
250 dobj_appcl.y = window->y;
251 dobj_appcl.sizex = window->sizex;
252 dobj_appcl.sizey = window->sizey;
253 dobj_appcl.kbd = 0;
254 dobj_appcl.fd = -1;
256 create_dialog_object (ZDE_DOBJ_TYPE_APPCL, &dobj_appcl, 0, 0, window);
258 unsigned l = strlen (r);
259 unsigned i;
261 char *str = (char *) strdup (r);
263 for (i = l; i > 1; i --) {
264 if (str[i] == '/') {
265 str[i] = '\0';
266 break;
270 chdir ((char *) "/");
272 chdir ((char *) str);
274 sprintf (desc, "exec %s", str+i+1);
276 free (str);
278 system (desc);
280 schedule ();
282 while (!(window->state & WINDOW_STATE_CLOSE)) {
283 schedule ();
285 if (window != zde_win_last)
286 continue;
288 if (zde_cursor->state == XCURSOR_STATE_LBUTTON) {
293 free (window);
295 return (0);
298 void *handler_icon_filesystem (void *r)
300 if (!r)
301 return (0);
303 char desc[64];
304 sprintf (desc, "Filesystem - %s", r);
306 /* create test window */
307 zde_win_t *window = create_window (desc, 0);
309 if (!window)
310 return (0);
312 chdir ((char *) "/");
314 if (strlen (r) > 0)
315 chdir ((char *) r);
317 getdir ();
319 dirent_t *dir = dirent;
321 if (!dir)
322 return (0);
324 unsigned id = 0;
326 for (;;) {
327 if (dir[id].attrib & VFS_FILEATTR_DIR)
328 create_icon_window (dir[id].name, 0, handler_icon_filesystem, (void *) r, window);
329 if (dir[id].attrib & VFS_FILEATTR_FILE) {
330 if (dir[id].attrib & VFS_FILEATTR_BIN || strstr (dir[id].name, ".x"))
331 create_icon_window (dir[id].name, 1, handler_icon_exec, (void *) r, window);
332 else
333 create_icon_window (dir[id].name, 1, handler_icon_reader, (void *) r, window);
336 if (!dir[id].next)
337 break;
339 id ++;
342 free (dir);
344 while (!(window->state & WINDOW_STATE_CLOSE)) {
345 schedule ();
347 if (window != zde_win_last)
348 continue;
350 if (zde_cursor->state == XCURSOR_STATE_LBUTTON) {
355 free (window);
357 return (0);