Subversion Repositories Kolibri OS

Rev

Rev 8473 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include "tp.h"
  2. #include <kos32sys.h>
  3.  
  4. typedef unsigned short int uint16_t;
  5. typedef unsigned int uint32_t;
  6.  
  7. extern void _tp_raise(TP,tp_obj);
  8. extern tp_obj tp_dict(TP);
  9. extern tp_obj tp_method(TP,tp_obj self,tp_obj v(TP));
  10. extern tp_obj tp_number(tp_num v);
  11. extern tp_obj tp_list(TP);
  12. extern void _tp_list_append(TP,_tp_list *self, tp_obj v);
  13. extern tp_obj tp_call(TP, const char *mod, const char *fnc, tp_obj params);
  14. extern void _tp_call(TP,tp_obj *dest, tp_obj fnc, tp_obj params);
  15. extern int tp_bool(TP,tp_obj v);
  16. extern tp_obj tp_has(TP,tp_obj self, tp_obj k);
  17. // #define _cdecl __attribute__((cdecl))
  18. extern int (* _cdecl con_printf)(const char* format,...);
  19. static tp_obj kolibri_show(TP)
  20. {
  21.     tp_obj self = TP_TYPE(TP_DICT);
  22.     uint16_t xpos = (uint16_t)(tp_get(tp, self, tp_string("x")).number.val);
  23.     uint16_t ypos = (uint16_t)tp_get(tp, self, tp_string("y")).number.val;
  24.     uint16_t height = (uint16_t)tp_get(tp, self, tp_string("height")).number.val;
  25.     uint16_t width = (uint16_t)tp_get(tp, self, tp_string("width")).number.val;
  26.     uint16_t fixedsize = (uint16_t)tp_get(tp, self, tp_string("fixedsize")).number.val;
  27.     uint32_t bgcolor = (uint32_t)tp_get(tp, self, tp_string("bgcolor")).number.val;
  28.     uint32_t status;
  29.     uint32_t style;
  30.     uint32_t x = xpos * 0x10000 + width;
  31.     uint32_t y = ypos * 0x10000 + height;
  32.     if (fixedsize)
  33.         style = 0;
  34.     else
  35.         style = 0x33000000 + (bgcolor & 0xFFFFFF);
  36.     asm volatile ("int $0x40"::"a"(12), "b"(1));
  37.     asm volatile ("int $0x40"::
  38.                   "a"(0), "b"(x), "c"(y), "d"(style),
  39.                   "S"(0), "D"(0));
  40.     asm volatile ("int $0x40"::"a"(12), "b"(2));
  41.     /* If window has additional handler, run it. */
  42.     if (tp_bool(tp, tp_has(tp, self, tp_string("on_show"))))
  43.     {
  44.         tp_obj result;
  45.         tp_obj fnc = tp_get(tp, self, tp_string("on_show"));
  46.         tp_obj param_list = tp_list(tp); /* Prepare parameters. */
  47.         _tp_list_append(tp, param_list.list.val, self);
  48.         _tp_call(tp, &result, fnc, param_list);
  49.     }
  50.     return tp_None;
  51. }
  52.  
  53. static void window_function(void)
  54. {
  55.     uint32_t ev;
  56.     /* Wait for event. */
  57.     do {
  58.         asm volatile("int $0x40":"=a"(ev):"a"(10));
  59.     } while(ev != 3);
  60.     asm volatile("int $040"::"a"(-1));
  61. }
  62.  
  63. static tp_obj kolibri_default_handler(TP)
  64. {
  65.     return tp_None;
  66. }
  67.  
  68. /* Run window_function() in separated thread. */
  69. static tp_obj kolibri_run(TP)
  70. {
  71.     tp_obj self = TP_TYPE(TP_DICT);
  72.     tp_obj redraw = tp_get(tp, self, tp_string("show"));
  73.     tp_obj result;
  74.     tp_obj key_handler = tp_None;
  75.     tp_obj button_handler = tp_None;
  76.     int    button_id;
  77.     uint32_t ev;
  78.     int    leave=0;
  79.     tp_obj param_list;
  80.     /* Obtain handlers. */
  81.     if (tp_bool(tp, tp_has(tp, self, tp_string("on_key"))))
  82.         key_handler = tp_get(tp, self, tp_string("on_key"));
  83.     if (tp_bool(tp, tp_has(tp, self, tp_string("on_button"))))
  84.         button_handler = tp_get(tp, self, tp_string("on_button"));
  85.  
  86.     while(!leave){
  87.         asm volatile("int $0x40":"=a"(ev):"a"(10));
  88.         switch (ev)
  89.         {
  90.             case 1:
  91.                 _tp_call(tp, &result, redraw, tp_None);
  92.                 break;
  93.             case 2:
  94.                 if (key_handler.type == TP_FNC)
  95.                 {
  96.                     param_list = tp_list(tp); /* Prepare parameters. */
  97.                     _tp_list_append(tp, param_list.list.val, self);
  98.                    
  99.                     oskey_t key;
  100.                     key = get_key();
  101.                     _tp_list_append(tp, param_list.list.val, tp_number(key.code));
  102.                     _tp_call(tp, &result, key_handler, param_list);
  103.                 }
  104.                 break;
  105.             case 3:
  106.                 button_id = get_os_button();
  107.                 if (button_id == 1)
  108.                     leave = 1;
  109.                 else if (button_handler.type == TP_FNC)
  110.                 {
  111.                     param_list = tp_list(tp); /* Prepare parameters. */
  112.                     _tp_list_append(tp, param_list.list.val, self);
  113.                     _tp_list_append(tp, param_list.list.val, tp_number(button_id));
  114.                     _tp_call(tp, &result, button_handler, param_list);
  115.                 }
  116.                 break;
  117.             default:
  118.                 con_printf("Got unknown event %d\n", ev);
  119.                 break;
  120.         }
  121.     };
  122.     return tp_None;
  123. }
  124.  
  125. static tp_obj kolibri_print_text(TP)
  126. {
  127.     tp_obj self = TP_TYPE(TP_DICT);
  128.     uint32_t textcolor = (uint32_t)tp_get(tp, self, tp_string("textcolor")).number.val;
  129.     uint16_t x = (uint16_t)tp_get(tp, self, tp_string("curx")).number.val;
  130.     uint16_t y = (uint16_t)tp_get(tp, self, tp_string("cury")).number.val;
  131.     uint32_t ofs;
  132.     uint32_t width = (uint32_t)tp_get(tp, self, tp_string("width")).number.val;
  133.     tp_obj text = TP_TYPE(TP_STRING);
  134.  
  135.     draw_text_sys((char *)text.string.val, x, y, text.string.len, textcolor);
  136.     /* Update cursor position. */
  137.     ofs = 6 * text.string.len;
  138.     tp_set(tp, self, tp_string("cury"), tp_number(y + 9 * ((x + ofs) / width)));
  139.     tp_set(tp, self, tp_string("curx"), tp_number((x + ofs)%width));
  140.  
  141.     return tp_None;
  142. }
  143.  
  144. tp_obj kolibri_mainwindow(TP)
  145. {
  146.     tp_obj obj = tp_dict(tp);
  147.     obj = tp_dict(tp);
  148.     tp_set(tp, obj, tp_string("x"), TP_TYPE(TP_NUMBER));
  149.     tp_set(tp, obj, tp_string("y"), TP_TYPE(TP_NUMBER));
  150.     tp_set(tp, obj, tp_string("height"), TP_TYPE(TP_NUMBER));
  151.     tp_set(tp, obj, tp_string("width"), TP_TYPE(TP_NUMBER));
  152.     tp_set(tp, obj, tp_string("curx"), tp_number(0));
  153.     tp_set(tp, obj, tp_string("cury"), tp_number(0));
  154.     tp_set(tp, obj, tp_string("fixedsize"), TP_TYPE(TP_NUMBER));
  155.     tp_set(tp, obj, tp_string("textcolor"), tp_number(0x202020));
  156.     tp_set(tp, obj, tp_string("bgcolor"), tp_number(0xFFFFFF));
  157.     tp_set(tp, obj, tp_string("show"), tp_method(tp, obj, kolibri_show));
  158.     tp_set(tp, obj, tp_string("run"), tp_method(tp, obj, kolibri_run));
  159.     /*tp_set(tp, obj, tp_string("keyhandler"), tp_method(tp, obj, kolibri_default_handler));
  160.     tp_set(tp, obj, tp_string("buttonhandler"), tp_method(tp, obj, kolibri_default_handler));*/
  161.     tp_set(tp, obj, tp_string("print_text"), tp_method(tp, obj, kolibri_print_text));
  162.     return obj;
  163. }
  164.