Commit ea247dfd authored by mbionchi's avatar mbionchi

try to factor out curses screen initialization routines

parent a30b7114
bin_PROGRAMS = telnetsite
telnetsite_SOURCES = main.c anim.c anim.h site.c site.h splash.c splash.h data.c data.h scroll.c scroll.h render.c render.h log.c log.h module.h
telnetsite_SOURCES = main.c anim.c anim.h site.c site.h splash.c splash.h data.c data.h scroll.c scroll.h render.c render.h log.c log.h winch.c winch.h module.h
telnetsite_LDADD = ${ncurses_LIBS} -ldl
AM_CFLAGS = -Isrc/
......@@ -18,6 +18,7 @@
#include "site.h"
#include "splash.h"
#include "data.h"
#include "winch.h"
#include <getopt.h>
#include <stdlib.h>
......@@ -50,8 +51,26 @@ int main(int argc, char **argv) {
fprintf(stderr, "Usage: %s --site <path-to-dir> [--splash <path-to-file>]\n", binary_name);
exit(1);
}
had_winch = 0;
signal(SIGWINCH, set_had_winch);
WINDOW *main_window = initscr();
cbreak();
halfdelay(1);
noecho();
curs_set(0);
nonl();
keypad(main_window, 1);
if (splash_path) {
splash(splash_path);
}
site(site_path);
if (site_path) {
site(site_path);
}
endwin();
return 0;
}
......@@ -21,6 +21,8 @@
#include "render.h"
#include "scroll.h"
#include "data.h"
#include "winch.h"
#include <errno.h>
#include <ncurses.h>
#include <stdlib.h>
......@@ -46,13 +48,8 @@ size_t get_index_width(struct section **sections, size_t n_sections) {
return max_len;
}
sig_atomic_t had_winch = 0;
int reload_content = 0;
void winch_handler(int signo) {
had_winch = 1;
}
/*
* NOTE:
* scrollok(window, 0) needs to be called every time before
......@@ -64,8 +61,6 @@ void winch_handler(int signo) {
* - refactor the event loop, it's horrifying atm
*/
void site(char *path) {
signal(SIGWINCH, winch_handler);
struct window index_window;
struct window separator_window;
struct window content_window;
......@@ -88,14 +83,6 @@ void site(char *path) {
gen_err_opening(&content_window.content);
}
WINDOW *main_window = initscr();
cbreak();
halfdelay(1);
noecho();
curs_set(0);
nonl();
keypad(main_window, 1);
int index_left_margin = 2;
int index_cols = get_index_width(sections, n_sections);
int index_scroll_threshold = 5;
......@@ -298,12 +285,11 @@ void site(char *path) {
if (content_window.content.type == STATIC && content_window.content.lines != NULL) {
anim_tick(&content_window);
if (had_winch) {
delwin(index_window.window);
delwin(separator_window.window);
delwin(content_window.window);
endwin();
endwin();
refresh();
index_window.rows = LINES;
......@@ -336,6 +322,7 @@ void site(char *path) {
render_separator(&separator_window);
render_ncontent(&content_window);
refresh();
wrefresh(index_window.window);
......@@ -401,5 +388,4 @@ void site(char *path) {
delwin(index_window.window);
delwin(separator_window.window);
delwin(content_window.window);
endwin();
}
......@@ -20,6 +20,7 @@
#include "data.h"
#include "render.h"
#include "anim.h"
#include "winch.h"
#include <stdio.h>
#include <stddef.h>
......@@ -43,11 +44,7 @@ void splash(char *path) {
gen_err_opening(&splash_window.content);
}
splash_window.window = initscr();
cbreak();
halfdelay(1);
noecho();
curs_set(0);
splash_window.window = newwin(0,0,0,0);
splash_window.cols = COLS;
splash_window.rows = LINES;
......@@ -65,10 +62,24 @@ void splash(char *path) {
wrefresh(splash_window.window);
int ch = getch();
// 410: idk why, but it has to do with SIGWINCH
while (ch == ERR || ch == 410) {
while (ch == ERR) {
if (splash_window.content.type == STATIC) {
anim_tick(&splash_window);
if (had_winch) {
delwin(splash_window.window);
endwin();
refresh();
splash_window.window = newwin(0,0,0,0);
splash_window.cols = COLS;
splash_window.rows = LINES;
free_nlines(splash_window.content.lines->formatted, splash_window.content.lines->n_formatted);
free_anim_refs(&splash_window);
splash_window.content.lines->n_formatted = flow_nlines(splash_window.content.lines->raw, splash_window.content.lines->n_raw, &splash_window.content.lines->formatted, splash_window.cols, 0);
render_ncontent(&splash_window);
refresh();
wrefresh(splash_window.window);
had_winch = 0;
}
wrefresh(splash_window.window);
}
ch = getch();
......@@ -77,6 +88,6 @@ void splash(char *path) {
free_anim_refs(&splash_window);
clear();
wrefresh(splash_window.window);
endwin();
delwin(splash_window.window);
return;
}
/*
* This file is part of telnet-site.
*
* telnet-site is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* telnet-site is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with telnet-site. If not, see <https://www.gnu.org/licenses/>.
*/
#include "winch.h"
#include <signal.h>
void set_had_winch(int signo) {
if (signo == SIGWINCH) {
had_winch = 1;
}
}
/*
* This file is part of telnet-site.
*
* telnet-site is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* telnet-site is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with telnet-site. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _TELNET_SITE_WINCH_H
#define _TELNET_SITE_WINCH_H
#include <signal.h>
sig_atomic_t had_winch;
void set_had_winch(int signo);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment