First/gui.c

148 lines
3.4 KiB
C
Raw Normal View History

/* gui.c
This file is part of the "First" CPU simulator project.
Copyright (c) 2024 Efthymios Kritikos
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
#include <ncurses.h>
2024-01-23 17:38:44 +00:00
#include <string.h>
#include "simdata.h"
int terminal_width;
int terminal_height;
WINDOW *tabs;
int monochrome;
int get_terminal_size(){
int new_height,new_width;
getmaxyx(stdscr,new_height,new_width);
int changed=(new_width!=terminal_width)||(new_height!=terminal_height);
terminal_width=new_width;
terminal_height=new_height;
return changed;
}
int gui_ncurses_refresh(){
if(refresh()==ERR)
return 1;
if(wrefresh(tabs)==ERR)
return 1;
return 0;
}
2024-01-23 17:38:44 +00:00
char *tab_name[]={"Overview","Memory","Test"};
unsigned int CURRENT_TAB=0;
enum CPU_STATE_t CPU_STATE=STOPPED;
void update_tabs(){
wattron(tabs,COLOR_PAIR(2));
for(int i=0;i<terminal_width;i++)
wprintw(tabs," ");
int x=1;
for(unsigned int i=0;i<sizeof(tab_name)/sizeof(tab_name[0]);i++){
if(i==CURRENT_TAB)
wattron(tabs,COLOR_PAIR(4));
else
wattron(tabs,COLOR_PAIR(5));
mvwprintw(tabs,0,x," %1d",i);
x+=2;
if(i==CURRENT_TAB)
wattron(tabs,COLOR_PAIR(3));
else
wattron(tabs,COLOR_PAIR(2));
mvwprintw(tabs,0,x," %s ",tab_name[i]);
x+=strlen(tab_name[i])+2+3;
}
switch(CPU_STATE){
case RUNNING:
wattron(tabs,COLOR_PAIR(6));
mvwprintw(tabs,0,terminal_width-13,"[ RUNNING ");
break;
case SINGLE_STEPPING:
wattron(tabs,COLOR_PAIR(7));
mvwprintw(tabs,0,terminal_width-21,"[ SINGLE-STEPPING ");
break;
case STOPPED:
wattron(tabs,COLOR_PAIR(8));
mvwprintw(tabs,0,terminal_width-13,"[ STOPPED ");
break;
}
mvwprintw(tabs,0,terminal_width-3,"]");
}
int start_gui(){
if(!initscr())
return 1;
monochrome=( has_colors() == FALSE );
2024-01-23 17:38:44 +00:00
init_color(8, 700, 700, 700);
init_color(COLOR_WHITE, 100, 1000, 1000);
if(!monochrome){
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
2024-01-23 17:38:44 +00:00
//tab colors
init_pair(2, COLOR_BLACK, 8);
init_pair(3, 8, COLOR_BLACK);
init_pair(4, COLOR_WHITE, COLOR_BLACK);
init_pair(5, COLOR_WHITE, 8);
init_pair(6, COLOR_BLACK,COLOR_GREEN);
init_pair(7, COLOR_BLACK,COLOR_YELLOW);
init_pair(8, COLOR_BLACK,COLOR_RED);
}
noecho();
curs_set(0);
get_terminal_size();
mvprintw((terminal_height-3)/2+3,terminal_width/2-15,"Initialising the simulator...");
2024-01-23 17:38:44 +00:00
tabs = newwin(1,terminal_width,0,0);
update_tabs();
if(gui_ncurses_refresh())
return 1;
return 0;
}
int gui_error(char *str){
WINDOW *error_win = newwin(3,40,terminal_height/2-1,terminal_width/2-20);
box(error_win, 0 , 0);
if(!monochrome)
wattron(error_win,COLOR_PAIR(1));
mvwprintw(error_win,1,1,"%s",str);
if(wrefresh(error_win)==ERR)
return 1;
getch();
delwin(error_win);
return 0;
if(!monochrome)
wattroff(error_win,COLOR_PAIR(1));
}
int gui_continue_request(){
if(getch()==ERR)
return 1;
return 0;
}
int end_gui(){
if(endwin()==ERR)
return 1;
return 0;
}