Project

General

Profile

Revision 297

chat working

View differences:

proj/DR.mk
1
PROG=proj
2

  
3
.PATH: ${.CURDIR}/src
4
.PATH: ${.CURDIR}/libs/graph/src
5
.PATH: ${.CURDIR}/libs/kbc/src
6
.PATH: ${.CURDIR}/libs/rtc/src
7
.PATH: ${.CURDIR}/libs/timer/src
8
.PATH: ${.CURDIR}/libs/uart/src
9
.PATH: ${.CURDIR}/libs/classes/src
10
.PATH: ${.CURDIR}/libs/utils/src
11

  
12
SRCS= proj.c list.c queue.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c uart.c hltp.c makecode_map.c menu.c
13
IPATHS=-I./include -I./libs/graph/include -I./libs/kbc/include -I./libs/rtc/include -I./libs/timer/include -I./libs/uart/include -I./libs/classes/include -I./libs/utils/include -I./maps -I./media/xpm
14

  
15
CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO #-D __LCOM_OPTIMIZED_
16
CPPFLAGS += -D DIOGO
17

  
18
DPADD += ${LIBLCF}
19
LDADD += -llcf
20

  
21
.include <minix.lcom.mk>
22 0

  
proj/TB.mk
1
PROG=proj
2

  
3
.PATH: ${.CURDIR}/src
4
.PATH: ${.CURDIR}/libs/graph/src
5
.PATH: ${.CURDIR}/libs/kbc/src
6
.PATH: ${.CURDIR}/libs/rtc/src
7
.PATH: ${.CURDIR}/libs/timer/src
8
.PATH: ${.CURDIR}/libs/uart/src
9
.PATH: ${.CURDIR}/libs/classes/src
10
.PATH: ${.CURDIR}/libs/utils/src
11

  
12
SRCS= proj.c list.c queue.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c uart.c makecode_map.c menu.c
13
IPATHS=-I./include -I./libs/graph/include -I./libs/kbc/include -I./libs/rtc/include -I./libs/timer/include -I./libs/uart/include -I./libs/classes/include -I./libs/utils/include -I./maps -I./media/xpm
14

  
15
CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO #-D __LCOM_OPTIMIZED_
16
CPPFLAGS += -D TELMO
17

  
18
DPADD += ${LIBLCF}
19
LDADD += -llcf
20

  
21
.include <minix.lcom.mk>
22 0

  
proj/libs/graph/include/font.h
29 29
void (text_set_color) (text_t *p, uint32_t color);
30 30
void (text_set_valign)(text_t *p, enum text_valign valign);
31 31
void (text_set_halign)(text_t *p, enum text_halign halign);
32
int16_t (text_get_x)  (const text_t *p);
33
int16_t (text_get_y)  (const text_t *p);
32 34
int (text_draw)     (const text_t *p);
33 35

  
34 36
#endif //FONT_H_INCLUDED
proj/libs/graph/src/font.c
133 133
void (text_set_color) (text_t *p, uint32_t color         ){ p->color = color  ; }
134 134
void (text_set_valign)(text_t *p, enum text_valign valign){ p->valign = valign; }
135 135
void (text_set_halign)(text_t *p, enum text_halign halign){ p->halign = halign; }
136
int16_t (text_get_x)  (const text_t *p){ return p->x; }
137
int16_t (text_get_y)  (const text_t *p){ return p->y; }
136 138

  
137 139
int (text_draw)(const text_t *p){
138 140
    if(p == NULL) return NULL_PTR;
proj/libs/graph/src/menu.c
22 22
    rectangle_t *frame;
23 23
};
24 24

  
25
static int menu_recalculate(menu_t *menu){
26
    const int Wtotal = 2*W1+W;
27
    const int Htotal = 2*H1+menu->sz*H+(menu->sz-1)*H2;
28
    int x = graph_get_XRes()/2-Wtotal/2;
29
    int y = graph_get_YRes()/2-Htotal/2;
30
    rectangle_set_pos (menu->frame, x, y);
31
    rectangle_set_size(menu->frame, Wtotal, Htotal);
32
    x += W1; y += H1;
33
    for(size_t i = 0; i < menu->sz; ++i, y+=H+H2){
34
        rectangle_set_pos(menu->r[i], x, y);
35
        text_set_pos     (menu->t[i], x+W/2, y+H/2);
36
    }
37
    return SUCCESS;
38
}
39

  
25 40
menu_t* (menu_ctor)(const font_t *fnt){
26 41
    if(fnt == NULL) return NULL;
27 42
    menu_t *ret = (menu_t*)malloc(sizeof(menu_t));
......
31 46
    ret->r = NULL;
32 47
    ret->t = NULL;
33 48

  
34
    ret->frame = rectangle_ctor(0, 0, 800, 500);
49
    ret->frame = rectangle_ctor(0, 0, 0, 0);
35 50
    rectangle_set_fill_color(ret->frame, GRAPH_BLACK);
36 51
    rectangle_set_outline_width(ret->frame, 6);
37 52
    rectangle_set_outline_color(ret->frame, GRAPH_WHITE);
38 53
    rectangle_set_fill_trans(ret->frame, GRAPH_TRANSPARENT);
39 54

  
55
    menu_recalculate(ret);
56

  
40 57
    return ret;
41 58
}
42 59

  
43
static int menu_recalculate(menu_t *menu){
44
    const int Wtotal = 2*W1+W;
45
    const int Htotal = 2*H1+menu->sz*H+(menu->sz-1)*H2;
46
    int x = graph_get_XRes()/2-Wtotal/2;
47
    int y = graph_get_YRes()/2-Htotal/2;
48
    rectangle_set_pos (menu->frame, x, y);
49
    rectangle_set_size(menu->frame, Wtotal, Htotal);
50
    x += W1; y += H1;
51
    for(size_t i = 0; i < menu->sz; ++i, y+=H+H2){
52
        rectangle_set_pos(menu->r[i], x, y);
53
        text_set_pos     (menu->t[i], x+W/2, y+H/2);
54
    }
55
    return SUCCESS;
56
}
57

  
58 60
int (menu_add_item)(menu_t *menu, const char *s){
59 61
    rectangle_t *r = rectangle_ctor(0, 0, W, H);
60 62
    text_t      *t = text_ctor(menu->fnt, s);
proj/libs/uart/include/uart.h
50 50
int uart_disable_int_tx(int base_addr);
51 51

  
52 52
/// NCTP - Non-critical transmission protocol
53
int nctp_init(void (*proc_func)(const uint8_t*, const size_t));
53
int nctp_init(void);
54
int nctp_set_processor(void (*proc_func)(const uint8_t*, const size_t));
54 55
int nctp_free(void);
55 56

  
56 57
int nctp_send(size_t num, uint8_t* ptr[], size_t sz[]);
proj/libs/uart/src/uart.c
286 286
queue_t *in  = NULL;
287 287
void (*process)(const uint8_t*, const size_t);
288 288

  
289
int nctp_init(void (*proc_func)(const uint8_t*, const size_t)){
289
int nctp_init(){
290 290
    out = queue_ctor(); if(out == NULL) return NULL_PTR;
291 291
    in  = queue_ctor(); if(in  == NULL) return NULL_PTR;
292
    return SUCCESS;
293
}
294
int nctp_set_processor(void (*proc_func)(const uint8_t*, const size_t)){
292 295
    process = proc_func;
293 296
    return SUCCESS;
294 297
}
proj/src/interrupts_func.c
49 49
                                         NULL,
50 50
                                         NULL,
51 51
                                     };
52

  
52
/*
53 53
static void process_received(const uint8_t *p, const size_t sz){
54 54
    void *q = NULL;
55 55
    hltp_type t = hltp_interpret(p, sz, &q);
......
61 61
        default: break;
62 62
    }
63 63
}
64

  
64
*/
65 65
int (subscribe_all)(void) {
66 66

  
67 67
    /// Timer interrupt handling
......
123 123
        return SBCR_ERROR;
124 124
    }
125 125
    uart_subscribed = 1;
126
    nctp_init(process_received);
126
    nctp_init();
127 127

  
128 128
    return SUCCESS;
129 129
}
proj/src/proj.c
70 70
    /// subscribe interrupts
