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