Use 2 windows
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 15 Dec 2024 14:52:03 +0000 (15:52 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 15 Dec 2024 14:52:03 +0000 (15:52 +0100)
One is used for the commands and the second one will be used for ASCII
representation of the circuitry.

inc/FET_sim.h
src/c_addfet.c
src/c_addnode.c
src/c_draw.c
src/c_help.c
src/c_setnode.c
src/colors.c
src/main.c
src/terminal.c
src/text.c

index 2914198afe7483395c167cabd838993d74066e4d..e8efc85a2353296f10dc26bc1a83b469309b3fb6 100644 (file)
@@ -2,6 +2,7 @@
 # define FET_SIM_H
 
 # include "libft.h"
+# include <ncurses.h>
 
 # define MAX_ARGS 4
 
@@ -103,22 +104,22 @@ int               transfer_mosfet(t_mosfet *mosfet, t_node *from, t_node *to);
 int            merge_nodes(t_node *node1, t_node *node2);
 
 void   free_node(void *node);
-int            process_input(t_vec *nodes, t_vec *mosfets);
-int            get_input(t_input *input);
+int            process_input(WINDOW *command_win, t_vec *nodes, t_vec *mosfets);
+int            get_input(WINDOW *command_win, t_input *input);
 
 const char     *state_color_escape(t_state state);
-void           draw_single(t_vec *mosfets, size_t i);
+void           draw_single(WINDOW *command_win, t_vec *mosfets, size_t i);
 
 void   update_nodes(t_vec *nodes);
 int            should_open(t_type type, t_state state);
 int            sim_step(t_vec *nodes, t_vec *mosfets);
 
-void   setup_terminal(void);
-void   clean_terminal(void);
-void   print_start(void);
-void   command_not_found(const char *input);
-void   print_help(t_command c);
-void   print_index_error(size_t index, size_t size);
+void   setup_terminal(WINDOW **command_win, WINDOW **schematics_win);
+void   clean_terminal(WINDOW *command_win, WINDOW *schematics_win);
+void   print_start(WINDOW *command_win);
+void   command_not_found(WINDOW *command_win, const char *input);
+void   print_help(WINDOW *command_win, t_command c);
+void   print_index_error(WINDOW *command_win, size_t index, size_t size);
 
 int            construct_input(t_input *input, char **split_inp);
 
@@ -129,12 +130,13 @@ t_state   resolve_state(t_vec *connected_nodes);
 void   apply_state(t_state state, t_vec *connected_nodes);
 t_state        reduce_state(t_state state);
 
-int    c_addfet(t_input input, t_vec *mosfets);
-int    c_addnode(t_input input, t_vec *nodes);
+int    c_addfet(WINDOW *command_win, t_input input, t_vec *mosfets);
+int    c_addnode(WINDOW *command_win, t_input input, t_vec *nodes);
 int    c_bind(t_input input, t_vec *nodes, t_vec *mosfets);
 int    c_connect(t_input input, t_vec *nodes, t_vec *mosfets);
-int    c_draw(t_input input, t_vec *mosfets);
-int    c_help(t_input input);
+int    c_draw(WINDOW *command_win, t_input input, t_vec *mosfets);
+int    c_help(WINDOW *command_win, t_input input);
 int    c_next(t_input input, t_vec *nodes, t_vec *mosfets);
-int    c_setnode(t_input input, t_vec *nodes);
+int    c_setnode(WINDOW *command_win, t_input input, t_vec *nodes);
+
 #endif //FET_SIM_H
index 8293552ee6255d82777c90ba359eb1c9cc61685f..0491f5279abab2d00ad1bd379213745b713e98e2 100644 (file)
@@ -2,27 +2,27 @@
 #include "libft.h"
 #include <ncurses.h>
 
-int    c_addfet(t_input input, t_vec *mosfets)
+int    c_addfet(WINDOW *command_win, t_input input, t_vec *mosfets)
 {
        size_t  i;
 
        if (input.argc == 1 && input.argv[0].type == type)
        {
                add_mosfet(mosfets, input.argv[0].val.type);
-               printw("Index of added FET:\n\t%lu ", mosfets->size - 1);
+               wprintw(command_win, "Index of added FET:\n\t%lu ", mosfets->size - 1);
        }
        else if (input.argc == 2 && input.argv[0].type == type && input.argv[1].type == num)
        {
                i = 0;
-               printw("Indeces of added FETs:\n\t");
+               wprintw(command_win, "Indeces of added FETs:\n\t");
                while (i < input.argv[1].val.num)
                {
-                       printw("%lu ", mosfets->size);
+                       wprintw(command_win, "%lu ", mosfets->size);
                        add_mosfet(mosfets, input.argv[0].val.type);
                        ++i;
                }
        }
-       printw("\n");
+       wprintw(command_win, "\n");
        /*else
                print_wrong_usage(input);*/
        return (1);
index a1488490fcf3bb271f01c66360e08cebcf21c1e3..09bf13ade7f565bc395fa685d9dab0d9c3ffe470 100644 (file)
@@ -2,23 +2,23 @@
 #include "libft.h"
 #include <ncurses.h>
 
-static int add_nodes(t_vec *nodes, size_t num, t_state state)
+static int add_nodes(WINDOW *command_win, t_vec *nodes, size_t num, t_state state)
 {
        size_t  i;
 
        i = 0;
-       printw("Indexes of added nodes:\n\t");
+       wprintw(command_win, "Indexes of added nodes:\n\t");
        while (i < num)
        {
-               printw("%lu ", nodes->size);
+               wprintw(command_win, "%lu ", nodes->size);
                add_node(nodes, state);
                ++i;
        }
-       printw("\n");
+       wprintw(command_win, "\n");
        return (1);
 }
 
-int    c_addnode(t_input input, t_vec *nodes)
+int    c_addnode(WINDOW *command_win, t_input input, t_vec *nodes)
 {
        size_t  new_nodes_num;
        t_state set_state;
@@ -43,5 +43,5 @@ int   c_addnode(t_input input, t_vec *nodes)
                set_state = input.argv[0].val.state;
                new_nodes_num = input.argv[1].val.num;
        }
-       return (add_nodes(nodes, new_nodes_num, set_state));
+       return (add_nodes(command_win, nodes, new_nodes_num, set_state));
 }
index b0d8a7e52a2979a618d0def96340c55e58d67eec..7760d90bdef2faab3e4df3410fe5d103ccd9e1be 100644 (file)
@@ -1,7 +1,7 @@
 #include "FET_sim.h"
 #include "libft.h"
 
-int    c_draw(t_input input, t_vec *mosfets)
+int    c_draw(WINDOW *command_win, t_input input, t_vec *mosfets)
 {
        size_t  i;
 
@@ -10,13 +10,13 @@ int c_draw(t_input input, t_vec *mosfets)
                i = 0;
                while (i < mosfets->size)
                {
-                       draw_single(mosfets, i);
+                       draw_single(command_win, mosfets, i);
                        ++i;
                }
        }
        else if (input.argc == 1 && input.argv[0].type == num)
        {
-               draw_single(mosfets, input.argv[0].val.num);
+               draw_single(command_win, mosfets, input.argv[0].val.num);
        }
        /*else
        {
index 3ebad7f839870a698de614017e442517783ab9cb..8a8ed159c0f122d4aa1b174bbe2f9bd427b6ca95 100644 (file)
@@ -1,11 +1,11 @@
 #include "FET_sim.h"
 
-int    c_help(t_input input)
+int    c_help(WINDOW *command_win, t_input input)
 {
        if (input.argc == 0)
-               print_help(none);
+               print_help(command_win, none);
        else if (input.argc == 1 && input.argv[0].type == command)
-               print_help(input.argv[0].val.command);
+               print_help(command_win, input.argv[0].val.command);
        /*else
                print_wrong_usage(input);*/
        return (1);
index ab849b05c3b074f5792cc1179954ac3103f77c53..6bb50b6598001e59902c8806598e7fd24915f7ee 100644 (file)
@@ -1,7 +1,7 @@
 #include "FET_sim.h"
 #include "libft.h"
 
-int    c_setnode(t_input input, t_vec *nodes)
+int    c_setnode(WINDOW *command_win, t_input input, t_vec *nodes)
 {
        t_node *node;
 
@@ -13,7 +13,7 @@ int   c_setnode(t_input input, t_vec *nodes)
        node = ft_vec_access(nodes, input.argv[0].val.num);
        if (!node)
        {
-               print_index_error(input.argv[0].val.num, nodes->size);
+               print_index_error(command_win, input.argv[0].val.num, nodes->size);
                return (1);
        }
        node->set_state = input.argv[1].val.state;
index 85ebb9425205e1fe7e2c57fbef029d464ff67900..01bb7ca1fe0db560c85ef44de78e2fda36b0d4e4 100644 (file)
@@ -14,8 +14,8 @@ void  init_colors(void)
 {
        if (!has_colors())
                return ;
-       start_color();
        use_default_colors();
+       start_color();
        init_pair(NC_RED, COLOR_RED, -1);
        init_pair(NC_GREEN, COLOR_GREEN, -1);
        init_pair(NC_BLUE, COLOR_BLUE, -1);
@@ -34,40 +34,40 @@ t_nc_color  get_state_color(t_state state)
                return (NC_RED);
 }
 
-static void    print_in_color(const char *str, t_nc_color color)
+static void    print_in_color(WINDOW *command_win, const char *str, t_nc_color color)
 {
-       attron(COLOR_PAIR(color));
+       wattron(command_win, COLOR_PAIR(color));
        while (*str)
-               addch(*(str++));
-       attroff(COLOR_PAIR(color));
+               waddch(command_win, *(str++));
+       wattroff(command_win, COLOR_PAIR(color));
        return ;
 }
 
-static void    draw_top(t_mosfet *mosfet)
+static void    draw_top(WINDOW *command_win, t_mosfet *mosfet)
 {
        if (mosfet->source)
        {
-               printw("      %lu\n", mosfet->source->id);
-               print_in_color("      |\n", get_state_color(mosfet->source->state));
+               wprintw(command_win, "      %lu\n", mosfet->source->id);
+               print_in_color(command_win, "      |\n", get_state_color(mosfet->source->state));
        }
        else
        {
-               print_in_color("     NULL\n", NC_RED);
-               print_in_color("      |\n", NC_RED);
+               print_in_color(command_win, "     NULL\n", NC_RED);
+               print_in_color(command_win, "      |\n", NC_RED);
        }
        return ;
 }
 
-static void    draw_switch(t_mosfet *mosfet)
+static void    draw_switch(WINDOW *command_win, t_mosfet *mosfet)
 {
        t_nc_color      color;
 
        if (!mosfet->gate || !mosfet->is_opened)
        {
                if (mosfet->gate && should_open(mosfet->type, mosfet->gate->state))
-                       addch('\\');
+                       waddch(command_win, '\\');
                else
-                       addch('-');
+                       waddch(command_win, '-');
        }
        else if (mosfet->is_opened)
        {
@@ -78,49 +78,49 @@ static void draw_switch(t_mosfet *mosfet)
                else
                        color = NC_RED;
                if (should_open(mosfet->type, mosfet->gate->state))
-                       print_in_color("|", color);
+                       print_in_color(command_win, "|", color);
                else
-                       print_in_color("\\", color);
+                       print_in_color(command_win, "\\", color);
        }
        return ;
 }
 
-static void    draw_middle(t_mosfet *mosfet)
+static void    draw_middle(WINDOW *command_win, t_mosfet *mosfet)
 {
        if (mosfet->gate)
        {
-               printw("%4lu", mosfet->gate->id);
-               print_in_color("-", get_state_color(mosfet->gate->state));
+               wprintw(command_win, "%4lu", mosfet->gate->id);
+               print_in_color(command_win, "-", get_state_color(mosfet->gate->state));
        }
        else
-               print_in_color("NULL-", NC_RED);
-       addch(mosfet->type);
-       draw_switch(mosfet);
-       addch('\n');
+               print_in_color(command_win, "NULL-", NC_RED);
+       waddch(command_win, mosfet->type);
+       draw_switch(command_win, mosfet);
+       waddch(command_win, '\n');
        return ;
 }
 
-static void    draw_bottom(t_mosfet *mosfet)
+static void    draw_bottom(WINDOW *command_win, t_mosfet *mosfet)
 {
-       printw("      ");
+       wprintw(command_win, "      ");
        if (mosfet->drain)
-               print_in_color("|\n", get_state_color(mosfet->drain->state));
+               print_in_color(command_win, "|\n", get_state_color(mosfet->drain->state));
        else
-               print_in_color("|\n", NC_RED);
+               print_in_color(command_win, "|\n", NC_RED);
        if (mosfet->drain)
-               printw("      %lu", mosfet->drain->id);
+               wprintw(command_win, "      %lu", mosfet->drain->id);
        else
-               print_in_color("     NULL", NC_RED);
-       printw("\n\n");
+               print_in_color(command_win, "     NULL", NC_RED);
+       wprintw(command_win, "\n\n");
        return ;
 }
 
-void   draw_single(t_vec *mosfets, size_t i)
+void   draw_single(WINDOW *command_win, t_vec *mosfets, size_t i)
 {
        t_mosfet        *mosfet;
 
        mosfet = ft_vec_access(mosfets, i);
-       draw_top(mosfet);
-       draw_middle(mosfet);
-       draw_bottom(mosfet);
+       draw_top(command_win, mosfet);
+       draw_middle(command_win, mosfet);
+       draw_bottom(command_win, mosfet);
 }
index 68978cc61134667fa3ec6c9bd1a191f125587851..146baeb13b1e4049b2d9b0a1a198b2a6877841f2 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <ncurses.h>
 
 /*
 void   build_graph(const char *filename, t_vec *nodes, t_vec *mosfets)
@@ -212,41 +213,41 @@ int       construct_input(t_input *input, char **split_inp)
        return (1);
 }
 
-int    process_input(t_vec *nodes, t_vec *mosfets)
+int    process_input(WINDOW *command_win, t_vec *nodes, t_vec *mosfets)
 {
        int                             res;
        static t_input  input = {.command = help, .argc = 0};
 
-       if (!get_input(&input))
+       if (!get_input(command_win, &input))
                return (1);
        res = 1;
        if (input.command == next)
                res = c_next(input, nodes, mosfets);
        else if (input.command == draw)
-               res = c_draw(input, mosfets);
+               res = c_draw(command_win, input, mosfets);
        else if (input.command == setnode)
-               res = c_setnode(input, nodes);
+               res = c_setnode(command_win, input, nodes);
        else if (input.command == addnode)
-               res = c_addnode(input, nodes);
+               res = c_addnode(command_win, input, nodes);
        else if (input.command == addfet)
-               res = c_addfet(input, mosfets);
+               res = c_addfet(command_win, input, mosfets);
        else if (input.command == bind)
                res = c_bind(input, nodes, mosfets);
        else if (input.command == connect)
                res = c_connect(input, nodes, mosfets);
        else if (input.command == help)
-               res = c_help(input);
+               res = c_help(command_win, input);
        else if (input.command == exitsim)
                res = 0;
        return (res);
 }
 
-void   cleanup(t_vec *nodes, t_vec *mosfets)
+void   cleanup(t_vec *nodes, t_vec *mosfets, WINDOW *command_win, WINDOW *schematics_win)
 {
        update_nodes(NULL);
        ft_vec_free(nodes, free_node);
        ft_vec_free(mosfets, NULL);
-       clean_terminal();
+       clean_terminal(command_win, schematics_win);
        return ;
 }
 
@@ -254,14 +255,16 @@ int       main(void)
 {
        t_vec   nodes;
        t_vec   mosfets;
+       WINDOW  *command_win;
+       WINDOW  *schematics_win;
 
        ft_vec_init(&nodes, sizeof(t_node));
        ft_vec_init(&mosfets, sizeof(t_mosfet));
-       setup_terminal();
-       print_start();
+       setup_terminal(&command_win, &schematics_win);
+       print_start(command_win);
        //if (argc > 1)
        //      build_graph(argv[1], &nodes, &mosfets);
-       while (process_input(&nodes, &mosfets));
-       cleanup(&nodes, &mosfets);
+       while (process_input(command_win, &nodes, &mosfets));
+       cleanup(&nodes, &mosfets, command_win, schematics_win);
        return (0);
 }
index 3c7a2cc06b8b0080b356892d3dfccd23541de055..4afea4dd95403e3c326fb666dc4fda60dfd2e788 100644 (file)
@@ -4,16 +4,20 @@
 #include <stdlib.h>
 
 #define INPUT_BUFFER_SIZE 100
+#define COMMAND_WIN_COLS 60
 
-void   setup_terminal(void)
+void   setup_terminal(WINDOW **command_win, WINDOW **schematics_win)
 {
        initscr();
        init_colors();
-       keypad(stdscr, TRUE);
+       *schematics_win = newwin(0, COLS - COMMAND_WIN_COLS, 0, 0);
+       *command_win = newwin(0, COMMAND_WIN_COLS, 0, COLS - COMMAND_WIN_COLS);
+       keypad(*command_win, TRUE);
        nonl();
        cbreak();
        echo();
-       scrollok(stdscr, TRUE);
+       scrollok(*command_win, TRUE);
+       return ;
 }
 
 void   free_split(char **sp)
@@ -30,14 +34,14 @@ void        free_split(char **sp)
        return ;
 }
 
-int    get_input(t_input *input)
+int    get_input(WINDOW *command_win, t_input *input)
 {
        int             res;
        char    str_inp[INPUT_BUFFER_SIZE + 1];
        char    **split_inp;
 
-       addstr("FET_sim> ");
-       if (getnstr(str_inp, INPUT_BUFFER_SIZE) == ERR)
+       wprintw(command_win, "FET_sim> ");
+       if (wgetnstr(command_win, str_inp, INPUT_BUFFER_SIZE) == ERR)
        {
                input->command = exitsim;
                input->argc = 0;
@@ -51,15 +55,17 @@ int get_input(t_input *input)
        res = construct_input(input, split_inp);
        if (!res)
        {
-               command_not_found(str_inp);
+               command_not_found(command_win, str_inp);
        }
        free_split(split_inp);
        return (res);
 }
 
 
-void   clean_terminal(void)
+void   clean_terminal(WINDOW *command_win, WINDOW *schematics_win)
 {
+       delwin(schematics_win);
+       delwin(command_win);
        endwin();
        return ;
 }
index eeed0f2f9fabf138eff3128575404e7fc759f49b..600d4a0328d7b3d0a525004220ee0b1446facd0f 100644 (file)
@@ -70,55 +70,55 @@ static const char   g_general_help_str[] = ""
        "help [COMMAND] \t\t- shows this help or help for COMMAND\n"
        "exit [...] \t\t- exits this program\n\n";
 
-void   print_help(t_command c)
+void   print_help(WINDOW *command_win, t_command c)
 {
        if (c == next)
-               printw(g_next_help_str);
+               wprintw(command_win, g_next_help_str);
        else if (c == draw)
-               printw(g_draw_help_str);
+               wprintw(command_win, g_draw_help_str);
        else if (c == setnode)
-               printw(g_setnode_help_str);
+               wprintw(command_win, g_setnode_help_str);
        else if (c == addnode)
-               printw(g_addnode_help_str);
+               wprintw(command_win, g_addnode_help_str);
        else if (c == addfet)
-               printw(g_addfet_help_str);
+               wprintw(command_win, g_addfet_help_str);
        else if (c == bind)
-               printw(g_bind_help_str);
+               wprintw(command_win, g_bind_help_str);
        else if (c == help)
-               printw(g_help_help_str);
+               wprintw(command_win, g_help_help_str);
        else if (c == exitsim)
-               printw(g_help_exit_str);
+               wprintw(command_win, g_help_exit_str);
        else
-               printw(g_general_help_str, g_version_str);
+               wprintw(command_win, g_general_help_str, g_version_str);
        return ;
 }
 
-void   command_not_found(const char *input)
+void   command_not_found(WINDOW *command_win, const char *input)
 {
-       printw("The command \"%s\" is not a proper FET_sim command.\n", input);
+       wprintw(command_win, "The command \"%s\" is not a proper FET_sim command.\n", input);
        return ;
 }
 
-void   print_start(void)
+void   print_start(WINDOW *command_win)
 {
-       printw("FET_sim, version %s\n"
+       wprintw(command_win, "FET_sim, version %s\n"
                        "FET_sim is a simulator of FET logic.\n"
                        "If you need some guidence, use the \"help\" command.\n",
                        g_version_str);
        return ;
 }
 
-void   print_index_error(size_t index, size_t size)
+void   print_index_error(WINDOW *command_win, size_t index, size_t size)
 {
        if (size > 0)
        {
-               printw("This action is invalid as given index %lu "
+               wprintw(command_win, "This action is invalid as given index %lu "
                        "is larger then the highest index present %lu.\n",
                        index, size - 1);
        }
        else
        {
-               printw("This action is invalid as there is no element yet.\n");
+               wprintw(command_win, "This action is invalid as there is no element yet.\n");
        }
        return ;
 }