Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / zde / appcl.c
blob52cd8f6f797ceb5a9377a13ca62f30646425fc09
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <pthread.h>
25 #include <libx/base.h>
26 #include <libx/object.h>
27 #include <libx/image.h>
28 #include <libx/cursor.h>
29 #include <libx/text.h>
30 #include <ipc.h>
32 #include "appcl.h"
34 static char zstr[128];
36 static int zappcl_fd;
37 static appcl_t *zappcl;
38 static unsigned short zsizex;
39 static unsigned short zsizey;
40 static unsigned zstate;
41 static zbitmap_t bitmap_button;
42 static unsigned zitem_sel;
43 static unsigned zitem_curr;
45 extern unsigned short image_button[];
47 appcl_t *zgui_window ()
49 return zappcl;
52 void zgui_puts (unsigned short x, unsigned short y, char *str, unsigned color)
54 if (!zappcl)
55 return;
57 int i = 0;
58 int j = 0;
59 int k = 0;
61 for (i = 0; str[i]; i ++) {
62 if (str[i] == '\n') {
63 j += 6;
64 k = 0;
65 continue;
66 } else
67 if (str[i] == '\t') {
68 k += 4*5;
69 continue;
72 if (zappcl->sizex < k+15) {
73 j += 6;
74 k = 0;
75 continue;
78 if (j < zappcl->sizey)
79 xtext_putch (zappcl->x+4+x+k, zappcl->y+21+y+j, color, str[i]);
81 k += 6;
84 //xtext_puts (zappcl->x+4+x, zappcl->y+21+y, color, str);
88 int zgui_cursor (unsigned short *x, unsigned short *y)
90 if (!zappcl || !x || !y)
91 return 0;
93 if (zappcl->mousex-4 > zappcl->sizex)
94 *x = zappcl->sizex-1;
95 else if (zappcl->mousex-4 < 0)
96 *x = 0;
97 else
98 *x = zappcl->mousex-4;
100 if (zappcl->mousey-21 > zappcl->sizey)
101 *y = zappcl->sizey-1;
102 else if (zappcl->mousey-21 < 0)
103 *y = 0;
104 else
105 *y = zappcl->mousey-21;
107 return (zappcl->state & APPCL_STATE_LBUTTON) ? 1 : 0;
110 void zgui_input (unsigned short x, unsigned short y, char *str, unsigned len, unsigned color)
112 if (!zappcl)
113 return;
115 zitem_curr ++;
117 if (x+4 <= zappcl->mousex && x+8+((short) len*6) >= zappcl->mousex &&
118 y+19 <= zappcl->mousey && y+28 >= zappcl->mousey) {
119 if (zappcl->state & APPCL_STATE_LBUTTON) {
120 zappcl->state &= ~APPCL_STATE_LBUTTON;
121 zitem_sel = zitem_curr;
125 if (zitem_curr == zitem_sel)
126 if (zappcl->kbd) {
127 switch (zappcl->kbd) {
128 case '\b':
130 int i;
131 for (i = len-1; i >= 0; i --) {
132 if (str[i]) {
133 str[i] = 0;
134 break;
138 break;
139 default:
141 unsigned i;
142 for (i = 0; i < len; i ++) {
143 if (!str[i]) {
144 str[i] = zappcl->kbd;
145 break;
151 zappcl->kbd = 0;
154 xtext_puts (zappcl->x+4+x, zappcl->y+21+y, color, str);
157 void zgui_bitmap (zbitmap_t *bitmap)
159 unsigned i = 0;
160 unsigned j = 0;
161 unsigned k = 0;
162 unsigned pix = 0;
164 if (!bitmap)
165 return;
167 if (!bitmap->data)
168 return;
170 for (i = bitmap->posx+(bitmap->posy*bitmap->resx); i < bitmap->resx*bitmap->resy; i ++) {
171 if (k >= bitmap->resx) {
172 j ++;
173 k = 0;
176 if (k < bitmap->sizex && j < bitmap->sizey)
177 xpixel ((unsigned) zappcl->x+4+bitmap->x+k, (unsigned) zappcl->y+21+bitmap->y+j, (unsigned short) bitmap->data[i]);
179 k ++;
183 static void draw_bitmapspec (zbitmap_t *bitmap)
185 unsigned i = 0;
186 unsigned j = 0;
187 unsigned k = 0;
188 unsigned pix = 0;
190 if (!bitmap)
191 return;
193 if (!bitmap->data)
194 return;
196 unsigned short w = bitmap->data[299+34];
198 for (i = bitmap->posx+(bitmap->posy*13); i < 13*25; i ++) {
199 if (k >= 14) {
200 j ++;
201 k = 0;
204 pix = 355-i;
206 //if (pix < 355)
207 // continue;
209 if (bitmap->data[pix] != w)
210 if (k < bitmap->sizex && j < bitmap->sizey)
211 xpixel ((unsigned) bitmap->x+k, (unsigned) bitmap->y+j, image_button[pix]);
213 k ++;
217 int zgui_button (unsigned short x, unsigned short y, char *str)
219 if (!zappcl)
220 return -1;
222 zitem_curr ++;
224 if (zitem_curr != zitem_sel)
225 return -1;
227 unsigned len = strlen (str);
229 /*xrectfill (zappcl->x+4+x, zappcl->y+21+y, zappcl->x+8+x+(len*6), zappcl->y+31+y, ~0);*/
231 bitmap_button.x = zappcl->x+4+x;
232 bitmap_button.y = zappcl->y+21+y;
233 bitmap_button.posx = 0;
234 bitmap_button.posy = 0;
235 bitmap_button.sizex = 4;
236 bitmap_button.sizey = 23;
238 draw_bitmapspec (&bitmap_button);
240 unsigned l;
241 for (l = 0; l < len; l ++) {
242 bitmap_button.x = zappcl->x+8+x+(l*6);
243 bitmap_button.y = zappcl->y+21+y;
244 bitmap_button.posx = 4;
245 bitmap_button.posy = 0;
246 bitmap_button.sizex = 6;
247 bitmap_button.sizey = 23;
249 draw_bitmapspec (&bitmap_button);
252 bitmap_button.x = zappcl->x+8+x+(len*6);
253 bitmap_button.y = zappcl->y+21+y;
254 bitmap_button.posx = 9;
255 bitmap_button.posy = 0;
256 bitmap_button.sizex = 4;
257 bitmap_button.sizey = 23;
259 draw_bitmapspec (&bitmap_button);
261 int i = 0;
263 if (x+4 <= zappcl->mousex && x+12+((short) len*6) >= zappcl->mousex &&
264 y+22 <= zappcl->mousey && y+43 >= zappcl->mousey) {
265 if (zappcl->state & APPCL_STATE_LBUTTON) {
266 zappcl->state &= ~APPCL_STATE_LBUTTON;
267 i ++;
271 if (!i)
272 xtext_puts (zappcl->x+8+x, zappcl->y+28+y, 0, str);
273 else
274 return 0;
276 return -1;
279 int zgui_resize (unsigned short x, unsigned short y)
281 if (!zappcl)
282 return -1;
284 zsizex = x;
285 zsizey = y;
287 zstate |= APPCL_STATE_RESIZE;
289 return 0;
292 unsigned zgui_event ()
294 zitem_curr = 0;
296 if (zappcl->state & APPCL_STATE_REDRAW) {
297 zappcl->state &= ~APPCL_STATE_REDRAW;
298 zappcl->state |= APPCL_STATE_RECVOK;
300 ipc_send (zappcl_fd, zstr, sizeof (appcl_t));
303 int ret = ipc_recv (zappcl_fd, zstr, 128);
305 if (ret > 0) {
306 if (zstate & APPCL_STATE_RESIZE) {
307 zappcl->sizex = zsizex;
308 zappcl->sizey = zsizey;
309 zappcl->state |= APPCL_STATE_RESIZE;
312 return zappcl->state;
315 schedule ();
317 return 0;
320 int zgui_init ()
322 zappcl = 0;
323 zstate = 0;
324 zitem_sel = 1;
325 zitem_curr = 0;
326 bitmap_button.data = image_button;
328 int ret = 0;
329 zappcl_fd = ipc_open ("/zdeserver", 0);
331 if (zappcl_fd > 0) {
332 int ret = ipc_recv (zappcl_fd, zstr, 127);
334 if (ret > 0) {
335 zstr[ret] = '\0';
337 if (strcmp (zstr, "ZDE"))
338 ret = -1;
340 } else
341 ret = -1;
343 xinit ();
345 zappcl = (appcl_t *) zstr;
347 memset (zstr, 0, sizeof (appcl_t));
349 return ret;
352 int zgui_exit ()
354 ipc_close (zappcl_fd);
356 return 0;