Debian Codegraph @

common/core_main.h

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #ifndef CORE_MAIN_H
19 #define CORE_MAIN_H 1
20
21 #include "free42.h"
22
23
24 /**********************************/
25 /* Shell/Core interface functions */
26 /**********************************/
27
28 /* core_init()
29 *
30 * This function initializes the emulator core. If the read_state parameter is
31 * 1, the core should read saved state from the file named by the
32 * state_file_name parameter, reading from the offset indicated by the offset
33 * parameter; if read_state is 0, or if there is a problem reading the saved
34 * state, it should perform a hard reset.
35 * If the read_state parameter is 1, the 'version' parameter should contain the
36 * state file version number; otherwise its value is not used.
37 * This is guaranteed to be the first function called on the emulator core.
38 */
39 void core_init(int read_state, int4 version, const char *state_file_name, int offset);
40
41 /* core_save_state()
42 *
43 * This function is called by the mobile apps when they are placed in
44 * background mode, and by the desktop apps when they are shutting down.
45 * It writes all the simulator's persistent state to a file given by the
46 * state_file_name parameter, creating it if it doesn't already exist.
47 */
48 void core_save_state(const char *state_file_name);
49
50 /* core_cleanup()
51 *
52 * This function deletes down the emulator core state from memory. It may be
53 * called during app shutdown, although that's not really necessary unless you
54 * are checking for memory leaks. It should be called between saving state and
55 * loading a new state, that is, when switching states.
56 */
57 void core_cleanup();
58
59 /* core_repaint_display()
60 *
61 * This function asks the emulator core to repaint the display. The core will
62 * respond by immediately calling shell_blitter() to repaint the entire
63 * display.
64 * The shell uses this function to re-generate the display after switching
65 * skins.
66 */
67 void core_repaint_display();
68
69 /* core_menu()
70 *
71 * The shell uses this function to check if a menu is active. This affects
72 * whether or not clicking in the display, to activate menu keys, is
73 * enabled.
74 */
75 bool core_menu();
76
77 /* core_alpha_menu()
78 *
79 * The shell uses this function to check if the core is in "alpha" mode (i.e.
80 * the ALPHA menu or any of its submenus is active). This affects how events
81 * from the keyboard (the real PC keyboard, not the on-screen one emulated by
82 * Free42) are handled: in alpha mode, printable ASCII characters are sent
83 * straight to the core; outside alpha mode, all key events are translated
84 * sequences of HP-42S key events according to the keymap file.
85 */
86 bool core_alpha_menu();
87
88 /* core_hex_menu()
89 *
90 * The shell uses this function to check if the core is in "hex" mode (i.e.
91 * the A..F submenu of the BASE application is active). This affects how events
92 * from the keyboard (the real PC keyboard, not the on-screen one emulated by
93 * Free42) are handled: in hex mode, 'A' through 'F' and 'a' through 'f' are
94 * translated to keycodes 1 through 6, regardless of the keyboard map.
95 */
96 bool core_hex_menu();
97
98 /* core_special_menu_key()
99 *
100 * The shell can call this function if the physical cursor left, cursor right,
101 * or delete keys have been pressed; the core will return the appropriate
102 * menu key to trigger. This is to support menus like the matrix editor.
103 * Which can be: 1=left, 2=shift left, 3=right, 4=shift right, 5=del.
104 * The return value is a menu key number 1-6, or 0 if no special menu key
105 * is available.
106 */
107 int core_special_menu_key(int which);
108
109 /* core_keydown()
110 *
111 * This function informs the emulator core that an HP-42S key was pressed. Keys
112 * are identified using a numeric key code, which corresponds to the key
113 * numbers returned by the HP-42S 'GETKEY' function: 'Sigma+' is 1, '1/x' is 2,
114 * and so on. The shift key is 28 -- 'shift' handling is performed by the
115 * emulator core, so it needs to receive raw key codes, unlike the 'GETKEY'
116 * function, which handles the shift key itself and then returns key codes in
117 * the range 38..74 for shifted keys. The core_keydown() function should only
118 * be called with key codes from 1 to 37, inclusive.
119 * Keys that cause immediate action should be handled immediately when this
120 * function is called, and calls to core_keytimeout1(), core_keytimeout2(),
121 * and core_keyup() should be ignored (until the next core_keydown(), that
122 * is!). Keys that are handled only when *released* should not be handled
123 * immediately when this function is called; the emulator core should store the
124 * key code, handle core_keytimeout1() and core_keytimeout2() as appropriate,
125 * and not perform any action until core_keyup() is called.
126 * RETURNS: a flag indicating whether or not the front end should call this
127 * function again as soon as possible. This will be 1 if the calculator is
128 * running a user program, and is only returning execution to the shell because
129 * it has detected that there is a pending event.
130 * The 'enqueued' pointer is a return parameter that the emulator core uses to
131 * tell the shell if it has enqueued the keystroke. If this is 1, the shell
132 * should not send call timeout1(), timeout2(), or keyup() for this keystroke.
133 * NOTE: a key code of 0 (zero) signifies 'no key'; this is needed if the shell
134 * is calling this function because it asked to be called (by returning 1 the
135 * last time) but no keystrokes are available. It is not necessary to balance
136 * the keydown call with a keyup in this case.
137 * The 'repeat' pointer is a return parameter that the emulator core uses to
138 * ask the shell to auto-repeat the current key. If this is set to 1 or 2, the
139 * shell will not call timeout1() and timeout2(), but will repeatedly call
140 * core_repeat() until the key is released. (1 requests a slow repeat rate, for
141 * SST/BST; 2 requests a fast repeat rate, for number/alpha entry.)
142 */
143 bool core_keydown(int key, bool *enqueued, int *repeat);
144
145 /* core_keydown_command()
146 *
147 * This function is equivalent to core_keydown(), except that instead of a key
148 * number, it expects a command name. This can be used to implement skins that
149 * map skin keys or keyboard keys directly to calculator commands, which is
150 * useful if you want to assign commands to the keyboard that aren't directly
151 * mapped on the original calculator. Without this, you'd have to create such a
152 * mapping by creating a macro that performs XEQ and spells out the command
153 * name, or selects the command from a menu or the FCN catalog, all of which
154 * have potentially undesirable side effects.
155 * The is_text parameter signals that the command is actually literal text to
156 * be entered. This should only happen when ALPHA is active. This corresponds
157 * to secondary macros with '' delimiters.
158 */
159 bool core_keydown_command(const char *name, bool is_text, bool *enqueued, int *repeat);
160
161 /* core_repeat()
162 *
163 * This function is called by the shell to signal auto-repeating key events.
164 * It is the core's responsibility to keep track of *which* key is repeating.
165 * The function can return 0, to request repeating to stop; 1, which requests
166 * a slow repeat rate, for SST/BST; or 2, which requests a fast repeat rate,
167 * for number/alpha entry.
168 */
169 int core_repeat();
170
171 /* core_keytimeout1()
172 *
173 * This function informs the emulator core that the currently pressed key has
174 * been held down for 1/4 of a second. (If the key is released less than 1/4
175 * second after being pressed, this function is not called.)
176 * For keys that do not execute immediately, this marks the moment when the
177 * calculator displays the key's function name.
178 */
179 void core_keytimeout1();
180
181 /* core_keytimeout2()
182 *
183 * This function informs the emulator core that 2 seconds have passed since
184 * core_keytimeout1() was called. (If the key is released less than 2 seconds
185 * after core_keytimeout1() is called, this function is not called.)
186 * This marks the moment when the calculator switches from displaying the key's
187 * function name to displaying 'NULL' (informing the user that the key has been
188 * annulled and so no operation will be performed when it is released).
189 */
190 void core_keytimeout2();
191
192 /* core_timeout3()
193 *
194 * A wakeup call that the core can request by calling shell_request_timeout3().
195 * Used to implement PSE and the brief linger period after MEM, SHOW, and
196 * shift-VARMENU. The 'repaint' parameter says whether the callback is invoked
197 * because the timeout period expired (1), or because a key event is on its
198 * way (0); if a key event is on its way, the emulator core should not repaint
199 * the screen, in order to avoid flashing.
200 * If the function returns 'true', this means that the timeout was used for
201 * PSE, and the shell should resume program execution.
202 */
203 bool core_timeout3(bool repaint);
204
205 /* core_keyup()
206 *
207 * This function informs the emulator core that the currently pressed key (that
208 * is, the one whose key code was given to it in the most recent call to
209 * core_keydown()) has been released.
210 * This function is always called when a key is released, regardless of how
211 * long it was held down, and regardless of whether or not core_keytimeout1()
212 * and core_keytimeout2() were called following the most recent
213 * core_keydown().
214 * RETURNS: a flag indicating whether or not the front end should call this
215 * function again as soon as possible. This will be 1 if the calculator is
216 * running a user program, and is only returning execution to the shell because
217 * it has detected that there is a pending event.
218 */
219 bool core_keyup();
220
221 /* core_powercycle()
222 *
223 * This tells the core to pretend that a power cycle has just taken place.
224 * Usually called right after core_init().
225 * The core should respond by performing power-down activities, immediately
226 * followed by power-up activities. (The emulator core is never expected to
227 * actually emulate the state of *being* off -- the host environment can handle
228 * being off just fine by itself, thank you very much -- it's just the side
229 * effects of the *transitions* of a power cycle we want emulated (stopping
230 * program execution, restarting it if flag 11 is set, clearing flags 11 and
231 * 44, and maybe other stuff once I discover it).
232 * This function returns a flag telling the shell whether or not the core wants
233 * to run again -- this will be true if flag 11 was set, so there's program
234 * execution to be done.
235 */
236 bool core_powercycle();
237
238 /* core_list_programs()
239 *
240 * This function is called by the shell when the user activates the Export
241 * Program command. The core returns a list of program names, corresponding to
242 * the programs as they appear in the PGM catalog, but in reverse order (the
243 * .END. is last). Each item corresponds to one program, and consists of the
244 * names of all the global labels in that program. Programs that contain no
245 * global labels are given the name END (or .END.).
246 * The indexes into the list of program names should be used to identify
247 * programs to core_export_programs().
248 * The function returns a dynamically allocated buffer. The first four bytes
249 * are the number of programs returned (as a big-endian int) which is
250 * guaranteed to always be at least 1. This is followed by a sequence of
251 * 'program_count' null-terminated strings.
252 * This function will return NULL if it fails to allocate the buffer.
253 * The caller should free() the buffer once it is finished using it.
254 */
255 char *core_list_programs();
256
257 /* core_program_size()
258 * This function returns the size of a program, specified by its index.
259 * The indexes correspond to those returned by core_list_programs(). The caller
260 * should *only* use those indexes; any indexes outside of that range will
261 * cause weirdness and mayhem.
262 * The size returned by this function does not include the 3 bytes for the
263 * END; this matches what the HP-42S displays on line 00, but the caller should
264 * be aware that the actual byte stream produced by core_export_programs() will
265 * be 3 bytes longer.
266 */
267 int4 core_program_size(int prgm_index);
268
269 /* core_export_programs()
270 *
271 * This function is called by the shell after the user has selected a nonempty
272 * set of programs (from the list returned by core_list_programs()) and
273 * confirmed the operation (supplied a file name etc.).
274 * The 'count' parameter indicates how many programs are to be exported; the
275 * 'indexes' parameter is an array of program indexes.
276 * When called by the core during state file saving, raw_file_name will be
277 * NULL, which tells it to write to the already-opened state file.
278 */
279 void core_export_programs(int count, const int *indexes, const char *raw_file_name);
280
281 /* core_import_programs()
282 *
283 * This function imports programs from the file named by the raw_file_name
284 * parameter. It will read at most num_progs programs. When called by the
285 * shell, num_progs should be 0, which tells it to load the entire file.
286 * When called by the core during state file loading, raw_file_name will be
287 * NULL, which tells it to read from the already-opened state file.
288 */
289 void core_import_programs(int num_progs, const char *raw_file_name);
290
291 /* core_copy()
292 *
293 * Returns a string representation of the contents of the X register. In
294 * program mode, returns a listing of the current program. I alpha mode,
295 * returns the contents of the alpha register.
296 * Used by the shell to implement the Copy command.
297 * The caller should free the returned text using free(3).
298 */
299 char *core_copy();
300
301 /* core_paste()
302 *
303 * In normal mode, puts the given value on the stack, parsing it as tab-
304 * delimited spreadsheet cells, complex or real scalars, or, if nothing
305 * matches, as a simple string. In program mode, attempts to parse the text as
306 * a program listing, and adds the converted code as a new program at the end
307 * of program memory. In alpha mode, pastes the text in the alpha register,
308 * appending it to whatever is already there.
309 * Used by the shell to implement the Paste command.
310 */
311 void core_paste(const char *s);
312
313 #if defined(ANDROID) || defined(IPHONE)
314
315 /* core_get_char_pixels()
316 *
317 * Gets character pixels from the calculator's 8x5 characters, for the
318 * given UTF-8 encoded character, provided as a null-terminated string.
319 */
320 void core_get_char_pixels(const char *ch, char *pixels);
321
322 #endif
323
324 /* core_update_allow_big_stack()
325 *
326 * Updates the big stack state and the UI to reflect a change in the
327 * global Allow Big Stack setting.
328 */
329 void core_update_allow_big_stack();
330
331 /* core_settings
332 *
333 * This is a struct that stores user-configurable core settings. The shell
334 * should provide the appropriate controls in a "Preferences" dialog box to
335 * allow the user to view and change these settings.
336 */
337 struct core_settings_struct {
338 bool matrix_singularmatrix;
339 bool matrix_outofrange;
340 bool auto_repeat;
341 bool allow_big_stack;
342 bool localized_copy_paste;
343 };
344
345 extern core_settings_struct core_settings;
346
347
348 /*******************/
349 /* Keyboard repeat */
350 /*******************/
351
352 extern int repeating;
353 extern int repeating_shift;
354 extern int repeating_key;
355
356
357 /*******************/
358 /* Other functions */
359 /*******************/
360
361 extern bool quitting;
362
363 void set_alpha_entry(bool state);
364 void set_running(bool state);
365 bool program_running();
366 bool alpha_active();
367 void set_annunciators(int updn, int shf, int prt, int run, int g, int rad);
368 int dequeue_key();
369
370 void do_interactive(int command);
371 int find_builtin(const char *name, int namelen);
372
373 void sst();
374 void bst();
375
376 int find_menu_key(int key);
377 void start_incomplete_command(int cmd_id);
378 void finish_command_entry(bool refresh);
379 void finish_xeq();
380 bool start_alpha_prgm_line();
381 void finish_alpha_prgm_line();
382 int shiftcharacter(char c);
383 void set_old_pc(int4 pc);
384 const char *number_format();
385
386
387 #endif