diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-23 06:50:42 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-23 06:50:42 -0400 |
commit | bcb87139a2d8c5b308cfb82635e9483e867c9dbc (patch) | |
tree | 822a967fe0a795b605f1b97d5cd063d5b4b7179d /testing/toxic/windows.c | |
parent | 733066a57e8d5403c91475fb6b095f44a81bf270 (diff) | |
parent | 4d89d63c3f1489e248261983f6c73a8b083b3336 (diff) |
Merge branch 'master' into new-api
Diffstat (limited to 'testing/toxic/windows.c')
-rw-r--r-- | testing/toxic/windows.c | 247 |
1 files changed, 0 insertions, 247 deletions
diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c deleted file mode 100644 index 7f547d0a..00000000 --- a/testing/toxic/windows.c +++ /dev/null | |||
@@ -1,247 +0,0 @@ | |||
1 | #include "friendlist.h" | ||
2 | #include "prompt.h" | ||
3 | #include "dhtstatus.h" | ||
4 | #include "windows.h" | ||
5 | |||
6 | extern char *DATA_FILE; | ||
7 | extern int store_data(Messenger *m, char *path); | ||
8 | |||
9 | static ToxWindow windows[MAX_WINDOWS_NUM]; | ||
10 | static ToxWindow *active_window; | ||
11 | static ToxWindow *prompt; | ||
12 | static Messenger *m; | ||
13 | |||
14 | /* CALLBACKS START */ | ||
15 | void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) | ||
16 | { | ||
17 | int n = add_req(public_key); | ||
18 | wprintw(prompt->window, "\nFriend request from:\n"); | ||
19 | |||
20 | int i; | ||
21 | |||
22 | for (i = 0; i < KEY_SIZE_BYTES; ++i) { | ||
23 | wprintw(prompt->window, "%02x", public_key[i] & 0xff); | ||
24 | } | ||
25 | |||
26 | wprintw(prompt->window, "\nWith the message: %s\n", data); | ||
27 | wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); | ||
28 | |||
29 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { | ||
30 | if (windows[i].onFriendRequest != NULL) | ||
31 | windows[i].onFriendRequest(&windows[i], public_key, data, length); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | ||
36 | { | ||
37 | int i; | ||
38 | |||
39 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { | ||
40 | if (windows[i].onMessage != NULL) | ||
41 | windows[i].onMessage(&windows[i], m, friendnumber, string, length); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | ||
46 | { | ||
47 | int i; | ||
48 | |||
49 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { | ||
50 | if (windows[i].onAction != NULL) | ||
51 | windows[i].onAction(&windows[i], m, friendnumber, string, length); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | ||
56 | { | ||
57 | wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); | ||
58 | int i; | ||
59 | |||
60 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { | ||
61 | if (windows[i].onNickChange != NULL) | ||
62 | windows[i].onNickChange(&windows[i], friendnumber, string, length); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | ||
67 | { | ||
68 | wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); | ||
69 | int i; | ||
70 | |||
71 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { | ||
72 | if (windows[i].onStatusChange != NULL) | ||
73 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | void on_friendadded(Messenger *m, int friendnumber) | ||
78 | { | ||
79 | friendlist_onFriendAdded(m, friendnumber); | ||
80 | |||
81 | if (store_data(m, DATA_FILE)) { | ||
82 | wprintw(prompt->window, "\nCould not store Messenger data\n"); | ||
83 | } | ||
84 | } | ||
85 | /* CALLBACKS END */ | ||
86 | |||
87 | int add_window(Messenger *m, ToxWindow w) | ||
88 | { | ||
89 | if (LINES < 2) | ||
90 | return -1; | ||
91 | |||
92 | int i; | ||
93 | |||
94 | for (i = 0; i < MAX_WINDOWS_NUM; i++) { | ||
95 | if (windows[i].window) | ||
96 | continue; | ||
97 | |||
98 | w.window = newwin(LINES - 2, COLS, 0, 0); | ||
99 | |||
100 | if (w.window == NULL) | ||
101 | return -1; | ||
102 | |||
103 | windows[i] = w; | ||
104 | w.onInit(&w, m); | ||
105 | |||
106 | active_window = windows + i; | ||
107 | return i; | ||
108 | } | ||
109 | |||
110 | return -1; | ||
111 | } | ||
112 | |||
113 | /* Deletes window w and cleans up */ | ||
114 | void del_window(ToxWindow *w) | ||
115 | { | ||
116 | active_window = windows; // Go to prompt screen | ||
117 | delwin(w->window); | ||
118 | |||
119 | if (w->x) | ||
120 | free(w->x); | ||
121 | |||
122 | w->window = NULL; | ||
123 | memset(w, 0, sizeof(ToxWindow)); | ||
124 | clear(); | ||
125 | refresh(); | ||
126 | } | ||
127 | |||
128 | /* Shows next window when tab or back-tab is pressed */ | ||
129 | void set_next_window(int ch) | ||
130 | { | ||
131 | ToxWindow *end = windows + MAX_WINDOWS_NUM - 1; | ||
132 | ToxWindow *inf = active_window; | ||
133 | |||
134 | while (true) { | ||
135 | if (ch == '\t') { | ||
136 | if (++active_window > end) | ||
137 | active_window = windows; | ||
138 | } else if (--active_window < windows) | ||
139 | active_window = end; | ||
140 | |||
141 | if (active_window->window) | ||
142 | return; | ||
143 | |||
144 | if (active_window == inf) { // infinite loop check | ||
145 | endwin(); | ||
146 | exit(2); | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | |||
151 | void set_active_window(int index) | ||
152 | { | ||
153 | if (index < 0 || index >= MAX_WINDOWS_NUM) | ||
154 | return; | ||
155 | |||
156 | active_window = windows + index; | ||
157 | } | ||
158 | |||
159 | ToxWindow *init_windows() | ||
160 | { | ||
161 | int n_prompt = add_window(m, new_prompt()); | ||
162 | |||
163 | if (n_prompt == -1 | ||
164 | || add_window(m, new_friendlist()) == -1 | ||
165 | || add_window(m, new_dhtstatus()) == -1) { | ||
166 | fprintf(stderr, "add_window() failed.\n"); | ||
167 | endwin(); | ||
168 | exit(1); | ||
169 | } | ||
170 | |||
171 | prompt = &windows[n_prompt]; | ||
172 | active_window = prompt; | ||
173 | |||
174 | return prompt; | ||
175 | } | ||
176 | |||
177 | static void draw_bar() | ||
178 | { | ||
179 | static int odd = 0; | ||
180 | int blinkrate = 30; | ||
181 | |||
182 | attron(COLOR_PAIR(4)); | ||
183 | mvhline(LINES - 2, 0, '_', COLS); | ||
184 | attroff(COLOR_PAIR(4)); | ||
185 | |||
186 | move(LINES - 1, 0); | ||
187 | |||
188 | attron(COLOR_PAIR(4) | A_BOLD); | ||
189 | printw(" TOXIC " TOXICVER "|"); | ||
190 | attroff(COLOR_PAIR(4) | A_BOLD); | ||
191 | |||
192 | int i; | ||
193 | |||
194 | for (i = 0; i < (MAX_WINDOWS_NUM); ++i) { | ||
195 | if (windows[i].window) { | ||
196 | if (windows + i == active_window) | ||
197 | attron(A_BOLD); | ||
198 | |||
199 | odd = (odd + 1) % blinkrate; | ||
200 | |||
201 | if (windows[i].blink && (odd < (blinkrate / 2))) | ||
202 | attron(COLOR_PAIR(3)); | ||
203 | |||
204 | clrtoeol(); | ||
205 | printw(" %s", windows[i].title); | ||
206 | |||
207 | if (windows[i].blink && (odd < (blinkrate / 2))) | ||
208 | attroff(COLOR_PAIR(3)); | ||
209 | |||
210 | if (windows + i == active_window) { | ||
211 | attroff(A_BOLD); | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | |||
216 | refresh(); | ||
217 | } | ||
218 | |||
219 | void prepare_window(WINDOW *w) | ||
220 | { | ||
221 | mvwin(w, 0, 0); | ||
222 | wresize(w, LINES - 2, COLS); | ||
223 | } | ||
224 | |||
225 | void draw_active_window(Messenger *m) | ||
226 | { | ||
227 | |||
228 | ToxWindow *a = active_window; | ||
229 | wint_t ch = 0; | ||
230 | |||
231 | prepare_window(a->window); | ||
232 | a->blink = false; | ||
233 | draw_bar(); | ||
234 | a->onDraw(a, m); | ||
235 | |||
236 | /* Handle input */ | ||
237 | #ifdef HAVE_WIDECHAR | ||
238 | get_wch(&ch); | ||
239 | #else | ||
240 | ch = getch(); | ||
241 | #endif | ||
242 | |||
243 | if (ch == '\t' || ch == KEY_BTAB) | ||
244 | set_next_window((int) ch); | ||
245 | else if (ch != ERR) | ||
246 | a->onKey(a, m, ch); | ||
247 | } | ||