Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
9137 turbocat 1
/*
2
 * An example of using threads to create a copy of a window.
3
 * Built on top of the /programs/develop/examples/thread/trunk/thread.asm example.
4
 *
5
 * Created by turbocat (Maxim Logaev) 2021.
6
*/
7
 
8
#include 
9
#include 
10
#include 
11
 
12
#define TH_STACK_SIZE 1024
13
 
14
enum BUTTONS{
15
    BTN_QUIT      = 1,
16
    BTN_CREATE_TH = 2,
17
};
18
 
19
ksys_colors_table_t sys_colors;
20
 
21
extern int main();
22
 
23
void redraw_window(void){
24
    ksys_pos_t mouse_pos = _ksys_get_mouse_pos(KSYS_MOUSE_SCREEN_POS);
25
    _ksys_start_draw();
26
    _ksys_create_window(mouse_pos.x, mouse_pos.y, 140, 60, "Threads", sys_colors.work_area, 0x14);
27
    _ksys_define_button(10, 30, 120, 20,  BTN_CREATE_TH, sys_colors.work_button);
28
    _ksys_draw_text("Create thread!", 15, 34, 0, 0x90000000 | sys_colors.work_button_text);
29
    _ksys_end_draw();
30
}
31
 
32
void create_thread(void){
33
    unsigned tid;  // New thread ID
34
    void *th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
35
    if(!th_stack){
36
         _ksys_debug_puts("Memory allocation error for thread!");
37
         return;
38
    }
39
    tid = _ksys_create_thread(main, th_stack+TH_STACK_SIZE); // Create new thread with entry "main"
40
    if(tid==-1){
41
        _ksys_debug_puts("Unable to create a new thread!");
42
        return;
43
    }
44
    debug_printf("New thread created (TID=%u)\n", tid);
45
}
46
 
47
int main(){
48
    _ksys_get_system_colors(&sys_colors);
49
    int gui_event;
50
    redraw_window();
51
 
52
    while(1){
53
        gui_event = _ksys_get_event();
54
        switch(gui_event){
55
        case KSYS_EVENT_REDRAW:
56
            redraw_window();
57
            break;
58
        case KSYS_EVENT_BUTTON:
59
            switch (_ksys_get_button()){
60
            case BTN_CREATE_TH:
61
                create_thread();
62
                break;
63
            case BTN_QUIT:
64
                _ksys_exit();
65
            }
66
            break;
67
         }
68
     }
69
}