71 71
    if (subscribe_all()) { return 1; }
72 72

  
73
    #ifndef DIOGO
74
        /// initialize graphics
75
        if(graph_init(GRAPH_MODE)){
76
            printf("%s: failed to initalize graphics.\n", __func__);
77
            if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
78
            return 1;
79
        }
80
    #endif
73
    /// initialize graphics
74
    if(graph_init(GRAPH_MODE)){
75
        printf("%s: failed to initalize graphics.\n", __func__);
76
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
77
        return 1;
78
    }
81 79

  
82 80
    /// Load stuff
83 81
    {
84
        #ifndef DIOGO
85
            graph_clear_screen();
86
            text_t *txt = text_ctor(consolas, "Loading...");
87
            text_draw(txt);
88
            text_dtor(txt);
89
            graph_draw();
90
        #endif
82
        graph_clear_screen();
83
        text_t *txt = text_ctor(consolas, "Loading...");
84
        text_draw(txt);
85
        text_dtor(txt);
86
        graph_draw();
91 87

  
92 88
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
93 89
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
......
99 95
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
100 96
    }
101 97

  
102
    #ifndef DIOGO
103
        menu_t *main_menu = menu_ctor(consolas);
104
        menu_add_item(main_menu, "Play");
105
        menu_add_item(main_menu, "Chat");
