Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
emutools.h
Go to the documentation of this file.
1 /* Xemu - emulation (running on Linux/Unix/Windows/OSX, utilizing SDL2) of some
2  8 bit machines, including the Commodore LCD and Commodore 65 and MEGA65 as well.
3  Copyright (C)2016-2022 LGB (Gábor Lénárt) <lgblgblgb@gmail.com>
4 
5  The goal of emutools.c is to provide a relative simple solution
6  for relative simple emulators using SDL2.
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21 
22 #ifndef XEMU_COMMON_EMUTOOLS_H_INCLUDED
23 #define XEMU_COMMON_EMUTOOLS_H_INCLUDED
24 
25 #include <SDL.h>
27 
28 #ifndef XEMU_NO_SDL_DIALOG_OVERRIDE
29 extern int (*SDL_ShowSimpleMessageBox_custom)(Uint32, const char*, const char*, SDL_Window* );
30 extern int (*SDL_ShowMessageBox_custom)(const SDL_MessageBoxData*, int* );
31 #else
32 #define SDL_ShowSimpleMessageBox_custom SDL_ShowSimpleMessageBox
33 #define SDL_ShowMessageBox_custom SDL_ShowMessageBox
34 #endif
35 
36 #ifdef XEMU_ARCH_HTML
37 #include <emscripten.h>
38 #define EMSCRIPTEN_SDL_BASE_DIR "/files/"
39 #define MSG_POPUP_WINDOW(sdlflag, title, msg, win) \
40  do { if (1 || sdlflag == SDL_MESSAGEBOX_ERROR) { EM_ASM_INT({ window.alert(Pointer_stringify($0)); }, msg); } } while(0)
41 #else
42 #define MSG_POPUP_WINDOW(sdlflag, title, msg, win) SDL_ShowSimpleMessageBox_custom(sdlflag, title, msg, win)
43 #define INSTALL_DIRECTORY_ENTRY_NAME "default-files"
44 #endif
45 
46 #ifdef XEMU_ARCH_MAC
47 extern int macos_gui_started;
48 #endif
49 
50 #define APP_ORG "xemu-lgb"
51 #ifndef APP_DESC_APPEND
52 #define APP_DESC_APPEND " - Xemu"
53 #endif
54 
55 #ifdef XEMU_ARCH_HTML
56 #define XEMU_MAIN_LOOP(func,p1,p2) emscripten_set_main_loop(func,p1,p2)
57 #else
58 #define XEMU_MAIN_LOOP(func,p1,p2) for (;;) func()
59 #endif
60 
61 extern void sysconsole_open ( void );
62 extern void sysconsole_close ( const char *waitmsg );
63 extern int sysconsole_toggle ( int set );
64 
65 #define XEMU_CPU_STAT_INFO_BUFFER_SIZE 64
66 extern void xemu_get_timing_stat_string ( char *buf, unsigned int size );
67 extern const char *xemu_get_uname_string ( void );
68 
69 // You should define this in your emulator, most probably with resetting the keyboard matrix
70 // Purpose: emulator windows my cause the emulator does not get the key event normally, thus some keys "seems to be stucked"
71 extern void clear_emu_events ( void );
72 
73 extern void xemu_drop_events ( void );
74 
75 extern int set_mouse_grab ( SDL_bool state, int force_allow );
76 extern SDL_bool is_mouse_grab ( void );
77 extern void save_mouse_grab ( void );
78 extern void restore_mouse_grab ( void );
79 
80 extern int allow_mouse_grab;
81 
82 static XEMU_INLINE int CHECK_SNPRINTF( int ret, int limit )
83 {
84  if (ret < 0 || ret >= limit - 1) {
85  fprintf(stderr, "SNPRINTF-ERROR: too long string or other error (ret=%d) ..." NL, ret);
86  return -1;
87  }
88  return 0;
89 }
90 
91 #define _REPORT_WINDOW_(sdlflag, str, ...) do { \
92  char _buf_for_win_msg_[4096]; \
93  CHECK_SNPRINTF(snprintf(_buf_for_win_msg_, sizeof _buf_for_win_msg_, __VA_ARGS__), sizeof _buf_for_win_msg_); \
94  fprintf(stderr, str ": %s" NL, _buf_for_win_msg_); \
95  if (debug_fp) \
96  fprintf(debug_fp, str ": %s" NL, _buf_for_win_msg_); \
97  if (sdl_win) { \
98  save_mouse_grab(); \
99  MSG_POPUP_WINDOW(sdlflag, sdl_window_title, _buf_for_win_msg_, sdl_win); \
100  clear_emu_events(); \
101  xemu_drop_events(); \
102  SDL_RaiseWindow(sdl_win); \
103  restore_mouse_grab(); \
104  xemu_timekeeping_start(); \
105  } else \
106  MSG_POPUP_WINDOW(sdlflag, sdl_window_title, _buf_for_win_msg_, sdl_win); \
107 } while (0)
108 
109 #define INFO_WINDOW(...) _REPORT_WINDOW_(SDL_MESSAGEBOX_INFORMATION, "INFO", __VA_ARGS__)
110 #define WARNING_WINDOW(...) _REPORT_WINDOW_(SDL_MESSAGEBOX_WARNING, "WARNING", __VA_ARGS__)
111 #define ERROR_WINDOW(...) _REPORT_WINDOW_(SDL_MESSAGEBOX_ERROR, "ERROR", __VA_ARGS__)
112 
113 #define FATAL(...) do { \
114  ERROR_WINDOW(__VA_ARGS__); \
115  XEMUEXIT(1); \
116 } while (0)
117 
118 extern int _sdl_emu_secured_modal_box_ ( const char *items_in, const char *msg );
119 #define QUESTION_WINDOW(items, msg) _sdl_emu_secured_modal_box_(items, msg)
120 
121 extern int i_am_sure_override;
122 extern const char *str_are_you_sure_to_exit;
123 
124 #define ARE_YOU_SURE_OVERRIDE 1
125 #define ARE_YOU_SURE_DEFAULT_YES 2
126 #define ARE_YOU_SURE_DEFAULT_NO 4
127 
128 extern int ARE_YOU_SURE ( const char *s, int flags );
129 
130 extern char *sdl_window_title;
131 extern char *window_title_custom_addon;
132 extern char *window_title_info_addon;
133 extern SDL_Window *sdl_win;
134 extern Uint32 sdl_winid;
135 extern SDL_PixelFormat *sdl_pix_fmt;
138 extern int seconds_timer_trigger;
140 extern int sysconsole_is_open;
143 extern SDL_version sdlver_compiled, sdlver_linked;
145 
146 #define XEMU_VIEWPORT_ADJUST_LOGICAL_SIZE 1
147 //#define XEMU_VIEWPORT_WIN_SIZE_FOLLOW_LOGICAL 2
148 
149 extern void xemu_set_viewport ( unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int flags );
150 extern void xemu_get_viewport ( unsigned int *x1, unsigned int *y1, unsigned int *x2, unsigned int *y2 );
151 
152 extern void xemu_window_snap_to_optimal_size ( int forced );
153 
154 extern int xemu_init_debug ( const char *fn );
155 extern time_t xemu_get_unixtime ( void );
156 extern struct tm *xemu_get_localtime ( void );
157 extern Uint8 xemu_hour_to_bcd12h ( Uint8 hours, int hour_offset );
158 extern unsigned int xemu_get_microseconds ( void );
159 extern void *xemu_malloc ( size_t size );
160 extern void *xemu_realloc ( void *p, size_t size );
161 
162 extern int xemu_is_first_time_user ( void );
163 
164 #if !defined(XEMU_ARCH_HTML) && !defined(XEMU_CPU_ARM)
165 #define HAVE_MM_MALLOC
166 #endif
167 
168 #ifdef HAVE_MM_MALLOC
169 extern void *xemu_malloc_ALIGNED ( size_t size );
170 #else
171 extern void *_xemu_malloc_ALIGNED_emulated ( size_t size );
172 #define xemu_malloc_ALIGNED _xemu_malloc_ALIGNED_emulated
173 #endif
174 
175 extern const char EMPTY_STR[];
176 extern const int ZERO_INT;
177 extern const int ONE_INT;
178 
179 extern char *xemu_strdup ( const char *s );
180 extern void xemu_restrdup ( char **ptr, const char *str );
181 extern void xemu_set_full_screen ( int setting );
182 extern void xemu_set_screen_mode ( int setting );
183 extern void xemu_timekeeping_delay ( int td_em );
184 extern void xemu_pre_init ( const char *app_organization, const char *app_name, const char *slogan );
185 extern int xemu_init_sdl ( void );
186 extern int xemu_post_init (
187  const char *window_title, // title of our window
188  int is_resizable, // allow window resize? [0 = no]
189  int texture_x_size, int texture_y_size, // raw size of texture (in pixels)
190  int logical_x_size, int logical_y_size, // "logical" size in pixels, ie to correct aspect ratio, etc, can be the as texture of course, if it's OK ...
191  int win_x_size, int win_y_size, // default window size, in pixels [note: if logical/texture size combo does not match in ratio with this, black stripes you will see ...]
192  Uint32 pixel_format, // SDL pixel format we want to use (an SDL constant, like SDL_PIXELFORMAT_ARGB8888) Note: it can gave serve impact on performance, ARGB8888 recommended
193  int n_colours, // number of colours emulator wants to use
194  const Uint8 *colours, // RGB components of each colours, we need 3 * n_colours bytes to be passed!
195  Uint32 *store_palette, // this will be filled with generated palette, n_colours Uint32 values will be placed
196  int render_scale_quality, // render scale quality, must be 0, 1 or 2 _ONLY_
197  int locked_texture_update, // use locked texture method [non zero], or malloc'ed stuff [zero]. NOTE: locked access doesn't allow to _READ_ pixels and you must fill ALL pixels!
198  void (*shutdown_callback)(void) // callback function called on exit (can be nULL to not have any emulator specific stuff)
199 );
200 extern int xemu_set_icon_from_xpm ( char *xpm[] );
201 extern void xemu_timekeeping_start ( void );
202 extern void xemu_render_dummy_frame ( Uint32 colour, int texture_x_size, int texture_y_size );
203 extern Uint32 *xemu_start_pixel_buffer_access ( int *texture_tail );
204 extern void xemu_update_screen ( void );
205 
206 
207 static XEMU_INLINE Uint16 xemu_u8p_to_u16le ( const Uint8 *const p ) {
208  return p[0] | (p[1] << 8);
209 }
210 static XEMU_INLINE Uint32 xemu_u8p_to_u32le ( const Uint8 *const p ) {
211  return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
212 }
213 static XEMU_INLINE Uint64 xemu_u8p_to_u64le ( const Uint8 *const p ) {
214  return (Uint64)p[0] | ((Uint64)p[1] << 8) | ((Uint64)p[2] << 16) | ((Uint64)p[3] << 24) | ((Uint64)p[4] << 32) | ((Uint64)p[5] << 40) | ((Uint64)p[6] << 48) | ((Uint64)p[7] << 56);
215 }
216 static XEMU_INLINE void xemu_u16le_to_u8p ( Uint8 *const p, const Uint16 data ) {
217  p[0] = (data ) & 0xFF;
218  p[1] = (data >> 8) & 0xFF;
219 }
220 static XEMU_INLINE void xemu_u32le_to_u8p ( Uint8 *const p, const Uint32 data ) {
221  p[0] = (data ) & 0xFF;
222  p[1] = (data >> 8) & 0xFF;
223  p[2] = (data >> 16) & 0xFF;
224  p[3] = (data >> 24) & 0xFF;
225 }
226 static XEMU_INLINE void xemu_u64le_to_u8p ( Uint8 *const p, const Uint64 data ) {
227  p[0] = (data ) & 0xFF;
228  p[1] = (data >> 8) & 0xFF;
229  p[2] = (data >> 16) & 0xFF;
230  p[3] = (data >> 24) & 0xFF;
231  p[4] = (data >> 32) & 0xFF;
232  p[5] = (data >> 40) & 0xFF;
233  p[6] = (data >> 48) & 0xFF;
234  p[7] = (data >> 56) & 0xFF;
235 }
236 
237 typedef char sha1_hash_str[41];
238 typedef Uint8 sha1_hash_bytes[20];
239 
240 extern void sha1_checksum_as_words ( Uint32 hash[5], const Uint8 *data, Uint32 size );
241 extern void sha1_checksum_as_bytes ( sha1_hash_bytes hash_bytes, const Uint8 *data, Uint32 size );
242 extern void sha1_checksum_as_string ( sha1_hash_str hash_str, const Uint8 *data, Uint32 size );
243 
244 #ifdef XEMU_OSD_SUPPORT
245 #include "xemu/gui/osd.h"
246 #endif
247 
248 #include <dirent.h>
249 #ifdef XEMU_ARCH_WIN
250 # include <sys/stat.h>
251  typedef _WDIR XDIR;
252  extern int xemu_winos_utf8_to_wchar ( wchar_t *restrict o, const char *restrict i, size_t size );
253  extern int xemu_os_open ( const char *fn, int flags );
254  extern int xemu_os_creat ( const char *fn, int flags, int pmode );
255  extern FILE *xemu_os_fopen ( const char *restrict fn, const char *restrict mode );
256  extern int xemu_os_unlink ( const char *fn );
257  extern int xemu_os_mkdir ( const char *fn, const int mode );
258  extern XDIR *xemu_os_opendir ( const char *fn );
259  extern int xemu_os_closedir ( XDIR *dir );
260  extern int xemu_os_stat ( const char *fn, struct stat *statbuf );
261 #else
262  typedef DIR XDIR;
263 # define xemu_os_open open
264 # define xemu_os_creat creat
265 # define xemu_os_fopen fopen
266 # define xemu_os_unlink unlink
267 # define xemu_os_mkdir mkdir
268 # define xemu_os_opendir opendir
269 # define xemu_os_closedir closedir
270 # define xemu_os_stat stat
271 #endif
272 #define xemu_os_close close
273 extern int xemu_os_readdir ( XDIR *dirp, char *fn );
274 extern int xemu_os_file_exists ( const char *fn );
275 
276 #endif
_sdl_emu_secured_modal_box_
int _sdl_emu_secured_modal_box_(const char *items_in, const char *msg)
Definition: screen.c:469
sdl_inst_dir
char * sdl_inst_dir
Definition: emutools.h:139
window_title_info_addon
char * window_title_info_addon
Definition: emutools.c:91
xemu_window_snap_to_optimal_size
void xemu_window_snap_to_optimal_size(int forced)
Definition: emutools.c:808
xemu_restrdup
void xemu_restrdup(char **ptr, const char *str)
Definition: emutools.c:286
sha1_checksum_as_bytes
void sha1_checksum_as_bytes(sha1_hash_bytes hash_bytes, const Uint8 *data, Uint32 size)
Definition: emutools.c:1840
xemu_set_screen_mode
void xemu_set_screen_mode(int setting)
Definition: emutools.c:343
i_am_sure_override
int i_am_sure_override
Definition: emutools.c:74
allow_mouse_grab
int allow_mouse_grab
Definition: emutools.c:118
clear_emu_events
void clear_emu_events(void)
Definition: commodore_65.c:193
sysconsole_toggle
int sysconsole_toggle(int set)
Definition: emutools.c:1455
save_mouse_grab
void save_mouse_grab(void)
Definition: emutools.c:151
sha1_hash_str
char sha1_hash_str[41]
Definition: emutools.h:237
xemu_realloc
void * xemu_realloc(void *p, size_t size)
Definition: emutools.c:235
flags
Uint8 flags
Definition: z8k1.c:126
sdl_pref_dir
char * sdl_pref_dir
Definition: emutools.c:97
xemu_get_uname_string
const char * xemu_get_uname_string(void)
Definition: emutools.c:468
xemu_timekeeping_delay
void xemu_timekeeping_delay(int td_em)
Definition: emutools.c:405
xemu_hour_to_bcd12h
Uint8 xemu_hour_to_bcd12h(Uint8 hours, int hour_offset)
Definition: emutools.c:211
sdl_on_wayland
int sdl_on_wayland
Definition: emutools.h:136
sdl_pix_fmt
SDL_PixelFormat * sdl_pix_fmt
Definition: emutools.c:80
EMPTY_STR
const char EMPTY_STR[]
Definition: emutools.c:57
colour
Uint32 colour
Definition: vera.c:67
seconds_timer_trigger
int seconds_timer_trigger
Definition: emutools.c:101
xemu_drop_events
void xemu_drop_events(void)
Definition: emutools.c:294
xemu_init_debug
int xemu_init_debug(const char *fn)
Definition: emutools.c:582
xemu_get_viewport
void xemu_get_viewport(unsigned int *x1, unsigned int *y1, unsigned int *x2, unsigned int *y2)
Definition: emutools.c:877
xemu_frame_pixel_access_p
Uint32 * xemu_frame_pixel_access_p
Definition: emutools.c:93
osd.h
xemu_os_closedir
#define xemu_os_closedir
Definition: emutools.h:269
is_mouse_grab
SDL_bool is_mouse_grab(void)
Definition: emutools.c:145
xemu_os_creat
#define xemu_os_creat
Definition: emutools.h:264
xemu_malloc
void * xemu_malloc(size_t size)
Definition: emutools.c:226
_xemu_malloc_ALIGNED_emulated
void * _xemu_malloc_ALIGNED_emulated(size_t size)
Definition: emutools.c:244
sha1_checksum_as_words
void sha1_checksum_as_words(Uint32 hash[5], const Uint8 *data, Uint32 size)
Definition: emutools.c:1805
xemu_set_full_screen
void xemu_set_full_screen(int setting)
Definition: emutools.c:311
ONE_INT
const int ONE_INT
Definition: emutools.c:59
xemu_set_icon_from_xpm
int xemu_set_icon_from_xpm(char *xpm[])
Definition: emutools.c:1066
xemu_get_microseconds
unsigned int xemu_get_microseconds(void)
Definition: emutools.c:205
fn
const char * fn
Definition: roms.c:42
sha1_hash_bytes
Uint8 sha1_hash_bytes[20]
Definition: emutools.h:238
dirp
XDIR * dirp
Definition: hdos.c:63
register_new_texture_creation
int register_new_texture_creation
Definition: emutools.c:84
XEMU_INLINE
#define XEMU_INLINE
Definition: emutools_basicdefs.h:126
m65-memcontent-generator.data
data
Definition: m65-memcontent-generator.py:119
sdl_on_x11
int sdl_on_x11
Definition: emutools.c:81
Uint32
uint32_t Uint32
Definition: fat32.c:49
sdl_default_win_x_size
int sdl_default_win_x_size
Definition: emutools.c:112
xemu_update_screen
void xemu_update_screen(void)
Definition: emutools.c:1184
xemu_get_localtime
struct tm * xemu_get_localtime(void)
Definition: emutools.c:187
sdlver_linked
SDL_version sdlver_linked
Definition: emutools.h:143
Uint8
uint8_t Uint8
Definition: fat32.c:51
sysconsole_open
void sysconsole_open(void)
Definition: emutools.c:1294
sdlver_compiled
SDL_version sdlver_compiled
Definition: configuration.c:86
xemu_os_file_exists
int xemu_os_file_exists(const char *fn)
Definition: emutools.c:1725
sdl_window_title
char * sdl_window_title
Definition: emutools.c:89
xemu_pre_init
void xemu_pre_init(const char *app_organization, const char *app_name, const char *slogan)
Definition: emutools.c:651
emutools_basicdefs.h
xemu_os_opendir
#define xemu_os_opendir
Definition: emutools.h:268
xemu_os_stat
#define xemu_os_stat
Definition: emutools.h:270
sysconsole_is_open
int sysconsole_is_open
Definition: emutools.c:109
ZERO_INT
const int ZERO_INT
Definition: emutools.c:58
restore_mouse_grab
void restore_mouse_grab(void)
Definition: emutools.c:158
sdl_default_win_y_size
int sdl_default_win_y_size
Definition: emutools.h:141
dir
DIR * dir
Definition: cpmfs.c:46
sdl_win
SDL_Window * sdl_win
Definition: screen.c:43
xemu_start_pixel_buffer_access
Uint32 * xemu_start_pixel_buffer_access(int *texture_tail)
Definition: emutools.c:1153
sdl_base_dir
char * sdl_base_dir
Definition: emutools.h:139
xemu_init_sdl
int xemu_init_sdl(void)
Definition: emutools.c:744
NL
#define NL
Definition: fat32.c:37
xemu_app_name
char * xemu_app_name
Definition: emutools.h:137
xemu_strdup
char * xemu_strdup(const char *s)
Definition: emutools.c:278
xemu_os_unlink
#define xemu_os_unlink
Definition: emutools.h:266
xemu_get_unixtime
time_t xemu_get_unixtime(void)
Definition: emutools.c:199
xemu_render_dummy_frame
void xemu_render_dummy_frame(Uint32 colour, int texture_x_size, int texture_y_size)
Definition: emutools.c:1132
size
int size
Definition: inject.c:37
xemu_os_fopen
#define xemu_os_fopen
Definition: emutools.h:265
ARE_YOU_SURE
int ARE_YOU_SURE(const char *s, int flags)
Definition: emutools.c:1202
colours
Uint32 colours[0x100]
Definition: vera.c:83
xemu_app_org
char * xemu_app_org
Definition: emutools.c:85
Uint16
uint16_t Uint16
Definition: fat32.c:50
xemu_os_readdir
int xemu_os_readdir(XDIR *dirp, char *fn)
Definition: emutools.c:1733
sha1_checksum_as_string
void sha1_checksum_as_string(sha1_hash_str hash_str, const Uint8 *data, Uint32 size)
Definition: emutools.c:1853
xemu_set_viewport
void xemu_set_viewport(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int flags)
Definition: emutools.c:844
sdl_winid
Uint32 sdl_winid
Definition: screen.c:49
xemu_post_init
int xemu_post_init(const char *window_title, int is_resizable, int texture_x_size, int texture_y_size, int logical_x_size, int logical_y_size, int win_x_size, int win_y_size, Uint32 pixel_format, int n_colours, const Uint8 *colours, Uint32 *store_palette, int render_scale_quality, int locked_texture_update, void(*shutdown_callback)(void))
Definition: emutools.c:908
SDL_ShowMessageBox_custom
int(* SDL_ShowMessageBox_custom)(const SDL_MessageBoxData *, int *)
Definition: emutools.c:63
xemu_get_timing_stat_string
void xemu_get_timing_stat_string(char *buf, unsigned int size)
Definition: emutools.c:538
xemu_timekeeping_start
void xemu_timekeeping_start(void)
Definition: emutools.c:1122
set_mouse_grab
int set_mouse_grab(SDL_bool state, int force_allow)
Definition: emutools.c:131
SDL_ShowSimpleMessageBox_custom
int(* SDL_ShowSimpleMessageBox_custom)(Uint32, const char *, const char *, SDL_Window *)
Definition: emutools.c:62
xemu_malloc_ALIGNED
void * xemu_malloc_ALIGNED(size_t size)
str_are_you_sure_to_exit
const char * str_are_you_sure_to_exit
Definition: emutools.c:75
XDIR
DIR XDIR
Definition: emutools.h:262
xemu_is_first_time_user
int xemu_is_first_time_user(void)
Definition: emutools.c:623
xemu_os_mkdir
#define xemu_os_mkdir
Definition: emutools.h:267
sysconsole_close
void sysconsole_close(const char *waitmsg)
Definition: emutools.c:1393
window_title_custom_addon
char * window_title_custom_addon
Definition: emutools.c:90
xemu_os_open
#define xemu_os_open
Definition: emutools.h:263
buf
Uint8 buf[512]
Definition: fat32.c:155