1 /*
2   Window
3 */
4 
5 #include "window.h"
6 
7 #include "config.h"
8 #include "console.h"
9 #include "file.h"
10 #include "head.h"
11 #include "infocom.h"
12 #include "jump.h"
13 #include "mem.h"
14 #include "os.h"
15 #include "print.h"
16 #include "shared.h"
17 #include "var.h"
18 #include "wio.h"
19 
20 static bool cursor_pos_saved;
21 
22 /*
23 **	Enhanced Windowing and Screen Printing Functions.
24 */
25 
set_current_window(word the_window)26 void set_current_window(word the_window)
27 {
28   extern word current_window;
29   extern bool disable_script;
30 
31   if(hd_five())
32     flush_prt_buff();
33   current_window = the_window;
34   if(the_window != 0)
35   {
36     /* Use the Upper Window. */
37     save_cursor_position();
38     use_window(STATUS_WINDOW);
39     disable_script = 1;
40     goto_xy(0, 0);
41   }
42   else
43   {
44     /* Use the Lower Window. */
45     use_window(TEXT_WINDOW);
46     disable_script = 0;
47     restore_cursor_position();
48     set_text_mode(0);
49   }
50 }
51 
split_screen(word param)52 void split_screen(word param)
53 {
54   extern bool windowing_enabled;
55   extern word status_height;
56   extern int linecount;
57   word old_height = status_height;
58 
59   /* Addition from Zip set_status_size */
60   if(!hd_plus())
61     ++param;
62 
63   if(param == 0)
64   {
65     /* Use the entire Screen. */
66 
67     use_window(FULL_SCREEN);
68     /* restore_cursor_position(); */
69     windowing_enabled = 0;
70     status_height = 0;
71     linecount = 0;
72   }
73   else
74   {
75     windowing_enabled = 1;
76     status_height = min(param, hd_height() - 1);
77     if(!hd_plus())
78     {
79       save_cursor_position();
80       erase_window(0, status_height);
81       restore_cursor_position();
82     }
83     use_window(TEXT_WINDOW);
84     if(hd_five())
85     {
86       int x, y;
87       get_xy(&x, &y);
88       if(y < status_height)
89         set_xy(0, hd_height() - 1);
90     }
91   }
92   {
93     int i;
94     start_update();
95     for(i = status_height; i < old_height; ++i) touch(i);
96     finish_update();
97   }
98 }
99 
save_cursor_position(void)100 void save_cursor_position(void)
101 {
102   if(!cursor_pos_saved)
103   {
104     save_cursor();
105     cursor_pos_saved = 1;
106   }
107 }
108 
restore_cursor_position(void)109 void restore_cursor_position(void)
110 {
111   if(cursor_pos_saved)
112   {
113     restore_cursor();
114     cursor_pos_saved = 0;
115   }
116   else if(hd_version() > VERSION_4)
117   {
118     set_xy(0, hd_height() - 1);
119   }
120 }
121 
do_beep(word sound)122 void do_beep(word sound)
123 {
124   USE(sound);
125 }
126 
erase_line(word mode)127 void erase_line(word mode)
128 {
129   if(mode == 1)
130     erase_to_eoln();
131 }
132 
do_clear_screen(word param)133 void do_clear_screen(word param)
134 {
135   extern word status_height;
136 
137   switch((signed_word) param)
138   {
139     case -1:
140       split_screen((word) 0);
141       erase_window(0, (word) hd_height());
142       if(hd_version() == VERSION_4)
143 	set_xy(0, hd_height() - 1);
144       break;
145     case 0:
146       erase_window(status_height, (word) hd_height());
147       break;
148     case 1:
149       erase_window(0, status_height);
150       break;
151   }
152 }
153 
set_cursor_posn(word y,word x)154 void set_cursor_posn(word y, word x)
155 {
156   extern word current_window;
157 
158   if(current_window != 0)
159   {
160     if(hd_five())
161       flush_prt_buff();
162     goto_xy(x - 1, y - 1);
163   }
164 }
165 
set_text_mode(word mode)166 void set_text_mode(word mode)
167 {
168   /* Pack down from bit field.  Limits the display somewhat. */
169   int font = FONT_NORMAL;
170   if(mode & 8) font = FONT_FIXED;
171   if(mode & 4) font = FONT_EMPH;
172   if(mode & 2) font = FONT_BOLD;
173   if(mode & 1) font = FONT_REVS;
174   if(mode == 9) font = FONT_FIXED_REVS;
175   print_char(make_font_request(font));
176 }
177 
set_text_colour(word fore,word back)178 void set_text_colour(word fore,  word back)
179 {
180   print_char(make_attr_request(make_attr(fore, back)));
181 }
182 
io_buffer_mode(word param)183 void io_buffer_mode(word param)
184 {
185   extern bool use_buffered_io;
186 
187   if(param == 0)
188   {
189     use_buffered_io = 0;
190     flush_prt_buff();
191   }
192   else
193   {
194     use_buffered_io = 1;
195   }
196 }
197 
io_mode(signed_word mode,word buffer)198 void io_mode(signed_word mode, word buffer)
199 {
200   extern long_word internal_io_buffer;
201   extern word int_io_buff_length;
202   extern bool enable_screen;
203   extern bool script_on;
204   extern bool use_internal_buffer;
205 
206   switch(mode)
207   {
208     case 1:
209       enable_screen = 1;
210       break;
211     case 2:
212       if(!script_on)
213       {
214         hd_set_script(1);
215         script_on = open_script();
216 	if(!script_on)
217 	  hd_err_script();
218       }
219       break;
220     case 3:
221       use_internal_buffer = 1;
222       internal_io_buffer  = buffer;
223       int_io_buff_length  = 0;
224       break;
225     case -1:
226       enable_screen = 0;
227       break;
228     case -2:
229       script_on = 0;
230       hd_set_script(0);
231       close_script();
232       break;
233     case -3:
234       use_internal_buffer = 0;
235       wr_word_addr(internal_io_buffer, int_io_buff_length);
236       /* internal_io_buffer += 1; */
237       wr_byte_addr(internal_io_buffer + 2 + int_io_buff_length, 0);
238       break;
239   }
240 }
241 
get_key(void)242 void get_key(void)
243 {
244   extern word param_stack[];
245   extern int linecount;
246 
247   if(hd_five())
248     flush_prt_buff();
249 
250   linecount = 0;
251   if(param_stack[1] == 1)
252   {
253 #if 0 /* wait_for_key is TRUE */
254     if(param_stack[0] < 3
255     || wait_for_key(param_stack[2] / 10, param_stack[3]))
256 #endif
257       store(read_the_key());
258   }
259   else
260   {
261     store(0);
262   }
263 }
264 
read_the_key(void)265 word read_the_key(void)
266 {
267   int ch, done;
268   do
269   {
270     done = 1;
271     ch = get_ch();
272     switch(ch)
273     {
274       case '\a':
275       case '\013':
276       case '\b':
277       case '\r':
278 	break;
279       case 'P' - '@':
280 	ch = 0x5C;
281 	break;
282       case 'N' - '@':
283 	ch = ' ';
284 	break;
285       case 'F' - '@':
286 	ch = '+';
287 	break;
288       case 'B' - '@':
289 	ch = '-';
290 	break;
291       case '\n':
292 	ch = '\r';
293 	break;
294       default:
295 	done = ch >= ' ';
296 	break;
297     }
298   } while(!done);
299   return ch;
300 }
301 
302 #if 0
303 bool wait_for_key(word time_out, word address)
304 {
305   /* Optimise until kbd_hit implemented fully */
306   return 1;
307   word delay = time_out;
308   do
309   {
310     do
311     {
312       long now = os_time();
313       do
314       {
315 	if(kbd_hit())
316 	  return 1;
317       } while(os_time() - now < 1);
318     } while(--delay != 0);
319   } while(special_gosub(address) == 0);
320   return 0;
321 }
322 #endif
323