106
        menu_add_item(main_menu, "Exit");
107
    #endif
98
    menu_t *main_menu = menu_ctor(consolas);
99
    menu_add_item(main_menu, "Play");
100
    menu_add_item(main_menu, "Chat");
101
    menu_add_item(main_menu, "Exit");
108 102

  
109
    #ifndef DIOGO
110
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
111
        uint8_t last_lb = 0;
112
        struct packet pp;
113
        keys_t *keys = get_key_presses();
114
    #endif
103
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
104
    uint8_t last_lb = 0;
105
    struct packet pp;
106
    keys_t *keys = get_key_presses();
107

  
115 108
    /// loop stuff
116 109
    int ipc_status;
117 110
    message msg;
118 111

  
119
    #ifdef DIOGO
120
        char buffer[1024]; // buffer
121
        int buffer_pos = 0;
122
    #endif
112
    int click = 0;
123 113

  
124
    #ifndef DIOGO
125
        int click = 0;
126
    #endif
127

  
128 114
    int good = true;
129 115

  
130 116
    while (good) {
......
140 126
                        if (msg.m_notify.interrupts & n) {
141 127
                            interrupt_handler(i);
142 128
                            switch (i) {
143
                            #ifndef DIOGO
144 129
                            case TIMER0_IRQ:
145 130

  
146 131
                                graph_clear_screen();
......
159 144
                                graph_draw();
160 145

  
161 146
                                break;
162
                            #endif
163 147
                            case KBC_IRQ:
164 148
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
165
                                #ifdef DIOGO
166
                                else if ((scancode[0]) == ENTER_MAKE_CODE) {
167
                                    buffer[buffer_pos] = '\0';
168
                                    printf("\nSending string -%s-", buffer);
169
                                    printf(" (output: %d)\n",
170
                                        hltp_send_string(buffer));
171
                                    buffer_pos = 0;
172
                                }
173
                                else {
174
                                    char c = map_makecode(scancode[0]);
175
                                    if (c == ERROR_CODE) break;
176
                                    buffer[buffer_pos++] = c;
177
                                    printf("%c", c);
178
                                }
179
                                #endif
180
                            #ifndef DIOGO
181 149
                            case MOUSE_IRQ:
182 150
                                if (counter_mouse_ih >= 3) {
183 151
                                    mouse_parse_packet(packet_mouse_ih, &pp);
......
187 155
                                    counter_mouse_ih = 0;
188 156
                                }
189 157
                                break;
190
                            #endif
191
                            #ifdef DIOGO
192
                            case COM1_IRQ:
193
                                nctp_ih();
194
                                break;
195
                            #endif
196 158
                            }
197 159
                        }
198 160
                    }
......
221 183
        return 1;
222 184
    }
223 185

  
224

  
225 186
    if (cleanup()) {
226 187
        printf("%s: failed to cleanup.\n", __func__);
227 188
        return 1;
......
362 323
    return SUCCESS;
363 324
}
364 325

  
326
#define CHAT_MAX_SIZE   64
327
#define CHAT_MAX_NUM    10
328

  
329
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
330
rectangle_t *r_text               =  NULL;
331

  
332
static void chat_process(const uint8_t *p, const size_t sz){
333
    char buffer2[CHAT_MAX_NUM+3];
334
    void *dest = NULL;
335
    hltp_type tp = hltp_interpret(p, sz, &dest);
336
    switch(tp){
337
        case hltp_type_string:
338
            strcpy(buffer2, dest);
339
            strncat(buffer2, " <", 2);
340
            for(size_t i = CHAT_MAX_NUM-1; i; --i)
341
                text_set_text(t_text[i], text_get_string(t_text[i-1]));
342
            text_set_text(t_text[0], buffer2);
343
            for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
344
                if(text_get_string(t_text[i])[0] == '>'){
345
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
346
                    text_set_halign(t_text[i], text_halign_left);
347
                }else{
348
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
349
                    text_set_halign(t_text[i], text_halign_right);
350
                }
351
            }
352
            break;
353
        default: break;
354
    }
