OSDN Git Service

[LOGGER][Qt] Rename agar_logger.[cpp|h] to csp_logger.[cpp|h] .
[csp-qt/common_source_project-fm7.git] / source / src / config.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ config ]
8 */
9 #if defined(_USE_QT)
10 #include <string>
11 #include <vector>
12 #include "fileio.h"
13 #include "csp_logger.h"
14 #include "qt_main.h"
15 # if defined(Q_OS_WIN)
16 # include <windows.h>
17 # endif
18 #endif
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include "common.h"
23 #include "config.h"
24 #include "fileio.h"
25 #if defined(_USE_AGAR)
26 #include "agar_main.h"
27 #endif
28
29 config_t config;
30
31 #ifndef CONFIG_NAME
32 #define CONFIG_NAME "conf"
33 #endif
34
35 //extern _TCHAR* get_parent_dir(_TCHAR* file);
36 BOOL MyWritePrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, int Value, LPCTSTR lpFileName)
37 {
38         _TCHAR String[32];
39         return MyWritePrivateProfileString(lpAppName, lpKeyName, create_string(_T("%d"), Value), lpFileName);
40 }
41  
42 BOOL MyWritePrivateProfileBool(LPCTSTR lpAppName, LPCTSTR lpKeyName, bool Value, LPCTSTR lpFileName)
43 {
44         return MyWritePrivateProfileString(lpAppName, lpKeyName, create_string(_T("%d"), Value ? 1 : 0), lpFileName);
45 }
46  
47 bool MyGetPrivateProfileBool(LPCTSTR lpAppName, LPCTSTR lpKeyName, bool bDefault, LPCTSTR lpFileName)
48 {
49         return (MyGetPrivateProfileInt(lpAppName, lpKeyName, bDefault ? 1 : 0, lpFileName) != 0);
50 }
51
52 void initialize_config()
53 {
54         int i;
55         // initial settings
56         memset(&config, 0, sizeof(config_t));
57         config.window_mode = 1; 
58         // control
59 #if defined(USE_BOOT_MODE) && defined(BOOT_MODE_DEFAULT)
60         config.boot_mode = BOOT_MODE_DEFAULT;
61 #endif
62 #if defined(USE_CPU_TYPE) && defined(CPU_TYPE_DEFAULT)
63         config.cpu_type = CPU_TYPE_DEFAULT;
64 #endif
65 #if defined(USE_DIPSWITCH) && defined(DIPSWITCH_DEFAULT)
66         config.dipswitch = DIPSWITCH_DEFAULT;
67 #endif
68 #if defined(USE_DEVICE_TYPE) && defined(DEVICE_TYPE_DEFAULT)
69         config.device_type = DEVICE_TYPE_DEFAULT;
70 #endif
71 #if defined(USE_DRIVE_TYPE) && defined(DRIVE_TYPE_DEFAULT)
72         config.drive_type = DRIVE_TYPE_DEFAULT;
73 #endif
74 #if defined(USE_FD1)
75         for(int drv = 0; drv < MAX_FD; drv++) {
76 #if defined(CORRECT_DISK_TIMING_DEFAULT)
77                 config.correct_disk_timing[drv] = CORRECT_DISK_TIMING_DEFAULT;
78 #else
79                 config.correct_disk_timing[drv] = true;
80 #endif
81 #if defined(IGNORE_DISK_CRC_DEFAULT)
82                 config.ignore_disk_crc[drv] = IGNORE_DISK_CRC_DEFAULT;
83 #endif
84         }
85 #elif defined(USE_FD1)
86         for(int drv = 0; drv < MAX_FD; drv++) {
87                 config.ignore_disk_crc[drv] = false;
88         }
89 #endif  
90 #if defined(USE_TAPE)
91         config.wave_shaper = true;
92         config.direct_load_mzt = true;
93         config.baud_high = true;
94 #endif
95
96         // sound
97 #if defined(SOUND_RATE_DEFAULT)
98         config.sound_frequency = SOUND_RATE_DEFAULT;
99 #else
100         config.sound_frequency = 6;     // 48KHz
101 #endif
102         config.sound_latency = 1;       // 100msec
103         config.general_sound_level = 0;
104 #if defined(USE_SOUND_DEVICE_TYPE) && defined(SOUND_DEVICE_TYPE_DEFAULT)
105         config.sound_device_type = SOUND_DEVICE_TYPE_DEFAULT;
106 #elif defined(USE_SOUND_DEVICE_TYPE)
107         config.sound_device_type = 0;
108 #endif
109         
110         // input
111 #ifdef _WIN32
112         config.use_direct_input = true;
113         config.disable_dwm = false;
114 #endif
115         config.keyboard_type = 0;
116 #ifdef USE_JOYSTICK
117         for(int i = 0; i < 4; i++) {
118                 for(int j = 0; j < 16; j++) {
119                         config.joy_buttons[i][j] = (i << 4) | j;
120                 }
121         }
122 #endif  
123         // printer
124 #if defined(USE_PRINTER) && defined(PRINTER_DEVICE_TYPE_DEFAULT)
125         config.printer_device_type = PRINTER_DEVICE_TYPE_DEFAULT;
126 #elif defined(USE_PRINTER)
127         config.printer_device_type = 0;
128 #endif
129 #if defined(USE_QT)
130         config.video_width = 640;
131         config.video_height = 480;
132         config.video_codec_type = 0; // MPEG4
133         
134         config.video_h264_bitrate = 512;
135         config.video_h264_bframes = 4;
136         config.video_h264_b_adapt = 2;
137         config.video_h264_minq = 14;
138         config.video_h264_maxq = 25;
139         config.video_h264_subme = 8;
140         
141         config.video_mpeg4_bitrate = 512;
142         config.video_mpeg4_bframes = 4;
143         config.video_mpeg4_minq = 1;
144         config.video_mpeg4_maxq = 20;
145         
146         config.audio_codec_type = 0;
147         config.video_threads = 0;
148         config.audio_bitrate = 160;
149         config.video_frame_rate = 30;
150 #endif
151         // screen
152 #ifndef ONE_BOARD_MICRO_COMPUTER
153 #ifdef _WIN32
154         config.use_d3d9 = true;
155 #endif
156         config.fullscreen_stretch_type = 1;     // Stretch (Aspect)
157 #endif
158         
159 #if defined(_USE_QT)
160         config.use_opengl_scanline = false;
161         config.opengl_scanline_vert = false;
162         config.opengl_scanline_horiz = false;
163         config.use_opengl_filters = false;
164         config.opengl_filter_num = 0;
165
166         config.log_to_syslog = false;
167         config.log_to_console = true;
168         for(int i = 0; i < (256 -32); i++) {
169                 for(int j = 0; j <8; j++) {
170                         config.dev_log_to_syslog[i][j] = false;
171                         config.dev_log_to_console[i][j] = true;
172                         config.dev_log_recording[i][j] = true;
173                 }
174         }
175 #endif  
176 }
177
178 void load_config(const _TCHAR *config_path)
179 {
180         int drv, i;
181         // initial settings
182         initialize_config();
183
184         // control
185 #ifdef USE_BOOT_MODE
186         config.boot_mode = MyGetPrivateProfileInt(_T("Control"), _T("BootMode"), config.boot_mode, config_path);
187 #endif
188 #ifdef USE_CPU_TYPE
189         config.cpu_type = MyGetPrivateProfileInt(_T("Control"), _T("CPUType"), config.cpu_type, config_path);
190 #endif
191 #ifdef USE_DIPSWITCH
192         config.dipswitch = MyGetPrivateProfileInt(_T("Control"), _T("DipSwitch"), config.dipswitch, config_path);
193 #endif
194 #ifdef USE_DEVICE_TYPE
195         config.device_type = MyGetPrivateProfileInt(_T("Control"), _T("DeviceType"), config.device_type, config_path);
196 #endif
197 #ifdef USE_DRIVE_TYPE
198         config.drive_type = MyGetPrivateProfileInt(_T("Control"), _T("DriveType"), config.drive_type, config_path);
199 #endif
200 #ifdef USE_FD1
201         {
202                 for(drv = 0; drv < MAX_FD; drv++) {
203                 config.correct_disk_timing[drv] = MyGetPrivateProfileBool(_T("Control"), create_string(_T("CorrectDiskTiming%d"), drv + 1), config.correct_disk_timing[drv], config_path);
204                 config.ignore_disk_crc[drv] = MyGetPrivateProfileBool(_T("Control"), create_string(_T("IgnoreDiskCRC%d"), drv + 1), config.ignore_disk_crc[drv], config_path);
205                 }
206         }
207 #endif
208
209 #ifdef USE_TAPE
210         config.tape_sound = MyGetPrivateProfileBool(_T("Control"), _T("TapeSound"), config.tape_sound, config_path);
211         config.wave_shaper = MyGetPrivateProfileBool(_T("Control"), _T("WaveShaper"), config.wave_shaper, config_path);
212         config.direct_load_mzt = MyGetPrivateProfileBool(_T("Control"), _T("DirectLoadMZT"), config.direct_load_mzt, config_path);
213         config.baud_high = MyGetPrivateProfileBool(_T("Control"), _T("BaudHigh"), config.baud_high, config_path);
214 #endif
215         
216         // recent files
217 #ifdef USE_CART1
218         MyGetPrivateProfileString(_T("RecentFiles"), _T("InitialCartDir"), _T(""), config.initial_cart_dir, _MAX_PATH, config_path);
219         for(drv = 0; drv < MAX_CART; drv++) {
220                 for(i = 0; i < MAX_HISTORY; i++) {
221                         MyGetPrivateProfileString(_T("RecentFiles"), create_string(_T("RecentCartPath%d_%d"), drv + 1, i + 1), _T(""), config.recent_cart_path[drv][i], _MAX_PATH, config_path);
222                 }
223         }
224 #endif
225 #ifdef USE_FD1
226         MyGetPrivateProfileString(_T("RecentFiles"), _T("InitialDiskDir"), _T(""), config.initial_floppy_disk_dir, _MAX_PATH, config_path);
227     //    get_parent_dir(config.initial_disk_dir);
228         for(drv = 0; drv < MAX_FD; drv++) {
229                 for(i = 0; i < MAX_HISTORY; i++) {
230                         MyGetPrivateProfileString(_T("RecentFiles"), create_string(_T("RecentDiskPath%d_%d"), drv + 1, i + 1), _T(""), config.recent_floppy_disk_path[drv][i], _MAX_PATH, config_path);
231                 }
232         }
233 #endif
234 #ifdef USE_QD1
235         MyGetPrivateProfileString(_T("RecentFiles"), _T("InitialQuickDiskDir"), _T(""), config.initial_quick_disk_dir, _MAX_PATH, config_path);
236         for(drv = 0; drv < MAX_QD; drv++) {
237                 for(i = 0; i < MAX_HISTORY; i++) {
238                         MyGetPrivateProfileString(_T("RecentFiles"), create_string(_T("RecentQuickDiskPath%d_%d"), drv + 1, i + 1), _T(""), config.recent_quick_disk_path[drv][i], _MAX_PATH, config_path);
239                 }
240         }
241 #endif
242
243 #ifdef USE_TAPE
244         MyGetPrivateProfileString(_T("RecentFiles"), _T("InitialTapeDir"), _T(""),
245                                                         config.initial_tape_dir, _MAX_PATH, config_path);
246         for(i = 0; i < MAX_HISTORY; i++) {
247                 MyGetPrivateProfileString(_T("RecentFiles"), create_string(_T("RecentTapePath1_%d"), i + 1), _T(""), config.recent_tape_path[i], _MAX_PATH, config_path);
248         }
249 #endif
250
251 #ifdef USE_LASER_DISC
252         MyGetPrivateProfileString(_T("RecentFiles"), _T("InitialLaserDiscDir"), _T(""),
253                                                         config.initial_laser_disc_dir, _MAX_PATH, config_path);
254         for(int i = 0; i < MAX_HISTORY; i++) {
255                 MyGetPrivateProfileString(_T("RecentFiles"), create_string(_T("RecentLaserDiscPath1_%d"), i + 1), _T(""), config.recent_laser_disc_path[i], _MAX_PATH, config_path);
256         }
257 #endif
258 #ifdef USE_BINARY_FILE1
259         MyGetPrivateProfileString(_T("RecentFiles"), _T("InitialBinaryDir"), _T(""),
260                                                         config.initial_binary_dir, _MAX_PATH, config_path);
261         for(drv = 0; drv < MAX_BINARY; drv++) {
262                 for(i = 0; i < MAX_HISTORY; i++) {
263                         _TCHAR name[64];
264                         my_stprintf_s(name, 64, _T("RecentBinaryPath%d_%d"), drv + 1, i + 1);
265                         MyGetPrivateProfileString(_T("RecentFiles"), (const _TCHAR *)name, _T(""),
266                                                                         config.recent_binary_path[drv][i], _MAX_PATH, config_path);
267                 }
268         }
269 #endif
270 #if defined(USE_BUBBLE1)
271         MyGetPrivateProfileString(_T("RecentFiles"), _T("InitialBubbleDir"), _T(""),
272                                                         config.initial_bubble_casette_dir, _MAX_PATH, config_path);
273         for(drv = 0; drv < MAX_BUBBLE; drv++) {
274                 for(i = 0; i < MAX_HISTORY; i++) {
275                         _TCHAR name[64];
276                         my_stprintf_s(name, 64, _T("RecentBubblePath%d_%d"), drv + 1, i + 1);
277                         MyGetPrivateProfileString(_T("RecentFiles"), (const _TCHAR *)name, _T(""),
278                                                                         config.recent_bubble_casette_path[drv][i], _MAX_PATH, config_path);
279                 }
280         }
281 #endif
282         
283         // screen
284 #ifndef ONE_BOARD_MICRO_COMPUTER
285         config.window_mode = MyGetPrivateProfileInt(_T("Screen"), _T("WindowMode"), config.window_mode, config_path);
286 #ifdef _WIN32
287         config.use_d3d9 = MyGetPrivateProfileBool(_T("Screen"), _T("UseD3D9"), config.use_d3d9, config_path);
288         config.wait_vsync = MyGetPrivateProfileBool(_T("Screen"), _T("WaitVSync"), config.wait_vsync, config_path);
289 #endif
290 //-     config.stretch_type = MyGetPrivateProfileInt(_T("Screen"), _T("StretchType"), config.stretch_type, config_path);
291         config.window_stretch_type = MyGetPrivateProfileInt(_T("Screen"), _T("WindowStretchType"), config.window_stretch_type, config_path);
292         config.fullscreen_stretch_type = MyGetPrivateProfileInt(_T("Screen"), _T("FullScreenStretchType"), config.fullscreen_stretch_type, config_path);
293
294 #else
295         config.window_mode = MyGetPrivateProfileInt(_T("Screen"), _T("WindowMode"), config.window_mode, config_path);
296 #endif
297 #ifdef USE_MONITOR_TYPE
298         config.monitor_type = MyGetPrivateProfileInt(_T("Screen"), _T("MonitorType"), config.monitor_type, config_path);
299 #endif
300 #ifdef USE_CRT_FILTER
301         config.crt_filter = MyGetPrivateProfileBool(_T("Screen"), _T("CRTFilter"), config.crt_filter, config_path);
302 #endif
303 #ifdef USE_SCANLINE
304         config.scan_line = MyGetPrivateProfileBool(_T("Screen"), _T("ScanLine"), config.scan_line, config_path);
305 #endif
306
307 #ifdef USE_SCREEN_ROTATE
308         config.rotate_type = MyGetPrivateProfileInt(_T("Screen"), _T("RotateType"), config.rotate_type, config_path);
309 #endif
310 #if defined(_USE_QT)
311         config.use_opengl_scanline = MyGetPrivateProfileBool(_T("Screen"), _T("UseOpenGLScanLine"),
312                                                                                                            config.use_opengl_scanline, config_path);
313         config.opengl_scanline_vert = MyGetPrivateProfileBool(_T("Screen"), _T("OpenGLScanLineVert"),
314                                                                                                            config.opengl_scanline_vert, config_path);;
315         config.opengl_scanline_horiz = MyGetPrivateProfileBool(_T("Screen"), _T("OpenGLScanLineHoriz"),
316                                                                                                            config.opengl_scanline_horiz, config_path);;
317         config.use_opengl_filters = MyGetPrivateProfileBool(_T("Screen"), _T("UseOpenGLFilters"),
318                                                                                                            config.use_opengl_filters, config_path);
319         config.opengl_filter_num =      MyGetPrivateProfileInt(_T("Screen"), _T("OpenGLFilterNum"),
320                                                                                                          config.opengl_filter_num, config_path);
321 #endif  
322         // sound
323         config.sound_frequency = MyGetPrivateProfileInt(_T("Sound"), _T("Frequency"), config.sound_frequency, config_path);
324         config.sound_latency = MyGetPrivateProfileInt(_T("Sound"), _T("Latency"), config.sound_latency, config_path);
325 #ifdef USE_SOUND_DEVICE_TYPE
326         config.sound_device_type = MyGetPrivateProfileInt(_T("Sound"), _T("DeviceType"), config.sound_device_type, config_path);
327 #endif
328 #ifdef USE_SOUND_VOLUME
329         for(int i = 0; i < USE_SOUND_VOLUME; i++) {
330                 int tmp_l = MyGetPrivateProfileInt(_T("Sound"), create_string(_T("VolumeLeft%d"), i + 1), config.sound_volume_l[i], config_path);
331                 int tmp_r = MyGetPrivateProfileInt(_T("Sound"), create_string(_T("VolumeRight%d"), i + 1), config.sound_volume_r[i], config_path);
332 #ifdef _USE_QT
333                 // Note: when using balance , levels are -40±20db to 0±20db.
334                 config.sound_volume_l[i] = max(-60, min(20, tmp_l));
335                 config.sound_volume_r[i] = max(-60, min(20, tmp_r));
336 #else
337                 config.sound_volume_l[i] = max(-40, min(0, tmp_l));
338                 config.sound_volume_r[i] = max(-40, min(0, tmp_r));
339 #endif
340         }
341 #endif
342         MyGetPrivateProfileString(_T("Sound"), _T("FMGenDll"), _T("mamefm.dll"), config.fmgen_dll_path, _MAX_PATH, config_path);
343         config.general_sound_level = MyGetPrivateProfileInt(_T("Sound"), _T("GeneralSoundLevel"), config.general_sound_level, config_path);
344         // input
345 #ifdef _WIN32
346         config.use_direct_input = MyGetPrivateProfileBool(_T("Input"), _T("UseDirectInput"), config.use_direct_input, config_path);
347         config.disable_dwm = MyGetPrivateProfileBool(_T("Input"), _T("DisableDwm"), config.disable_dwm, config_path);
348 #endif
349         config.keyboard_type = MyGetPrivateProfileInt(_T("Input"), _T("KeyboardType"), config.keyboard_type, config_path);
350 #ifdef USE_JOYSTICK
351         for(int i = 0; i < 4; i++) {
352                 for(int j = 0; j < 16; j++) {
353                         config.joy_buttons[i][j] = MyGetPrivateProfileInt(_T("Input"), create_string(_T("JoyButtons%d_%d"), i + 1, j + 1), config.joy_buttons[i][j], config_path);
354                 }
355         }
356 #endif   
357 #if defined(_USE_QT)
358         for(i = 0; i < 16; i++) {
359                 _TCHAR name[256];
360                 my_stprintf_s(name, 256, _T("AssignedJoystick"), i + 1);
361                 MyGetPrivateProfileString(_T("Input"), (const _TCHAR *)name, _T(""),
362                                                                   config.assigned_joystick_name[i], 256, config_path);
363         }
364 #endif  
365         // printer
366 #ifdef USE_PRINTER
367         config.printer_device_type = MyGetPrivateProfileInt(_T("Printer"), _T("DeviceType"), config.printer_device_type, config_path);
368         MyGetPrivateProfileString(_T("Printer"), _T("PrinterDll"), _T("printer.dll"), config.printer_dll_path, _MAX_PATH, config_path);
369 #endif
370 #if defined(_USE_QT)
371         config.video_width   = MyGetPrivateProfileInt(_T("Video"), _T("VideoWidth"), config.video_width, config_path);
372         if(config.video_width < 128) config.video_width = 128;
373         config.video_height  = MyGetPrivateProfileInt(_T("Video"), _T("VideoHeight"), config.video_height, config_path);
374         if(config.video_height < 80) config.video_height = 80;
375         
376         config.video_codec_type = MyGetPrivateProfileInt(_T("Video"), _T("VideoCodecType"), config.video_codec_type, config_path);
377         if(config.video_codec_type > 1) config.video_codec_type = 1;
378         if(config.video_codec_type < 0) config.video_codec_type = 0;
379         
380         config.audio_codec_type = MyGetPrivateProfileInt(_T("Video"), _T("AudioCodecType"), config.audio_codec_type, config_path);
381         if(config.video_codec_type > 2) config.audio_codec_type = 2;
382         if(config.video_codec_type < 0) config.audio_codec_type = 0;
383         
384         config.video_h264_bitrate = MyGetPrivateProfileInt(_T("Video"), _T("H264Bitrate"), config.video_h264_bitrate, config_path);
385         if(config.video_h264_bitrate < 64) config.video_h264_bitrate = 64;
386
387         config.video_h264_bframes = MyGetPrivateProfileInt(_T("Video"), _T("H264BFrames"), config.video_h264_bframes, config_path);
388         if(config.video_h264_bframes < 0) config.video_h264_bframes = 0;
389         if(config.video_h264_bframes > 10) config.video_h264_bframes = 10;
390
391         config.video_h264_b_adapt = MyGetPrivateProfileInt(_T("Video"), _T("H264BAdapt"), config.video_h264_b_adapt, config_path);
392         if(config.video_h264_b_adapt < 0) config.video_h264_b_adapt = 0;
393         if(config.video_h264_b_adapt > 2) config.video_h264_b_adapt = 2;
394         
395         config.video_h264_subme   = MyGetPrivateProfileInt(_T("Video"), _T("H264Subme"), config.video_h264_subme, config_path);
396         if(config.video_h264_subme < 0) config.video_h264_subme = 0;
397         if(config.video_h264_subme > 11) config.video_h264_subme = 11;
398
399         config.video_h264_minq   = MyGetPrivateProfileInt(_T("Video"), _T("H264MinQ"), config.video_h264_minq, config_path);
400         if(config.video_h264_minq < 0) config.video_h264_minq = 0;
401         if(config.video_h264_minq > 63) config.video_h264_minq = 63;
402
403         config.video_h264_maxq   = MyGetPrivateProfileInt(_T("Video"), _T("H264MaxQ"), config.video_h264_maxq, config_path);
404         if(config.video_h264_maxq < 0) config.video_h264_maxq = 0;
405         if(config.video_h264_maxq > 63) config.video_h264_maxq = 63;
406         
407         config.video_mpeg4_bitrate = MyGetPrivateProfileInt(_T("Video"), _T("MPEG4Bitrate"), config.video_mpeg4_bitrate, config_path);
408         if(config.video_mpeg4_bitrate < 64) config.video_mpeg4_bitrate = 64;
409
410         config.video_mpeg4_bframes = MyGetPrivateProfileInt(_T("Video"), _T("MPEG4BFrames"), config.video_mpeg4_bframes, config_path);
411         if(config.video_mpeg4_bframes < 0) config.video_mpeg4_bframes = 0;
412         if(config.video_mpeg4_bframes > 10) config.video_mpeg4_bframes = 10;
413
414         config.video_mpeg4_minq   = MyGetPrivateProfileInt(_T("Video"), _T("MPEG4MinQ"), config.video_mpeg4_minq, config_path);
415         if(config.video_mpeg4_minq < 1) config.video_mpeg4_minq = 1;
416         if(config.video_mpeg4_minq > 31) config.video_mpeg4_minq = 31;
417
418         config.video_mpeg4_maxq   = MyGetPrivateProfileInt(_T("Video"), _T("MPEG4MaxQ"), config.video_mpeg4_maxq, config_path);
419         if(config.video_mpeg4_maxq < 1) config.video_mpeg4_maxq = 1;
420         if(config.video_mpeg4_maxq > 31) config.video_mpeg4_maxq = 31;
421         if(config.video_mpeg4_maxq < config.video_mpeg4_minq) {
422                 int n;
423                 n = config.video_mpeg4_maxq;
424                 config.video_mpeg4_maxq  = config.video_mpeg4_minq;
425                 config.video_mpeg4_minq = n;
426         }
427
428         config.video_threads = MyGetPrivateProfileInt(_T("Video"), _T("VideoThreads"), config.video_threads, config_path);
429         if(config.video_threads < 0) config.video_threads = 0;
430         if(config.video_threads > 16) config.video_threads = 16;
431         
432         config.audio_bitrate = MyGetPrivateProfileInt(_T("Video"), _T("AudioBitrate"), config.audio_bitrate, config_path);
433         if(config.audio_bitrate < 16) config.audio_bitrate = 16;
434         if(config.audio_bitrate > 448) config.audio_bitrate = 448;
435         
436         config.video_frame_rate = MyGetPrivateProfileInt(_T("Video"), _T("VideoFramerate"), config.video_frame_rate, config_path);
437         if(config.video_frame_rate < 15) config.video_frame_rate = 15;
438         if(config.video_frame_rate > 75) config.video_frame_rate = 75;
439
440 #endif  
441 #if defined(_USE_QT)
442         config.log_to_syslog = MyGetPrivateProfileBool(_T("Emulator"), _T("WriteToSyslog"), config.log_to_syslog, config_path);
443         config.log_to_console = MyGetPrivateProfileBool(_T("Emulator"), _T("WriteToConsole"), config.log_to_console, config_path);
444         for(int ii = 0; ii < (256 - 32); ii++) {
445                 uint32_t flags = 0;
446                 flags = MyGetPrivateProfileInt(_T("Emulator"), create_string(_T("SyslogEnabled%d"), ii), 0x0000, config_path);
447                 for(int jj = 0; jj < 8; jj++) {
448                         config.dev_log_to_syslog[ii][jj] = ((flags & 0x0001) != 0) ? true : false;
449                         csp_logger->set_device_node_log(ii, 1, jj, config.dev_log_to_syslog[ii][jj]);
450                         flags >>= 1;
451                 }
452                 flags = 0;
453                 flags = MyGetPrivateProfileInt(_T("Emulator"), create_string(_T("ConsoleLogEnabled%d"), ii), 0xffff, config_path);
454                 for(int jj = 0; jj < 8; jj++) {
455                         config.dev_log_to_console[ii][jj] = ((flags & 0x0001) != 0) ? true : false;
456                         csp_logger->set_device_node_log(ii, 2, jj, config.dev_log_to_console[ii][jj]);
457                         flags >>= 1;
458                 }
459                 flags = MyGetPrivateProfileInt(_T("Emulator"), create_string(_T("RecordLogEnabled%d"), ii), 0x00, config_path);
460                 for(int jj = 0; jj < 8; jj++) {
461                         config.dev_log_recording[ii][jj] = ((flags & 0x0001) != 0) ? true : false;
462                         csp_logger->set_device_node_log(ii, 0, jj, config.dev_log_recording[ii][jj]);
463                         flags >>= 1;
464                 }
465         }
466 #endif
467 #if defined(_USE_QT) && !defined(Q_OS_WIN)
468         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Read config done.");
469 #endif
470 }
471
472 void save_config(const _TCHAR *config_path)
473 {
474         int drv, i;
475 #if !defined(_MSC_VER)
476         {
477                 FILEIO *pt = new FILEIO;
478                 if(pt->Fopen(config_path, FILEIO_WRITE_ASCII) != true) {
479                         delete pt;
480                         return;
481                 }
482                 pt->Fclose();
483                 delete pt;
484         }
485         
486 #endif  
487         // control
488 # ifdef USE_BOOT_MODE
489         MyWritePrivateProfileInt(_T("Control"), _T("BootMode"), config.boot_mode, config_path);
490 #endif
491 #ifdef USE_CPU_TYPE
492         MyWritePrivateProfileInt(_T("Control"), _T("CPUType"), config.cpu_type, config_path);
493 #endif
494 #ifdef USE_DIPSWITCH
495         MyWritePrivateProfileInt(_T("Control"), _T("DipSwitch"), config.dipswitch, config_path);
496 #endif
497 #ifdef USE_DEVICE_TYPE
498         MyWritePrivateProfileInt(_T("Control"), _T("DeviceType"), config.device_type, config_path);
499 #endif
500 #ifdef USE_DRIVE_TYPE
501         MyWritePrivateProfileInt(_T("Control"), _T("DriveType"), config.drive_type, config_path);
502 #endif
503 #ifdef USE_FD1
504         {
505                 for(drv = 0; drv < MAX_FD; drv++) {
506                 MyWritePrivateProfileBool(_T("Control"), create_string(_T("CorrectDiskTiming%d"), drv + 1), config.correct_disk_timing[drv], config_path);
507                 MyWritePrivateProfileBool(_T("Control"), create_string(_T("IgnoreDiskCRC%d"), drv + 1), config.ignore_disk_crc[drv], config_path);
508                 }
509         }
510
511 #endif
512 #ifdef USE_TAPE
513         MyWritePrivateProfileBool(_T("Control"), _T("TapeSound"), config.tape_sound, config_path);
514         MyWritePrivateProfileBool(_T("Control"), _T("WaveShaper"), config.wave_shaper, config_path);
515         MyWritePrivateProfileBool(_T("Control"), _T("DirectLoadMZT"), config.direct_load_mzt, config_path);
516         MyWritePrivateProfileBool(_T("Control"), _T("BaudHigh"), config.baud_high, config_path);
517 #endif
518         
519         // recent files
520 #ifdef USE_CART1
521         MyWritePrivateProfileString(_T("RecentFiles"), _T("InitialCartDir"), config.initial_cart_dir, config_path);
522         for(drv = 0; drv < MAX_CART; drv++) {
523                 for(i = 0; i < MAX_HISTORY; i++) {
524                         MyWritePrivateProfileString(_T("RecentFiles"), create_string(_T("RecentCartPath%d_%d"), drv + 1, i + 1), config.recent_cart_path[drv][i], config_path);
525                 }
526         }
527 #endif
528 #ifdef USE_FD1
529         MyWritePrivateProfileString(_T("RecentFiles"), _T("InitialDiskDir"), config.initial_floppy_disk_dir, config_path);
530         for(drv = 0; drv < MAX_FD; drv++) {
531                 for(i = 0; i < MAX_HISTORY; i++) {
532                         MyWritePrivateProfileString(_T("RecentFiles"), create_string(_T("RecentDiskPath%d_%d"), drv + 1, i + 1), config.recent_floppy_disk_path[drv][i], config_path);
533                 }
534         }
535 #endif
536 #ifdef USE_QD1
537         MyWritePrivateProfileString(_T("RecentFiles"), _T("InitialQuickDiskDir"), config.initial_quick_disk_dir, config_path);
538         for(drv = 0; drv < MAX_QD; drv++) {
539                 for(i = 0; i < MAX_HISTORY; i++) {
540                         MyWritePrivateProfileString(_T("RecentFiles"), create_string(_T("RecentQuickDiskPath%d_%d"), drv + 1, i + 1), config.recent_quick_disk_path[drv][i], config_path);
541                 }
542         }
543 #endif
544 #ifdef USE_TAPE
545         MyWritePrivateProfileString(_T("RecentFiles"), _T("InitialTapeDir"), config.initial_tape_dir, config_path);
546         for(i = 0; i < MAX_HISTORY; i++) {
547                 MyWritePrivateProfileString(_T("RecentFiles"), create_string(_T("RecentTapePath1_%d"), i + 1), config.recent_tape_path[i], config_path);
548         }
549 #endif
550 #ifdef USE_COMPACT_DISC
551         MyWritePrivateProfileString(_T("RecentFiles"), _T("InitialCompactDiscDir"), config.initial_compact_disc_dir, config_path);
552         for(int i = 0; i < MAX_HISTORY; i++) {
553                 MyWritePrivateProfileString(_T("RecentFiles"), create_string(_T("RecentCompactDiscPath1_%d"), i + 1), config.recent_compact_disc_path[i], config_path);
554         }
555 #endif
556 #ifdef USE_LASER_DISC
557         MyWritePrivateProfileString(_T("RecentFiles"), _T("InitialLaserDiscDir"), config.initial_laser_disc_dir, config_path);
558         for(int i = 0; i < MAX_HISTORY; i++) {
559                 MyWritePrivateProfileString(_T("RecentFiles"), create_string(_T("RecentLaserDiscPath1_%d"), i + 1), config.recent_laser_disc_path[i], config_path);
560         }
561 #endif
562 #ifdef USE_BINARY_FILE1
563         MyWritePrivateProfileString(_T("RecentFiles"), _T("InitialBinaryDir"), config.initial_binary_dir, config_path);
564         for(drv = 0; drv < MAX_BINARY; drv++) {
565                 for(i = 0; i < MAX_HISTORY; i++) {
566                         MyWritePrivateProfileString(_T("RecentFiles"), create_string(_T("RecentBinaryPath%d_%d"), drv + 1, i + 1), config.recent_binary_path[drv][i], config_path);
567                 }
568         }
569 #endif
570 #if defined(USE_BUBBLE1)
571         MyWritePrivateProfileString(_T("RecentFiles"), _T("InitialBubbleDir"), config.initial_bubble_casette_dir, config_path);
572         for(drv = 0; drv < MAX_BUBBLE; drv++) {
573                 for(i = 0; i < MAX_HISTORY; i++) {
574                         MyWritePrivateProfileString(_T("RecentFiles"), create_string(_T("RecentBubblePath%d_%d"), drv + 1, i + 1), config.recent_bubble_casette_path[drv][i], config_path);                     
575                 }
576         }
577 #endif
578 #if defined(_USE_QT)
579         config.use_opengl_scanline = MyGetPrivateProfileBool(_T("Screen"), _T("UseOpenGLScanLine"), config.use_opengl_scanline, config_path);
580         config.opengl_scanline_vert = MyGetPrivateProfileBool(_T("Screen"), _T("OpenGLScanLineVert"), config.opengl_scanline_vert, config_path);;
581         config.opengl_scanline_horiz = MyGetPrivateProfileBool(_T("Screen"), _T("OpenGLScanLineHoriz"), config.opengl_scanline_horiz, config_path);;
582         config.use_opengl_filters = MyGetPrivateProfileBool(_T("Screen"), _T("UseOpenGLFilters"), config.use_opengl_filters, config_path);
583         config.opengl_filter_num = MyGetPrivateProfileInt(_T("Screen"), _T("OpenGLFilterNum"), config.opengl_filter_num, config_path);
584 #endif
585         
586         // screen
587 #ifndef ONE_BOARD_MICRO_COMPUTER
588         MyWritePrivateProfileInt(_T("Screen"), _T("WindowMode"), config.window_mode, config_path);
589 #ifdef _WIN32
590         MyWritePrivateProfileBool(_T("Screen"), _T("UseD3D9"), config.use_d3d9, config_path);
591         MyWritePrivateProfileBool(_T("Screen"), _T("WaitVSync"), config.wait_vsync, config_path);
592 #endif
593         //MyWritePrivateProfileInt(_T("Screen"), _T("StretchType"), config.stretch_type, config_path);
594         MyWritePrivateProfileInt(_T("Screen"), _T("WindowStretchType"), config.window_stretch_type, config_path);
595         MyWritePrivateProfileInt(_T("Screen"), _T("FullScreenStretchType"), config.fullscreen_stretch_type, config_path);
596
597 #else
598         MyWritePrivateProfileInt(_T("Screen"), _T("WindowMode"), config.window_mode, config_path);
599 #endif
600 #ifdef USE_MONITOR_TYPE
601         MyWritePrivateProfileInt(_T("Screen"), _T("MonitorType"), config.monitor_type, config_path);
602 #endif
603 #ifdef USE_CRT_FILTER
604         MyWritePrivateProfileBool(_T("Screen"), _T("CRTFilter"), config.crt_filter, config_path);
605 #endif
606 #ifdef USE_SCANLINE
607         MyWritePrivateProfileBool(_T("Screen"), _T("ScanLine"), config.scan_line, config_path);
608 #endif
609 #ifdef USE_SCREEN_ROTATE
610         MyWritePrivateProfileInt(_T("Screen"), _T("RotateType"), config.rotate_type, config_path);
611 #endif
612 #if defined(_USE_QT)
613         MyWritePrivateProfileBool(_T("Screen"), _T("UseOpenGLScanLine"),
614                                                         config.use_opengl_scanline, config_path);
615         MyWritePrivateProfileBool(_T("Screen"), _T("OpenGLScanLineVert"),
616                                                         config.opengl_scanline_vert, config_path);;
617         MyWritePrivateProfileBool(_T("Screen"), _T("OpenGLScanLineHoriz"),
618                                                         config.opengl_scanline_horiz, config_path);;
619         MyWritePrivateProfileBool(_T("Screen"), _T("UseOpenGLFilters"),
620                                                         config.use_opengl_filters, config_path);
621         MyWritePrivateProfileInt(_T("Screen"), _T("OpenGLFilterNum"),
622                                                    config.opengl_filter_num, config_path);
623 #endif  
624         
625         // sound
626         MyWritePrivateProfileInt(_T("Sound"), _T("Frequency"), config.sound_frequency, config_path);
627         MyWritePrivateProfileInt(_T("Sound"), _T("Latency"), config.sound_latency, config_path);
628 #ifdef USE_SOUND_DEVICE_TYPE
629         MyWritePrivateProfileInt(_T("Sound"), _T("DeviceType"), config.sound_device_type, config_path);
630 #endif
631 #ifdef USE_SOUND_VOLUME
632         for(int i = 0; i < USE_SOUND_VOLUME; i++) {
633                 MyWritePrivateProfileInt(_T("Sound"), create_string(_T("VolumeLeft%d"), i + 1), config.sound_volume_l[i], config_path);
634                 MyWritePrivateProfileInt(_T("Sound"), create_string(_T("VolumeRight%d"), i + 1), config.sound_volume_r[i], config_path);
635         }
636 #endif
637
638         MyWritePrivateProfileString(_T("Sound"), _T("FMGenDll"), config.fmgen_dll_path, config_path);
639         MyWritePrivateProfileInt(_T("Sound"), _T("GeneralSoundLevel"), config.general_sound_level, config_path);
640         // input
641 #ifdef _WIN32
642         MyWritePrivateProfileBool(_T("Input"), _T("UseDirectInput"), config.use_direct_input, config_path);
643         MyWritePrivateProfileBool(_T("Input"), _T("DisableDwm"), config.disable_dwm, config_path);
644 #endif
645         MyWritePrivateProfileInt(_T("Input"), _T("KeyboardType"), config.keyboard_type, config_path);
646 #ifdef USE_JOYSTICK   
647         for(int i = 0; i < 4; i++) {
648                 for(int j = 0; j < 16; j++) {
649                         MyWritePrivateProfileInt(_T("Input"), create_string(_T("JoyButtons%d_%d"), i + 1, j + 1), config.joy_buttons[i][j], config_path);
650                 }
651         }
652 #endif   
653 #if defined(_USE_QT)
654         for(i = 0; i < 16; i++) {
655                 _TCHAR name[256];
656                 my_stprintf_s(name, 256, _T("AssignedJoystick%d"), i + 1);
657                 MyWritePrivateProfileString(_T("Input"), (const _TCHAR *)name, 
658                                                                         config.assigned_joystick_name[i], config_path);
659         }
660 #endif  
661         
662         // printer
663 #ifdef USE_PRINTER
664         MyWritePrivateProfileInt(_T("Printer"), _T("DeviceType"), config.printer_device_type, config_path);
665 #endif
666 #if defined(_USE_QT)
667         MyWritePrivateProfileInt(_T("Video"), _T("VideoWidth"), config.video_width, config_path);
668         MyWritePrivateProfileInt(_T("Video"), _T("VideoHeight"), config.video_height, config_path);
669         MyWritePrivateProfileInt(_T("Video"), _T("VideoCodecType"), config.video_codec_type, config_path);
670         MyWritePrivateProfileInt(_T("Video"), _T("AudioCodecType"), config.audio_codec_type, config_path);
671         
672         MyWritePrivateProfileInt(_T("Video"), _T("H264Bitrate"), config.video_h264_bitrate, config_path);
673         MyWritePrivateProfileInt(_T("Video"), _T("H264BFrames"), config.video_h264_bframes, config_path);
674         MyWritePrivateProfileInt(_T("Video"), _T("H264BAdapt"), config.video_h264_b_adapt, config_path);
675         MyWritePrivateProfileInt(_T("Video"), _T("H264MinQ"), config.video_h264_minq, config_path);
676         MyWritePrivateProfileInt(_T("Video"), _T("H264MaxQ"), config.video_h264_maxq, config_path);
677         MyWritePrivateProfileInt(_T("Video"), _T("H264Subme"), config.video_h264_subme, config_path);
678         
679         MyWritePrivateProfileInt(_T("Video"), _T("MPEG4Bitrate"), config.video_mpeg4_bitrate, config_path);
680         MyWritePrivateProfileInt(_T("Video"), _T("MPEG4BFrames"), config.video_mpeg4_bframes, config_path);
681         MyWritePrivateProfileInt(_T("Video"), _T("MPEG4MinQ"), config.video_mpeg4_minq, config_path);
682         MyWritePrivateProfileInt(_T("Video"), _T("MPEG4MaxQ"), config.video_mpeg4_maxq, config_path);
683         
684         MyWritePrivateProfileInt(_T("Video"), _T("VideoThreads"), config.video_threads, config_path);
685         MyWritePrivateProfileInt(_T("Video"), _T("AudioBitrate"), config.audio_bitrate, config_path);
686         MyWritePrivateProfileInt(_T("Video"), _T("VideoFramerate"), config.video_frame_rate, config_path);
687 #endif  
688 #if defined(_USE_QT)
689         MyWritePrivateProfileBool(_T("Emulator"), _T("WriteToSyslog"), config.log_to_syslog, config_path);
690         MyWritePrivateProfileBool(_T("Emulator"), _T("WriteToConsole"), config.log_to_console, config_path);
691         
692         for(int ii = 0; ii < (256 - 32); ii++) {
693                 uint32_t flags = 0;
694                 flags = 0;
695                 for(int jj = 0; jj < 8; jj++) {
696                         flags <<= 1;
697                         if(config.dev_log_to_syslog[ii][jj]) flags |= 0x0001;
698                 }
699                 flags = MyWritePrivateProfileInt(_T("Emulator"), create_string(_T("SyslogEnabled%d"), ii), flags, config_path);
700
701                 flags = 0;
702                 for(int jj = 0; jj < 8; jj++) {
703                         flags <<= 1;
704                         if(config.dev_log_to_console[ii][jj]) flags |= 0x0001;
705                 }
706                 flags = MyWritePrivateProfileInt(_T("Emulator"), create_string(_T("ConsoleLogEnabled%d"), ii), flags, config_path);
707
708                 flags = 0;
709                 for(int jj = 0; jj < 8; jj++) {
710                         flags <<= 1;
711                         if(config.dev_log_recording[ii][jj]) flags |= 0x0001;
712                 }
713                 flags = MyWritePrivateProfileInt(_T("Emulator"), create_string(_T("RecordLogEnabled%d"), ii), flags, config_path);
714         }
715 #endif
716 #if defined(_USE_QT) && !defined(Q_OS_WIN)
717         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Write config done.");
718 #endif
719
720         
721 }
722
723 #define STATE_VERSION   5
724
725 void save_config_state(void *f)
726 {
727         FILEIO *state_fio = (FILEIO *)f;
728         int drv;
729         
730         state_fio->FputUint32(STATE_VERSION);
731         
732 #ifdef USE_BOOT_MODE
733         state_fio->FputInt32(config.boot_mode);
734 #endif
735 #ifdef USE_CPU_TYPE
736         state_fio->FputInt32(config.cpu_type);
737 #endif
738 #ifdef USE_DIPSWITCH
739         state_fio->FputUint32(config.dipswitch);
740 #endif
741 #ifdef USE_DEVICE_TYPE
742         state_fio->FputInt32(config.device_type);
743 #endif
744 #ifdef USE_DRIVE_TYPE
745         state_fio->FputInt32(config.drive_type);
746 #endif
747 #ifdef USE_FD1
748         for(int drv = 0; drv < MAX_FD; drv++) {
749                 state_fio->FputBool(config.correct_disk_timing[drv]);
750                 state_fio->FputBool(config.ignore_disk_crc[drv]);
751         }
752 //      for(int drv = 0; drv < MAX_FD; drv++) {
753 //              state_fio->FputBool(config.fdd_hack_fast_transfer[drv]);
754 //      }
755 #endif
756 #ifdef USE_MONITOR_TYPE
757         state_fio->FputInt32(config.monitor_type);
758 #endif
759 #ifdef USE_SOUND_DEVICE_TYPE
760         state_fio->FputInt32(config.sound_device_type);
761 #endif
762 #ifdef USE_PRINTER
763         state_fio->FputInt32(config.printer_device_type);
764 #endif
765         state_fio->FputInt32(config.keyboard_type);
766 }
767
768 bool load_config_state(void *f)
769 {
770         FILEIO *state_fio = (FILEIO *)f;
771         
772         if(state_fio->FgetUint32() != STATE_VERSION) {
773                 return false;
774         }
775 #ifdef USE_BOOT_MODE
776         config.boot_mode = state_fio->FgetInt32();
777 #endif
778 #ifdef USE_CPU_TYPE
779         config.cpu_type = state_fio->FgetInt32();
780 #endif
781 #ifdef USE_DIPSWITCH
782         config.dipswitch = state_fio->FgetUint32();
783 #endif
784 #ifdef USE_DEVICE_TYPE
785         config.device_type = state_fio->FgetInt32();
786 #endif
787 #ifdef USE_DRIVE_TYPE
788         config.drive_type = state_fio->FgetInt32();
789 #endif
790 #ifdef USE_FD1
791         for(int drv = 0; drv < MAX_FD; drv++) {
792                 config.correct_disk_timing[drv] = state_fio->FgetBool();
793                 config.ignore_disk_crc[drv] = state_fio->FgetBool();
794         }
795 //      for(int drv = 0; drv < MAX_FD; drv++) {
796 //              config.fdd_hack_fast_transfer[drv] = state_fio->FgetBool();
797 //      }
798 #endif
799 #ifdef USE_MONITOR_TYPE
800         config.monitor_type = state_fio->FgetInt32();
801 #endif
802 #ifdef USE_SOUND_DEVICE_TYPE
803         config.sound_device_type = state_fio->FgetInt32();
804 #endif
805 #ifdef USE_PRINTER
806         config.printer_device_type = state_fio->FgetInt32();
807 #endif
808         config.keyboard_type = state_fio->FgetInt32();
809         return true;
810 }
811