Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / calc / main.c
blobdf61c104c94b336f6e49d3531663bb18ca411355
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 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 <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 #define ESC 1
27 #define ARROWLEFT 75
28 #define ARROWRIGHT 77
29 #define ARROWUP 72
30 #define ARROWDOWN 80
31 #define ENTER 28
33 char *buf;
34 char opt;
35 unsigned buf_len;
37 unsigned char curr_button_x = 0;
38 unsigned char curr_button_y = 0;
40 int number = 0;
42 unsigned key_pressed (int keycode)
44 int scancode = getkey ();
46 if (scancode == keycode)
47 return 1;
49 if (scancode == keycode+128)
50 return 2;
51 else
52 return 0;
55 int number_get ()
57 int num = 0;
58 char key = 0;
60 while (key != '\n') {
61 key = getch ();
63 if ((key >= '0' && key <= '9') || key == '\b') {
64 buf[num] = key;
65 num ++;
66 putch (key);
69 schedule ();
72 buf[num] = '\0';
74 return atoi (buf);
77 char opt_get ()
79 int num = 0;
80 char key = 0;
82 while (key != '\n') {
83 key = getch ();
84 if (key) {
85 buf[num] = key;
86 num ++;
87 putch (key);
91 return buf[0];
94 void draw_button (unsigned char x, unsigned char y, unsigned active, char c)
98 if (active == 1) {
99 gotoxy (x+1, y+1);
100 setcolor (14, 0);
101 putch (175);
102 putch (c);
103 putch (174);
104 } else {
105 gotoxy (x+1, y+1);
106 setcolor (7, 0);
107 putch (' ');
108 putch (c);
109 putch (' ');
113 setcolor (15, 0);
115 // levy horni roh
116 gotoxy (x, y);
117 putch (218);
119 putch (196);
120 putch (196);
121 putch (196);
123 // pravy horni roh
124 putch (191);
126 gotoxy (x, y+1);
127 putch (179);
129 gotoxy (x+4, y+1);
130 putch (179);
132 // levy dolni roh
133 gotoxy (x, y+2);
134 putch (192);
136 putch (196);
137 putch (196);
138 putch (196);
140 // pravy dolni roh
141 putch (217);
144 void draw_calc ()
146 unsigned r = 0;
148 /* okraje */
149 gotoxy (0, 0);
150 // levy horni roh
151 putch (201);
153 for (r = 0; r < 25; r ++)
154 putch (205);
156 // pravy horni roh
157 putch (187);
159 for (r = 1; r < 20; r ++) {
160 gotoxy (0, r);
161 putch (186);
164 for (r = 1; r < 20; r ++) {
165 gotoxy (26, r);
166 putch (186);
169 putch ('\n');
171 // levy dolni roh
172 putch (200);
174 for (r = 0; r < 25; r ++)
175 putch (205);
177 // pravy dolni roh
178 putch (188);
181 /* display */
183 // levy horni roh
184 gotoxy (2, 2);
185 putch (218);
187 for (r = 0; r < 21; r ++)
188 putch (196);
190 // pravy horni roh
191 putch (191);
193 gotoxy (2, 3);
194 putch (179);
196 gotoxy (24, 3);
197 putch (179);
199 // levy dolni roh
200 gotoxy (2, 4);
201 putch (192);
203 for (r = 0; r < 21; r ++)
204 putch (196);
206 // pravy dolni roh
207 putch (217);
210 void display_write (char *str)
212 /* clear display */
213 gotoxy (3, 3);
214 /* for (r = 0; r < 21; r ++)
215 putch (' ');*/
217 //unsigned l = strlen (str);
219 //gotoxy (24-l, 3);
220 printf (str);
223 unsigned buf_i = 0;
224 unsigned key_handler ()
226 int scancode = getkey ();
228 if (!scancode)
229 return 2;
231 if (buf_i)
232 display_write (buf);
234 char key = 0;
235 switch (scancode) {
236 case ESC:
237 return 0;
238 case ARROWUP:
239 if (curr_button_y > 0)
240 curr_button_y --;
242 break;
243 case ARROWDOWN:
244 if (curr_button_y < 3)
245 curr_button_y ++;
246 break;
247 case ARROWLEFT:
248 if (curr_button_x > 0)
249 curr_button_x --;
250 break;
251 case ARROWRIGHT:
252 if (curr_button_x < 3)
253 curr_button_x ++;
254 break;
255 case ENTER:
256 /* 7 */
257 if (curr_button_x == 0 && curr_button_y == 1) {
258 buf[buf_i] = '7';
259 buf_i ++;
260 buf[buf_i+1] = '\0';
261 break;
263 /* 8 */
264 if (curr_button_x == 1 && curr_button_y == 1) {
265 buf[buf_i] = '8';
266 buf_i ++;
267 buf[buf_i+1] = '\0';
268 break;
270 /* 9 */
271 if (curr_button_x == 2 && curr_button_y == 1) {
272 buf[buf_i] = '9';
273 buf_i ++;
274 buf[buf_i+1] = '\0';
275 break;
277 /* 4 */
278 if (curr_button_x == 0 && curr_button_y == 2) {
279 buf[buf_i] = '4';
280 buf_i ++;
281 buf[buf_i+1] = '\0';
282 break;
284 /* 5 */
285 if (curr_button_x == 1 && curr_button_y == 2) {
286 buf[buf_i] = '5';
287 buf_i ++;
288 buf[buf_i+1] = '\0';
289 break;
291 /* 6 */
292 if (curr_button_x == 2 && curr_button_y == 2) {
293 buf[buf_i] = '6';
294 buf_i ++;
295 buf[buf_i+1] = '\0';
296 break;
298 /* 1 */
299 if (curr_button_x == 0 && curr_button_y == 3) {
300 buf[buf_i] = '1';
301 buf_i ++;
302 buf[buf_i+1] = '\0';
303 break;
305 /* 2 */
306 if (curr_button_x == 1 && curr_button_y == 3) {
307 buf[buf_i] = '2';
308 buf_i ++;
309 buf[buf_i+1] = '\0';
310 break;
312 /* 3 */
313 if (curr_button_x == 2 && curr_button_y == 3) {
314 buf[buf_i] = '3';
315 buf_i ++;
316 buf[buf_i+1] = '\0';
317 break;
319 /* 0 */
320 if (curr_button_x == 3 && curr_button_y == 2) {
321 buf[buf_i] = '0';
322 buf_i ++;
323 buf[buf_i+1] = '\0';
324 break;
327 /* + */
328 if (curr_button_x == 3 && curr_button_y == 0) {
329 if (buf_i > 0)
330 break;
332 number += atoi (buf);
334 //memset (buf, 0, 15);
335 itoa (number, buf, 10);
336 //buf[15] = '\0';
337 //display_write (buf);
338 //buf_i = strlen (buf);
339 break;
342 /* = */
343 if (curr_button_x == 3 && curr_button_y == 3) {
344 if (buf_i > 0)
345 break;
347 //memset (buf, 0, 15);
348 itoa (number, buf, 10);
349 //gotoxy (3,3);
350 printf (buf);
351 sleep (5);
352 //buf_i = strlen (buf);
353 break;
356 break;
359 key = getch ();
360 if (key >= '0' && key <= '9') {
361 buf[buf_i] = key;
362 buf_i ++;
363 buf[buf_i+1] = '\0';
364 //gotoxy (3,3);
365 //unsigned x = 0;
366 //for (x = 0; x < buf_i; x ++)
367 // putch (buf[x]);
371 // 1. lajna
372 draw_button (2, 7, 0, '/');
373 draw_button (8, 7, 0, '*');
374 draw_button (14, 7, 0, '-');
375 draw_button (20, 7, 0, '+');
377 // 2. lajna
378 draw_button (2, 10, 0, '7');
379 draw_button (8, 10, 0, '8');
380 draw_button (14, 10, 0, '9');
381 draw_button (20, 10, 0, 'C');
383 // 3. lajna
384 draw_button (2, 13, 0, '4');
385 draw_button (8, 13, 0, '5');
386 draw_button (14, 13, 0, '6');
387 draw_button (20, 13, 0, '0');
389 // 4. lajna
390 draw_button (2, 16, 0, '1');
391 draw_button (8, 16, 0, '2');
392 draw_button (14, 16, 0, '3');
393 draw_button (20, 16, 0, '=');
396 if (curr_button_x == 0 && curr_button_y == 0)
397 draw_button (2, 7, 1, '/');
398 else
399 draw_button (2, 7, 0, '/');
401 if (curr_button_x == 1 && curr_button_y == 0)
402 draw_button (8, 7, 1, '*');
403 else
404 draw_button (8, 7, 0, '*');
406 if (curr_button_x == 2 && curr_button_y == 0)
407 draw_button (14, 7, 1, '-');
408 else
409 draw_button (14, 7, 0, '-');
411 if (curr_button_x == 3 && curr_button_y == 0)
412 draw_button (20, 7, 1, '+');
413 else
414 draw_button (20, 7, 0, '+');
416 if (curr_button_x == 0 && curr_button_y == 1)
417 draw_button (2, 10, 1, '7');
418 else
419 draw_button (2, 10, 0, '7');
421 if (curr_button_x == 1 && curr_button_y == 1)
422 draw_button (8, 10, 1, '8');
423 else
424 draw_button (8, 10, 0, '8');
426 if (curr_button_x == 2 && curr_button_y == 1)
427 draw_button (14, 10, 1, '9');
428 else
429 draw_button (14, 10, 0, '9');
431 if (curr_button_x == 3 && curr_button_y == 1)
432 draw_button (20, 10, 1, 'C');
433 else
434 draw_button (20, 10, 0, 'C');
436 if (curr_button_x == 0 && curr_button_y == 2)
437 draw_button (2, 13, 1, '4');
438 else
439 draw_button (2, 13, 0, '4');
441 if (curr_button_x == 1 && curr_button_y == 2)
442 draw_button (8, 13, 1, '5');
443 else
444 draw_button (8, 13, 0, '5');
446 if (curr_button_x == 2 && curr_button_y == 2)
447 draw_button (14, 13, 1, '6');
448 else
449 draw_button (14, 13, 0, '6');
451 if (curr_button_x == 3 && curr_button_y == 2)
452 draw_button (20, 13, 1, '0');
453 else
454 draw_button (20, 13, 0, '0');
456 if (curr_button_x == 0 && curr_button_y == 3)
457 draw_button (2, 16, 1, '1');
458 else
459 draw_button (2, 16, 0, '1');
461 if (curr_button_x == 1 && curr_button_y == 3)
462 draw_button (8, 16, 1, '2');
463 else
464 draw_button (8, 16, 0, '2');
466 if (curr_button_x == 2 && curr_button_y == 3)
467 draw_button (14, 16, 1, '3');
468 else
469 draw_button (14, 16, 0, '3');
471 if (curr_button_x == 3 && curr_button_y == 3)
472 draw_button (20, 16, 1, '=');
473 else
474 draw_button (20, 16, 0, '=');
476 setcolor (15, 0);
478 draw_calc ();
480 return 1;
483 void textualgui ()
485 draw_calc ();
487 //display_write ("Ahoj svete !!");
489 unsigned t = 1;
490 curr_button_x = 0;
491 curr_button_y = 0;
492 buf_i = 0;
494 buf = (char *) malloc (sizeof (char) * 16);
495 buf[0] = '\0';
496 buf[1] = '\0';
497 buf[2] = '\0';
498 buf_len = 0;
500 number = 0;
502 while (1) {
503 if (t > 1000) {
504 /* exit app */
505 if (!key_handler ())
506 break;
508 t = 0;
511 schedule ();
512 t ++;
516 void textual ()
518 int a = 0, b = 0;
519 int res = 0;
520 char oper = 0;
522 buf = (char *) malloc (sizeof (char) * 16);
524 printf ("Calc\n\n");
526 printf ("\nType first number: ");
527 a = number_get ();
529 printf ("\nSecond number: ");
530 b = number_get ();
532 printf ("\nChoose operation (+,-,*,/): ");
534 oper = opt_get ();
536 if (oper == '+')
537 res = a + b;
538 else if (oper == '-')
539 res = a - b;
540 else if (oper == '*')
541 res = a * b;
542 else if (oper == '/')
543 res = a / b;
545 printf ("\nResult: %d\n", res);
547 free (buf);
550 int main (int argc, char **argv) // like main in a normal C program
552 cls ();
554 if (argc > 1) {
555 if (!strcmp (argv[1], "-g"))
556 textualgui ();
557 } else
558 textual ();
560 return 0;