Kernel 0.5.9; New stable release after long time with lot of new features ! This...
[ZeXOS.git] / apps / calc / main.c
blobfecc7bc8e24421c1471e96d75874514ef611a899
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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 ();
62 if ((key >= '0' && key <= '9') || key == '\b') {
63 buf[num] = key;
64 num ++;
65 putch (key);
69 buf[num] = '\0';
71 return atoi (buf);
74 char opt_get ()
76 int num = 0;
77 char key = 0;
79 while (key != '\n') {
80 key = getch ();
81 if (key) {
82 buf[num] = key;
83 num ++;
84 putch (key);
88 return buf[0];
91 void draw_button (unsigned char x, unsigned char y, unsigned active, char c)
95 if (active == 1) {
96 gotoxy (x+1, y+1);
97 setcolor (14, 0);
98 putch (175);
99 putch (c);
100 putch (174);
101 } else {
102 gotoxy (x+1, y+1);
103 setcolor (7, 0);
104 putch (' ');
105 putch (c);
106 putch (' ');
110 setcolor (15, 0);
112 // levy horni roh
113 gotoxy (x, y);
114 putch (218);
116 putch (196);
117 putch (196);
118 putch (196);
120 // pravy horni roh
121 putch (191);
123 gotoxy (x, y+1);
124 putch (179);
126 gotoxy (x+4, y+1);
127 putch (179);
129 // levy dolni roh
130 gotoxy (x, y+2);
131 putch (192);
133 putch (196);
134 putch (196);
135 putch (196);
137 // pravy dolni roh
138 putch (217);
141 void draw_calc ()
143 unsigned r = 0;
145 /* okraje */
146 gotoxy (0, 0);
147 // levy horni roh
148 putch (201);
150 for (r = 0; r < 25; r ++)
151 putch (205);
153 // pravy horni roh
154 putch (187);
156 for (r = 1; r < 20; r ++) {
157 gotoxy (0, r);
158 putch (186);
161 for (r = 1; r < 20; r ++) {
162 gotoxy (26, r);
163 putch (186);
166 putch ('\n');
168 // levy dolni roh
169 putch (200);
171 for (r = 0; r < 25; r ++)
172 putch (205);
174 // pravy dolni roh
175 putch (188);
178 /* display */
180 // levy horni roh
181 gotoxy (2, 2);
182 putch (218);
184 for (r = 0; r < 21; r ++)
185 putch (196);
187 // pravy horni roh
188 putch (191);
190 gotoxy (2, 3);
191 putch (179);
193 gotoxy (24, 3);
194 putch (179);
196 // levy dolni roh
197 gotoxy (2, 4);
198 putch (192);
200 for (r = 0; r < 21; r ++)
201 putch (196);
203 // pravy dolni roh
204 putch (217);
207 void display_write (char *str)
209 /* clear display */
210 gotoxy (3, 3);
211 unsigned r = 0;
212 /* for (r = 0; r < 21; r ++)
213 putch (' ');*/
215 //unsigned l = strlen (str);
217 //gotoxy (24-l, 3);
218 puts (str);
221 unsigned buf_i = 0;
222 unsigned key_handler ()
224 int scancode = getkey ();
226 if (!scancode)
227 return 2;
229 if (buf_i)
230 display_write (buf);
232 char key = 0;
233 switch (scancode) {
234 case ESC:
235 return 0;
236 case ARROWUP:
237 if (curr_button_y > 0)
238 curr_button_y --;
240 break;
241 case ARROWDOWN:
242 if (curr_button_y < 3)
243 curr_button_y ++;
244 break;
245 case ARROWLEFT:
246 if (curr_button_x > 0)
247 curr_button_x --;
248 break;
249 case ARROWRIGHT:
250 if (curr_button_x < 3)
251 curr_button_x ++;
252 break;
253 case ENTER:
254 /* 7 */
255 if (curr_button_x == 0 && curr_button_y == 1) {
256 buf[buf_i] = '7';
257 buf_i ++;
258 buf[buf_i+1] = '\0';
259 break;
261 /* 8 */
262 if (curr_button_x == 1 && curr_button_y == 1) {
263 buf[buf_i] = '8';
264 buf_i ++;
265 buf[buf_i+1] = '\0';
266 break;
268 /* 9 */
269 if (curr_button_x == 2 && curr_button_y == 1) {
270 buf[buf_i] = '9';
271 buf_i ++;
272 buf[buf_i+1] = '\0';
273 break;
275 /* 4 */
276 if (curr_button_x == 0 && curr_button_y == 2) {
277 buf[buf_i] = '4';
278 buf_i ++;
279 buf[buf_i+1] = '\0';
280 break;
282 /* 5 */
283 if (curr_button_x == 1 && curr_button_y == 2) {
284 buf[buf_i] = '5';
285 buf_i ++;
286 buf[buf_i+1] = '\0';
287 break;
289 /* 6 */
290 if (curr_button_x == 2 && curr_button_y == 2) {
291 buf[buf_i] = '6';
292 buf_i ++;
293 buf[buf_i+1] = '\0';
294 break;
296 /* 1 */
297 if (curr_button_x == 0 && curr_button_y == 3) {
298 buf[buf_i] = '1';
299 buf_i ++;
300 buf[buf_i+1] = '\0';
301 break;
303 /* 2 */
304 if (curr_button_x == 1 && curr_button_y == 3) {
305 buf[buf_i] = '2';
306 buf_i ++;
307 buf[buf_i+1] = '\0';
308 break;
310 /* 3 */
311 if (curr_button_x == 2 && curr_button_y == 3) {
312 buf[buf_i] = '3';
313 buf_i ++;
314 buf[buf_i+1] = '\0';
315 break;
317 /* 0 */
318 if (curr_button_x == 3 && curr_button_y == 2) {
319 buf[buf_i] = '0';
320 buf_i ++;
321 buf[buf_i+1] = '\0';
322 break;
325 /* + */
326 if (curr_button_x == 3 && curr_button_y == 0) {
327 if (buf_i > 0)
328 break;
330 number += atoi (buf);
332 //memset (buf, 0, 15);
333 itoa (number, buf, 10);
334 //buf[15] = '\0';
335 //display_write (buf);
336 //buf_i = strlen (buf);
337 break;
340 /* = */
341 if (curr_button_x == 3 && curr_button_y == 3) {
342 if (buf_i > 0)
343 break;
345 //memset (buf, 0, 15);
346 itoa (number, buf, 10);
347 //gotoxy (3,3);
348 puts (buf);
349 sleep (5);
350 //buf_i = strlen (buf);
351 break;
354 break;
357 key = getch ();
358 if (key >= '0' && key <= '9') {
359 buf[buf_i] = key;
360 buf_i ++;
361 buf[buf_i+1] = '\0';
362 //gotoxy (3,3);
363 //unsigned x = 0;
364 //for (x = 0; x < buf_i; x ++)
365 // putch (buf[x]);
369 // 1. lajna
370 draw_button (2, 7, 0, '/');
371 draw_button (8, 7, 0, '*');
372 draw_button (14, 7, 0, '-');
373 draw_button (20, 7, 0, '+');
375 // 2. lajna
376 draw_button (2, 10, 0, '7');
377 draw_button (8, 10, 0, '8');
378 draw_button (14, 10, 0, '9');
379 draw_button (20, 10, 0, 'C');
381 // 3. lajna
382 draw_button (2, 13, 0, '4');
383 draw_button (8, 13, 0, '5');
384 draw_button (14, 13, 0, '6');
385 draw_button (20, 13, 0, '0');
387 // 4. lajna
388 draw_button (2, 16, 0, '1');
389 draw_button (8, 16, 0, '2');
390 draw_button (14, 16, 0, '3');
391 draw_button (20, 16, 0, '=');
394 if (curr_button_x == 0 && curr_button_y == 0)
395 draw_button (2, 7, 1, '/');
396 else
397 draw_button (2, 7, 0, '/');
399 if (curr_button_x == 1 && curr_button_y == 0)
400 draw_button (8, 7, 1, '*');
401 else
402 draw_button (8, 7, 0, '*');
404 if (curr_button_x == 2 && curr_button_y == 0)
405 draw_button (14, 7, 1, '-');
406 else
407 draw_button (14, 7, 0, '-');
409 if (curr_button_x == 3 && curr_button_y == 0)
410 draw_button (20, 7, 1, '+');
411 else
412 draw_button (20, 7, 0, '+');
414 if (curr_button_x == 0 && curr_button_y == 1)
415 draw_button (2, 10, 1, '7');
416 else
417 draw_button (2, 10, 0, '7');
419 if (curr_button_x == 1 && curr_button_y == 1)
420 draw_button (8, 10, 1, '8');
421 else
422 draw_button (8, 10, 0, '8');
424 if (curr_button_x == 2 && curr_button_y == 1)
425 draw_button (14, 10, 1, '9');
426 else
427 draw_button (14, 10, 0, '9');
429 if (curr_button_x == 3 && curr_button_y == 1)
430 draw_button (20, 10, 1, 'C');
431 else
432 draw_button (20, 10, 0, 'C');
434 if (curr_button_x == 0 && curr_button_y == 2)
435 draw_button (2, 13, 1, '4');
436 else
437 draw_button (2, 13, 0, '4');
439 if (curr_button_x == 1 && curr_button_y == 2)
440 draw_button (8, 13, 1, '5');
441 else
442 draw_button (8, 13, 0, '5');
444 if (curr_button_x == 2 && curr_button_y == 2)
445 draw_button (14, 13, 1, '6');
446 else
447 draw_button (14, 13, 0, '6');
449 if (curr_button_x == 3 && curr_button_y == 2)
450 draw_button (20, 13, 1, '0');
451 else
452 draw_button (20, 13, 0, '0');
454 if (curr_button_x == 0 && curr_button_y == 3)
455 draw_button (2, 16, 1, '1');
456 else
457 draw_button (2, 16, 0, '1');
459 if (curr_button_x == 1 && curr_button_y == 3)
460 draw_button (8, 16, 1, '2');
461 else
462 draw_button (8, 16, 0, '2');
464 if (curr_button_x == 2 && curr_button_y == 3)
465 draw_button (14, 16, 1, '3');
466 else
467 draw_button (14, 16, 0, '3');
469 if (curr_button_x == 3 && curr_button_y == 3)
470 draw_button (20, 16, 1, '=');
471 else
472 draw_button (20, 16, 0, '=');
474 setcolor (15, 0);
476 draw_calc ();
478 return 1;
481 void textualgui ()
483 draw_calc ();
485 //display_write ("Ahoj svete !!");
487 unsigned t = 1;
488 curr_button_x = 0;
489 curr_button_y = 0;
490 buf_i = 0;
492 char *buf = (char *) malloc (sizeof (char) * 16);
493 buf[0] = '\0';
494 buf[1] = '\0';
495 buf[2] = '\0';
496 buf_len = 0;
498 number = 0;
500 while (1) {
501 if (t > 1000) {
502 /* exit app */
503 if (!key_handler ())
504 break;
506 t = 0;
509 schedule ();
510 t ++;
514 void textual ()
516 char *buf = (char *) malloc (sizeof (char) * 16);
517 //int *num = (char *) malloc (sizeof (int) * 5);
518 int a = 0, b = 0;
519 unsigned char i = 0;
520 int res = 0;
522 unsigned end = 0;
523 char oper = 0;
526 puts ("Calc\n\n");
528 while (!end) {
530 puts ("\nZadej prvni cislo: ");
531 a = number_get ();
532 puts ("\nZadej druhe cislo: ");
533 b = number_get ();
535 puts ("\nZadej operaci (+,-,*,/): ");
537 oper = opt_get ();
539 if (oper == '+')
540 res = a + b;
541 else if (oper == '-')
542 res = a - b;
543 else if (oper == '*')
544 res = a * b;
545 else if (oper == '/')
546 res = a * b;
548 itoa (res, buf, 10);
550 puts ("\n-> ");
551 puts (buf);
554 itoa (res, buf, 10);
556 //puts ("\nVysledek je: ");
557 puts (buf);
558 //puts ("\n");
561 int main (int argc, char **argv) // like main in a normal C program
563 cls ();
565 /*if (argl > 1) {
566 if (!strcmp (arg, "-g"))
567 textualgui ();
568 } else*/
569 textual ();
571 exit (1);
573 return 0;