# define FET_SIM_H
# include "libft.h"
+# include <ncurses.h>
# define MAX_ARGS 4
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);
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
#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);
#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;
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));
}
#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;
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
{
#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);
#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;
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;
{
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);
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)
{
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);
}
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
+#include <ncurses.h>
/*
void build_graph(const char *filename, t_vec *nodes, t_vec *mosfets)
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 ;
}
{
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);
}
#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)
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;
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 ;
}
"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 ;
}