1 /*
2 
3 /usr/src/ext2ed/win.c
4 
5 A part of the extended file system 2 disk editor.
6 
7 --------------------------------------------------------
8 Window management - Interfacing with the ncurses library
9 --------------------------------------------------------
10 
11 First written on: April 17 1995
12 Modified on : April 05 2001 Christian.Bac@int-evry.fr
13 it looks like readline does not like that initscr decides to set the tty to
14 noecho.
15 
16 Copyright (C) 1995 Gadi Oxman
17 
18 */
19 
20 #include "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <termios.h>
25 #include <unistd.h>
26 #include <sys/ioctl.h>
27 
28 #include "ext2ed.h"
29 #include "../version.h"
30 
31 struct struct_pad_info show_pad_info;
32 WINDOW *title_win,*show_win,*command_win,*mt_win1,*mt_win2,*show_pad;
33 
34 /* to remember configuration after initscr
35  * and modify it
36  */
37 struct termios termioInit, termioCurrent;
38 
draw_title_win(void)39 void draw_title_win (void)
40 {
41 	char title_string [128];
42 
43 	werase(title_win);
44 	box (title_win,0,0);
45 	sprintf (title_string,"EXT2ED - Extended-2 File System editor ver %s (%s)", E2FSPROGS_VERSION, E2FSPROGS_DATE);
46 	wmove (title_win,TITLE_WIN_LINES/2,(COLS-strlen (title_string))/2);
47 	wprintw (title_win,title_string);
48 	wrefresh(title_win);
49 }
50 
setup_show_win(void)51 void setup_show_win(void)
52 {
53 	wbkgdset (show_win,A_REVERSE);werase (show_win);
54 	show_pad_info.line=0;
55 	show_pad_info.col=0;
56 	show_pad_info.display_lines=LINES-TITLE_WIN_LINES-SHOW_WIN_LINES-COMMAND_WIN_LINES-2;
57 	show_pad_info.display_cols=COLS;
58 	show_pad_info.max_line=show_pad_info.display_lines-1;show_pad_info.max_col=show_pad_info.display_cols-1;
59 	show_pad_info.disable_output=0;
60 }
61 
init_windows(void)62 void init_windows (void)
63 {
64 	initscr ();
65 	tcgetattr(0,&termioInit); /* save initial config */
66 	termioCurrent = termioInit;
67 	termioCurrent.c_lflag |= ECHO; /* set echo on */
68 	tcsetattr(0,TCSANOW,&termioCurrent);
69 
70 	if (LINES<TITLE_WIN_LINES+SHOW_WIN_LINES+COMMAND_WIN_LINES+3) {
71 		printf ("Sorry, your terminal screen is too small\n");
72 		printf ("Error - Can not initialize windows\n");
73 		exit (1);
74 	}
75 
76 	title_win=newwin (TITLE_WIN_LINES,COLS,0,0);
77 	show_win=newwin (SHOW_WIN_LINES,COLS,TITLE_WIN_LINES,0);
78 	show_pad=newpad (SHOW_PAD_LINES,SHOW_PAD_COLS);
79 	mt_win1=newwin (1,COLS,TITLE_WIN_LINES+SHOW_WIN_LINES,0);
80 	mt_win2=newwin (1,COLS,LINES-COMMAND_WIN_LINES-1,0);
81 	command_win=newwin (COMMAND_WIN_LINES,COLS,LINES-COMMAND_WIN_LINES,0);
82 
83 	if (title_win==NULL || show_win==NULL || show_pad==NULL || command_win==NULL) {
84 		printf ("Error - Not enough memory - Can not initialize windows\n");exit (1);
85 	}
86 
87 	draw_title_win();
88 
89 	setup_show_win();
90 
91 	scrollok (command_win,TRUE);
92 
93 	refresh_title_win ();
94 	refresh_show_win ();
95 	refresh_show_pad();
96 	refresh_command_win ();
97 	wrefresh(mt_win1);
98 	wrefresh(mt_win2);
99 }
100 
refresh_title_win(void)101 void refresh_title_win (void)
102 {
103 	wrefresh (title_win);
104 }
105 
refresh_show_win(void)106 void refresh_show_win (void)
107 {
108 	int current_page,total_pages;
109 
110 	current_page=show_pad_info.line/show_pad_info.display_lines+1;
111 	if (show_pad_info.line%show_pad_info.display_lines)
112 		current_page++;
113 	total_pages=show_pad_info.max_line/show_pad_info.display_lines+1;
114 
115 	wmove (show_win,2,COLS-18);
116 	wprintw (show_win,"Page %d of %d\n",current_page,total_pages);
117 
118 	wmove (show_win,2,COLS-18);
119 	wrefresh (show_win);
120 }
121 
122 
refresh_show_pad(void)123 void refresh_show_pad (void)
124 
125 {
126 	int left,top,right,bottom,i;
127 
128 	if (show_pad_info.disable_output)
129 		return;
130 
131 	if (show_pad_info.max_line < show_pad_info.display_lines-1) {
132 		for (i=show_pad_info.max_line+1;i<show_pad_info.display_lines;i++) {
133 			wmove (show_pad,i,0);wprintw (show_pad,"\n");
134 		}
135 	}
136 	left=0;right=show_pad_info.display_cols-1;
137 	top=TITLE_WIN_LINES+SHOW_WIN_LINES+1;bottom=top+show_pad_info.display_lines-1;
138 
139 	if (show_pad_info.line > show_pad_info.max_line-show_pad_info.display_lines+1)
140 		show_pad_info.line=show_pad_info.max_line-show_pad_info.display_lines+1;
141 
142 	if (show_pad_info.line < 0)
143 		show_pad_info.line=0;
144 
145 #ifdef OLD_NCURSES
146 	prefresh (show_pad,show_pad_info.line,show_pad_info.col,top,left,show_pad_info.display_lines-1,show_pad_info.display_cols-1);
147 #else
148 	prefresh (show_pad,show_pad_info.line,show_pad_info.col,top,left,top+show_pad_info.display_lines-1,left+show_pad_info.display_cols-1);
149 #endif
150 }
151 
refresh_command_win(void)152 void refresh_command_win (void)
153 {
154 	wrefresh (command_win);
155 }
156 
close_windows(void)157 void close_windows (void)
158 {
159 //	echo ();
160 	tcsetattr(0,TCSANOW,&termioInit);
161 
162 	delwin (title_win);
163 	delwin (command_win);
164 	delwin (show_win);
165 	delwin (show_pad);
166 
167 	endwin ();
168 }
169 
show_info(void)170 void show_info (void)
171 {
172 	int block_num,block_offset;
173 
174 	block_num=device_offset/file_system_info.block_size;
175 	block_offset=device_offset%file_system_info.block_size;
176 
177 	wmove (show_win,0,0);
178 	wprintw (show_win,"Offset %-3ld in block %ld. ",block_offset,block_num);
179 	if (current_type != NULL)
180 		wprintw (show_win,"Type: %s\n",current_type->name);
181 	else
182 		wprintw (show_win,"Type: %s\n","none");
183 
184 	refresh_show_win ();
185 }
186 
187 
redraw_all(void)188 void redraw_all (void)
189 {
190 	int min_lines = TITLE_WIN_LINES+SHOW_WIN_LINES+COMMAND_WIN_LINES+3;
191 	struct winsize ws;
192 	int	save_col, save_lines;
193 
194 	/* get the size of the terminal connected to stdout */
195 	ioctl(1, TIOCGWINSZ, &ws);
196 	/*
197 	 * Do it again because GDB doesn't stop before the first ioctl
198 	 * call, we want an up-to-date size when we're
199 	 * single-stepping.
200 	 */
201 	if (ioctl(1, TIOCGWINSZ, &ws) == 0) {
202 		if (ws.ws_row < min_lines)
203 			ws.ws_row = min_lines;
204 		if ((ws.ws_row != LINES) || (ws.ws_col != COLS)) {
205 			wmove (show_win,2,COLS-18);
206 			wclrtoeol(show_win);
207 			wrefresh(show_win);
208 			resizeterm(ws.ws_row, ws.ws_col);
209 			wresize(title_win, TITLE_WIN_LINES,COLS);
210 			wresize(show_win, SHOW_WIN_LINES,COLS);
211 			wresize(command_win, COMMAND_WIN_LINES,COLS);
212 			wresize(mt_win1, 1,COLS);
213 			wresize(mt_win2, 1,COLS);
214 			mvwin(mt_win2, LINES-COMMAND_WIN_LINES-1,0);
215 			mvwin(command_win, LINES-COMMAND_WIN_LINES,0);
216 			draw_title_win();
217 			show_pad_info.display_lines=LINES-TITLE_WIN_LINES-SHOW_WIN_LINES-COMMAND_WIN_LINES-2;
218 			show_pad_info.display_cols=COLS;
219 		}
220 	}
221 	clearok(title_win, 1);
222 	clearok(show_win, 1);
223 	clearok(command_win, 1);
224 	clearok(mt_win1, 1);
225 	clearok(mt_win2, 1);
226 	wrefresh(mt_win1);
227 	wrefresh(mt_win2);
228 	refresh_show_pad();
229 	refresh_show_win();
230 	refresh_title_win ();
231 	refresh_command_win ();
232 }
233