From ad29d2b009d3f4625a7f281d08de0f0f2154ccb6 Mon Sep 17 00:00:00 2001 From: khamidou Date: Mon, 30 Mar 2009 22:45:50 +0200 Subject: [PATCH] screen modifications --- Makefile | 5 ++++- buffer.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- buffer.h | 6 +++++- main.c | 4 ++++ screen.c | 33 ++++++++++++++++++++++++++++++- screen.h | 3 ++- 6 files changed, 106 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 68c67c0..25b9103 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-c +CFLAGS=-c -g LDFLAGS=-lcurses OBJS=main.o errors.o screen.o buffer.o @@ -18,3 +18,6 @@ screen.o: screen.c buffer.o: buffer.c $(CC) $(CFLAGS) buffer.c +clean: $(OBJS) + rm $(OBJS) + rm te \ No newline at end of file diff --git a/buffer.c b/buffer.c index 686e5bf..4e2bb30 100644 --- a/buffer.c +++ b/buffer.c @@ -4,7 +4,12 @@ #include "error.h" #include "buffer.h" -struct te_char *alloc_and_insert_char(struct te_buffer *buf) +void init_buffers(void) +{ + TAILQ_INIT(&buffers_head); +} + +struct te_char *alloc_and_insert_char(struct te_buffer *buf, char contents) { if (buf == NULL) fail("The buffer parameter to alloc_and_insert_char is NULL"); @@ -16,6 +21,7 @@ struct te_char *alloc_and_insert_char(struct te_buffer *buf) TAILQ_INSERT_TAIL(&buf->chars_head, c, chars); + c->contents = contents; return c; } @@ -26,6 +32,7 @@ struct te_buffer *alloc_and_insert_buffer(void) if (b == NULL) fail("Unable to allocate memory for te_buffer struct"); + TAILQ_INIT(&b->chars_head); TAILQ_INSERT_TAIL(&buffers_head, b, buffers); return b; @@ -50,3 +57,54 @@ void free_buffer(struct te_buffer *b) free(b); } + + +struct te_buffer* load_buffer(char *filename) +{ + FILE *fp = fopen(filename, "r"); + + if (fp == NULL) + fail("Unable to open %s", filename); + + struct te_buffer *b = alloc_and_insert_buffer(); + + char c = getc(fp); + + while(!feof(fp)) { + alloc_and_insert_char(b, c); + c = getc(fp); + } + + return b; +} + +void scroll_up(struct te_buffer *buf) +{ + +} + +void scroll_down(struct te_buffer *buf) +{ + +} + +void move_left(struct te_buffer *buf) +{ + +} + +void move_right(struct te_buffer *buf) +{ + +} + +void move_up(struct te_buffer *buf) +{ + +} + +void move_down(struct te_buffer *buf) +{ + +} + diff --git a/buffer.h b/buffer.h index bbba875..47248ee 100644 --- a/buffer.h +++ b/buffer.h @@ -1,6 +1,7 @@ #ifndef __BUFFER_H__ #define __BUFFER_H__ +#include #include "queue.h" struct te_char { @@ -11,16 +12,19 @@ struct te_char { struct te_buffer { char *name; int dirty; + int x, y; + struct te_char *point; TAILQ_HEAD(te_chars_head, te_char) chars_head; TAILQ_ENTRY(te_buffer) buffers; }; TAILQ_HEAD(te_buffers_head, te_buffer) buffers_head; -struct te_char *alloc_and_insert_char(struct te_buffer *buf); +struct te_char *alloc_and_insert_char(struct te_buffer *buf, char contents); struct te_buffer *alloc_and_insert_buffer(void); void free_char(struct te_buffer *b, struct te_char *c); void free_buffer(struct te_buffer *b); +struct te_buffer* load_buffer(char *filename); void init_buffers(void); void scroll_up(struct te_buffer *buf); diff --git a/main.c b/main.c index c212b68..9065c17 100644 --- a/main.c +++ b/main.c @@ -8,8 +8,12 @@ int main(int argc, char **argv) { + init_buffers(); init_windows(); + struct te_buffer *b = load_buffer("screen.c"); + paint_buffer(b); miniprintf("Over the top!"); + statusprintf("Reverse video !"); while(1) { } diff --git a/screen.c b/screen.c index 68c2b97..5135c70 100644 --- a/screen.c +++ b/screen.c @@ -5,9 +5,14 @@ void init_windows(void) initscr(); cbreak(); noecho(); + scrollok(stdscr, FALSE); keypad(stdscr, TRUE); - buffer_win = subwin(stdscr, LINES - 2, COLS - 2, 0, 0); + buffer_win = subwin(stdscr, LINES - 2, COLS, 0, 0); + + status_win = subwin(stdscr, 1, COLS, LINES - 2, 0); + wattron(status_win, A_REVERSE); + minibuffer_win = subwin(stdscr, 1, COLS, LINES - 1, 0); if (signal(SIGWINCH, console_signal_handler) == SIG_ERR) @@ -21,7 +26,21 @@ void cleanup_windows(void) void paint_buffer(struct te_buffer *buf) { + if (buf == NULL) + return; + + struct te_char *c; + int y, x; + + TAILQ_FOREACH(c, &buf->chars_head, chars) { + getyx(stdscr, y, x); + if (y >= LINES -2) + break; + waddch(buffer_win, c->contents); + } + + refresh(); } void screen_move_left(void) @@ -44,6 +63,18 @@ void screen_move_down(void) } +void statusprintf(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwprintw(status_win, fmt, ap); + va_end(ap); + + wclrtoeol(status_win); + wrefresh(status_win); +} + + void miniprintf(char *fmt, ...) { va_list ap; diff --git a/screen.h b/screen.h index ac65c86..0d95fb1 100644 --- a/screen.h +++ b/screen.h @@ -7,7 +7,7 @@ #include "buffer.h" -WINDOW *buffer_win, *minibuffer_win; +WINDOW *buffer_win, *status_win, *minibuffer_win; void init_windows(void); void cleanup_windows(void); @@ -18,6 +18,7 @@ void screen_move_right(void); void screen_move_up(void); void screen_move_down(void); +void statusprintf(char *fmt, ...); void miniprintf(char *fmt, ...); void console_signal_handler(int sig); -- 2.43.0