Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2039 jaeger 1
#include "tp.h"
2
#include 
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
                    _tp_list_append(tp, param_list.list.val, tp_number(__menuet__getkey()));
99
                    _tp_call(tp, &result, key_handler, param_list);
100
                }
101
                break;
102
            case 3:
103
                button_id = __menuet__get_button_id();
104
                if (button_id == 1)
105
                    leave = 1;
106
                else if (button_handler.type == TP_FNC)
107
                {
108
                    param_list = tp_list(tp); /* Prepare parameters. */
109
                    _tp_list_append(tp, param_list.list.val, self);
110
                    _tp_list_append(tp, param_list.list.val, tp_number(button_id));
111
                    _tp_call(tp, &result, button_handler, param_list);
112
                }
113
                break;
114
            default:
115
                con_printf("Got unknown event %d\n", ev);
116
                break;
117
        }
118
    };
119
    return tp_None;
120
}
121
 
122
static tp_obj kolibri_print_text(TP)
123
{
124
    tp_obj self = TP_TYPE(TP_DICT);
125
    uint32_t textcolor = (uint32_t)tp_get(tp, self, tp_string("textcolor")).number.val;
126
    uint16_t x = (uint16_t)tp_get(tp, self, tp_string("curx")).number.val;
127
    uint16_t y = (uint16_t)tp_get(tp, self, tp_string("cury")).number.val;
128
    uint32_t ofs;
129
    uint32_t width = (uint32_t)tp_get(tp, self, tp_string("width")).number.val;
130
    tp_obj text = TP_TYPE(TP_STRING);
131
 
132
    __menuet__write_text(x, y, textcolor, (char *)text.string.val, text.string.len);
133
    /* Update cursor position. */
134
    ofs = 6 * text.string.len;
135
    tp_set(tp, self, tp_string("cury"), tp_number(y + 9 * ((x + ofs) / width)));
136
    tp_set(tp, self, tp_string("curx"), tp_number((x + ofs)%width));
137
 
138
    return tp_None;
139
}
140
 
141
tp_obj kolibri_mainwindow(TP)
142
{
143
    tp_obj obj = tp_dict(tp);
144
    obj = tp_dict(tp);
145
    tp_set(tp, obj, tp_string("x"), TP_TYPE(TP_NUMBER));
146
    tp_set(tp, obj, tp_string("y"), TP_TYPE(TP_NUMBER));
147
    tp_set(tp, obj, tp_string("height"), TP_TYPE(TP_NUMBER));
148
    tp_set(tp, obj, tp_string("width"), TP_TYPE(TP_NUMBER));
149
    tp_set(tp, obj, tp_string("curx"), tp_number(0));
150
    tp_set(tp, obj, tp_string("cury"), tp_number(0));
151
    tp_set(tp, obj, tp_string("fixedsize"), TP_TYPE(TP_NUMBER));
152
    tp_set(tp, obj, tp_string("textcolor"), tp_number(0x202020));
153
    tp_set(tp, obj, tp_string("bgcolor"), tp_number(0xFFFFFF));
154
    tp_set(tp, obj, tp_string("show"), tp_method(tp, obj, kolibri_show));
155
    tp_set(tp, obj, tp_string("run"), tp_method(tp, obj, kolibri_run));
156
    /*tp_set(tp, obj, tp_string("keyhandler"), tp_method(tp, obj, kolibri_default_handler));
157
    tp_set(tp, obj, tp_string("buttonhandler"), tp_method(tp, obj, kolibri_default_handler));*/
158
    tp_set(tp, obj, tp_string("print_text"), tp_method(tp, obj, kolibri_print_text));
159
    return obj;
160
}