first version of textlog with GUI

introducing generic "view" with two instantiations: stdout and textlist
This commit is contained in:
Cedric Roux
2016-04-28 15:02:01 +02:00
parent 29859bb930
commit ca945b53de
10 changed files with 326 additions and 11 deletions
+13
View File
@@ -0,0 +1,13 @@
CC=gcc
CFLAGS=-Wall -g -pthread -I..
OBJS=stdout.o textlist.o
view.a: $(OBJS)
ar cr view.a $(OBJS)
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
clean:
rm -f *.a *.o
+35
View File
@@ -0,0 +1,35 @@
#include "view.h"
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
struct stdout {
view common;
pthread_mutex_t lock;
};
static void clear(view *this)
{
/* do nothing */
}
static void append(view *_this, char *s)
{
struct stdout *this = (struct stdout *)_this;
if (pthread_mutex_lock(&this->lock)) abort();
printf("%s\n", s);
if (pthread_mutex_unlock(&this->lock)) abort();
}
view *new_stdout(void)
{
struct stdout *ret = calloc(1, sizeof(struct stdout));
if (ret == NULL) abort();
ret->common.clear = clear;
ret->common.append = (void (*)(view *, ...))append;
if (pthread_mutex_init(&ret->lock, NULL)) abort();
return (view *)ret;
}
+85
View File
@@ -0,0 +1,85 @@
#include "view.h"
#include "../utils.h"
#include "gui/gui.h"
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
struct textlist {
view common;
gui *g;
widget *w;
int maxsize;
int cursize;
float refresh_rate;
int autoscroll;
pthread_mutex_t lock;
list * volatile to_append;
};
static void _append(struct textlist *this, char *s)
{
if (this->cursize == this->maxsize) {
text_list_del(this->g, this->w, 0);
this->cursize--;
}
text_list_add(this->g, this->w, s, -1);
this->cursize++;
}
static void *textlist_thread(void *_this)
{
struct textlist *this = _this;
while (1) {
if (pthread_mutex_lock(&this->lock)) abort();
while (this->to_append != NULL) {
char *s = this->to_append->data;
this->to_append = list_remove_head(this->to_append);
_append(this, s);
free(s);
}
if (pthread_mutex_unlock(&this->lock)) abort();
sleepms(1000/this->refresh_rate);
}
return 0;
}
static void clear(view *this)
{
/* TODO */
}
static void append(view *_this, char *s)
{
struct textlist *this = (struct textlist *)_this;
char *dup;
if (pthread_mutex_lock(&this->lock)) abort();
dup = strdup(s); if (dup == NULL) abort();
this->to_append = list_append(this->to_append, dup);
if (pthread_mutex_unlock(&this->lock)) abort();
}
view *new_textlist(int maxsize, float refresh_rate, gui *g, widget *w)
{
struct textlist *ret = calloc(1, sizeof(struct textlist));
if (ret == NULL) abort();
ret->common.clear = clear;
ret->common.append = (void (*)(view *, ...))append;
ret->cursize = 0;
ret->maxsize = maxsize;
ret->refresh_rate = refresh_rate;
ret->g = g;
ret->w = w;
ret->autoscroll = 1;
if (pthread_mutex_init(&ret->lock, NULL)) abort();
new_thread(textlist_thread, ret);
return (view *)ret;
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef _VIEW_H_
#define _VIEW_H_
#include "gui/gui.h"
/* defines the public API of views */
typedef struct view {
void (*clear)(struct view *this);
void (*append)(struct view *this, ...);
} view;
view *new_stdout(void);
view *new_textlist(int maxsize, float refresh_rate, gui *g, widget *w);
#endif /* _VIEW_H_ */