diff options
Diffstat (limited to 'testing/toxic/main.c')
-rw-r--r-- | testing/toxic/main.c | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c new file mode 100644 index 00000000..0aad6777 --- /dev/null +++ b/testing/toxic/main.c | |||
@@ -0,0 +1,229 @@ | |||
1 | /* | ||
2 | * Toxic -- Tox Curses Client | ||
3 | */ | ||
4 | |||
5 | #include <curses.h> | ||
6 | #include <stdio.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <stdbool.h> | ||
9 | #include <stdint.h> | ||
10 | |||
11 | #include "../../core/Messenger.h" | ||
12 | #include "../../core/network.h" | ||
13 | |||
14 | #include "windows.h" | ||
15 | |||
16 | extern ToxWindow new_prompt(); | ||
17 | extern int add_req(uint8_t* public_key); // XXX | ||
18 | |||
19 | #define TOXWINDOWS_MAX_NUM 32 | ||
20 | |||
21 | static ToxWindow windows[TOXWINDOWS_MAX_NUM]; | ||
22 | static int w_num; | ||
23 | static int w_active; | ||
24 | static ToxWindow* prompt; | ||
25 | |||
26 | // CALLBACKS START | ||
27 | void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { | ||
28 | int n = add_req(public_key); | ||
29 | |||
30 | wprintw(prompt->window, "\nFriend request.\nUse \"accept %d\" to accept it.\n", n); | ||
31 | } | ||
32 | |||
33 | void on_message(int friendnumber, uint8_t* string, uint16_t length) { | ||
34 | wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string); | ||
35 | } | ||
36 | |||
37 | void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { | ||
38 | wprintw(prompt->window, "\n(nick) %d: %s!\n", friendnumber, string); | ||
39 | } | ||
40 | |||
41 | void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { | ||
42 | wprintw(prompt->window, "\n(status) %d: %s!\n", friendnumber, string); | ||
43 | } | ||
44 | // CALLBACKS END | ||
45 | |||
46 | static void init_term() { | ||
47 | // Setup terminal. | ||
48 | initscr(); | ||
49 | cbreak(); | ||
50 | keypad(stdscr, 1); | ||
51 | noecho(); | ||
52 | timeout(2000); | ||
53 | |||
54 | if(has_colors()) { | ||
55 | start_color(); | ||
56 | init_pair(1, COLOR_GREEN, COLOR_BLACK); | ||
57 | init_pair(2, COLOR_CYAN, COLOR_BLACK); | ||
58 | init_pair(3, COLOR_RED, COLOR_BLACK); | ||
59 | init_pair(4, COLOR_BLUE, COLOR_BLACK); | ||
60 | } | ||
61 | |||
62 | refresh(); | ||
63 | } | ||
64 | |||
65 | static void init_tox() { | ||
66 | // Init core. | ||
67 | initMessenger(); | ||
68 | |||
69 | // Callbacks. | ||
70 | m_callback_friendrequest(on_request); | ||
71 | m_callback_friendmessage(on_message); | ||
72 | m_callback_namechange(on_nickchange); | ||
73 | m_callback_userstatus(on_statuschange); | ||
74 | } | ||
75 | |||
76 | static int add_window(ToxWindow w) { | ||
77 | if(w_num == TOXWINDOWS_MAX_NUM) | ||
78 | return -1; | ||
79 | |||
80 | if(LINES < 2) | ||
81 | return -1; | ||
82 | |||
83 | w.window = newwin(LINES - 2, COLS, 0, 0); | ||
84 | |||
85 | if(w.window == NULL) | ||
86 | return -1; | ||
87 | |||
88 | windows[w_num++] = w; | ||
89 | w.onInit(&w); | ||
90 | |||
91 | return w_num; | ||
92 | } | ||
93 | |||
94 | static void init_windows() { | ||
95 | w_num = 0; | ||
96 | w_active = 0; | ||
97 | |||
98 | if(add_window(new_prompt()) == -1) { | ||
99 | fprintf(stderr, "add_window() failed.\n"); | ||
100 | |||
101 | endwin(); | ||
102 | exit(1); | ||
103 | } | ||
104 | |||
105 | prompt = &windows[0]; | ||
106 | } | ||
107 | |||
108 | static void do_tox() { | ||
109 | static bool dht_on = false; | ||
110 | |||
111 | if(!dht_on && DHT_isconnected()) { | ||
112 | dht_on = true; | ||
113 | wprintw(prompt->window, "\nDHT connected!\n"); | ||
114 | } | ||
115 | else if(dht_on && !DHT_isconnected()) { | ||
116 | dht_on = false; | ||
117 | wprintw(prompt->window, "\nDHT disconnected!\n"); | ||
118 | } | ||
119 | |||
120 | doMessenger(); | ||
121 | } | ||
122 | |||
123 | static void load_data() { | ||
124 | FILE* fd; | ||
125 | size_t len; | ||
126 | uint8_t* buf; | ||
127 | |||
128 | if((fd = fopen("data", "r")) != NULL) { | ||
129 | fseek(fd, 0, SEEK_END); | ||
130 | len = ftell(fd); | ||
131 | fseek(fd, 0, SEEK_SET); | ||
132 | |||
133 | buf = malloc(len); | ||
134 | |||
135 | if(buf == NULL) { | ||
136 | fprintf(stderr, "malloc() failed.\n"); | ||
137 | fclose(fd); | ||
138 | exit(1); | ||
139 | } | ||
140 | |||
141 | if(fread(buf, len, 1, fd) != 1){ | ||
142 | fprintf(stderr, "fread() failed.\n"); | ||
143 | free(buf); | ||
144 | fclose(fd); | ||
145 | exit(1); | ||
146 | } | ||
147 | |||
148 | Messenger_load(buf, len); | ||
149 | } | ||
150 | else { | ||
151 | len = Messenger_size(); | ||
152 | buf = malloc(len); | ||
153 | |||
154 | if(buf == NULL) { | ||
155 | fprintf(stderr, "malloc() failed.\n"); | ||
156 | exit(1); | ||
157 | } | ||
158 | |||
159 | Messenger_save(buf); | ||
160 | |||
161 | fd = fopen("data", "w"); | ||
162 | if(fd == NULL) { | ||
163 | fprintf(stderr, "fopen() failed.\n"); | ||
164 | free(buf); | ||
165 | exit(1); | ||
166 | } | ||
167 | |||
168 | if(fwrite(buf, len, 1, fd) != 1){ | ||
169 | fprintf(stderr, "fwrite() failed.\n"); | ||
170 | free(buf); | ||
171 | fclose(fd); | ||
172 | exit(1); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | free(buf); | ||
177 | fclose(fd); | ||
178 | } | ||
179 | |||
180 | static void draw_bar() { | ||
181 | size_t i; | ||
182 | |||
183 | attron(COLOR_PAIR(4)); | ||
184 | mvhline(LINES - 2, 0, '_', COLS); | ||
185 | attroff(COLOR_PAIR(4)); | ||
186 | |||
187 | move(LINES - 1, 0); | ||
188 | |||
189 | for(i=0; i<w_num; i++) { | ||
190 | if(i == w_active) { | ||
191 | attron(A_BOLD); | ||
192 | } | ||
193 | |||
194 | printw(" %s ", windows[i].title); | ||
195 | |||
196 | if(i == w_active) { | ||
197 | attroff(A_BOLD); | ||
198 | } | ||
199 | } | ||
200 | |||
201 | refresh(); | ||
202 | } | ||
203 | |||
204 | int main(int argc, char* argv[]) { | ||
205 | int ch; | ||
206 | ToxWindow* a; | ||
207 | |||
208 | init_term(); | ||
209 | init_tox(); | ||
210 | load_data(); | ||
211 | init_windows(); | ||
212 | |||
213 | while(true) { | ||
214 | do_tox(); | ||
215 | |||
216 | a = &windows[w_active]; | ||
217 | a->onDraw(a); | ||
218 | draw_bar(); | ||
219 | |||
220 | ch = getch(); | ||
221 | if(ch != ERR) { | ||
222 | a->onKey(a, ch); | ||
223 | } | ||
224 | |||
225 | } | ||
226 | |||
227 | return 0; | ||
228 | } | ||
229 | |||