about summary refs log tree commit diff stats
path: root/src/ui
diff options
context:
space:
mode:
authorImmae <ismael.bouya@normalesup.org>2014-06-23 00:44:35 +0200
committerJames Booth <boothj5@gmail.com>2014-06-23 21:05:46 +0100
commit6a9e19303eed29d5985d0e8fb00ef48c9d6b25e1 (patch)
treeb506f485265f6481853f4f204a4adab0a482943b /src/ui
parent66ad23b35b3df519e45e2bf46741d8b874a18e74 (diff)
downloadprofani-tty-6a9e19303eed29d5985d0e8fb00ef48c9d6b25e1.tar.gz
Added buffer handling. Buffer are initialized, pushed, and freed. No use is made of them so far
Signed-off-by: James Booth <boothj5@gmail.com>
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/buffer.c106
-rw-r--r--src/ui/buffer.h29
-rw-r--r--src/ui/window.c4
-rw-r--r--src/ui/window.h2
4 files changed, 140 insertions, 1 deletions
diff --git a/src/ui/buffer.c b/src/ui/buffer.c
new file mode 100644
index 00000000..0c0d4d90
--- /dev/null
+++ b/src/ui/buffer.c
@@ -0,0 +1,106 @@
+#include "config.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
+
+#include <glib.h>
+#ifdef HAVE_NCURSESW_NCURSES_H
+#include <ncursesw/ncurses.h>
+#elif HAVE_NCURSES_H
+#include <ncurses.h>
+#endif
+
+#include "ui/window.h"
+#include "ui/buffer.h"
+
+ProfBuff*
+buffer_create() {
+  ProfBuff* new_buff = malloc(sizeof(struct prof_buff_t));
+  new_buff->wrap = 0;
+  new_buff->current = 0;
+  return new_buff;
+}
+
+void
+buffer_free(ProfBuff* buffer) {
+  int i = 0;
+  int imax = buffer->current;
+  if(buffer->wrap == 1) {
+    imax = BUFF_SIZE;
+  }
+  for(i = 0; i < imax; i++) {
+    free(buffer->entry[i].message);
+    free(buffer->entry[i].from);
+  }
+  free(buffer);
+}
+
+void
+buffer_push(ProfBuff* buffer, const char show_char, GTimeVal *tstamp, int flags, int attrs, const char * const from, const char * const message) {
+  int *current = &(buffer->current);
+  ProfBuffEntry e = buffer->entry[*current];
+
+  if(buffer->wrap == 1) {
+    free(e.message);
+    free(e.from);
+  }
+  e.show_char = show_char;
+  e.tstamp = *tstamp;
+  e.flags = flags;
+  e.attrs = attrs;
+
+  e.from = malloc(strlen(from)+1);
+  strcpy(e.from, from);
+
+  e.message = malloc(strlen(message)+1);
+  strcpy(e.message, message);
+
+  *current += 1;
+  if(*current >= BUFF_SIZE) {
+    *current = 0;
+    buffer->wrap = 1;
+  }
+}
+
+int
+buffer_yield(ProfBuff* buffer, int line, ProfBuffEntry** list) {
+  //Returns the (line+1)-th last line
+  //e.g. if line == 0, returns the last line
+  //To correct for splitted lines...
+  int current = buffer->current;
+  int imax = current;
+  if(buffer->wrap == 1) {
+    imax = BUFF_SIZE;
+  }
+  int i = 1;
+  int j = 0;
+  while(i <= imax) {
+    ProfBuffEntry e = buffer->entry[(current - i) % BUFF_SIZE];
+    if(j == line && (e.flags & NO_EOL) == 0) {
+      //We found our line and we are at its last entry
+      int nb_entries = 1;
+      while(i + nb_entries <= imax) {
+        e = buffer->entry[(current - i - nb_entries) % BUFF_SIZE];
+        if((e.flags & NO_EOL) == 0) {
+          break;
+        }
+        nb_entries += 1;
+      }
+      if((buffer->wrap == 1) && (i + nb_entries > imax)) {
+        //The line is at the top of the buffer, we don't know if it's complete
+        return 0;
+      }
+      else {
+        *list = &(buffer->entry[(current - i - nb_entries + 1) % BUFF_SIZE]);
+        return nb_entries;
+      }
+    }
+    if((e.flags & NO_EOL) == 0) {
+      j++;
+    }
+    i++;
+  }
+  return 0;
+}
diff --git a/src/ui/buffer.h b/src/ui/buffer.h
new file mode 100644
index 00000000..65bd8b8d
--- /dev/null
+++ b/src/ui/buffer.h
@@ -0,0 +1,29 @@
+#ifndef UI_BUFFER_H
+#define UI_BUFFER_H
+
+#include "config.h"
+
+//#include "ui/window.h"
+#define BUFF_SIZE 1000
+
+typedef struct prof_buff_entry_t {
+  char show_char;
+  GTimeVal tstamp;
+  int flags;
+  int attrs;
+  char *from;
+  char *message;
+} ProfBuffEntry;
+
+typedef struct prof_buff_t {
+  ProfBuffEntry entry[BUFF_SIZE];
+  int wrap;
+  int current;
+} ProfBuff;
+
+
+ProfBuff* buffer_create();
+void buffer_free(ProfBuff* buffer);
+void buffer_push(ProfBuff* buffer, const char show_char, GTimeVal *tstamp, int flags, int attrs, const char * const from, const char * const message);
+int buffer_yield(ProfBuff* buffer, int line, ProfBuffEntry** list);
+#endif
diff --git a/src/ui/window.c b/src/ui/window.c
index 3f0099b9..4131fa8a 100644
--- a/src/ui/window.c
+++ b/src/ui/window.c
@@ -44,6 +44,7 @@ win_create(const char * const title, int cols, win_type_t type)
     new_win->from = strdup(title);
     new_win->win = newpad(PAD_SIZE, cols);
     wbkgd(new_win->win, COLOUR_TEXT);
+    new_win->buffer = buffer_create();
     new_win->y_pos = 0;
     new_win->x_pos = 0;
     new_win->paged = 0;
@@ -60,6 +61,7 @@ win_create(const char * const title, int cols, win_type_t type)
 void
 win_free(ProfWin* window)
 {
+    buffer_free(window->buffer);
     delwin(window->win);
     free(window->from);
     free(window);
@@ -357,7 +359,7 @@ void win_save_print(ProfWin *window, const char show_char, GTimeVal *tstamp, int
       date_fmt = g_date_time_format(time, "%H:%M:%S");
     }
     g_date_time_unref(time);
-//  buffer_push(window->buffer, time, from, message, attrs);
+    buffer_push(window->buffer, show_char, tstamp, flags, attrs, from, message);
     if((flags & 2) == 0) {
       wattron(window->win, COLOUR_TIME);
       wprintw(window->win, "%s %c ", date_fmt, show_char);
diff --git a/src/ui/window.h b/src/ui/window.h
index 258cefe3..d585036d 100644
--- a/src/ui/window.h
+++ b/src/ui/window.h
@@ -32,6 +32,7 @@
 #endif
 
 #include "contact.h"
+#include "ui/buffer.h"
 
 #define NO_ME   1
 #define NO_EOL  4
@@ -53,6 +54,7 @@ typedef enum {
 typedef struct prof_win_t {
     char *from;
     WINDOW *win;
+    ProfBuff *buffer;
     win_type_t type;
     gboolean is_otr;
     gboolean is_trusted;