Index: /neercs/trunk/src/main.c
===================================================================
--- /neercs/trunk/src/main.c	(revision 2399)
+++ /neercs/trunk/src/main.c	(revision 2400)
@@ -49,9 +49,4 @@
     int eof = 0, refresh = 1, command = 0;
 
-    /* Add-ons*/
-    int mini = 1;
-    int status = 1;
-    int help = 0;
-
 
     nt = argc - 1;
@@ -101,5 +96,8 @@
     screen_list->count = 0;
     screen_list->width  = cucul_get_canvas_width(cv);
-    screen_list->height = cucul_get_canvas_height(cv) - ((mini*6) + (status));
+    screen_list->mini = 1;
+    screen_list->help = 0;
+    screen_list->status = 1;
+    screen_list->height = cucul_get_canvas_height(cv) - ((screen_list->mini*6) + (screen_list->status));
     screen_list->wm_type = WM_VSPLIT;
     screen_list->in_bell = 0;
@@ -163,70 +161,13 @@
         }
         else if(ret)
-            for(i = 0; i < screen_list->count; i++)
-            {
-                /* FIXME: try a new strategy: read all filedescriptors until
-                 * each of them starved at least once. */
-
-                if(screen_list->screen[i]->fd < 0 ||
-                   !FD_ISSET(screen_list->screen[i]->fd, &fdset))
-                    continue;
-
-                for(;;)
-                {
-                    ssize_t nr;
-
-                    screen_list->screen[i]->buf =
-                        realloc(screen_list->screen[i]->buf,
-                                screen_list->screen[i]->total + 1024);
-                    nr = read(screen_list->screen[i]->fd,
-                              screen_list->screen[i]->buf +
-                              screen_list->screen[i]->total, 1024);
-
-                    if(nr > 0)
-                    {
-                        screen_list->screen[i]->total += nr;
-                        continue;
-                    }
-
-                    if(nr == 0 || errno != EWOULDBLOCK) {
-                        close(screen_list->screen[i]->fd);
-                        screen_list->screen[i]->fd = -1;
-                        destroy_screen(screen_list->screen[i]);
-                        remove_screen(screen_list, i, 0);
-                        if(i < prevpty) prevpty--;
-                        if(i == pty)
-                        {
-                            pty = prevpty;
-                            prevpty = 0;
-                        }
-                        if(i < pty) pty--;
-                        refresh = 1;
-                    }
-
-                    break;
-                }
-            }
-
+        {
+            /* Read each terminal content */
+            refresh |= update_screens_contents(screen_list, &fdset, &pty, &prevpty);
+        }
+        /* No more screens, exit */
         if(!screen_list->count) break;
 
-        for(i = 0; i < screen_list->count; i++)
-        if(screen_list->screen[i]->total)
-        {
-            unsigned long int bytes;
-
-            bytes = import_term(screen_list,
-                                screen_list->screen[i],
-                                screen_list->screen[i]->buf,
-                                screen_list->screen[i]->total);
-
-            if(bytes > 0)
-            {
-                screen_list->screen[i]->total -= bytes;
-                memmove(screen_list->screen[i]->buf,
-                        screen_list->screen[i]->buf + bytes,
-                        screen_list->screen[i]->total);
-                refresh = 1;
-            }
-        }
+        /* Update each screen canvas  */
+        refresh |= update_terms(screen_list);
 
         /* Get events, if any */
@@ -250,5 +191,5 @@
                 case 'm':
                 case 0x0d: //CACA_KEY_CTRL_M:
-                    mini = !mini;
+                    screen_list->mini = !screen_list->mini;
                     refresh = 1;
                     break;
@@ -289,5 +230,5 @@
                 case 'h':
                 case 0x08: //CACA_KEY_CTRL_H:
-                    help = !help;
+                    screen_list->help = !screen_list->help;
                     refresh = 1;
                     break;
@@ -309,5 +250,5 @@
                     write(screen_list->screen[pty]->fd, "\x1b[D", 3); break;
                 case CACA_KEY_ESCAPE:
-                    if(help) help = 0;
+                    if(screen_list->help) screen_list->help = 0;
                 default:
                     write(screen_list->screen[pty]->fd, &c, 1); break;
@@ -327,100 +268,9 @@
         }
 
