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