0
|
1 #include <stdio.h> |
|
2 #include <stdlib.h> |
|
3 #include <string.h> |
|
4 #include <ncurses.h> |
|
5 #include <panel.h> |
|
6 #include <time.h> |
|
7 #include <ctype.h> |
|
8 |
|
9 #include "screen.h" |
|
10 #include "utils.h" |
|
11 #include "buddies.h" |
|
12 #include "parsecfg.h" |
|
13 #include "lang.h" |
|
14 #include "server.h" |
|
15 |
|
16 /* Definicion de tipos */ |
|
17 #define window_entry(n) list_entry(n, window_entry_t, list) |
|
18 |
|
19 LIST_HEAD(window_list); |
|
20 |
|
21 /* Variables globales a SCREEN.C */ |
3
|
22 static WINDOW *rosterWnd, *chatWnd, *inputWnd; |
|
23 static WINDOW *logWnd, *logWnd_border; |
|
24 static PANEL *rosterPanel, *chatPanel, *inputPanel; |
2
|
25 static PANEL *logPanel, *logPanel_border; |
3
|
26 static int maxY, maxX; |
|
27 static window_entry_t *currentWindow; |
0
|
28 |
12
|
29 static int chatmode; |
14
|
30 int update_roaster; |
12
|
31 |
11
|
32 static char inputLine[INPUTLINE_LENGTH]; |
|
33 static char *ptr_inputline; |
1
|
34 |
0
|
35 |
|
36 /* Funciones */ |
|
37 |
|
38 int scr_WindowHeight(WINDOW * win) |
|
39 { |
|
40 int x, y; |
|
41 getmaxyx(win, y, x); |
|
42 return x; |
|
43 } |
|
44 |
1
|
45 void scr_draw_box(WINDOW * win, int y, int x, int height, int width, |
|
46 int Color, chtype box, chtype border) |
0
|
47 { |
|
48 int i, j; |
|
49 |
|
50 wattrset(win, COLOR_PAIR(Color)); |
|
51 for (i = 0; i < height; i++) { |
|
52 wmove(win, y + i, x); |
|
53 for (j = 0; j < width; j++) |
|
54 if (!i && !j) |
|
55 waddch(win, border | ACS_ULCORNER); |
|
56 else if (i == height - 1 && !j) |
|
57 waddch(win, border | ACS_LLCORNER); |
|
58 else if (!i && j == width - 1) |
|
59 waddch(win, box | ACS_URCORNER); |
|
60 else if (i == height - 1 && j == width - 1) |
|
61 waddch(win, box | ACS_LRCORNER); |
|
62 else if (!i) |
|
63 waddch(win, border | ACS_HLINE); |
|
64 else if (i == height - 1) |
|
65 waddch(win, box | ACS_HLINE); |
|
66 else if (!j) |
|
67 waddch(win, border | ACS_VLINE); |
|
68 else if (j == width - 1) |
|
69 waddch(win, box | ACS_VLINE); |
|
70 else |
|
71 waddch(win, box | ' '); |
|
72 } |
|
73 } |
|
74 |
|
75 int FindColor(char *name) |
|
76 { |
|
77 if (!strcmp(name, "default")) |
|
78 return -1; |
|
79 if (!strcmp(name, "black")) |
|
80 return COLOR_BLACK; |
|
81 if (!strcmp(name, "red")) |
|
82 return COLOR_RED; |
|
83 if (!strcmp(name, "green")) |
|
84 return COLOR_GREEN; |
|
85 if (!strcmp(name, "yellow")) |
|
86 return COLOR_YELLOW; |
|
87 if (!strcmp(name, "blue")) |
|
88 return COLOR_BLUE; |
|
89 if (!strcmp(name, "magenta")) |
|
90 return COLOR_MAGENTA; |
|
91 if (!strcmp(name, "cyan")) |
|
92 return COLOR_CYAN; |
|
93 if (!strcmp(name, "white")) |
|
94 return COLOR_WHITE; |
|
95 |
|
96 return -1; |
|
97 } |
|
98 |
|
99 void ParseColors(void) |
|
100 { |
|
101 char *colors[11] = { |
|
102 "", "", |
|
103 "borderlines", |
|
104 "jidonlineselected", |
|
105 "jidonline", |
|
106 "jidofflineselected", |
|
107 "jidoffline", |
|
108 "text", |
|
109 NULL |
|
110 }; |
|
111 |
|
112 char *tmp = malloc(1024); |
|
113 char *color1; |
|
114 char *background = cfg_read("color_background"); |
|
115 char *backselected = cfg_read("color_backselected"); |
|
116 int i = 0; |
|
117 |
|
118 while (colors[i]) { |
|
119 sprintf(tmp, "color_%s", colors[i]); |
|
120 color1 = cfg_read(tmp); |
|
121 |
|
122 switch (i + 1) { |
|
123 case 1: |
|
124 init_pair(1, COLOR_BLACK, COLOR_WHITE); |
|
125 break; |
|
126 case 2: |
|
127 init_pair(2, COLOR_WHITE, COLOR_BLACK); |
|
128 break; |
|
129 case 3: |
|
130 init_pair(3, FindColor(color1), FindColor(background)); |
|
131 break; |
|
132 case 4: |
|
133 init_pair(4, FindColor(color1), FindColor(backselected)); |
|
134 break; |
|
135 case 5: |
|
136 init_pair(5, FindColor(color1), FindColor(background)); |
|
137 break; |
|
138 case 6: |
|
139 init_pair(6, FindColor(color1), FindColor(backselected)); |
|
140 break; |
|
141 case 7: |
|
142 init_pair(7, FindColor(color1), FindColor(background)); |
|
143 break; |
|
144 case 8: |
|
145 init_pair(8, FindColor(color1), FindColor(background)); |
|
146 break; |
|
147 } |
|
148 i++; |
|
149 } |
|
150 } |
|
151 |
|
152 |
|
153 window_entry_t *scr_CreatePanel(char *title, int x, int y, int lines, |
3
|
154 int cols, int dont_show) |
0
|
155 { |
|
156 window_entry_t *tmp = calloc(1, sizeof(window_entry_t)); |
|
157 |
|
158 tmp->win = newwin(lines, cols, y, x); |
|
159 tmp->panel = new_panel(tmp->win); |
|
160 tmp->name = (char *) calloc(1, 1024); |
|
161 strncpy(tmp->name, title, 1024); |
9
|
162 |
0
|
163 scr_draw_box(tmp->win, 0, 0, lines, cols, COLOR_GENERAL, 0, 0); |
11
|
164 //mvwprintw(tmp->win, 0, (cols - (2 + strlen(title))) / 2, " %s ", title); |
|
165 if ((!dont_show)) { |
3
|
166 currentWindow = tmp; |
|
167 } else { |
9
|
168 if (currentWindow) |
|
169 top_panel(currentWindow->panel); |
|
170 else |
|
171 top_panel(chatPanel); |
3
|
172 } |
9
|
173 |
0
|
174 list_add_tail(&tmp->list, &window_list); |
|
175 update_panels(); |
|
176 |
|
177 return tmp; |
|
178 } |
|
179 |
|
180 |
1
|
181 void scr_CreatePopup(char *title, char *texto, int corte, int type, |
|
182 char *returnstring) |
0
|
183 { |
|
184 WINDOW *popupWin; |
|
185 PANEL *popupPanel; |
|
186 |
|
187 int lineas = 0; |
|
188 int cols = 0; |
|
189 |
|
190 char **submsgs; |
|
191 int n = 0; |
|
192 int i; |
|
193 |
|
194 char *instr = (char *) calloc(1, 1024); |
|
195 |
|
196 /* fprintf(stderr, "\r\n%d", lineas); */ |
|
197 |
|
198 submsgs = ut_SplitMessage(texto, &n, corte); |
|
199 |
|
200 switch (type) { |
|
201 case 1: |
|
202 case 0: |
|
203 lineas = n + 4; |
|
204 break; |
|
205 } |
|
206 |
|
207 cols = corte + 3; |
|
208 popupWin = newwin(lineas, cols, (maxY - lineas) / 2, (maxX - cols) / 2); |
|
209 popupPanel = new_panel(popupWin); |
|
210 |
|
211 /*ATENCION!!! Colorear el popup ?? |
|
212 / box (popupWin, 0, 0); */ |
|
213 scr_draw_box(popupWin, 0, 0, lineas, cols, COLOR_POPUP, 0, 0); |
|
214 mvwprintw(popupWin, 0, (cols - (2 + strlen(title))) / 2, " %s ", title); |
|
215 |
|
216 for (i = 0; i < n; i++) |
|
217 mvwprintw(popupWin, i + 1, 2, "%s", submsgs[i]); |
|
218 |
|
219 |
|
220 for (i = 0; i < n; i++) |
|
221 free(submsgs[i]); |
|
222 free(submsgs); |
|
223 |
|
224 switch (type) { |
|
225 case 0: |
|
226 mvwprintw(popupWin, n + 2, |
|
227 (cols - (2 + strlen(i18n("Press any key")))) / 2, |
|
228 i18n("Press any key")); |
|
229 update_panels(); |
|
230 doupdate(); |
|
231 getch(); |
|
232 break; |
|
233 case 1: |
|
234 { |
|
235 char ch; |
|
236 int scroll = 0; |
|
237 int input_x = 0; |
|
238 |
|
239 wmove(popupWin, 3, 1); |
|
240 wrefresh(popupWin); |
|
241 keypad(popupWin, TRUE); |
|
242 while ((ch = getch()) != '\n') { |
|
243 switch (ch) { |
|
244 case 0x09: |
|
245 case KEY_UP: |
|
246 case KEY_DOWN: |
|
247 break; |
|
248 case KEY_RIGHT: |
|
249 case KEY_LEFT: |
|
250 break; |
|
251 case KEY_BACKSPACE: |
|
252 case 127: |
|
253 if (input_x || scroll) { |
|
254 /* wattrset (popupWin, 0); */ |
|
255 if (!input_x) { |
|
256 scroll = scroll < cols - 3 ? 0 : scroll - (cols - 3); |
|
257 wmove(popupWin, 3, 1); |
|
258 for (i = 0; i < cols; i++) |
|
259 waddch |
|
260 (popupWin, |
|
261 instr |
|
262 [scroll |
|
263 + input_x + i] ? instr[scroll + input_x + i] : ' '); |
|
264 input_x = strlen(instr) - scroll; |
|
265 } else |
|
266 input_x--; |
|
267 instr[scroll + input_x] = '\0'; |
|
268 mvwaddch(popupWin, 3, input_x + 1, ' '); |
|
269 wmove(popupWin, 3, input_x + 1); |
|
270 wrefresh(popupWin); |
|
271 } |
|
272 default: |
|
273 if ( /*ch<0x100 && */ isprint(ch) || ch == '�' |
|
274 || ch == '�') { |
|
275 if (scroll + input_x < 1024) { |
|
276 instr[scroll + input_x] = ch; |
|
277 instr[scroll + input_x + 1] = '\0'; |
|
278 if (input_x == cols - 3) { |
|
279 scroll++; |
|
280 wmove(popupWin, 3, 1); |
|
281 for (i = 0; i < cols - 3; i++) |
|
282 waddch(popupWin, instr[scroll + i]); |
|
283 } else { |
|
284 wmove(popupWin, 3, 1 + input_x++); |
|
285 waddch(popupWin, ch); |
|
286 } |
|
287 wrefresh(popupWin); |
|
288 } else { |
|
289 flash(); |
|
290 } |
|
291 } |
|
292 } |
|
293 } |
|
294 } |
|
295 if (returnstring != NULL) |
|
296 strcpy(returnstring, instr); |
|
297 break; |
|
298 } |
|
299 |
|
300 del_panel(popupPanel); |
|
301 delwin(popupWin); |
|
302 update_panels(); |
|
303 doupdate(); |
|
304 free(instr); |
2
|
305 keypad(inputWnd, TRUE); |
0
|
306 } |
|
307 |
|
308 |
|
309 |
|
310 void scr_RoolWindow(void) |
|
311 { |
|
312 } |
|
313 |
3
|
314 window_entry_t *scr_SearchWindow(char *winId) |
0
|
315 { |
|
316 struct list_head *pos, *n; |
|
317 window_entry_t *search_entry = NULL; |
|
318 |
|
319 list_for_each_safe(pos, n, &window_list) { |
|
320 search_entry = window_entry(pos); |
|
321 if (search_entry->name) { |
3
|
322 if (!strcasecmp(search_entry->name, winId)) { |
0
|
323 return search_entry; |
|
324 } |
|
325 } |
|
326 } |
|
327 return NULL; |
|
328 } |
|
329 |
3
|
330 void scr_ShowWindow(char *winId) |
0
|
331 { |
|
332 int n, width, i; |
3
|
333 window_entry_t *tmp = scr_SearchWindow(winId); |
0
|
334 if (tmp != NULL) { |
|
335 top_panel(tmp->panel); |
3
|
336 currentWindow = tmp; |
12
|
337 chatmode = TRUE; |
15
|
338 tmp->hidden_msg = FALSE; |
14
|
339 update_roaster = TRUE; |
0
|
340 width = scr_WindowHeight(tmp->win); |
|
341 for (n = 0; n < tmp->nlines; n++) { |
|
342 mvwprintw(tmp->win, n + 1, 1, ""); |
|
343 for (i = 0; i < width - 2; i++) |
|
344 waddch(tmp->win, ' '); |
|
345 mvwprintw(tmp->win, n + 1, 1, "%s", tmp->texto[n]); |
|
346 } |
9
|
347 //move(CHAT_WIN_HEIGHT - 1, maxX - 1); |
0
|
348 update_panels(); |
|
349 doupdate(); |
11
|
350 } else { |
|
351 top_panel(chatPanel); |
|
352 currentWindow = tmp; |
0
|
353 } |
|
354 } |
|
355 |
|
356 void scr_ShowBuddyWindow(void) |
|
357 { |
|
358 buddy_entry_t *tmp = bud_SelectedInfo(); |
|
359 if (tmp->jid != NULL) |
|
360 scr_ShowWindow(tmp->jid); |
11
|
361 top_panel(inputPanel); |
0
|
362 } |
|
363 |
|
364 |
11
|
365 void scr_WriteInWindow(char *winId, char *texto, int TimeStamp, int force_show) |
0
|
366 { |
|
367 time_t ahora; |
|
368 int n; |
|
369 int i; |
|
370 int width; |
|
371 window_entry_t *tmp; |
9
|
372 int dont_show = FALSE; |
0
|
373 |
3
|
374 |
|
375 tmp = scr_SearchWindow(winId); |
|
376 |
12
|
377 if (!chatmode) |
|
378 dont_show = TRUE; |
|
379 else if ((!force_show) && ((!currentWindow || (currentWindow != tmp)))) |
9
|
380 dont_show = TRUE; |
|
381 scr_LogPrint("dont_show=%d", dont_show); |
3
|
382 |
0
|
383 if (tmp == NULL) { |
3
|
384 tmp = scr_CreatePanel(winId, 20, 0, CHAT_WIN_HEIGHT, maxX - 20, dont_show); |
1
|
385 tmp->texto = (char **) calloc((CHAT_WIN_HEIGHT+1) * 3, sizeof(char *)); |
|
386 for (n = 0; n < CHAT_WIN_HEIGHT * 3; n++) |
0
|
387 tmp->texto[n] = (char *) calloc(1, 1024); |
|
388 |
|
389 if (TimeStamp) { |
|
390 ahora = time(NULL); |
|
391 strftime(tmp->texto[tmp->nlines], 1024, "[%H:%M] ", |
|
392 localtime(&ahora)); |
|
393 strcat(tmp->texto[tmp->nlines], texto); |
|
394 } else { |
|
395 sprintf(tmp->texto[tmp->nlines], " %s", texto); |
|
396 } |
|
397 tmp->nlines++; |
|
398 } else { |
1
|
399 if (tmp->nlines < CHAT_WIN_HEIGHT - 2) { |
0
|
400 if (TimeStamp) { |
|
401 ahora = time(NULL); |
|
402 strftime(tmp->texto[tmp->nlines], 1024, |
|
403 "[%H:%M] ", localtime(&ahora)); |
|
404 strcat(tmp->texto[tmp->nlines], texto); |
|
405 } else { |
|
406 sprintf(tmp->texto[tmp->nlines], " %s", texto); |
|
407 } |
|
408 tmp->nlines++; |
|
409 } else { |
|
410 for (n = 0; n < tmp->nlines; n++) { |
|
411 memset(tmp->texto[n], 0, 1024); |
|
412 strncpy(tmp->texto[n], tmp->texto[n + 1], 1024); |
|
413 } |
|
414 if (TimeStamp) { |
|
415 ahora = time(NULL); |
|
416 strftime(tmp->texto[tmp->nlines - 1], 1024, |
|
417 "[%H:%M] ", localtime(&ahora)); |
|
418 strcat(tmp->texto[tmp->nlines - 1], texto); |
|
419 } else { |
|
420 sprintf(tmp->texto[tmp->nlines - 1], " %s", texto); |
|
421 } |
|
422 } |
|
423 } |
|
424 |
3
|
425 if (!dont_show) { |
|
426 top_panel(tmp->panel); |
|
427 width = scr_WindowHeight(tmp->win); |
|
428 for (n = 0; n < tmp->nlines; n++) { |
|
429 mvwprintw(tmp->win, n + 1, 1, ""); |
|
430 for (i = 0; i < width - 2; i++) |
|
431 waddch(tmp->win, ' '); |
|
432 mvwprintw(tmp->win, n + 1, 1, "%s", tmp->texto[n]); |
|
433 } |
|
434 |
|
435 update_panels(); |
|
436 doupdate(); |
13
|
437 } else { |
15
|
438 tmp->hidden_msg = TRUE; |
14
|
439 update_roaster = TRUE; |
0
|
440 } |
|
441 } |
|
442 |
|
443 void scr_InitCurses(void) |
|
444 { |
|
445 initscr(); |
|
446 noecho(); |
|
447 raw(); |
2
|
448 //cbreak(); |
0
|
449 start_color(); |
|
450 use_default_colors(); |
|
451 |
|
452 ParseColors(); |
|
453 |
|
454 getmaxyx(stdscr, maxY, maxX); |
1
|
455 inputLine[0] = 0; |
|
456 ptr_inputline = inputLine; |
0
|
457 |
|
458 return; |
|
459 } |
|
460 |
|
461 void scr_DrawMainWindow(void) |
|
462 { |
1
|
463 /* Draw main panels */ |
12
|
464 rosterWnd = newwin(CHAT_WIN_HEIGHT, 20, 0, 0); |
0
|
465 rosterPanel = new_panel(rosterWnd); |
12
|
466 scr_draw_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, 20, COLOR_GENERAL, 0, 0); |
0
|
467 mvwprintw(rosterWnd, 0, (20 - strlen(i18n("Roster"))) / 2, |
|
468 i18n("Roster")); |
|
469 |
1
|
470 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - 20, 0, 20); |
0
|
471 chatPanel = new_panel(chatWnd); |
1
|
472 scr_draw_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - 20, COLOR_GENERAL, 0, 0); |
11
|
473 //mvwprintw(chatWnd, 0, |
|
474 // ((maxX - 20) - strlen(i18n("Status Window"))) / 2, |
|
475 // i18n("Status Window")); |
4
|
476 //wbkgd(chatWnd, COLOR_PAIR(COLOR_GENERAL)); |
17
|
477 mvwprintw(chatWnd, 1, 1, "This is the status window"); |
0
|
478 |
12
|
479 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0); |
1
|
480 logPanel_border = new_panel(logWnd_border); |
12
|
481 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0); |
1
|
482 // mvwprintw(logWnd_border, 0, |
|
483 // ((maxX - 20) - strlen(i18n("Log Window"))) / 2, |
|
484 // i18n("Log Window")); |
|
485 //logWnd = newwin(LOG_WIN_HEIGHT - 2, maxX-20 - 2, CHAT_WIN_HEIGHT+1, 20+1); |
12
|
486 logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1); |
1
|
487 logPanel = new_panel(logWnd); |
|
488 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
489 //wattrset(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
490 wprintw(logWnd, "Here we are\n"); |
4
|
491 scr_LogPrint("Here we are :-)"); |
1
|
492 |
|
493 scrollok(logWnd,TRUE); |
|
494 idlok(logWnd,TRUE); // XXX Necessary? |
|
495 |
|
496 inputWnd = newwin(1, maxX, maxY-1, 0); |
|
497 inputPanel = new_panel(inputWnd); |
|
498 //wbkgd(inputWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
499 |
0
|
500 bud_DrawRoster(rosterWnd); |
|
501 update_panels(); |
|
502 doupdate(); |
|
503 return; |
|
504 } |
|
505 |
|
506 void scr_TerminateCurses(void) |
|
507 { |
|
508 clear(); |
|
509 refresh(); |
|
510 endwin(); |
|
511 return; |
|
512 } |
|
513 |
|
514 void scr_WriteIncomingMessage(char *jidfrom, char *text) |
|
515 { |
|
516 char **submsgs; |
|
517 int n, i; |
|
518 char *buffer = (char *) malloc(5 + strlen(text)); |
|
519 |
17
|
520 sprintf(buffer, "<-- %s", text); |
0
|
521 |
|
522 submsgs = |
|
523 ut_SplitMessage(buffer, &n, maxX - scr_WindowHeight(rosterWnd) - 20); |
|
524 |
|
525 for (i = 0; i < n; i++) { |
|
526 if (i == 0) |
11
|
527 scr_WriteInWindow(jidfrom, submsgs[i], TRUE, FALSE); |
0
|
528 else |
11
|
529 scr_WriteInWindow(jidfrom, submsgs[i], FALSE, FALSE); |
0
|
530 } |
|
531 |
|
532 for (i = 0; i < n; i++) |
|
533 free(submsgs[i]); |
|
534 |
|
535 free(submsgs); |
|
536 free(buffer); |
|
537 |
2
|
538 top_panel(inputPanel); |
|
539 //wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine); |
|
540 update_panels(); |
|
541 doupdate(); |
0
|
542 } |
|
543 |
|
544 void scr_WriteMessage(int sock) |
|
545 { |
|
546 char **submsgs; |
|
547 int n, i; |
|
548 char *buffer = (char *) calloc(1, 1024); |
|
549 char *buffer2 = (char *) calloc(1, 1024); |
|
550 buddy_entry_t *tmp = bud_SelectedInfo(); |
|
551 |
|
552 scr_ShowWindow(tmp->jid); |
|
553 |
|
554 ut_CenterMessage(i18n("write your message here"), 60, buffer2); |
|
555 |
|
556 scr_CreatePopup(tmp->jid, buffer2, 60, 1, buffer); |
|
557 |
|
558 if (strlen(buffer)) { |
17
|
559 sprintf(buffer2, "--> %s", buffer); |
0
|
560 |
|
561 submsgs = |
|
562 ut_SplitMessage(buffer2, &n, |
|
563 maxX - scr_WindowHeight(rosterWnd) - 20); |
|
564 for (i = 0; i < n; i++) { |
|
565 if (i == 0) |
11
|
566 scr_WriteInWindow(tmp->jid, submsgs[i], TRUE, TRUE); |
0
|
567 else |
11
|
568 scr_WriteInWindow(tmp->jid, submsgs[i], FALSE, TRUE); |
0
|
569 } |
|
570 |
|
571 for (i = 0; i < n; i++) |
|
572 free(submsgs[i]); |
|
573 free(submsgs); |
|
574 |
1
|
575 move(CHAT_WIN_HEIGHT - 1, maxX - 1); |
0
|
576 refresh(); |
|
577 sprintf(buffer2, "%s@%s/%s", cfg_read("username"), |
|
578 cfg_read("server"), cfg_read("resource")); |
|
579 srv_sendtext(sock, tmp->jid, buffer, buffer2); |
|
580 } |
|
581 free(buffer); |
|
582 free(buffer2); |
|
583 } |
|
584 |
|
585 int scr_Getch(void) |
|
586 { |
|
587 int ch; |
2
|
588 // keypad(inputWnd, TRUE); |
|
589 ch = wgetch(inputWnd); |
0
|
590 return ch; |
|
591 } |
|
592 |
|
593 WINDOW *scr_GetRosterWindow(void) |
|
594 { |
|
595 return rosterWnd; |
|
596 } |
|
597 |
|
598 WINDOW *scr_GetStatusWindow(void) |
|
599 { |
|
600 return chatWnd; |
|
601 } |
1
|
602 |
2
|
603 WINDOW *scr_GetInputWindow(void) |
|
604 { |
|
605 return inputWnd; |
|
606 } |
|
607 |
4
|
608 void scr_LogPrint(const char *fmt, ...) |
|
609 { |
|
610 time_t timestamp; |
|
611 char *buffer; |
|
612 va_list ap; |
|
613 |
|
614 buffer = (char *) calloc(1, 4096); |
|
615 |
|
616 timestamp = time(NULL); |
|
617 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
618 wprintw(logWnd, "\n%s", buffer); |
|
619 |
|
620 va_start(ap, fmt); |
|
621 vsnprintf(buffer, 4096, fmt, ap); |
|
622 va_end(ap); |
|
623 |
|
624 wprintw(logWnd, "%s", buffer); |
|
625 free(buffer); |
11
|
626 |
|
627 update_panels(); |
|
628 doupdate(); |
4
|
629 } |
|
630 |
|
631 |
2
|
632 void send_message(int sock, char *msg) |
|
633 { |
|
634 char **submsgs; |
|
635 char *buffer = (char *) calloc(1, 24+strlen(msg)); |
|
636 char *buffer2 = (char *) calloc(1, 1024); |
|
637 int n, i; |
|
638 buddy_entry_t *tmp = bud_SelectedInfo(); |
|
639 |
|
640 scr_ShowWindow(tmp->jid); |
|
641 |
17
|
642 sprintf(buffer, "--> %s", msg); |
2
|
643 |
|
644 submsgs = |
|
645 ut_SplitMessage(buffer, &n, |
|
646 maxX - scr_WindowHeight(rosterWnd) - 20); |
|
647 for (i = 0; i < n; i++) { |
|
648 if (i == 0) |
11
|
649 scr_WriteInWindow(tmp->jid, submsgs[i], TRUE, TRUE); |
2
|
650 else |
11
|
651 scr_WriteInWindow(tmp->jid, submsgs[i], FALSE, TRUE); |
2
|
652 } |
|
653 |
|
654 for (i = 0; i < n; i++) |
|
655 free(submsgs[i]); |
|
656 free(submsgs); |
|
657 |
|
658 //move(CHAT_WIN_HEIGHT - 1, maxX - 1); |
|
659 refresh(); |
|
660 sprintf(buffer2, "%s@%s/%s", cfg_read("username"), |
|
661 cfg_read("server"), cfg_read("resource")); |
3
|
662 srv_sendtext(sock, tmp->jid, msg, buffer2); |
2
|
663 free(buffer); |
|
664 free(buffer2); |
|
665 |
|
666 top_panel(inputPanel); |
|
667 } |
|
668 |
|
669 int process_line(char *line, int sock) |
|
670 { |
|
671 if (*line != '/') { |
|
672 send_message(sock, line); |
|
673 return 0; |
|
674 } |
|
675 if (!strcasecmp(line, "/quit")) { |
|
676 return 255; |
|
677 } |
|
678 // Commands handling |
|
679 // TODO |
|
680 // say... |
4
|
681 |
|
682 scr_LogPrint("Unrecognised command, sorry."); |
2
|
683 return 0; |
|
684 } |
|
685 |
|
686 int process_key(int key, int sock) |
1
|
687 { |
|
688 if (isprint(key)) { |
|
689 char tmpLine[INPUTLINE_LENGTH]; |
|
690 strcpy(tmpLine, ptr_inputline); |
|
691 *ptr_inputline++ = key; |
|
692 strcpy(ptr_inputline, tmpLine); |
|
693 } else { |
|
694 switch(key) { |
|
695 case KEY_BACKSPACE: |
|
696 if (ptr_inputline != (char*)&inputLine) { |
|
697 *--ptr_inputline = 0; |
|
698 } |
|
699 break; |
2
|
700 case KEY_DC: |
|
701 if (*ptr_inputline) |
|
702 strcpy(ptr_inputline, ptr_inputline+1); |
|
703 break; |
1
|
704 case KEY_LEFT: |
|
705 if (ptr_inputline != (char*)&inputLine) { |
|
706 ptr_inputline--; |
|
707 } |
|
708 break; |
|
709 case KEY_RIGHT: |
|
710 if (*ptr_inputline) |
|
711 ptr_inputline++; |
|
712 break; |
|
713 case 9: // Tab |
4
|
714 scr_LogPrint("I'm unable to complete yet"); |
1
|
715 break; |
|
716 case '\n': // Enter |
|
717 // XXX Test: |
12
|
718 chatmode = TRUE; |
|
719 if (inputLine[0] == 0) { |
|
720 scr_ShowBuddyWindow(); |
|
721 break; |
|
722 } |
2
|
723 if (process_line(inputLine, sock)) |
1
|
724 return 255; |
|
725 ptr_inputline = inputLine; |
|
726 *ptr_inputline = 0; |
|
727 break; |
|
728 case KEY_UP: |
|
729 bud_RosterUp(); |
17
|
730 if (chatmode) |
|
731 scr_ShowBuddyWindow(); |
1
|
732 break; |
|
733 case KEY_DOWN: |
|
734 bud_RosterDown(); |
17
|
735 if (chatmode) |
|
736 scr_ShowBuddyWindow(); |
1
|
737 break; |
2
|
738 case KEY_PPAGE: |
4
|
739 scr_LogPrint("PageUp??"); |
2
|
740 break; |
|
741 case KEY_NPAGE: |
4
|
742 scr_LogPrint("PageDown??"); |
2
|
743 break; |
|
744 case KEY_HOME: |
|
745 case 1: |
|
746 ptr_inputline = inputLine; |
|
747 break; |
|
748 case KEY_END: |
|
749 case 5: |
|
750 for (; *ptr_inputline; ptr_inputline++) ; |
|
751 break; |
12
|
752 case 21: // Ctrl-u |
2
|
753 strcpy(inputLine, ptr_inputline); |
|
754 ptr_inputline = inputLine; |
|
755 break; |
|
756 case KEY_EOL: |
12
|
757 case 11: // Ctrl-k |
2
|
758 *ptr_inputline = 0; |
|
759 break; |
12
|
760 case 27: // ESC |
|
761 currentWindow = NULL; |
|
762 chatmode = FALSE; |
|
763 top_panel(chatPanel); |
|
764 top_panel(inputPanel); |
|
765 break; |
2
|
766 default: |
4
|
767 scr_LogPrint("Unkown key=%o", key); |
1
|
768 } |
4
|
769 //scr_LogPrint("[%02x]", key); |
1
|
770 } |
|
771 mvwprintw(inputWnd, 0,0, "%s", inputLine); |
|
772 wclrtoeol(inputWnd); |
|
773 if (*ptr_inputline) { |
|
774 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine); |
|
775 } |
|
776 update_panels(); |
|
777 doupdate(); |
|
778 return 0; |
|
779 } |