diff --git a/.gitignore b/.gitignore index 2066820..602bc80 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bbs +build/ diff --git a/Makefile b/Makefile index df3b7d6..4519b7f 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,37 @@ -CC=gcc -CFLAGS=-std=c99 -pedantic -Wall -Wextra -LIBS=-lncursesw -EXECUTABLE=bbs +# SPDX-License-Identifier: MIT -.PHONY: all -all: make +# Makefile for Amber's BBS Interface +# Buildsys by Gale Faraday + +.PHONY: all clean run +.SUFFIXES: + +.DEFAULT_GOAL := all + +TARGET := $(shell basename $(CURDIR)) +SOURCES := src/ +BUILD := build/ +OBJS := $(patsubst $(SOURCES)%.c,$(BUILD)%.o,$(wildcard $(SOURCES)*.c)) +EXECUTABLE := $(TARGET) -make: - $(CC) $(CFLAGS) $(LIBS) bbs.c -o $(EXECUTABLE) +CC := gcc +LD := gcc +CFLAGS := -std=c99 -pedantic -Wall -Wextra -O2 -ggdb +LDFLAGS := $(CFLAGS) -lncursesw -.PHONY: execute -execute: +all: $(EXECUTABLE) + +run: $(EXECUTABLE) ./$(EXECUTABLE) -.PHONY: run -run: make execute +.IGNORE: clean +clean: + @rm -rvf $(BUILD) $(EXECUTABLE) + +$(EXECUTABLE): $(OBJS) + $(LD) $(LDFLAGS) -o $@ $^ + +$(OBJS): $(BUILD)%.o : $(SOURCES)%.c + -@mkdir -p $(BUILD) + $(CC) $(CFLAGS) -o $@ -c $< diff --git a/bbs.c b/src/bbs.c similarity index 56% rename from bbs.c rename to src/bbs.c index 57c642f..c59ef6c 100644 --- a/bbs.c +++ b/src/bbs.c @@ -1,56 +1,23 @@ +/* SPDX-License-Identifier: MIT */ + #include #include #include +#include "globals.h" +#include "screens.h" + const unsigned int GETCH_TIMEOUT = 10; /* in ms */ -const unsigned int MAX_SCREENS = 2; /* size of screens array */ const char* LOCALE = "en_US.UTF-8"; /* enable unicode support, set to "ANSI_X3.4-1968" for ascii */ -char errorMessage[60]; - -enum Status { - STATUS_QUIT, - STATUS_WAITING, - STATUS_NEED_REFRESH -}; - -enum ActiveScreen { - HOME, - ERROR -}; - -struct Screen { - char name[20]; - WINDOW *win; - void (*draw_screen)(struct Screen *, char *input); /* this is run about every 10ms */ -}; - -static void draw_home(struct Screen *screen, char *input) { - static char* banner = "" - " _ _ _ \n" - " | | | | | | \n" - " __ _ _ __ ___ | |__ ___ _ __ ___ _ __ | | __ _ ___ ___ _ __ ___| |_ \n" - " / _` | '_ ` _ \\| '_ \\ / _ \\ '__/ __| '_ \\| |/ _` |/ __/ _ \\ | '_ \\ / _ \\ __|\n" - " | (_| | | | | | | |_) | __/ | \\__ \\ |_) | | (_| | (_| __/_| | | | __/ |_ \n" - " \\__,_|_| |_| |_|_.__/ \\___|_| |___/ .__/|_|\\__,_|\\___\\___(_)_| |_|\\___|\\__|\n" - " | | \n" - " |_| \n"; - - mvwprintw(screen->win, 1, 2, "Thank you for visiting:"); - mvwprintw(screen->win, 2, 1, "%s", banner); - mvwprintw(screen->win, 10, 1, "Your current input is: %s", input); -} - -static void draw_error(struct Screen *screen, __attribute__((unused)) char *input) { - mvwprintw(screen->win, 1, 2, "%s", errorMessage); -} +char error_message[MAX_ERROR_MESSAGE_SIZE]; static void draw_screen (struct Screen *screen, char *input) { screen->draw_screen(screen, input); box(screen->win, 0, 0); - // wattron(screen->win, A_STANDOUT); + /* wattron(screen->win, A_STANDOUT); */ mvwprintw(screen->win, 0, 1, " %s ", screen->name); - // wattroff(screen->win, A_STANDOUT); + /* wattroff(screen->win, A_STANDOUT); */ } int main() { @@ -61,37 +28,25 @@ int main() { timeout(GETCH_TIMEOUT); /* set timeout for getch() */ setlocale(LC_CTYPE, LOCALE); /* set locale, UTF8 support is enabled here */ - unsigned int start_y = 0; - unsigned int start_x = 0; unsigned int terminal_width; unsigned int terminal_height; unsigned int old_terminal_width = 0; unsigned int old_terminal_height = 0; - struct Screen screens[MAX_SCREENS]; - enum Status status = STATUS_NEED_REFRESH; /* refresh screens[active_screen].window on first loop */ char input = ' '; - strcpy(errorMessage, ""); + strcpy(error_message, ""); getmaxyx(stdscr, terminal_height, terminal_width); unsigned int screen_before_error = HOME; enum ActiveScreen active_screen = HOME; - struct Screen home = { - "home", - newwin(terminal_height, terminal_width, start_y, start_x), - draw_home - }; - screens[HOME] = home; - struct Screen error = { - "error", - newwin(terminal_height, terminal_width, start_y, start_x), - draw_error - }; - screens[ERROR] = error; + + /* could be it's own init func within the screen's file. */ + screens[HOME].win = newwin(terminal_height, terminal_width, 0, 0); + screens[ERROR].win = newwin(terminal_height, terminal_width, 0, 0); /* main event loop */ while (status != STATUS_QUIT) { @@ -116,7 +71,7 @@ int main() { if (terminal_width < 80 || terminal_height < 24) { if (active_screen != ERROR) { - strcpy(errorMessage, "This program expects at least 80 rows by 24 columns."); + strcpy(error_message, "This program expects at least 80 rows by 24 columns."); screen_before_error = active_screen; }; active_screen = ERROR; @@ -136,7 +91,7 @@ int main() { }; /* clean up */ - delwin(screens[active_screen].win); + delwin(screens[active_screen].win); /* TODO: delete all windows in screens[] */ nocbreak(); endwin(); /* ends curses mode */ curs_set(1); diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..03d5adf --- /dev/null +++ b/src/error.c @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: MIT */ + +#include +#include "screens.h" + +void draw_error(struct Screen *screen, __attribute__((unused)) char *input) { + mvwprintw(screen->win, 1, 2, "%s", error_message); +} diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..292a6a8 --- /dev/null +++ b/src/error.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef ERROR_H /* header guard */ +#define ERROR_H + +#include "screens.h" + +void draw_error(struct Screen *screen, char *input); + +#endif diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 0000000..373266a --- /dev/null +++ b/src/globals.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: MIT */ + +#define MAX_SCREEN_NAME_SIZE 20 /* size of the screen name array */ +#define MAX_ERROR_MESSAGE_SIZE 60 /* size of error_message array */ + +#ifndef GLOBALS_H /* header guard */ +#define GLOBALS_H + +#include + +extern char error_message[]; + +struct Screen { + char name[MAX_SCREEN_NAME_SIZE]; + WINDOW *win; + void (*draw_screen)(struct Screen *, char *input); /* GETCH_TIMEOUT determines how often this is run, in ms */ +}; + +enum Status { + STATUS_QUIT, + STATUS_WAITING, + STATUS_NEED_REFRESH +}; + +enum ActiveScreen { + HOME, + ERROR +}; + +#endif diff --git a/src/home.c b/src/home.c new file mode 100644 index 0000000..ad4d8d7 --- /dev/null +++ b/src/home.c @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: MIT */ + +#include +#include "screens.h" + +void draw_home(struct Screen *screen, char *input) { + static char* banner = "" + " _ _ _ \n" + " | | | | | | \n" + " __ _ _ __ ___ | |__ ___ _ __ ___ _ __ | | __ _ ___ ___ _ __ ___| |_ \n" + " / _` | '_ ` _ \\| '_ \\ / _ \\ '__/ __| '_ \\| |/ _` |/ __/ _ \\ | '_ \\ / _ \\ __|\n" + " | (_| | | | | | | |_) | __/ | \\__ \\ |_) | | (_| | (_| __/_| | | | __/ |_ \n" + " \\__,_|_| |_| |_|_.__/ \\___|_| |___/ .__/|_|\\__,_|\\___\\___(_)_| |_|\\___|\\__|\n" + " | | \n" + " |_| \n"; + + mvwprintw(screen->win, 1, 2, "Thank you for visiting:"); + mvwprintw(screen->win, 2, 1, "%s", banner); + mvwprintw(screen->win, 10, 1, "Your current input is: %s", input); +} diff --git a/src/home.h b/src/home.h new file mode 100644 index 0000000..d88904c --- /dev/null +++ b/src/home.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef HOME_H /* header guard */ +#define HOME_H + +#include "screens.h" + +void draw_home(struct Screen *screen, char *input); + +#endif diff --git a/src/screens.c b/src/screens.c new file mode 100644 index 0000000..a506b25 --- /dev/null +++ b/src/screens.c @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: MIT */ + +#include "globals.h" +#include "home.h" +#include "error.h" + +/* NOTE: this should be compile time */ +struct Screen screens[] = { + { + "home", + NULL, + draw_home + }, + { + "error", + NULL, + draw_error + } +}; diff --git a/src/screens.h b/src/screens.h new file mode 100644 index 0000000..e9126bc --- /dev/null +++ b/src/screens.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef SCREENS_H /* header guard */ +#define SCREENS_H + +#include "globals.h" + +extern struct Screen screens[]; + +#endif