ncurses template
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
CFLAGS := -ggdb -Wall -pedantic
|
CFLAGS := -ggdb -Wall -pedantic -lcurses
|
||||||
|
|
||||||
SRC_DIR := src
|
SRC_DIR := src
|
||||||
OBJ_DIR := src/obj
|
OBJ_DIR := src/obj
|
||||||
@@ -7,9 +7,15 @@ BIN_DIR := bin
|
|||||||
SRCS := $(wildcard $(SRC_DIR)/*.c)
|
SRCS := $(wildcard $(SRC_DIR)/*.c)
|
||||||
OBJS := $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
|
OBJS := $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
|
||||||
|
|
||||||
|
UI_SRCS := $(wildcard $(SRC_DIR)/ui/*.c $(SRC_DIR)/ui/**/*.c)
|
||||||
|
OBJS += $(UI_SRCS:$(SRC_DIR)/ui/%.c=$(OBJ_DIR)/ui/%.o)
|
||||||
|
|
||||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/main.h
|
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/main.h
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/ui/%.o: $(SRC_DIR)/ui/%.c $(SRC_DIR)/ui/%.h
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
bbs-client: $(OBJS)
|
bbs-client: $(OBJS)
|
||||||
$(CC) $(CFLAGS) $^ -o $(BIN_DIR)/$@
|
$(CC) $(CFLAGS) $^ -o $(BIN_DIR)/$@
|
||||||
|
|
||||||
@@ -21,7 +27,7 @@ deps.mk: $(SRCS)
|
|||||||
$(CC) -MM $^ > $(SRC_DIR)/$@
|
$(CC) -MM $^ > $(SRC_DIR)/$@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJ_DIR)/*.o $(BIN_DIR)/bbs-client
|
rm -f $(OBJ_DIR)/*.o $(OBJ_DIR)/**/*.o $(BIN_DIR)/bbs-client
|
||||||
|
|
||||||
run:
|
run:
|
||||||
$(BIN_DIR)/bbs-client
|
$(BIN_DIR)/bbs-client
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
TODO:
|
TODO:
|
||||||
|
- [ ] More universal Makefile
|
||||||
- [ ] Check and fix registration and login
|
- [ ] Check and fix registration and login
|
||||||
+75
-12
@@ -1,23 +1,86 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
/* TODO: перед загрузкой файла спрашивать права доступа
|
#include "ui/app.h"
|
||||||
TODO: сделать при интерфес выбора по пунктам меню */
|
#include <ncurses.h>
|
||||||
|
|
||||||
|
void event_loop(app_t *app);
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
app_t *app;
|
||||||
|
app = calloc(1, sizeof(app_t));
|
||||||
|
|
||||||
|
init_nc();
|
||||||
|
|
||||||
|
app = init_app();
|
||||||
|
|
||||||
|
wrefresh(app->menu_win);
|
||||||
|
wrefresh(app->action_win);
|
||||||
|
|
||||||
|
event_loop(app);
|
||||||
|
|
||||||
|
destroy_app(app);
|
||||||
|
endwin();
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
params_t params;
|
|
||||||
init_params(¶ms);
|
|
||||||
analyze_args(argc, argv, ¶ms);
|
|
||||||
get_missing_params(¶ms);
|
|
||||||
int sd = init_client();
|
|
||||||
connect_to_server(sd, ¶ms);
|
|
||||||
query_loop(sd, ¶ms);
|
|
||||||
clear_params(¶ms);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void event_loop(app_t *app) {
|
||||||
|
int c;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
c = wgetch(app->win);
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
|
// 113 is q key. This exits the program
|
||||||
|
case 113:
|
||||||
|
return;
|
||||||
|
case KEY_F(9):
|
||||||
|
return;
|
||||||
|
case 'e':
|
||||||
|
// ac_edit(app);
|
||||||
|
break;
|
||||||
|
// 27 is ESC key. Usage of ESC is not recommended, but for now this clears
|
||||||
|
// the menus
|
||||||
|
case 27:
|
||||||
|
// app->menu_bar->selected_menu = -1;
|
||||||
|
break;
|
||||||
|
// 9 or \t is horizontal tab
|
||||||
|
case 9:
|
||||||
|
// if (app->active == N_MENU) {
|
||||||
|
// app->active = N_ACTION;
|
||||||
|
// } else {
|
||||||
|
// app->active = N_MENU;
|
||||||
|
// }
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_borders(app);
|
||||||
|
|
||||||
|
wnoutrefresh(app->win);
|
||||||
|
wnoutrefresh(app->menu_win);
|
||||||
|
wnoutrefresh(app->action_win);
|
||||||
|
doupdate();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// int main(int argc, char *argv[]) {
|
||||||
|
// params_t params;
|
||||||
|
// init_params(¶ms);
|
||||||
|
// analyze_args(argc, argv, ¶ms);
|
||||||
|
// get_missing_params(¶ms);
|
||||||
|
// int sd = init_client();
|
||||||
|
// connect_to_server(sd, ¶ms);
|
||||||
|
// query_loop(sd, ¶ms);
|
||||||
|
// clear_params(¶ms);
|
||||||
|
// return 0;
|
||||||
|
// }
|
||||||
+109
@@ -0,0 +1,109 @@
|
|||||||
|
#include "app.h"
|
||||||
|
// #include "action.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void ac_file(WINDOW *win, int is_action_w);
|
||||||
|
|
||||||
|
app_t *init_app() {
|
||||||
|
// get values from terminal size
|
||||||
|
int y_max, x_max;
|
||||||
|
getmaxyx(stdscr, y_max, x_max);
|
||||||
|
|
||||||
|
// allocate app struct memory
|
||||||
|
app_t *_app = calloc(1, sizeof(app_t));
|
||||||
|
|
||||||
|
// associate values for screen size
|
||||||
|
_app->cur_y = y_max;
|
||||||
|
_app->cur_x = x_max;
|
||||||
|
|
||||||
|
_app->win = newwin(_app->cur_y, _app->cur_x, 0, 0);
|
||||||
|
|
||||||
|
// define the width for each sub window
|
||||||
|
int menu_w_x = x_max / 6;
|
||||||
|
int action_w_x = x_max - menu_w_x - 2;
|
||||||
|
|
||||||
|
// create the menu window
|
||||||
|
_app->menu_win = newwin(y_max - 4, menu_w_x, 2, 1);
|
||||||
|
|
||||||
|
// create the action window
|
||||||
|
_app->action_win = newwin(y_max - 4, action_w_x, 2, menu_w_x + 1);
|
||||||
|
|
||||||
|
// dialogue
|
||||||
|
_app->modal.win = NULL;
|
||||||
|
|
||||||
|
// print decorative bars
|
||||||
|
print_bars(_app);
|
||||||
|
|
||||||
|
// here goes box borders
|
||||||
|
draw_borders(_app);
|
||||||
|
|
||||||
|
// init menus
|
||||||
|
// _app->menus = malloc(3 * sizeof(menu_t));
|
||||||
|
// _app->menus[0] = init_menu("(F)ile", 'f');
|
||||||
|
// _app->menus[1] = init_menu("(E)dit", 'e');
|
||||||
|
// _app->menus[2] = init_menu("(A)bout", 'a');
|
||||||
|
|
||||||
|
// set active window by default
|
||||||
|
// _app->active = N_MENU;
|
||||||
|
|
||||||
|
// call the action window content by default
|
||||||
|
// ac_file(_app->action_win, _app->active);
|
||||||
|
|
||||||
|
// refresh the windows
|
||||||
|
wnoutrefresh(_app->win);
|
||||||
|
wnoutrefresh(_app->menu_win);
|
||||||
|
wnoutrefresh(_app->action_win);
|
||||||
|
doupdate();
|
||||||
|
|
||||||
|
return _app;
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_nc() {
|
||||||
|
initscr();
|
||||||
|
noecho();
|
||||||
|
curs_set(0);
|
||||||
|
keypad(stdscr, TRUE);
|
||||||
|
|
||||||
|
if (!has_colors()) {
|
||||||
|
printf("Your terminal does not support color\n");
|
||||||
|
} else {
|
||||||
|
start_color();
|
||||||
|
init_pair(1, COLOR_RED, COLOR_BLACK);
|
||||||
|
init_pair(2, COLOR_GREEN, COLOR_BLACK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_borders(app_t *app) {
|
||||||
|
clear();
|
||||||
|
box(app->win, 0, 0);
|
||||||
|
|
||||||
|
// refresh the windows
|
||||||
|
wnoutrefresh(app->win);
|
||||||
|
wnoutrefresh(app->menu_win);
|
||||||
|
wnoutrefresh(app->action_win);
|
||||||
|
doupdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_bars(app_t *app) {
|
||||||
|
wattrset(app->win, A_REVERSE);
|
||||||
|
|
||||||
|
// print top and bottom bars
|
||||||
|
for (int i = 1; i < app->cur_x - 1; i++) {
|
||||||
|
mvwprintw(app->win, 1, i, " ");
|
||||||
|
mvwprintw(app->win, app->cur_y - 2, i, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// add content to the top bar
|
||||||
|
mvwprintw(app->win, 1, 2, "example curses app | ");
|
||||||
|
mvwprintw(app->win, 1, 20, "size %d, %d", app->cur_x, app->cur_y);
|
||||||
|
|
||||||
|
// add content to the bottom bar
|
||||||
|
mvwprintw(app->win, app->cur_y - 2, 2, "F1 - Help | F9 - Quit");
|
||||||
|
|
||||||
|
wattroff(app->win, A_REVERSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_app(app_t *app) {
|
||||||
|
delwin(app->win);
|
||||||
|
free(app);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef N_APP_H
|
||||||
|
#define N_APP_H
|
||||||
|
|
||||||
|
#include <ncurses.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
none = 0,
|
||||||
|
login = 1,
|
||||||
|
} dialogue_e;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
WINDOW *win;
|
||||||
|
WINDOW *menu_win;
|
||||||
|
WINDOW *action_win;
|
||||||
|
struct {
|
||||||
|
dialogue_e active;
|
||||||
|
WINDOW *win;
|
||||||
|
} modal;
|
||||||
|
|
||||||
|
// menu_bar_t *menu_bar;
|
||||||
|
// menu_t *menus;
|
||||||
|
// active_win_e active;
|
||||||
|
int cur_y, cur_x;
|
||||||
|
} app_t;
|
||||||
|
|
||||||
|
app_t *init_app();
|
||||||
|
void init_nc();
|
||||||
|
void destroy_app(app_t *app);
|
||||||
|
void draw_borders(app_t *app);
|
||||||
|
void print_bars(app_t *app);
|
||||||
|
|
||||||
|
#endif // N_APP_H
|
||||||
Reference in New Issue
Block a user