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