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