1 /*  Part of SWI-Prolog
2 
3     Author:        Jan Wielemaker
4     E-mail:        J.Wielemaker@vu.nl
5     WWW:           http://www.swi-prolog.org
6     Copyright (c)  1999-2012, University of Amsterdam
7     All rights reserved.
8 
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions
11     are met:
12 
13     1. Redistributions of source code must retain the above copyright
14        notice, this list of conditions and the following disclaimer.
15 
16     2. Redistributions in binary form must reproduce the above copyright
17        notice, this list of conditions and the following disclaimer in
18        the documentation and/or other materials provided with the
19        distribution.
20 
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25     COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32     POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #ifndef _CONSOLE_H_INCLUDED
36 #define _CONSOLE_H_INCLUDED
37 
38 #ifndef RLC_VENDOR
39 #define RLC_VENDOR TEXT("SWI")
40 #endif
41 
42 #define RLC_TITLE_MAX 256		/* max length of window title */
43 
44 #ifndef _export
45 #ifdef _MAKE_DLL
46 #define _export __declspec(dllexport)
47 #else
48 #define _export extern
49 #endif
50 #endif
51 
52 #include <signal.h>
53 #include <stddef.h>
54 #ifdef _MSC_VER
55 #include <stdint.h>
56 #pragma warning(disable : 4996)	/* deprecate open() etc */
57 #endif
58 
59 #define RLC_APPTIMER_ID	100		/* >=100: application timer */
60 
61 typedef struct
62 { int		 first;
63   int		 last;
64   int		 size;			/* size of the buffer */
65   TCHAR	        *buffer;		/* character buffer */
66   int		 flags;			/* flags for the queue */
67 } rlc_queue, *RlcQueue;
68 
69 #define RLC_EOF	0x1			/* Flags on the queue */
70 
71 typedef struct
72 { int		mark_x;
73   int		mark_y;
74 } rlc_mark, *RlcMark;
75 
76 typedef struct
77 { const TCHAR   *title;			/* window title */
78   const TCHAR   *key;			/* Last part of registry-key */
79   int		width;			/* # characters(0: default) */
80   int		height;			/* # characters (0: default) */
81   int		x;			/* # pixels (0: default) */
82   int		y;			/* # pixels (0: default) */
83   int		savelines;		/* # lines to save (0: default) */
84   TCHAR		face_name[32];		/* font name */
85   int		font_family;		/* family id */
86   int		font_size;
87   int		font_weight;
88   int		font_char_set;
89 } rlc_console_attr;
90 
91 typedef void * rlc_console;		/* console handle */
92 
93 typedef void	(*RlcUpdateHook)(void);	/* Graphics update hook */
94 typedef void	(*RlcTimerHook)(int);	/* Timer fireing hook */
95 typedef int	(*RlcRenderHook)(WPARAM);	/* Render one format */
96 typedef void	(*RlcRenderAllHook)(void); /* Render all formats */
97 typedef int	(*RlcMain)(rlc_console c, int, TCHAR**); /* main() */
98 typedef void	(*RlcInterruptHook)(rlc_console, int); /* Hook for Control-C */
99 typedef void	(*RlcResizeHook)(int, int); /* Hook for window change */
100 typedef void	(*RlcMenuHook)(rlc_console, const TCHAR *id); /* Hook for menu-selection */
101 typedef void	(*RlcFreeDataHook)(uintptr_t data); /* release data */
102 
103 #ifdef __WINDOWS__			/* <windows.h> is included */
104 					/* rlc_color(which, ...) */
105 #define RLC_WINDOW	  (0)		/* window background */
106 #define RLC_TEXT	  (1)		/* text color */
107 #define RLC_HIGHLIGHT	  (2)		/* selected text background */
108 #define RLC_HIGHLIGHTTEXT (3)		/* selected text */
109 
110 _export HANDLE	rlc_hinstance(void);	/* hInstance of WinMain() */
111 _export HWND	rlc_hwnd(rlc_console c); /* HWND of console window */
112 _export int	rlc_window_pos(rlc_console c,
113 			       HWND hWndInsertAfter,
114 			       int x, int y, int w, int h,
115 			       UINT flags); /* resize/reposition window */
116 _export int	rlc_main(HANDLE hI, HANDLE hPrevI,
117 			 LPTSTR cmd, int show, RlcMain main, HICON icon);
118 _export void	rlc_icon(rlc_console c, HICON icon);	/* Change icon */
119 _export COLORREF rlc_color(rlc_console c, int which, COLORREF color);
120 
121 typedef LRESULT	(*RlcMessageHook)(HWND hwnd, UINT message,
122 				  WPARAM wParam, LPARAM lParam);
123 _export RlcMessageHook  rlc_message_hook(RlcMessageHook hook);
124 
125 #endif /*__WINDOWS__*/
126 
127 _export RlcUpdateHook	rlc_update_hook(RlcUpdateHook updatehook);
128 _export RlcTimerHook	rlc_timer_hook(RlcTimerHook timerhook);
129 _export RlcRenderHook   rlc_render_hook(RlcRenderHook renderhook);
130 _export RlcRenderAllHook rlc_render_all_hook(RlcRenderAllHook renderallhook);
131 _export RlcInterruptHook rlc_interrupt_hook(RlcInterruptHook interrupthook);
132 _export RlcResizeHook	rlc_resize_hook(RlcResizeHook resizehook);
133 _export RlcMenuHook	rlc_menu_hook(RlcMenuHook menuhook);
134 _export int		rlc_copy_output_to_debug_output(int docopy);
135 
136 _export rlc_console	rlc_create_console(rlc_console_attr *attr);
137 _export void		rlc_title(rlc_console c,
138 				  TCHAR *title, TCHAR *old, int size);
139 _export void		rlc_yield(void);
140 _export void		rlc_word_char(int chr, int isword);
141 _export int		rlc_is_word_char(int chr);
142 _export int		rlc_iswin32s(void);	/* check for Win32S */
143 
144 _export void		rlc_free(void *ptr);
145 _export void *		rlc_malloc(size_t size);
146 _export void *		rlc_realloc(void *ptr, size_t size);
147 
148 _export size_t		rlc_read(rlc_console c, TCHAR *buf, size_t cnt);
149 _export size_t		rlc_write(rlc_console c, TCHAR *buf, size_t cnt);
150 _export int		rlc_close(rlc_console c);
151 _export int		rlc_flush_output(rlc_console c);
152 
153 _export int		getch(rlc_console c);
154 _export int		getche(rlc_console c);
155 _export int		getkey(rlc_console c);
156 _export int		kbhit(rlc_console c);
157 _export void		ScreenGetCursor(rlc_console c, int *row, int *col);
158 _export void		ScreenSetCursor(rlc_console c, int row, int col);
159 _export int		ScreenCols(rlc_console c);
160 _export int		ScreenRows(rlc_console c);
161 
162 _export int		rlc_insert_menu_item(rlc_console c,
163 					     const TCHAR *menu,
164 					     const TCHAR *label,
165 					     const TCHAR *before);
166 _export int		rlc_insert_menu(rlc_console c,
167 					const TCHAR *label,
168 					const TCHAR *before);
169 
170 		 /*******************************
171 		 *	  GET/SET VALUES	*
172 		 *******************************/
173 
174 #define RLC_APPLICATION_THREAD		0 /* thread-handle of application */
175 #define RLC_APPLICATION_THREAD_ID	1 /* thread id of application */
176 #define RLC_VALUE(N)			(1000+(N))
177 
178 _export int		rlc_get(rlc_console c, int what,
179 				uintptr_t *val);
180 _export int		rlc_set(rlc_console c, int what,
181 				uintptr_t val,
182 				RlcFreeDataHook hook);
183 
184 
185 		 /*******************************
186 		 *	 LINE EDIT STUFF	*
187 		 *******************************/
188 
189 typedef struct _line
190 { rlc_mark	origin;			/* origin of edit */
191   size_t	point;			/* location of the caret */
192   size_t	size;			/* # characters in buffer */
193   size_t	allocated;		/* # characters allocated */
194   size_t	change_start;		/* start of change */
195   int		complete;		/* line is completed */
196   int		reprompt;		/* repeat the prompt */
197   TCHAR	       *data;			/* the data (malloc'ed) */
198   rlc_console	console;		/* console I belong to */
199 } line, *Line;
200 
201 #define COMPLETE_MAX_WORD_LEN 256
202 #define COMPLETE_MAX_MATCHES 100
203 
204 #define COMPLETE_INIT	   0
205 #define COMPLETE_ENUMERATE 1
206 #define COMPLETE_CLOSE	   2
207 
208 struct _complete_data;
209 
210 typedef int (*RlcCompleteFunc)(struct _complete_data *);
211 
212 typedef struct _complete_data
213 { Line		line;			/* line we are completing */
214   int		call_type;		/* COMPLETE_* */
215   int		replace_from;		/* index to start replacement */
216   int		quote;			/* closing quote */
217   int		case_insensitive;	/* if TRUE: insensitive match */
218   TCHAR		candidate[COMPLETE_MAX_WORD_LEN];
219   TCHAR		buf_handle[COMPLETE_MAX_WORD_LEN];
220   RlcCompleteFunc function;		/* function for continuation */
221   void	       *ptr_handle;		/* pointer handle for client */
222   intptr_t	num_handle;		/* numeric handle for client */
223 } rlc_complete_data, *RlcCompleteData;
224 
225 _export RlcCompleteFunc rlc_complete_hook(RlcCompleteFunc func);
226 
227 _export TCHAR	*read_line(rlc_console console);
228 _export int	rlc_complete_file_function(RlcCompleteData data);
229 _export void	rlc_init_history(rlc_console c, int size);
230 _export void	rlc_add_history(rlc_console c, const TCHAR *line);
231 _export int	rlc_bind(int chr, const char *fname);
232 _export int	rlc_for_history(
233 		    rlc_console b,
234 		    int (*handler)(void *ctx, int no, const TCHAR *line),
235 		    void *ctx);
236 
237 #endif /* _CONSOLE_H_INCLUDED */
238