-        /* Refresh screen */
+        /* Resfresh screen */
         if(refresh || screen_list->in_bell)
         {
             refresh = 0;
-
-            screen_list->width  = cucul_get_canvas_width(cv);
-            screen_list->height = cucul_get_canvas_height(cv) - (mini*6);
-
-            update_windows_props(cv, screen_list, pty);
-
-            if(screen_list->screen[pty]->title)
-                caca_set_display_title(dp, screen_list->screen[pty]->title);
-            else
-                caca_set_display_title(dp, PACKAGE_STRING);
-
-            cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_DEFAULT);
-            cucul_clear_canvas(cv);
-            cucul_set_color_ansi(cv, CUCUL_LIGHTRED, CUCUL_BLACK);
-
-            for(i = screen_list->count - 1; i >=0; i--)
-            {
-                if(i!=pty)
-                {
-                    cucul_blit(cv,
-                               screen_list->screen[i]->x,
-                               screen_list->screen[i]->y,
-                               screen_list->screen[i]->cv, NULL);
-                    cucul_draw_cp437_box(cv,
-                                         screen_list->screen[i]->x - 1,
-                                         screen_list->screen[i]->y - 1,
-                                         screen_list->screen[i]->w + 2,
-                                         screen_list->screen[i]->h + 2);
-                    if(screen_list->screen[i]->title)
-                        cucul_printf(cv,
-                                     screen_list->screen[i]->x,
-                                     screen_list->screen[i]->y - 1,
-                                     " %.*s ",
-                                     screen_list->screen[i]->w - 3,
-                                     screen_list->screen[i]->title);
-                }
-
-            }
-
-            cucul_blit(cv,
-                       screen_list->screen[pty]->x,
-                       screen_list->screen[pty]->y,
-                       screen_list->screen[pty]->cv, NULL);
-
-            if(screen_list->screen[pty]->bell)
-            {
-                cucul_set_color_ansi(cv, CUCUL_RED, CUCUL_BLACK);
-                screen_list->screen[pty]->bell = 0;
-                screen_list->in_bell--;
-            }
-            else
-            {
-                cucul_set_color_ansi(cv, CUCUL_LIGHTGREEN, CUCUL_BLACK);
-            }
-
-            cucul_draw_cp437_box(cv,
-                                 screen_list->screen[pty]->x - 1,
-                                 screen_list->screen[pty]->y - 1,
-                                 screen_list->screen[pty]->w + 2,
-                                 screen_list->screen[pty]->h + 2);
-
-            if(screen_list->screen[pty]->title)
-            {
-                cucul_printf(cv,
-                             screen_list->screen[pty]->x,
-                             screen_list->screen[pty]->y - 1,
-                             " %.*s ",
-                             screen_list->screen[pty]->w - 3,
-                             screen_list->screen[pty]->title);
-            }
-
-            cucul_gotoxy(cv,
-                         screen_list->screen[pty]->x +
-                         cucul_get_cursor_x(screen_list->screen[pty]->cv),
-                         screen_list->screen[pty]->y +
-                         cucul_get_cursor_y(screen_list->screen[pty]->cv));
-
-
-            if(mini)
-            {
-                draw_thumbnails(cv, screen_list, pty);
-            }
-            if(status)
-            {
-                draw_status(cv, screen_list, pty);
-            }
-            if(help)
-            {
-                draw_help(cv, screen_list, pty);
-            }
-
-            caca_refresh_display(dp);
+            refresh_screens(cv, dp, screen_list,  pty);
         }
 
@@ -499,4 +349,186 @@
 }
 
