-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.c
429 lines (368 loc) · 11 KB
/
interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "interface.h"
#include "mmu.h"
#include "scheduler.h"
#include <curses.h>
#include <panel.h>
extern struct _PANEL_DATA panel_data;
void init_wins(WINDOW **wins) {
char label[80];
int32_t max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
wins[0] = newwin(5, max_x, 0, 0);
sprintf(label, "Queue");
win_show(wins[0], label, 1);
wins[1] = newwin(max_y - 8, max_x / 3, 5, 0);
sprintf(label, "Status");
win_show(wins[1], label, 2);
wins[4] = newwin(max_y - 8, max_x / 3, 5, max_x / 3);
sprintf(label, "TCB");
win_show(wins[4], label, 2);
wins[2] = newwin(max_y - 8, max_x / 3, 5, max_x - (max_x / 3));
sprintf(label, "Bit Map");
win_show(wins[2], label, 2);
wins[3] = newwin(max_y / 2, max_x / 2, (max_y / 2) + 1, max_x / 4);
sprintf(label, "Console");
win_show(wins[3], label, 3);
scrollok(wins[3], TRUE);
idlok(wins[3], TRUE);
max_y = getmaxy(wins[3]);
wsetscrreg(wins[3], 3, max_y - 1);
}
/* show the window with a border and a label */
void win_show(WINDOW *win, char *label, int label_color) {
int32_t width;
uint64_t width_conv;
__attribute__((unused)) int height;
getmaxyx(win, height, width);
if (width < 0) {
logger("ERROR: width < 0");
exit(EXIT_FAILURE);
} else {
width_conv = (uint64_t)width;
}
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width_conv, label, COLOR_PAIR(label_color));
}
/* print a string in the middle of a window */
void print_in_middle(WINDOW *win, int starty, int startx, uint64_t width,
char *string, chtype color) {
int x, y;
uint64_t temp, length;
int32_t tmp_conv;
if (win == NULL) {
win = stdscr;
}
getyx(win, y, x);
if (startx != 0) {
x = startx;
}
if (starty != 0) {
y = starty;
}
if (width == 0) {
width = 80;
}
length = strlen(string);
temp = (width - length) / 2;
if (temp > INT32_MAX) {
logger("ERROR: temp > MAX_INT32");
exit(EXIT_FAILURE);
} else {
tmp_conv = (int32_t)temp;
}
x = startx + tmp_conv;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
wmove(win, y + 1, x);
refresh();
}
void show_commands(WINDOW *console) {
int x, y;
getyx(console, y, x);
wmove(console, y, x = 4);
mvwaddstr(console, y, x, "create - create a new process\n");
mvwaddstr(console, y + 1, x, " create -m [mem] \n");
mvwaddstr(console, y + 2, x, " mem: 1 - 20\n");
mvwaddstr(console, y + 3, x, "kill - kill a process\n");
mvwaddstr(console, y + 4, x, " delete [pid]\n");
mvwaddstr(console, y + 5, x, " pid: 1 - 64\n");
wrefresh(console);
}
/* initializes the interface with all the other helper functions */
void init_interface(WINDOW **my_wins, PANEL **my_panels,
struct _PANEL_DATA *panel_datas) {
/* initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* initialize all the colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_pair(5, COLOR_BLACK, COLOR_WHITE);
init_pair(6, COLOR_YELLOW, COLOR_BLACK);
/* initialize windows */
init_wins(my_wins);
/* attach a panel to each window */
my_panels[0] = new_panel(my_wins[0]);
my_panels[1] = new_panel(my_wins[1]);
my_panels[2] = new_panel(my_wins[2]);
my_panels[4] = new_panel(my_wins[4]);
my_panels[3] = new_panel(my_wins[3]);
/* Initialize panel datas saying that nothing is hidden */
panel_datas[0].hide = FALSE;
panel_datas[1].hide = FALSE;
panel_datas[2].hide = FALSE;
panel_datas[4].hide = FALSE;
panel_datas[3].hide = FALSE;
set_panel_userptr(my_panels[0], &panel_datas[0]);
set_panel_userptr(my_panels[1], &panel_datas[1]);
set_panel_userptr(my_panels[2], &panel_datas[2]);
set_panel_userptr(my_panels[4], &panel_datas[4]);
set_panel_userptr(my_panels[3], &panel_datas[3]);
update_panels();
}
void show_keyboard_shortcuts() {
attron(COLOR_PAIR(4));
mvprintw(LINES - 2, 0, "Show or Hide Console with 'Tab'");
mvprintw(LINES - 1, 0, "F1 to Exit");
attroff(COLOR_PAIR(4));
doupdate();
}
void show_title(sched_info_t sched_info) {
attron(COLOR_PAIR(6));
mvprintw(LINES - 2, COLS - 28, "Process Management Simulator");
if (sched_info.algorithm == FIFO) {
mvprintw(LINES - 1, COLS - 10, "Using FIFO");
} else {
mvprintw(LINES - 1, COLS - 24, "Using RR with Quantum %d",
sched_info.time_quantum);
}
attroff(COLOR_PAIR(6));
doupdate();
}
void print_prompt_char(WINDOW *console_win) {
int x, y;
getyx(console_win, y, x);
x = 2;
y += 1;
wattron(console_win, COLOR_PAIR(4));
mvwprintw(console_win, y, x, "$");
wattroff(console_win, COLOR_PAIR(4));
wmove(console_win, y, x + 2);
wrefresh(console_win);
}
void do_backspace_action_on_console(WINDOW *win) {
int x, y;
getyx(win, y, x);
if (x > 4) {
wmove(win, y, x - 1);
/* waddch(console, ' ');
wmove(console, y, x); */
wdelch(win);
}
}
void do_enter_action_on_console(WINDOW *win) {
int x, y;
getyx(win, y, x);
wmove(win, ++y, x = 4);
}
void add_char_to_console(WINDOW *win, int ch) {
int x, y;
getyx(win, y, x);
waddch(win, ch);
wmove(win, y, x + 1);
}
void reset_panels(PANEL **panels) {
PANEL_DATA *temp;
if ((temp = ((PANEL_DATA *)panel_userptr(panels[3])))->hide == FALSE) {
hide_panel(panels[3]);
temp->hide = TRUE;
show_panel(panels[3]);
temp->hide = FALSE;
}
}
void print_tcb_of_current_process(WINDOW *win, p_queue_t *p) {
p_queue_t *current = p;
char temp[30];
restart_tcb(win);
if (current != NULL) {
sprintf(temp, "PID: %d", current->process->pid);
mvwaddnstr(win, 3, 3, temp, 30);
sprintf(temp, "PC: %d", current->process->time_used);
mvwaddnstr(win, 4, 3, temp, 30);
if (current->process->state == NEW) {
sprintf(temp, "State: NEW");
} else if (current->process->state == READY) {
sprintf(temp, "State: READY");
} else if (current->process->state == RUNNING) {
sprintf(temp, "State: RUNNING");
} else {
sprintf(temp, "State: ZOMBIE");
}
mvwaddnstr(win, 5, 3, temp, 30);
sprintf(temp, "Mem size: %u", current->process->mem_size);
mvwaddnstr(win, 6, 3, temp, 30);
sprintf(temp, "Mem start: %u", current->process->mem_start);
mvwaddnstr(win, 7, 3, temp, 30);
sprintf(temp, "Open file: insts%u.asm", current->process->file_d);
mvwaddnstr(win, 8, 3, temp, 30);
}
wrefresh(win);
}
void update_interface(WINDOW **wins, PANEL **panels, p_queue_t *p) {
print_process_queue(wins[0], p);
print_bit_map_of_processes_memory(wins[2]);
read_instructions_file(wins[1], p);
print_tcb_of_current_process(wins[4], p);
reset_panels(panels);
update_panels();
doupdate();
}
void print_process_queue(WINDOW *win, p_queue_t *p) {
char pid_text[MAXSTR];
p_queue_t *current = p;
int helper = 0;
restart_queue(win);
wmove(win, 1, 1);
if (current != NULL) {
sprintf(pid_text, " %d ", current->process->pid);
wattron(win, COLOR_PAIR(5));
mvwprintw(win, 3, helper * 10 + 1, "%s", pid_text);
/* mvwaddnstr(win, 3, helper * 10 + 1, pid_text, 10); */
wattroff(win, COLOR_PAIR(5));
refresh();
current = current->next;
}
while (current != NULL) {
helper += 1;
sprintf(pid_text, " %d ", current->process->pid);
mvwprintw(win, 3, helper * 10 + 1, "%s", pid_text);
/* mvwaddnstr(win, 3, helper * 10 + 1, pid_text, 10); */
refresh();
current = current->next;
}
}
void read_instructions_file(WINDOW *win, p_queue_t *p) {
FILE *fp;
char instructions[MAXINSTS][MAXSTR];
char instruction[MAXSTR + 10];
int i = 0;
int j = 0;
int k = 0;
p_queue_t *current = p;
restart_status(win);
if (p == NULL) {
logger("No process in queue");
return;
}
switch (current->process->pid % 5) {
case 0:
fp = fopen("insts/insts0.asm", "r");
break;
case 1:
fp = fopen("insts/insts1.asm", "r");
break;
case 2:
fp = fopen("insts/insts2.asm", "r");
break;
case 3:
fp = fopen("insts/insts3.asm", "r");
break;
case 4:
fp = fopen("insts/insts4.asm", "r");
break;
default:
logger("Error opening file");
exit(1);
}
if (fp == NULL) {
logger("Error opening file");
exit(1);
}
while (fgets(instructions[i], MAXSTR, fp) != NULL) {
i += 1;
}
do {
if (current->process->state == RUNNING) {
k = current->process->time_used;
break;
}
current = current->next;
} while (current != NULL);
current = p;
if (current->process->state == RUNNING) {
for (j = 0; j < i; j++) {
instructions[j][strcspn(instructions[j], "\r\n")] = 0;
if (j == k) {
/* mvwprintw(win, j + 3, 3, "%s\t\t<----\n", instructions[j]);
*/
sprintf(instruction, "%s\t\t<----", instructions[j]);
mvwaddnstr(win, j + 3, 3, instruction, 28);
} else {
mvwaddnstr(win, j + 3, 3, instructions[j], 28);
/* mvwprintw(win, j + 3, 3, "%s\n", instructions[j]); */
}
}
}
fclose(fp);
wrefresh(win);
}
void restart_tcb(WINDOW *win) {
char label[MAXSTR];
werase(win);
sprintf(label, "TCB");
win_show(win, label, 2);
wrefresh(win);
doupdate();
}
void restart_status(WINDOW *win) {
char label[MAXSTR];
werase(win);
sprintf(label, "Status");
win_show(win, label, 2);
wrefresh(win);
doupdate();
}
void restart_queue(WINDOW *win) {
char label[MAXSTR];
werase(win);
sprintf(label, "Queue");
win_show(win, label, 1);
wrefresh(win);
doupdate();
}
void restart_map(WINDOW *win) {
char label[MAXSTR];
werase(win);
sprintf(label, "Bit Map");
win_show(win, label, 2);
wrefresh(win);
doupdate();
}
void print_bit_map_of_processes_memory(WINDOW *win) {
int i, j;
int x, y;
int32_t scalex, scaley;
bool *memory = get_memory_map();
restart_map(win);
getmaxyx(win, y, x);
scalex = (x - 6) / 6;
scaley = (y - 6) / 6;
wmove(win, 1, 1);
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
mvwprintw(win, 2 + ((i + 1) * scaley), ((j + 0.75) * scalex), "%d",
memory[i * 5 + j]);
}
}
refresh();
free(memory);
}