355
}
356

  
365 357
int (chat)(void){
358
    int r;
359

  
360
    nctp_set_processor(chat_process);
361

  
362
    struct packet pp;
363

  
364
    rectangle_t *r_buffer = NULL; {
365
        r_buffer = rectangle_ctor(0,0,750,70);
366
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
367
                                    graph_get_YRes()*0.8-rectangle_get_h(r_buffer)/2);
368
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
369
        rectangle_set_outline_width(r_buffer, 2);
370
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
371
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
372
    }
373
    text_t      *t_buffer = NULL; {
374
        t_buffer = text_ctor(consolas, "");
375
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
376
                               rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
377
        text_set_halign(t_buffer, text_halign_left);
378
        text_set_valign(t_buffer, text_valign_center);
379
        text_set_color (t_buffer, TEXT_COLOR);
380
    }
381

  
382
    /** r_text */ {
383
        r_text = rectangle_ctor(0,0,750,500);
384
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
385
                                  graph_get_YRes()*0.1-rectangle_get_h(r_buffer)/2);
386
        rectangle_set_fill_color   (r_text, GRAPH_BLACK);
387
        rectangle_set_outline_width(r_text, 2);
388
        rectangle_set_outline_color(r_text, GRAPH_WHITE);
389
        rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
390
    }
391
    /** t_text */ {
392
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
393
            t_text[i] = text_ctor(consolas, " ");
394
            text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
395
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-30*i);
396
            text_set_halign(t_text[i], text_halign_left);
397
            text_set_valign(t_text[i], text_valign_bottom);
398
            text_set_color (t_text[i], TEXT_COLOR);
399
        }
400
    }
401

  
402
    /// loop stuff
403
    int ipc_status;
404
    message msg;
405

  
406
    char buffer[CHAT_MAX_SIZE] = "";
407

  
408
    int good = true;
409

  
410
    while (good) {
411
        /* Get a request message. */
412
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
413
            printf("driver_receive failed with %d", r);
414
            continue;
415
        }
416
        if (is_ipc_notify(ipc_status)) { /* received notification */
417
            switch (_ENDPOINT_P(msg.m_source)) {
418
                case HARDWARE: /* hardware interrupt notification */
419
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
420
                        if (msg.m_notify.interrupts & n) {
421
                            interrupt_handler(i);
422
                            switch (i) {
423
                            case TIMER0_IRQ:
424
                                graph_clear_screen();
425
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
426

  
427
                                rectangle_draw(r_buffer);
428
                                text_draw(t_buffer);
429

  
430
                                rectangle_draw(r_text);
431
                                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
432

  
433
                                sprite_draw(sp_crosshair);
434
                                graph_draw();
435
                                break;
436
                            case KBC_IRQ:
437
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
438
                                else if ((scancode[0]) == ENTER_MAKE_CODE) {
439
                                    hltp_send_string(buffer);
440
                                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
441
                                    strncat(buffer2, buffer, strlen(buffer));
442
                                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
443
                                        text_set_text(t_text[i], text_get_string(t_text[i-1]));
444
                                    text_set_text(t_text[0], buffer2);
445
                                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
446
                                        if(text_get_string(t_text[i])[0] == '>'){
447
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
448
                                            text_set_halign(t_text[i], text_halign_left);
449
                                        }else{
450
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
451
                                            text_set_halign(t_text[i], text_halign_right);
452
                                        }
453
                                    }
454

  
455
                                    buffer[0] = '\0';
456
                                    text_set_text(t_buffer, buffer);
457
                                }
458
                                else {
459
                                    char c = map_makecode(scancode[0]);
460
                                    if (c == ERROR_CODE) break;
461
                                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
462
                                    else                    printf("Char limit exceeded\n");
463
                                    text_set_text(t_buffer, buffer);
464
                                }
465
                            case MOUSE_IRQ:
466
                                if (counter_mouse_ih >= 3) {
467
                                    mouse_parse_packet(packet_mouse_ih, &pp);
468
                                    update_mouse(&pp);
469
                                    counter_mouse_ih = 0;
470
                                }
471
                                break;
472
                            case COM1_IRQ:
473
                                nctp_ih();
474
                                break;
475
                            }
476
                        }
477
                    }
478

  
479
                    break;
480
                default:
481
                    break; /* no other notifications expected: do nothing */
482
            }
483
        } else { /* received standart message, not a notification */
484
            /* no standart message expected: do nothing */
485
        }
486
    }
487

  
488
    rectangle_dtor(r_buffer);
489
    text_dtor     (t_buffer);
490

  
491
    rectangle_dtor(r_text);
492
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
493

  
366 494
    return SUCCESS;
367 495
}

Also available in: Unified diff