* New version 2.26
[alpine.git] / alpine / osdep / termin.wnt.c
blob9db85406f0769b429e2b081ed75237aae2cfee7e
1 /*
2 * ========================================================================
3 * Copyright 2006-2008 University of Washington
4 * Copyright 2013-2022 Eduardo Chappa
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
15 #include <system.h>
16 #include <general.h>
18 #include "../../c-client/mail.h" /* for MAILSTREAM and friends */
19 #include "../../c-client/osdep.h"
20 #include "../../c-client/rfc822.h" /* for soutr_t and such */
21 #include "../../c-client/misc.h" /* for cpystr proto */
22 #include "../../c-client/utf8.h" /* for CHARSET and such*/
23 #include "../../c-client/imap4r1.h"
25 #include "../../pith/charconv/utf8.h"
26 #include "../../pith/charconv/filesys.h"
28 #include "../../pith/osdep/color.h"
30 #include "../../pith/debug.h"
31 #include "../../pith/state.h"
32 #include "../../pith/conf.h"
33 #include "../../pith/detach.h"
35 #include "../pico/estruct.h"
36 #include "../pico/pico.h"
38 #include "../../pico/osdep/raw.h"
39 #include "../../pico/osdep/signals.h"
40 #include "../../pico/osdep/mouse.h"
41 #include "../../pico/keydefs.h"
43 #include "../talk.h"
44 #include "../radio.h"
45 #include "../dispfilt.h"
46 #include "../signal.h"
47 #include "../status.h"
48 #include "../titlebar.h"
50 #include "../../pico/osdep/mswin.h"
52 #include "termin.gen.h"
53 #include "termin.wnt.h"
54 #include "termout.gen.h"
56 #define RETURN_CH(X) return(key_rec ? ((*key_rec)(X)) : (int)(X))
58 /* global to tell us if the window was resized. */
59 static int DidResize = FALSE;
61 int pine_window_resize_callback(void);
63 /*----------------------------------------------------------------------
64 Initialize the tty driver to do single char I/O and whatever else
66 Input: struct pine
68 Result: tty driver is put in raw mode
69 ----------------------------------------------------------------------*/
70 int
71 init_tty_driver(struct pine *pine)
73 mswin_showwindow();
74 mswin_setresizecallback (pine_window_resize_callback);
75 init_mouse (); /* always a mouse under windows? */
76 return(PineRaw(1));
80 /*----------------------------------------------------------------------
81 End use of the tty, put it back into it's normal mode
83 Input: struct pine
85 Result: tty driver mode change
86 ----------------------------------------------------------------------*/
87 void
88 end_tty_driver(struct pine *pine)
90 dprint((2, "about to end_tty_driver\n"));
91 mswin_clearresizecallback (pine_window_resize_callback);
94 /*----------------------------------------------------------------------
95 Actually set up the tty driver
97 Args: state -- which state to put it in. 1 means go into raw, 0 out of
99 Result: returns 0 if successful and -1 if not.
100 ----*/
103 PineRaw(int state)
105 ps_global->low_speed = 0;
106 return(0);
109 /*----------------------------------------------------------------------
110 Read input characters with lots of processing for arrow keys and such
112 Input: none
114 Result: returns the character read. Possible special chars defined h file
117 This deals with function and arrow keys as well.
118 It returns ^T for up , ^U for down, ^V for forward and ^W for back.
119 These are just sort of arbitrarily picked and might be changed.
120 They are defined in defs.h. Didn't want to use 8 bit chars because
121 the values are signed chars, though it ought to work with negative
122 values.
124 The idea is that this routine handles all escape codes so it done in
125 only one place. Especially so the back arrow key can work when entering
126 things on a line. Also so all function keys can be broken and not
127 cause weird things to happen.
128 ----------------------------------------------------------------------*/
131 read_char(int tm)
133 unsigned ch = 0;
134 time_t timein;
135 int (*key_rec)(int);
137 key_rec = key_recorder;
138 if(ps_global->conceal_sensitive_debugging && debug < 10)
139 key_rec = NULL;
141 if(process_config_input((int *) &ch))
142 RETURN_CH(ch);
144 if (DidResize) {
145 DidResize = FALSE;
146 fix_windsize(ps_global);
147 return(KEY_RESIZE);
150 mswin_setcursor(MSWIN_CURSOR_ARROW);
152 if(tm){
153 timein = time(0L);
154 /* mswin_charavail() Yields control to other window apps. */
155 while (!mswin_charavail()) {
156 if(time(0L) >= timein + (time_t) tm){
157 ch = (tm < IDLE_TIMEOUT) ? NO_OP_COMMAND : NO_OP_IDLE;
158 goto gotone;
160 if (DidResize) {
161 DidResize = FALSE;
162 fix_windsize(ps_global);
163 return(KEY_RESIZE);
165 if(checkmouse(&ch,0,0,0))
166 goto gotone;
169 else
170 while(!mswin_charavail())
171 if(checkmouse(&ch,0,0,0))
172 goto gotone;
174 ch = mswin_getc_fast();
177 * mswin_getc_fast() returns UCS type codes (see keydefs.h). Map
178 * those values to what is expected from read_char(): Ctrl keys being
179 * in the 0..0x1f range, NO_OP_COMMAND, etc.
181 if(ch == NODATA)
182 ch = NO_OP_COMMAND;
183 else if(ch & CTRL)
185 ch &= ~CTRL;
186 if(ch >= '@' && ch < '@' + 32) {
187 ch -= '@';
189 else {
191 * This could be a ctrl key this part of Pine doesn't understand.
192 * For example, CTRL|KEY_LEFT. Map these to nops.
194 ch = NO_OP_COMMAND;
198 gotone:
199 /* More obtuse key mapping. If it is a mouse event, the return
200 * may be KEY_MOUSE, which indicates to the upper layer that it
201 * is a mouse event. Return it here to avoid the code that
202 * follows which would do a (ch & 0xff).
204 if (ch == KEY_MOUSE)
205 RETURN_CH(ch);
208 * WARNING: Hack notice.
209 * the mouse interaction complicates this expression a bit as
210 * if function key mode is set, PFn values are setup for return
211 * by the mouse event catcher. For now, just special case them
212 * since they don't conflict with any of the DOS special keys.
214 if((ch & 0xff) == ctrl('Z'))
215 RETURN_CH(do_suspend());
217 RETURN_CH (ch);
220 void
221 flush_input(void)
223 mswin_flush_input();
226 void
227 init_keyboard(int use_fkeys)
231 void
232 end_keyboard(int use_fkeys)
237 pre_screen_config_opt_enter(char *utf8string, int utf8string_size,
238 char *utf8prompt, ESCKEY_S *escape_list,
239 HelpType help, int *flags)
241 mswin_setwindow(NULL, NULL, NULL, NULL, NULL, NULL);
242 return(win_dialog_opt_enter(utf8string, utf8string_size, utf8prompt, escape_list,
243 help, flags));
247 win_dialog_opt_enter(char *utf8string, int utf8string_size, char *utf8prompt,
248 ESCKEY_S *escape_list, HelpType help, int *flags)
250 MDlgButton button_list[12];
251 LPTSTR free_names[12];
252 LPTSTR free_labels[12];
253 int i, b, return_v;
254 char **help_text;
255 char *utf8, *saved_string = NULL;
256 UCS *string, *s, *prompt;
257 int n;
259 memset(&free_names, 0, sizeof(LPTSTR) * 12);
260 memset(&free_labels, 0, sizeof(LPTSTR) * 12);
261 memset (&button_list, 0, sizeof (MDlgButton) * 12);
262 b = 0;
263 for (i = 0; escape_list && escape_list[i].ch != -1 && i < 11; ++i) {
264 if (escape_list[i].name != NULL
265 && escape_list[i].ch > 0 && escape_list[i].ch < 256) {
266 button_list[b].ch = escape_list[i].ch;
267 button_list[b].rval = escape_list[i].rval;
268 free_names[b] = utf8_to_lptstr(escape_list[i].name);
269 button_list[b].name = free_names[b];
270 free_labels[b] = utf8_to_lptstr(escape_list[i].label);
271 button_list[b].label = free_labels[b];
272 b++;
276 button_list[b].ch = -1;
278 if(utf8string)
279 saved_string = cpystr(utf8string);
281 /* assumption here is that HelpType is char ** */
282 help_text = help;
284 n = utf8string_size;
285 string = (UCS *) fs_get(n * sizeof(UCS));
286 s = utf8_to_ucs4_cpystr(utf8string);
287 if(s){
288 ucs4_strncpy(string, s, n);
289 string[n-1] = '\0';
290 fs_give((void **) &s);
293 prompt = utf8_to_ucs4_cpystr(utf8prompt ? utf8prompt : "");
295 return_v = mswin_dialog(prompt, string, n,
296 (flags && *flags & OE_APPEND_CURRENT),
297 (flags && *flags & OE_PASSWD_NOAST) ? 10 :
298 (flags && *flags & OE_PASSWD) ? 1 : 0,
299 button_list,
300 help_text, flags ? *flags : OE_NONE);
302 for(i = 0; i < 12; i++){
303 if(free_names[i])
304 fs_give((void **) &free_names[i]);
305 if(free_labels[i])
306 fs_give((void **) &free_labels[i]);
309 utf8 = ucs4_to_utf8_cpystr(string);
310 if(utf8){
311 strncpy(utf8string, utf8, utf8string_size);
312 utf8string[utf8string_size-1] = '\0';
313 fs_give((void **) &utf8);
316 if(string)
317 fs_give((void **) &string);
319 if(prompt)
320 fs_give((void **) &prompt);
322 if(flags && (saved_string && !utf8string || !saved_string && utf8string ||
323 (saved_string && string && strcmp(saved_string, utf8string))))
324 *flags |= OE_USER_MODIFIED;
326 if(saved_string)
327 fs_give((void **) &saved_string);
329 return(return_v);
333 /*----------------------------------------------------------------------
334 Flag the fact the window has resized.
337 pine_window_resize_callback (void)
339 DidResize = TRUE;
340 return(0);
343 void
344 intr_proc(int state)
346 return; /* no op */