+
+
+int update_terms(struct screen_list* screen_list)
+{
+    int i, refresh = 0;
+    for(i = 0; i < screen_list->count; i++)
+    {
+        if(screen_list->screen[i]->total)
+        {
+            unsigned long int bytes;
+
+            bytes = import_term(screen_list,
+                                screen_list->screen[i],
+                                screen_list->screen[i]->buf,
+                                screen_list->screen[i]->total);
+
+            if(bytes > 0)
+            {
+                screen_list->screen[i]->total -= bytes;
+                memmove(screen_list->screen[i]->buf,
+                        screen_list->screen[i]->buf + bytes,
+                        screen_list->screen[i]->total);
+                refresh = 1;
+            }
+        }
+    }
+    return refresh;
+}
+
+
+
+
+int update_screens_contents(struct screen_list* screen_list,
+                             fd_set *fdset,
+                             int *pty, int *prevpty)
+{
+    int i, refresh = 0;
+    for(i = 0; i < screen_list->count; i++)
+    {
+        /* FIXME: try a new strategy: read all filedescriptors until
+         * each of them starved at least once. */
+
+        if(screen_list->screen[i]->fd < 0 ||
+           !FD_ISSET(screen_list->screen[i]->fd, fdset))
+            continue;
+
+        for(;;)
+        {
+            ssize_t nr;
+
+            screen_list->screen[i]->buf =
+                realloc(screen_list->screen[i]->buf,
+                        screen_list->screen[i]->total + 1024);
+            nr = read(screen_list->screen[i]->fd,
+                      screen_list->screen[i]->buf +
+                      screen_list->screen[i]->total, 1024);
+
+            if(nr > 0)
+            {
+                screen_list->screen[i]->total += nr;
+                continue;
+            }
+
+            if(nr == 0 || errno != EWOULDBLOCK) {
+                close(screen_list->screen[i]->fd);
+                screen_list->screen[i]->fd = -1;
+                destroy_screen(screen_list->screen[i]);
+                remove_screen(screen_list, i, 0);
+                if(i < (*prevpty)) (*prevpty)--;
+                if(i == *pty)
+                {
+                    *pty = *prevpty;
+                    *prevpty = 0;
+                }
+                if(i < (*pty)) (*pty)--;
+                refresh = 1;
+            }
+
+            break;
+        }
+    }
+    return refresh;
+}
+
+void refresh_screens(cucul_canvas_t *cv,
+                     caca_display_t *dp,
+                     struct screen_list *screen_list,
+                     int pty)
+{
+    int i;
+
+    screen_list->width  = cucul_get_canvas_width(cv);
+    screen_list->height = cucul_get_canvas_height(cv) - (screen_list->mini*6);
+
+    update_windows_props(cv, screen_list, pty);
+
+    if(screen_list->screen[pty]->title)
+        caca_set_display_title(dp, screen_list->screen[pty]->title);
+    else
+        caca_set_display_title(dp, PACKAGE_STRING);
+
+    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_DEFAULT);
+    cucul_clear_canvas(cv);
+    cucul_set_color_ansi(cv, CUCUL_LIGHTRED, CUCUL_BLACK);
+
+    for(i = screen_list->count - 1; i >=0; i--)
+    {
+        if(i!=pty)
+        {
+            cucul_blit(cv,
+                       screen_list->screen[i]->x,
+                       screen_list->screen[i]->y,
+                       screen_list->screen[i]->cv, NULL);
+            cucul_draw_cp437_box(cv,
+                                 screen_list->screen[i]->x - 1,
+                                 screen_list->screen[i]->y - 1,
+                                 screen_list->screen[i]->w + 2,
+                                 screen_list->screen[i]->h + 2);
+            if(screen_list->screen[i]->title)
+                cucul_printf(cv,
+                             screen_list->screen[i]->x,
+                             screen_list->screen[i]->y - 1,
+                             " %.*s ",
+                             screen_list->screen[i]->w - 3,
+                             screen_list->screen[i]->title);
+        }
+    }
+
+    cucul_blit(cv,
+               screen_list->screen[pty]->x,
+               screen_list->screen[pty]->y,
+               screen_list->screen[pty]->cv, NULL);
+
+    if(screen_list->screen[pty]->bell)
+    {
+        cucul_set_color_ansi(cv, CUCUL_RED, CUCUL_BLACK);
+        screen_list->screen[pty]->bell = 0;
+        screen_list->in_bell--;
+    }
+    else
+    {
+        cucul_set_color_ansi(cv, CUCUL_LIGHTGREEN, CUCUL_BLACK);
+    }
+
+    cucul_draw_cp437_box(cv,
+                         screen_list->screen[pty]->x - 1,
+                         screen_list->screen[pty]->y - 1,
+                         screen_list->screen[pty]->w + 2,
+                         screen_list->screen[pty]->h + 2);
+
+    if(screen_list->screen[pty]->title)
+    {
+        cucul_printf(cv,
+                     screen_list->screen[pty]->x,
+                     screen_list->screen[pty]->y - 1,
+                     " %.*s ",
+                     screen_list->screen[pty]->w - 3,
+                     screen_list->screen[pty]->title);
+    }
+
+    cucul_gotoxy(cv,
+                 screen_list->screen[pty]->x +
+                 cucul_get_cursor_x(screen_list->screen[pty]->cv),
+                 screen_list->screen[pty]->y +
+                 cucul_get_cursor_y(screen_list->screen[pty]->cv));
+
+
+    if(screen_list->mini)
+    {
+        draw_thumbnails(cv, screen_list, pty);
+    }
+    if(screen_list->status)
+    {
+        draw_status(cv, screen_list, pty);
+    }
+    if(screen_list->help)
+    {
+        draw_help(cv, screen_list, pty);
+    }
+
+    caca_refresh_display(dp);
+}
 
 
Index: /neercs/trunk/src/neercs.h
===================================================================
--- /neercs/trunk/src/neercs.h	(revision 2399)
+++ /neercs/trunk/src/neercs.h	(revision 2400)
@@ -54,4 +54,9 @@
     int wm_type;
     int in_bell;
+    /* Add-ons*/
+    int mini;
+    int status;
+    int help;
+
     int count;
     int width, height;
@@ -63,5 +68,12 @@
 long int import_term(struct screen_list *screen_list, struct screen *sc, void const *data, unsigned int size);
 int set_tty_size(int fd, unsigned int w, unsigned int h);
-
+int update_terms(struct screen_list* screen_list);
+void refresh_screens(cucul_canvas_t *cv,
+                     caca_display_t *dp,
+                     struct screen_list *screen_list,
+                     int pty);
+int update_screens_contents(struct screen_list* screen_list,
+                             fd_set *fdset,
+                             int *pty, int *prevpty);
 
 /* Screens management */
