Project

General

Profile

Statistics
| Revision:

root / proj / src / chat.c @ 348

History | View | Annotate | Download (6.93 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "chat.h"
4

    
5
#include "proj.h"
6

    
7
#include "proj_macros.h"
8
#include "proj_func.h"
9
#include "ent.h"
10
#include "interrupts_func.h"
11
#include "graph.h"
12
#include "rectangle.h"
13
#include "hltp.h"
14
#include "makecode_map.h"
15
#include "errors.h"
16
#include <math.h>
17

    
18
#define CHAT_MAX_SIZE   75
19
#define CHAT_MAX_NUM    19
20
static text_t      *t_text[CHAT_MAX_NUM] = {NULL};
21
static rectangle_t *r_text               =  NULL;
22

    
23
static void chat_process(const uint8_t *p, const size_t sz){
24
    char buffer2[CHAT_MAX_NUM+3];
25
    void *dest = NULL;
26
    hltp_type tp = hltp_interpret(p, sz, &dest);
27
    switch(tp){
28
        case hltp_type_string:
29
        strcpy(buffer2, dest);
30
        strncat(buffer2, " <", 2);
31
        for(size_t i = CHAT_MAX_NUM-1; i; --i)
32
        text_set_string(t_text[i], text_get_string(t_text[i-1]));
33
        text_set_string(t_text[0], buffer2);
34
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
35
            if(text_get_string(t_text[i])[0] == '>'){
36
                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
37
                text_set_halign(t_text[i], text_halign_left);
38
            }else{
39
                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
40
                text_set_halign(t_text[i], text_halign_right);
41
            }
42
        }
43
        break;
44
        case hltp_type_invalid: break;
45
        case hltp_type_bullet : break;
46
        case hltp_type_host   : break;
47
        case hltp_type_remote : break;
48
    }
49
}
50

    
51
int (chat)(void){
52
    int r;
53

    
54
    nctp_dump();
55
    nctp_set_processor(chat_process);
56

    
57
    struct packet pp;
58

    
59
    char buffer[CHAT_MAX_SIZE] = "";
60
    rectangle_t *r_buffer = NULL; {
61
        r_buffer = rectangle_ctor(0,0,900,70);
62
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
63
        (int16_t)(graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2));
64
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
65
        rectangle_set_outline_width(r_buffer, 2);
66
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
67
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
68
    }
69
    text_t      *t_buffer = NULL; {
70
        t_buffer = text_ctor(font_get_default(), "");
71
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
72
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
73
        text_set_halign(t_buffer, text_halign_left);
74
        text_set_valign(t_buffer, text_valign_center);
75
        text_set_color (t_buffer, TEXT_COLOR);
76
    }
77
    text_t      *t_size   = NULL; {
78
        t_size = text_ctor(font_get_default(), "");
79
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
80
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
81
        text_set_halign(t_size, text_halign_right);
82
        text_set_valign(t_size, text_valign_bottom);
83
        text_set_color (t_size, TEXT_COLOR);
84
        text_set_size  (t_size, 18);
85
        char buffer2[20];
86
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
87
        text_set_string(t_size, buffer2);
88
    }
89

    
90
    /** r_text */ {
91
    r_text = rectangle_ctor(0,0,900,550);
92
    rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
93
    (int16_t)(graph_get_YRes()*0.09));
94
    rectangle_set_fill_color   (r_text, GRAPH_BLACK);
95
    rectangle_set_outline_width(r_text, 2);
96
    rectangle_set_outline_color(r_text, GRAPH_WHITE);
97
    rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
98
    }
99
    /** t_text */ {
100
    for(uint16_t i = 0; i < CHAT_MAX_NUM; ++i){
101
        t_text[i] = text_ctor(font_get_default(), " ");
102
        text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
103
        rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
104
        text_set_halign(t_text[i], text_halign_left);
105
        text_set_valign(t_text[i], text_valign_bottom);
106
        text_set_color (t_text[i], TEXT_COLOR);
107
    }
108
}
109

    
110
/// loop stuff
111
uint64_t int_vector = 0;
112
int good = true;
113
while (good) {
114
    /* Get a request message. */
115
    if((r = get_interrupts_vector(&int_vector))) return r;
116
    uint32_t n = 1;
117
    for (uint8_t i = 0; i < 32; i++, n <<= 1) {
118
        if (int_vector & n) {
119
            interrupt_handler(i);
120
            switch (i) {
121
                case TIMER0_IRQ:
122
                graph_clear_screen();
123
                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
124

    
125
                rectangle_draw(r_buffer);
126
                text_draw(t_buffer);
127
                text_draw(t_size);
128

    
129
                rectangle_draw(r_text);
130
                for(size_t j = 0; j < CHAT_MAX_NUM; ++j) text_draw(t_text[j]);
131

    
132
                sprite_draw(sp_crosshair);
133
                graph_draw();
134
                break;
135
                case KBC_IRQ:
136
                if      (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
137
                else if (keyboard_get_scancode()[0] == ENTER_MAKE_CODE) {
138
                    hltp_send_string(buffer);
139
                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
140
                    strncat(buffer2, buffer, strlen(buffer));
141
                    for(size_t j = CHAT_MAX_NUM-1; j; --j) text_set_string(t_text[i], text_get_string(t_text[i-1]));
142
                    text_set_string(t_text[0], buffer2);
143
                    for(size_t j = 0; j < CHAT_MAX_NUM; ++j){
144
                        if(text_get_string(t_text[j])[0] == '>'){
145
                            text_set_pos(t_text[j], rectangle_get_x(r_text)+50, text_get_y(t_text[j]));
146
                            text_set_halign(t_text[j], text_halign_left);
147
                        }else{
148
                            text_set_pos(t_text[j], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[j]));
149
                            text_set_halign(t_text[j], text_halign_right);
150
                        }
151
                    }
152
                    buffer[0] = '\0';
153
                } else if(keyboard_get_scancode()[0] == BACKSPACE_MAKE_CODE){
154
                    buffer[strlen(buffer)-1] = '\0';
155
                } else {
156
                    char c = map_makecode(keyboard_get_scancode()[0]);
157
                    if (c == ERROR_CODE) break;
158
                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
159
                    else                               printf("Char limit exceeded\n");
160
                }
161
                text_set_string(t_buffer, buffer);
162
                char buffer2[20];
163
                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
164
                text_set_string(t_size, buffer2);
165
                case MOUSE_IRQ:
166
                if (mouse_get_counter_mouse_ih() >= 3) {
167
                    mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
168
                    update_mouse(&pp);
169
                    mouse_set_counter_mouse_ih(0);
170
                }
171
                break;
172
                case COM1_IRQ: nctp_ih(); break;
173
            }
174
        }
175
    }
176
}
177

    
178
rectangle_dtor(r_buffer);
179
text_dtor     (t_buffer);
180

    
181
rectangle_dtor(r_text);
182
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
183

    
184
nctp_set_processor(NULL);
185

    
186
return SUCCESS;
187
}