Commit 2bb04f01 authored by Cedric Roux's avatar Cedric Roux

first version of the generic gui for the tracer

parent ee6789cd
......@@ -10,11 +10,14 @@ CFLAGS += -DT_USE_SHARED_MEMORY
LIBS += -lrt
PROG=tracer
OBJS=main.o plot.o database.o forward.o
OBJS=main.o plot.o database.o forward.o gui/gui.a
$(PROG): $(OBJS)
$(CC) $(CFLAGS) -o $(PROG) $(OBJS) $(LIBS)
gui/gui.a:
cd gui && make
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
......@@ -22,3 +25,4 @@ main.o: ../T_IDs.h ../T_defs.h
clean:
rm -f *.o $(PROG) core
cd gui && make clean
CC=gcc
CFLAGS=-Wall -g -pthread
OBJS=init.o loop.o toplevel_window.o x.o container.o widget.o \
gui.o label.o event.o xy_plot.o text_list.o
gui.a: $(OBJS)
ar cr gui.a $(OBJS)
test: test.o gui.a
$(CC) -o test $(OBJS) test.o -lX11 -pthread -lm
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
clean:
rm -f *.a *.o test
#include "gui.h"
#include "gui_defs.h"
#include <stdio.h>
#include <stdlib.h>
static void repack(gui *g, widget *_this)
{
printf("REPACK container %p\n", _this);
struct container_widget *this = _this;
this->hints_are_valid = 0;
return this->common.parent->repack(g, this->common.parent);
}
static void add_child(gui *g, widget *this, widget *child, int position)
{
printf("ADD_CHILD container\n");
widget_add_child_internal(g, this, child, position);
}
static void compute_vertical_hints(struct gui *g,
struct container_widget *this)
{
struct widget_list *l;
int cwidth, cheight;
int allocated_width = 0, allocated_height = 0;
/* get largest width */
l = this->common.children;
while (l) {
l->item->hints(g, l->item, &cwidth, &cheight);
if (cwidth > allocated_width) allocated_width = cwidth;
allocated_height += cheight;
l = l->next;
}
this->hint_width = allocated_width;
this->hint_height = allocated_height;
this->hints_are_valid = 1;
}
static void compute_horizontal_hints(struct gui *g,
struct container_widget *this)
{
struct widget_list *l;
int cwidth, cheight;
int allocated_width = 0, allocated_height = 0;
/* get largest height */
l = this->common.children;
while (l) {
l->item->hints(g, l->item, &cwidth, &cheight);
if (cheight > allocated_height) allocated_height = cheight;
allocated_width += cwidth;
l = l->next;
}
this->hint_width = allocated_width;
this->hint_height = allocated_height;
this->hints_are_valid = 1;
}
static void vertical_allocate(gui *_gui, widget *_this,
int x, int y, int width, int height)
{
printf("ALLOCATE container vertical %p\n", _this);
int cy = 0;
int cwidth, cheight;
struct gui *g = _gui;
struct container_widget *this = _this;
struct widget_list *l;
if (this->hints_are_valid == 1) goto hints_ok;
compute_vertical_hints(g, this);
hints_ok:
this->common.x = x;
this->common.y = y;
this->common.width = width;
this->common.height = height;
/* allocate */
l = this->common.children;
while (l) {
l->item->hints(g, l->item, &cwidth, &cheight);
l->item->allocate(g, l->item, this->common.x, this->common.y + cy,
//this->hint_width, cheight);
width, cheight);
cy += cheight;
l = l->next;
}
if (cy != this->hint_height) ERR("reachable?\n");
}
static void horizontal_allocate(gui *_gui, widget *_this,
int x, int y, int width, int height)
{
printf("ALLOCATE container horizontal %p\n", _this);
int cx = 0;
int cwidth, cheight;
struct gui *g = _gui;
struct container_widget *this = _this;
struct widget_list *l;
if (this->hints_are_valid == 1) goto hints_ok;
compute_horizontal_hints(g, this);
hints_ok:
this->common.x = x;
this->common.y = y;
this->common.width = width;
this->common.height = height;
/* allocate */
l = this->common.children;
while (l) {
l->item->hints(g, l->item, &cwidth, &cheight);
l->item->allocate(g, l->item, this->common.x + cx, this->common.y,
cwidth, this->hint_height);
cx += cwidth;
l = l->next;
}
if (cx != this->hint_width) ERR("reachable?\n");
}
static void vertical_hints(gui *_gui, widget *_w, int *width, int *height)
{
printf("HINTS container vertical %p\n", _w);
struct gui *g = _gui;
struct container_widget *this = _w;
if (this->hints_are_valid) {
*width = this->hint_width;
*height = this->hint_height;
return;
}
compute_vertical_hints(g, this);
*width = this->hint_width;
*height = this->hint_height;
}
static void horizontal_hints(gui *_gui, widget *_w, int *width, int *height)
{
printf("HINTS container horizontal %p\n", _w);
struct gui *g = _gui;
struct container_widget *this = _w;
if (this->hints_are_valid) {
*width = this->hint_width;
*height = this->hint_height;
return;
}
compute_horizontal_hints(g, this);
*width = this->hint_width;
*height = this->hint_height;
}
static void paint(gui *_gui, widget *_this)
{
printf("PAINT container\n");
struct gui *g = _gui;
struct widget *this = _this;
struct widget_list *l;
l = this->children;
while (l) {
l->item->paint(g, l->item);
l = l->next;
}
}
widget *new_container(gui *_gui, int vertical)
{
struct gui *g = _gui;
struct container_widget *w;
glock(g);
w = new_widget(g, CONTAINER, sizeof(struct container_widget));
w->vertical = vertical;
w->hints_are_valid = 0;
w->common.paint = paint;
w->common.add_child = add_child;
w->common.repack = repack;
if (vertical) {
w->common.allocate = vertical_allocate;
w->common.hints = vertical_hints;
} else {
w->common.allocate = horizontal_allocate;
w->common.hints = horizontal_hints;
}
gunlock(g);
return w;
}
#include "gui.h"
#include "gui_defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
/*****************************************************************/
/* generic functions */
/*****************************************************************/
static void event_list_append(struct gui *g, struct event *e)
{
struct event_list *new;
new = calloc(1, sizeof(struct event_list));
if (new == NULL) OOM;
new->item = e;
if (g->queued_events == NULL) {
g->queued_events = new;
new->last = new;
return;
}
g->queued_events->last->next = new;
g->queued_events->last = new;
}
static void free_event(struct event *e)
{
switch (e->type) {
case REPACK: /* nothing */ break;
case DIRTY: /* nothing */ break;
}
free(e);
}
/*****************************************************************/
/* sending events */
/*****************************************************************/
static event *new_event_repack(int id)
{
struct repack_event *ret;
ret = calloc(1, sizeof(struct repack_event));
if (ret == NULL) OOM;
ret->id = id;
return ret;
}
static event *new_event_dirty(int id)
{
struct dirty_event *ret;
ret = calloc(1, sizeof(struct dirty_event));
if (ret == NULL) OOM;
ret->id = id;
return ret;
}
void send_event(gui *_gui, enum event_type type, ...)
{
printf("send_event %d\n", type);
struct gui *g = _gui;
int do_write = 0;
va_list ap;
struct event *e;
if (g->queued_events == NULL) do_write = 1;
va_start(ap, type);
switch (type) {
case REPACK: {
int id;
id = va_arg(ap, int);
e = new_event_repack(id);
break;
}
case DIRTY: {
int id;
id = va_arg(ap, int);
e = new_event_dirty(id);
break;
}
}
va_end(ap);
e->type = type;
event_list_append(g, e);
if (do_write) {
char c = 1;
if (write(g->event_pipe[1], &c, 1) != 1)
ERR("error writing to pipe: %s\n", strerror(errno));
}
}
/*****************************************************************/
/* processing events */
/*****************************************************************/
static void repack_event(struct gui *g, int id)
{
struct widget *w = find_widget(g, id);
if (w == NULL) { WARN("widget id %d not found\n", id); return; }
w->repack(g, w);
}
/* TODO: put that function somewhere else? */
static struct toplevel_window_widget *get_toplevel_window(struct widget *w)
{
while (w != NULL) {
if (w->type == TOPLEVEL_WINDOW)
return (struct toplevel_window_widget *)w;
w = w->parent;
}
return NULL;
}
static void dirty_event(struct gui *g, int id)
{
struct widget *w = find_widget(g, id);
struct toplevel_window_widget *win;
if (w == NULL) { WARN("widget id %d not found\n", id); return; }
win = get_toplevel_window(w);
if (win == NULL)
{ WARN("widget id %d not contained in a window\n", id); return; }
g->xwin = win->x;
w->paint(g, w);
g->xwin = NULL;
g->repainted = 1;
}
static void process_event(struct gui *g, struct event *e)
{
printf("processing event type %d\n", e->type);
switch (e->type) {
case REPACK: repack_event(g, ((struct repack_event *)e)->id); break;
case DIRTY: dirty_event(g, ((struct dirty_event *)e)->id); break;
}
}
/* TODO: events' compression */
void gui_events(gui *_gui)
{
struct gui *g = _gui;
printf("gui_events START: head %p\n", g->queued_events);
while (g->queued_events) {
struct event_list *cur = g->queued_events;
g->queued_events = cur->next;
if (g->queued_events) g->queued_events->last = cur->last;
process_event(g, cur->item);
free_event(cur->item);
free(cur);
}
printf("gui_events DONE\n");
}
#include "gui.h"
#include "gui_defs.h"
#include "x.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void glock(gui *_gui)
{
struct gui *g = _gui;
if (pthread_mutex_lock(g->lock)) ERR("mutex error\n");
}
void gunlock(gui *_gui)
{
struct gui *g = _gui;
if (pthread_mutex_unlock(g->lock)) ERR("mutex error\n");
}
int new_color(gui *_gui, char *color)
{
struct gui *g = _gui;
return x_new_color(g->x, color);
}
#ifndef _GUI_H_
#define _GUI_H_
/* defines the public API of the GUI */
typedef void gui;
typedef void widget;
#define HORIZONTAL 0
#define VERTICAL 1
gui *gui_init(void);
/* position = -1 to put at the end */
void widget_add_child(gui *gui, widget *parent, widget *child, int position);
widget *new_toplevel_window(gui *gui, int width, int height, char *title);
widget *new_container(gui *gui, int vertical);
widget *new_label(gui *gui, const char *text);
widget *new_xy_plot(gui *gui, int width, int height, char *label,
int vruler_width);
widget *new_text_list(gui *_gui, int width, int nlines, int background_color);
void xy_plot_set_range(gui *gui, widget *this,
float xmin, float xmax, float ymin, float ymax);
void text_list_add(gui *gui, widget *this, const char *text, int position);
void gui_loop(gui *gui);
void glock(gui *gui);
void gunlock(gui *gui);
int new_color(gui *gui, char *color);
#endif /* _GUI_H_ */
#ifndef _GUI_DEFS_H_
#define _GUI_DEFS_H_
/* defines the private API of the GUI */
/*************************************************************************/
/* logging macros */
/*************************************************************************/
#define ERR(...) \
do { \
printf("%s:%d:%s: ERROR: ", __FILE__, __LINE__, __FUNCTION__); \
printf(__VA_ARGS__); \
abort(); \
} while (0)
#define WARN(...) \
do { \
printf("%s:%d:%s: WARNING: ", __FILE__, __LINE__, __FUNCTION__); \
printf(__VA_ARGS__); \
} while (0)
#define OOM ERR("out of memory\n")
/*************************************************************************/
/* widgets */
/*************************************************************************/
enum widget_type {
TOPLEVEL_WINDOW, CONTAINER, TEXT_LIST, XY_PLOT, BUTTON, LABEL
};
struct widget_list;
struct widget {
enum widget_type type;
int id;
int x; /* allocated x after packing */
int y; /* allocated y after packing */
int width; /* allocated width after packing */
int height; /* allocated height after packing */
struct widget_list *children;
struct widget *parent;
void (*repack)(gui *g, widget *this);
void (*add_child)(gui *g, widget *this, widget *child, int position);
void (*allocate)(gui *g, widget *this, int x, int y, int width, int height);
void (*hints)(gui *g, widget *this, int *width, int *height);
void (*paint)(gui *g, widget *this);
};
struct widget_list {
struct widget *item;
struct widget_list *next;
//struct widget_list *prev; /* unused? */
struct widget_list *last; /* valid only for the head of the list */
};
struct toplevel_window_widget {
struct widget common;
void *x; /* opaque X data (type x_window), used in x.c */
};
struct container_widget {
struct widget common;
int vertical;
int hints_are_valid; /* used to cache hints values */
int hint_width; /* cached hint values - invalid if */
int hint_height; /* repack_was_called == 1 */
};
struct text_list_widget {
struct widget common;
char **text;
int text_count;
int wanted_width;
int wanted_nlines; /* number of lines of text the user wants to see */
int allocated_nlines; /* actual number of visible lines */
int starting_line; /* points to the first visible line of text */
int line_height;
int baseline;
int background_color;
};
struct xy_plot_widget {
struct widget common;
float *x;
float *y;
int npoints;
char *label;
int label_width;
int label_height;
int label_baseline;
int vrule_width; /* the width of the vertical ruler text zone */
float xmin, xmax;
float ymin, ymax;
int wanted_width;
int wanted_height;
};
struct button_widget {
struct widget common;
};
struct label_widget {
struct widget common;
const char *t;
int color;
int width; /* as given by the graphic's backend */
int height; /* as given by the graphic's backend */
int baseline; /* as given by the graphic's backend */
};
/*************************************************************************/
/* events */
/*************************************************************************/
typedef void event;
enum event_type {
DIRTY, REPACK
};
struct event {
enum event_type type;
};
struct event_list {
struct event *item;
struct event_list *next;
struct event_list *last;
};
struct dirty_event {
struct event common;
int id;
};
struct repack_event {
struct event common;
int id;
};
/*************************************************************************/
/* main structure */
/*************************************************************************/
struct gui {
void *lock;
void *x; /* opaque X data (type x_connection), used in x.c */
struct widget_list *toplevel;
struct event_list *queued_events;
int event_pipe[2];
int next_id; /* tells what is the ID of
the next created widget */
int repainted; /* set to 1 when some widget has
* been repainted (TODO: can be any,
* to be optimized) */
void *xwin; /* set by a toplevel_window when
* it paints itself, to be used
* by its children */
};
/*************************************************************************/
/* internal functions */
/*************************************************************************/
widget *new_widget(struct gui *g, enum widget_type type, int size);
void widget_add_child_internal(
gui *_gui, widget *parent, widget *child, int position);
const char *widget_name(enum widget_type type);
void send_event(gui *gui, enum event_type type, ...);
void gui_events(gui *gui);
struct widget *find_widget(struct gui *g, int id);
#endif /* _GUI_DEFS_H_ */
#include "gui.h"
#include "gui_defs.h"
#include "x.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
gui *gui_init(void)
{
struct gui *ret;
ret = calloc(1, sizeof(struct gui));
if (ret == NULL) OOM;
ret->lock = malloc(sizeof(pthread_mutex_t));
if (ret->lock == NULL) OOM;
if (pthread_mutex_init(ret->lock, NULL))
ERR("mutex initialization failed\n");
if (pipe(ret->event_pipe))
ERR("%s\n", strerror(errno));
ret->x = x_open();
return ret;
}
#include "gui.h"
#include "gui_defs.h"
#include "x.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void paint(gui *_gui, widget *_w)
{
struct gui *g = _gui;
struct label_widget *l = _w;
printf("PAINT label '%s'\n", l->t);
x_draw_string(g->x, g->xwin, l->color,
l->common.x, l->common.y + l->baseline, l->t);
}
static void hints(gui *_gui, widget *_w, int *width, int *height)
{
struct label_widget *l = _w;
printf("HINTS label '%s'\n", l->t);
*width = l->width;
*height = l->height;
}
widget *new_label(gui *_gui, const char *label)
{
struct gui *g = _gui;
struct label_widget *w;
glock(g);
w = new_widget(g, LABEL, sizeof(struct label_widget));
w->t = strdup(label);
if (w->t == NULL) OOM;
w->color = FOREGROUND_COLOR;