OSDN Git Service

e7a58e42d984097f8a8ff34193ca6cb4434e4d47
[csp-qt/common_source_project-fm7.git] / source / src / qt / common / qt_utils.cpp
1 /*
2         Skelton for retropc emulator
3         Author : Takeda.Toshiya
4         Port to Qt : K.Ohta <whatisthis.sowhat _at_ gmail.com>
5         Date   : 2006.08.18 -
6
7         [ win32 main ] -> [ agar main ]
8 */
9
10 #include <stdio.h>
11 #include <string>
12 #include <vector>
13 #include <QApplication>
14 #include <QString>
15 #include <QTextCodec>
16 #include <QImage>
17 #include <QImageReader>
18 #include <QDateTime>
19 #include <QDir>
20 #include <QTranslator>
21 #include <QProcessEnvironment>
22 #include <QCommandLineParser>
23
24 #include "common.h"
25 #include "fileio.h"
26 #include "config.h"
27 #include "emu.h"
28 #include "menuclasses.h"
29 #include "mainwidget.h"
30 #include "commonclasses.h"
31 #include "qt_main.h"
32 #include "emu_thread.h"
33 #include "joy_thread.h"
34 #include "draw_thread.h"
35
36 #include "qt_gldraw.h"
37 #include "../gui/gl2/qt_glutil_gl2_0.h"
38 #include "csp_logger.h"
39 #include "dock_disks.h"
40 #include "menu_disk.h"
41 #include "menu_bubble.h"
42 #include "menu_flags_ext.h"
43 #include "dialog_movie.h"
44 #include "../avio/movie_saver.h"
45 // emulation core
46
47 EMU* emu;
48 QApplication *GuiMain = NULL;
49 extern config_t config;
50 #if defined(CSP_OS_WINDOWS)
51 CSP_Logger DLL_PREFIX_I *csp_logger;
52 #else
53 extern CSP_Logger *csp_logger;
54 #endif
55
56 // Start to define MainWindow.
57 class META_MainWindow *rMainWindow;
58
59
60 // buttons
61 #ifdef MAX_BUTTONS
62 #define MAX_FONT_SIZE 32
63 #endif
64
65 // menu
66 extern DLL_PREFIX_I std::string cpp_homedir;
67 extern DLL_PREFIX_I std::string cpp_confdir;
68 extern DLL_PREFIX_I std::string my_procname;
69 extern DLL_PREFIX_I std::string sRssDir;
70 bool now_menuloop = false;
71 static int close_notified = 0;
72 // timing control
73
74 // screen
75 unsigned int desktop_width;
76 unsigned int desktop_height;
77 //int desktop_bpp;
78 int prev_window_mode = 0;
79 bool now_fullscreen = false;
80
81 int window_mode_count;
82
83 void Ui_MainWindow::do_set_mouse_enable(bool flag)
84 {
85 #ifdef USE_MOUSE
86         if(emu == NULL) return;
87         emu->lock_vm();
88         if(flag) {
89                 graphicsView->grabMouse();
90                 emu->enable_mouse();
91         } else {
92                 graphicsView->releaseMouse();
93                 emu->disable_mouse();
94         }
95         emu->unlock_vm();
96 #endif  
97 }
98
99 void Ui_MainWindow::do_toggle_mouse(void)
100 {
101 #ifdef USE_MOUSE
102         if(emu == NULL) return;
103         emu->lock_vm();
104         bool flag = emu->is_mouse_enabled();
105         if(!flag) {
106                 graphicsView->grabMouse();
107                 emu->enable_mouse();
108         } else {
109                 graphicsView->releaseMouse();
110                 emu->disable_mouse();
111         }
112         emu->unlock_vm();
113 #endif  
114 }
115
116 void Ui_MainWindow::rise_movie_dialog(void)
117 {
118         CSP_DialogMovie *dlg = new CSP_DialogMovie(hSaveMovieThread, using_flags);
119         dlg->setWindowTitle(QApplication::translate("CSP_DialogMovie", "Configure movie encodings", 0));
120         dlg->show();
121 }
122 void Ui_MainWindow::LaunchEmuThread(void)
123 {
124         QString objNameStr;
125         GLDrawClass *glv = this->getGraphicsView();
126
127         int drvs;
128         
129         hRunEmu = new EmuThreadClass(rMainWindow, using_flags);
130         connect(hRunEmu, SIGNAL(message_changed(QString)), this, SLOT(message_status_bar(QString)));
131         connect(hRunEmu, SIGNAL(sig_is_enable_mouse(bool)), glv, SLOT(do_set_mouse_enabled(bool)));
132         connect(glv, SIGNAL(sig_key_down(uint32_t, uint32_t, bool)), hRunEmu, SLOT(do_key_down(uint32_t, uint32_t, bool)));
133         connect(glv, SIGNAL(sig_key_up(uint32_t, uint32_t)),hRunEmu, SLOT(do_key_up(uint32_t, uint32_t)));
134         connect(this, SIGNAL(sig_quit_widgets()), glv, SLOT(do_stop_run_vm()));
135
136         
137         //connect(hRunEmu, SIGNAL(sig_finished()), this, SLOT(delete_emu_thread()));
138         connect(this, SIGNAL(sig_vm_reset()), hRunEmu, SLOT(do_reset()));
139         connect(this, SIGNAL(sig_vm_specialreset()), hRunEmu, SLOT(do_special_reset()));
140
141         connect(this, SIGNAL(sig_emu_update_config()), hRunEmu, SLOT(do_update_config()));
142         connect(this, SIGNAL(sig_emu_update_volume_level(int, int)), hRunEmu, SLOT(do_update_volume_level(int, int)));
143         connect(this, SIGNAL(sig_emu_update_volume_balance(int, int)), hRunEmu, SLOT(do_update_volume_balance(int, int)));
144         connect(this, SIGNAL(sig_emu_start_rec_sound()), hRunEmu, SLOT(do_start_record_sound()));
145         connect(this, SIGNAL(sig_emu_stop_rec_sound()), hRunEmu, SLOT(do_stop_record_sound()));
146         connect(this, SIGNAL(sig_emu_set_display_size(int, int, int, int)), hRunEmu, SLOT(do_set_display_size(int, int, int, int)));
147         
148         if(using_flags->is_use_state()) {
149                 for(int i = 0; i < 10; i++) {
150                         connect(actionLoad_State[i], SIGNAL(sig_load_state(QString)), hRunEmu, SLOT(do_load_state(QString))); // OK?
151                         connect(actionSave_State[i], SIGNAL(sig_save_state(QString)), hRunEmu, SLOT(do_save_state(QString))); // OK?
152                 }
153         }
154 #if defined(USE_FLOPPY_DISK)
155         connect(this, SIGNAL(sig_write_protect_disk(int, bool)), hRunEmu, SLOT(do_write_protect_disk(int, bool)));
156         connect(this, SIGNAL(sig_open_disk(int, QString, int)), hRunEmu, SLOT(do_open_disk(int, QString, int)));
157         connect(this, SIGNAL(sig_close_disk(int)), hRunEmu, SLOT(do_close_disk(int)));
158         connect(hRunEmu, SIGNAL(sig_update_recent_disk(int)), this, SLOT(do_update_recent_disk(int)));
159         //connect(hRunEmu, SIGNAL(sig_change_osd_fd(int, QString)), this, SLOT(do_change_osd_fd(int, QString)));
160         drvs = USE_FLOPPY_DISK;
161         for(int ii = 0; ii < drvs; ii++) {
162                 menu_fds[ii]->setEmu(emu);
163                 connect(menu_fds[ii], SIGNAL(sig_update_inner_fd(int ,QStringList , class Action_Control **, QStringList , int, bool)),
164                                 this, SLOT(do_update_inner_fd(int ,QStringList , class Action_Control **, QStringList , int, bool)));
165         }
166 #endif
167 #if defined(USE_HARD_DISK)
168         connect(this, SIGNAL(sig_open_hard_disk(int, QString)), hRunEmu, SLOT(do_open_hard_disk(int, QString)));
169         connect(this, SIGNAL(sig_close_hard_disk(int)), hRunEmu, SLOT(do_close_hard_disk(int)));
170         connect(hRunEmu, SIGNAL(sig_update_recent_hard_disk(int)), this, SLOT(do_update_recent_hard_disk(int)));
171         //connect(hRunEmu, SIGNAL(sig_change_osd_fd(int, QString)), this, SLOT(do_change_osd_fd(int, QString)));
172 #endif
173 #if defined(USE_TAPE)
174         connect(this, SIGNAL(sig_play_tape(int, QString)), hRunEmu, SLOT(do_play_tape(int, QString)));
175         connect(this, SIGNAL(sig_rec_tape(int, QString)),  hRunEmu, SLOT(do_rec_tape(int, QString)));
176         connect(this, SIGNAL(sig_close_tape(int)),   hRunEmu, SLOT(do_close_tape(int)));
177         //connect(hRunEmu, SIGNAL(sig_change_osd_cmt(QString)), this, SLOT(do_change_osd_cmt(QString)));
178 # if defined(USE_TAPE_BUTTON)
179         connect(this, SIGNAL(sig_cmt_push_play(int)), hRunEmu, SLOT(do_cmt_push_play(int)));
180         connect(this, SIGNAL(sig_cmt_push_stop(int)), hRunEmu, SLOT(do_cmt_push_stop(int)));
181         connect(this, SIGNAL(sig_cmt_push_fast_forward(int)), hRunEmu, SLOT(do_cmt_push_fast_forward(int)));
182         connect(this, SIGNAL(sig_cmt_push_fast_rewind(int)),  hRunEmu, SLOT(do_cmt_push_fast_rewind(int)));
183         connect(this, SIGNAL(sig_cmt_push_apss_forward(int)), hRunEmu, SLOT(do_cmt_push_apss_forward(int)));
184         connect(this, SIGNAL(sig_cmt_push_apss_rewind(int)),  hRunEmu, SLOT(do_cmt_push_apss_rewind(int)));
185 # endif
186 #endif
187 #if defined(USE_QUICK_DISK)
188         connect(this, SIGNAL(sig_write_protect_quickdisk(int, bool)), hRunEmu, SLOT(do_write_protect_quickdisk(int, bool)));
189         connect(this, SIGNAL(sig_open_quickdisk(int, QString)), hRunEmu, SLOT(do_open_quickdisk(int, QString)));
190         connect(this, SIGNAL(sig_close_quickdisk(int)), hRunEmu, SLOT(do_close_quickdisk(int)));
191         //connect(hRunEmu, SIGNAL(sig_change_osd_qd(int, QString)), this, SLOT(do_change_osd_qd(int, QString)));
192 #endif
193 #if defined(USE_CART)
194         connect(this, SIGNAL(sig_open_cart(int, QString)), hRunEmu, SLOT(do_open_cart(int, QString)));
195         connect(this, SIGNAL(sig_close_cart(int)), hRunEmu, SLOT(do_close_cart(int)));
196 #endif
197 #if defined(USE_COMPACT_DISC)
198         connect(this, SIGNAL(sig_open_cdrom(int, QString)), hRunEmu, SLOT(do_open_cdrom(int, QString)));
199         connect(this, SIGNAL(sig_close_cdrom(int)), hRunEmu, SLOT(do_eject_cdrom(int)));
200         //connect(hRunEmu, SIGNAL(sig_change_osd_cdrom(QString)), this, SLOT(do_change_osd_cdrom(QString)));
201         // ToDo: multiple CDs
202 #endif  
203 #if defined(USE_LASER_DISC)
204         connect(this, SIGNAL(sig_open_laserdisc(int, QString)), hRunEmu, SLOT(do_open_laser_disc(int, QString)));
205         connect(this, SIGNAL(sig_close_laserdisc(int)), hRunEmu, SLOT(do_close_laser_disc(int)));
206         // ToDo: multiple LDs
207 #endif
208 #if defined(USE_BINARY_FILE)
209         connect(this, SIGNAL(sig_load_binary(int, QString)), hRunEmu, SLOT(do_load_binary(int, QString)));
210         connect(this, SIGNAL(sig_save_binary(int, QString)), hRunEmu, SLOT(do_save_binary(int, QString)));
211 #endif
212 #if defined(USE_BUBBLE)
213         connect(this, SIGNAL(sig_write_protect_bubble(int, bool)), hRunEmu, SLOT(do_write_protect_bubble_casette(int, bool)));
214         connect(this, SIGNAL(sig_open_bubble(int, QString, int)), hRunEmu, SLOT(do_open_bubble_casette(int, QString, int)));
215         connect(this, SIGNAL(sig_close_bubble(int)), hRunEmu, SLOT(do_close_bubble_casette(int)));
216         connect(hRunEmu, SIGNAL(sig_update_recent_bubble(int)), this, SLOT(do_update_recent_bubble(int)));
217         //connect(hRunEmu, SIGNAL(sig_change_osd_bubble(int, QString)), this, SLOT(do_change_osd_bubble(int, QString)));
218         drvs = USE_BUBBLE;
219         for(int ii = 0; ii < drvs; ii++) {
220                 menu_bubbles[ii]->setEmu(emu);
221                 connect(menu_bubbles[ii],
222                                 SIGNAL(sig_update_inner_bubble(int ,QStringList , class Action_Control **, QStringList , int, bool)),
223                                 this,
224                                 SLOT(do_update_inner_bubble(int ,QStringList , class Action_Control **, QStringList , int, bool)));
225         }
226 #endif
227         
228         connect(this, SIGNAL(quit_emu_thread()), hRunEmu, SLOT(doExit()));
229         connect(hRunEmu, SIGNAL(sig_mouse_enable(bool)),
230                         this, SLOT(do_set_mouse_enable(bool)));
231
232         
233 #ifdef USE_TAPE_BUTTON
234         hRunEmu->set_tape_play(false);
235 #endif
236 #if defined(USE_KEY_LOCKED) || defined(USE_LED_DEVICE)
237         connect(hRunEmu, SIGNAL(sig_send_data_led(quint32)), this, SLOT(do_recv_data_led(quint32)));
238 #endif
239 #ifdef USE_AUTO_KEY
240         connect(this, SIGNAL(sig_start_auto_key(QString)), hRunEmu, SLOT(do_start_auto_key(QString)));
241         connect(this, SIGNAL(sig_stop_auto_key()), hRunEmu, SLOT(do_stop_auto_key()));
242         connect(this, SIGNAL(sig_set_roma_kana(bool)), hRunEmu, SLOT(set_romakana(bool)));
243 #endif  
244         //connect(actionExit_Emulator, SIGNAL(triggered()), hRunEmu, SLOT(doExit()));
245         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "EmuThread : Start.");
246         objNameStr = QString("EmuThreadClass");
247         hRunEmu->setObjectName(objNameStr);
248         
249         hDrawEmu = new DrawThreadClass(emu->get_osd(), csp_logger, this);
250         emu->set_parent_handler(hRunEmu, hDrawEmu);
251         
252 #ifdef ONE_BOARD_MICRO_COMPUTER
253         QImageReader *reader = new QImageReader(":/background.png");
254         QImage *result = new QImage(reader->read()); // this acts as a default if the size is not matched
255         QImage result2 = result->convertToFormat(QImage::Format_ARGB32);
256         glv->updateBitmap(&result2);
257         emu->get_osd()->upload_bitmap(&result2);
258         delete result;
259         delete reader;
260         emu->get_osd()->set_buttons();
261 #endif
262         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "DrawThread : Start.");
263         connect(hDrawEmu, SIGNAL(sig_draw_frames(int)), hRunEmu, SLOT(print_framerate(int)));
264         //connect(emu->get_osd(), SIGNAL(sig_draw_frames(int)), hRunEmu, SLOT(print_framerate(int)));
265         connect(hRunEmu, SIGNAL(window_title_changed(QString)), this, SLOT(do_set_window_title(QString)));
266         connect(hDrawEmu, SIGNAL(message_changed(QString)), this, SLOT(message_status_bar(QString)));
267         connect(actionCapture_Screen, SIGNAL(triggered()), glv, SLOT(do_save_frame_screen()));
268
269         /*if(config.use_separate_thread_draw) {
270                 connect(hRunEmu, SIGNAL(sig_draw_thread(bool)), hDrawEmu, SLOT(doDraw(bool)), Qt::QueuedConnection);
271                 connect(hRunEmu, SIGNAL(sig_set_draw_fps(double)), hDrawEmu, SLOT(do_set_frames_per_second(double)), Qt::QueuedConnection);
272                 connect(hRunEmu, SIGNAL(sig_draw_one_turn(bool)), hDrawEmu, SLOT(do_draw_one_turn(bool)), Qt::QueuedConnection);
273                 } else*/ {
274                 connect(hRunEmu, SIGNAL(sig_draw_thread(bool)), hDrawEmu, SLOT(doDraw(bool)));
275                 connect(hRunEmu, SIGNAL(sig_set_draw_fps(double)), hDrawEmu, SLOT(do_set_frames_per_second(double)));
276                 connect(hRunEmu, SIGNAL(sig_draw_one_turn(bool)), hDrawEmu, SLOT(do_draw_one_turn(bool)));
277         }
278         //connect(hRunEmu, SIGNAL(sig_draw_thread(bool)), emu->get_osd(), SLOT(do_draw(bool)));
279         //connect(hRunEmu, SIGNAL(quit_draw_thread()), hDrawEmu, SLOT(doExit()));
280         connect(this, SIGNAL(quit_draw_thread()), hDrawEmu, SLOT(doExit()));
281
282         connect(glv, SIGNAL(do_notify_move_mouse(int, int)),
283                         hRunEmu, SLOT(moved_mouse(int, int)));
284         connect(glv, SIGNAL(do_notify_button_pressed(Qt::MouseButton)),
285                 hRunEmu, SLOT(button_pressed_mouse(Qt::MouseButton)));
286         connect(glv, SIGNAL(do_notify_button_released(Qt::MouseButton)),
287                         hRunEmu, SLOT(button_released_mouse(Qt::MouseButton)));
288 #ifdef USE_MOUSE
289         connect(glv, SIGNAL(sig_toggle_mouse(void)),
290                         this, SLOT(do_toggle_mouse(void)));
291 #endif
292         connect(hRunEmu, SIGNAL(sig_resize_screen(int, int)),
293                         glv, SLOT(resizeGL(int, int)));
294         connect(hRunEmu, SIGNAL(sig_resize_osd(int)), driveData, SLOT(setScreenWidth(int)));
295         
296         connect(glv, SIGNAL(sig_resize_uibar(int, int)),
297                         this, SLOT(resize_statusbar(int, int)));
298         connect(hRunEmu, SIGNAL(sig_resize_uibar(int, int)),
299                         this, SLOT(resize_statusbar(int, int)));
300
301         connect(emu->get_osd(), SIGNAL(sig_req_encueue_video(int, int, int)),
302                         hDrawEmu, SLOT(do_req_encueue_video(int, int, int)));
303         connect(hRunEmu, SIGNAL(sig_finished()), glv, SLOT(releaseKeyCode(void)));
304         connect(hRunEmu, SIGNAL(sig_finished()), this, SLOT(delete_emu_thread()));
305         objNameStr = QString("EmuDrawThread");
306         hDrawEmu->setObjectName(objNameStr);
307
308         if(config.use_separate_thread_draw) hDrawEmu->start();
309
310         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "DrawThread : Launch done.");
311
312         hSaveMovieThread = new MOVIE_SAVER(640, 400,  30, emu->get_osd(), &config);
313         
314         connect(actionStart_Record_Movie->binds, SIGNAL(sig_start_record_movie(int)), hRunEmu, SLOT(do_start_record_video(int)));
315         connect(this, SIGNAL(sig_start_saving_movie()),
316                         actionStart_Record_Movie->binds, SLOT(do_save_as_movie()));
317         connect(actionStart_Record_Movie, SIGNAL(triggered()), this, SLOT(do_start_saving_movie()));
318
319         connect(actionStop_Record_Movie->binds, SIGNAL(sig_stop_record_movie()), hRunEmu, SLOT(do_stop_record_video()));
320         connect(this, SIGNAL(sig_stop_saving_movie()), actionStop_Record_Movie->binds, SLOT(do_stop_saving_movie()));
321         connect(hSaveMovieThread, SIGNAL(sig_set_state_saving_movie(bool)), this, SLOT(do_set_state_saving_movie(bool)));
322         connect(actionStop_Record_Movie, SIGNAL(triggered()), this, SLOT(do_stop_saving_movie()));
323
324         connect(emu->get_osd(), SIGNAL(sig_save_as_movie(QString, int, int)),
325                         hSaveMovieThread, SLOT(do_open(QString, int, int)));
326         connect(emu->get_osd(), SIGNAL(sig_stop_saving_movie()), hSaveMovieThread, SLOT(do_close()));
327         
328         actionStop_Record_Movie->setIcon(QIcon(":/icon_process_stop.png"));
329         actionStop_Record_Movie->setVisible(false);
330         
331         connect(this, SIGNAL(sig_movie_set_width(int)), hSaveMovieThread, SLOT(do_set_width(int)));
332         connect(this, SIGNAL(sig_movie_set_height(int)), hSaveMovieThread, SLOT(do_set_height(int)));
333  
334         connect(emu->get_osd(), SIGNAL(sig_movie_set_width(int)), hSaveMovieThread, SLOT(do_set_width(int)));
335         connect(emu->get_osd(), SIGNAL(sig_movie_set_height(int)), hSaveMovieThread, SLOT(do_set_height(int)));
336    
337         connect(emu->get_osd(), SIGNAL(sig_enqueue_audio(int16_t*, int)), hSaveMovieThread, SLOT(enqueue_audio(int16_t *, int)));
338         connect(emu->get_osd(), SIGNAL(sig_enqueue_video(int, int, int, QImage *)),
339                         hSaveMovieThread, SLOT(enqueue_video(int, int, int, QImage *)), Qt::DirectConnection);
340         connect(glv->extfunc, SIGNAL(sig_push_image_to_movie(int, int, int, QImage *)),
341                         hSaveMovieThread, SLOT(enqueue_video(int, int, int, QImage *)));
342         connect(this, SIGNAL(sig_quit_movie_thread()), hSaveMovieThread, SLOT(do_exit()));
343
344         objNameStr = QString("EmuMovieThread");
345         hSaveMovieThread->setObjectName(objNameStr);
346         hSaveMovieThread->start();
347         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "MovieThread : Launch done.");
348
349         connect(action_SetupMovie, SIGNAL(triggered()), this, SLOT(rise_movie_dialog()));
350         connect(hRunEmu, SIGNAL(sig_change_osd(int, int, QString)), driveData, SLOT(updateMessage(int, int, QString)));
351         connect(hRunEmu, SIGNAL(sig_change_access_lamp(int, int, QString)), driveData, SLOT(updateLabel(int, int, QString)));
352         connect(hRunEmu, SIGNAL(sig_set_access_lamp(int, bool)), graphicsView, SLOT(do_display_osd_leds(int, bool)));
353
354         hRunEmu->start();
355         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "EmuThread : Launch done.");
356         this->set_screen_aspect(config.window_stretch_type);
357         emit sig_movie_set_width(SCREEN_WIDTH);
358         emit sig_movie_set_height(SCREEN_HEIGHT);
359 }
360
361 void Ui_MainWindow::LaunchJoyThread(void)
362 {
363 #if defined(USE_JOYSTICK)
364         hRunJoy = new JoyThreadClass(emu, emu->get_osd(), using_flags, &config, csp_logger);
365         connect(this, SIGNAL(quit_joy_thread()), hRunJoy, SLOT(doExit()));
366         hRunJoy->setObjectName("JoyThread");
367         hRunJoy->start();
368 #endif  
369 }
370
371 void Ui_MainWindow::StopJoyThread(void)
372 {
373 #if defined(USE_JOYSTICK)
374         emit quit_joy_thread();
375 #endif  
376 }
377
378 void Ui_MainWindow::delete_joy_thread(void)
379 {
380         //    delete hRunJoyThread;
381         //  delete hRunJoy;
382 }
383
384 void Ui_MainWindow::on_actionExit_triggered()
385 {
386         OnMainWindowClosed();
387 }
388
389 void Ui_MainWindow::OnWindowRedraw(void)
390 {
391         if(emu) {
392                 //emu->update_screen();
393         }
394 }
395
396 void Ui_MainWindow::OnWindowMove(void)
397 {
398         if(emu) {
399                 emu->suspend();
400         }
401 }
402
403
404 #ifdef USE_NOTIFY_POWER_OFF
405 bool Ui_MainWindow::GetPowerState(void)
406 {
407         if(close_notified == 0) return true;
408         return false;
409 }
410 #endif
411
412 #include <string>
413
414 void Ui_MainWindow::OnMainWindowClosed(void)
415 {
416         // notify power off
417 #ifdef USE_NOTIFY_POWER_OFF
418         if(emu) {
419                 if(!close_notified) {
420                         emu->lock_vm();
421                         emu->notify_power_off();
422                         emu->unlock_vm();
423                         close_notified = 1;
424                         return; 
425                 }
426         }
427 #endif
428         if(statusUpdateTimer != NULL) statusUpdateTimer->stop();
429 #if defined(USE_KEY_LOCKED) || defined(USE_LED_DEVICE)
430         if(ledUpdateTimer != NULL) ledUpdateTimer->stop();
431 #endif
432         emit quit_draw_thread();
433         emit quit_joy_thread();
434         emit quit_emu_thread();
435         emit sig_quit_movie_thread();
436         emit sig_quit_widgets();
437         
438         if(hSaveMovieThread != NULL) {
439                 hSaveMovieThread->wait();
440                 delete hSaveMovieThread;
441                 hSaveMovieThread = NULL;
442         }
443    
444         if(hDrawEmu != NULL) {
445                 hDrawEmu->wait();
446                 delete hDrawEmu;
447                 hDrawEmu = NULL;
448         }
449         if(hRunEmu != NULL) {
450                 OnCloseDebugger();
451                 hRunEmu->quit();
452                 hRunEmu->wait();
453                 delete hRunEmu;
454 #if 0
455                 save_config(create_local_path(_T("%s.ini"), _T(CONFIG_NAME)));
456 #else
457                 {
458                         char tmps[128];
459                         std::string localstr;
460                         snprintf(tmps, sizeof(tmps), _T("%s.ini"), _T(CONFIG_NAME));
461                         localstr = tmps;
462                         localstr = cpp_confdir + localstr;
463                         save_config(localstr.c_str());
464                 }
465 #endif
466         }
467 #if defined(USE_JOYSTICK)
468         if(hRunJoy != NULL) {
469                 hRunJoy->wait();
470                 delete hRunJoy;
471                 hRunJoy = NULL;
472         }
473 #endif
474         do_release_emu_resources();
475         hRunEmu = NULL;
476
477         // release window
478         if(now_fullscreen) {
479                 //ChangeDisplaySettings(NULL, 0);
480         }
481         now_fullscreen = false;
482         return;
483 }
484
485 extern "C" {
486
487 void LostFocus(QWidget *widget)
488 {
489         if(emu) {
490                 emu->key_lost_focus();
491         }
492 }
493  
494 }  // extern "C"
495
496 void Ui_MainWindow::do_release_emu_resources(void)
497 {
498         if(emu) {
499                 delete emu;
500                 emu = NULL;
501         }
502 }
503
504 extern void DLL_PREFIX_I get_long_full_path_name(_TCHAR* src, _TCHAR* dst);
505 extern _TCHAR* DLL_PREFIX_I get_parent_dir(_TCHAR* file);
506 extern void get_short_filename(_TCHAR *dst, _TCHAR *file, int maxlen);
507
508 #if defined(Q_OS_CYGWIN)        
509 #include <sys/stat.h>
510 #endif
511 static void my_util_mkdir(std::string n)
512 {
513 #if !defined(Q_OS_CYGWIN)       
514                 QDir dir = QDir::current();
515                 dir.mkdir( QString::fromStdString(n));
516 #else
517                 struct stat st;
518                 if(stat(n.c_str(), &st) != 0) {
519                         _mkdir(n.c_str()); // Not found
520                 }
521 #endif   
522 }       
523
524 static void setup_logs(void)
525 {
526         std::string delim;
527         char    *p;
528
529         my_procname = "emu";
530         my_procname = my_procname + CONFIG_NAME;
531 #if defined(Q_OS_WIN)
532         delim = "\\";
533 #else
534         delim = "/";
535 #endif
536 #if !defined(Q_OS_WIN)
537         p = SDL_getenv("HOME");
538         if(p == NULL) {
539                 p = SDL_getenv("PWD");
540                 if(p == NULL) {
541                         cpp_homedir = ".";
542                 } else {
543                         cpp_homedir = p;
544                 }
545                 std::string tmpstr;
546                 tmpstr = "Warning : Can't get HOME directory...Making conf on " +  cpp_homedir + delim;
547                 perror(tmpstr.c_str());
548         } else {
549                 cpp_homedir = p;
550         }
551 #else
552         cpp_homedir = ".";
553 #endif  
554         cpp_homedir = cpp_homedir + delim;
555
556 #if !defined(CSP_OS_WINDOWS)
557         cpp_confdir = cpp_homedir + ".config" + delim;
558         my_util_mkdir(cpp_confdir);
559 #else
560         cpp_confdir = cpp_homedir;
561 #endif
562
563         cpp_confdir = cpp_confdir + "CommonSourceCodeProject" + delim;
564         my_util_mkdir(cpp_confdir);
565         
566         cpp_confdir = cpp_confdir + my_procname + delim;
567         my_util_mkdir(cpp_confdir);
568    //AG_MkPath(cpp_confdir.c_str());
569    /* Gettext */
570 #ifndef RSSDIR
571 #if defined(_USE_AGAR) || defined(_USE_QT)
572         sRssDir = "/usr/local/share/";
573 #else
574         sRssDir = "." + delim;
575 #endif
576         sRssDir = sRssDir + "CommonSourceCodeProject" + delim + my_procname;
577 #else
578         sRssDir = RSSDIR;
579 #endif
580 }
581
582 QStringList virtualMediaList; // {TYPE, POSITION}
583 QCommandLineOption *_opt_fds[8];
584 QCommandLineOption *_opt_hdds[8];
585 QCommandLineOption *_opt_cmts[2];
586 QCommandLineOption *_opt_lds[2];
587 QCommandLineOption *_opt_cds[2];
588 QCommandLineOption *_opt_binaries[8];
589 QCommandLineOption *_opt_bubbles[8];
590 QCommandLineOption *_opt_qds[8];
591 QCommandLineOption *_opt_carts[8];
592 extern QCommandLineOption *_opt_homedir;
593 extern QCommandLineOption *_opt_cfgfile;
594 extern QCommandLineOption *_opt_cfgdir;
595 extern QCommandLineOption *_opt_resdir;
596 extern QCommandLineOption *_opt_opengl;
597 extern QCommandLineOption *_opt_envver;
598 extern QCommandLineOption *_opt_dump_envver;
599 extern QCommandLineOption *_opt_dipsw_on;
600 extern QCommandLineOption *_opt_dipsw_off;
601 extern QProcessEnvironment _envvers;
602 bool _b_dump_envver;
603 std::string config_fullpath;
604
605 void SetFDOptions(QCommandLineParser *cmdparser)
606 {
607 #if defined(USE_FLOPPY_DISK)
608         for(int i = 0; i < USE_FLOPPY_DISK; i++) {
609                 QString sfdType1 = QString::fromUtf8("fd%1").arg(i);
610                 QString sfdType2 = QString::fromUtf8("vFd%1").arg(i);
611                 QString sfdType3 = QString::fromUtf8("vFloppyDisk%1").arg(i);
612                 QStringList _cl;
613                 _cl.append(sfdType1);
614                 _cl.append(sfdType2);
615                 _cl.append(sfdType3);
616                 _opt_fds[i] = new QCommandLineOption(_cl,QCoreApplication::translate("main", "Set virtual floppy disk %1.").arg(i) , "[D88_SLOT@]fullpath");
617                 cmdparser->addOption(*_opt_fds[i]);
618                 _cl.clear();
619         }
620 #endif
621 }
622
623 void SetHDDOptions(QCommandLineParser *cmdparser)
624 {
625 #if defined(USE_HARD_DISK)
626         for(int i = 0; i < USE_HARD_DISK; i++) {
627                 QString sfdType1 = QString::fromUtf8("hd%1").arg(i);
628                 QString sfdType2 = QString::fromUtf8("vHd%1").arg(i);
629                 QString sfdType3 = QString::fromUtf8("vHardDisk%1").arg(i);
630                 QString sfdType4 = QString::fromUtf8("vHardDrive%1").arg(i);
631                 QStringList _cl;
632                 _cl.append(sfdType1);
633                 _cl.append(sfdType2);
634                 _cl.append(sfdType3);
635                 _cl.append(sfdType4);
636                 _opt_hdds[i] = new QCommandLineOption(_cl,QCoreApplication::translate("main", "Set virtual hard drive %1.").arg(i) , "[D88_SLOT@]fullpath");
637                 cmdparser->addOption(*_opt_hdds[i]);
638                 _cl.clear();
639         }
640 #endif
641 }
642
643 void SetBinaryOptions(QCommandLineParser *cmdparser)
644 {
645 #if defined(USE_BINARY_FILE)
646         for(int i = 0; i < USE_BINARY_FILE; i++) {
647                 QString sfdType1 = QString::fromUtf8("bin%1").arg(i);
648                 QString sfdType2 = QString::fromUtf8("vBinary%1").arg(i);
649                 QStringList _cl;
650                 _cl.append(sfdType1);
651                 _cl.append(sfdType2);
652                 _opt_binaries[i] = new QCommandLineOption(_cl,QCoreApplication::translate("main", "Set virtual binary image %1.").arg(i) , "fullpath");
653                 cmdparser->addOption(*_opt_binaries[i]);
654                 _cl.clear();
655         }
656 #endif
657 }
658
659 void SetCartOptions(QCommandLineParser *cmdparser)
660 {
661 #if defined(USE_CART)
662         for(int i = 0; i < USE_CART; i++) {
663                 QString sfdType1 = QString::fromUtf8("cart%1").arg(i);
664                 QString sfdType2 = QString::fromUtf8("vCart%1").arg(i);
665                 QString sfdType3 = QString::fromUtf8("vCartridge%1").arg(i);
666                 QStringList _cl;
667                 _cl.append(sfdType1);
668                 _cl.append(sfdType2);
669                 _cl.append(sfdType3);
670                 _opt_carts[i] = new QCommandLineOption(_cl,QCoreApplication::translate("main", "Set virtual cartridge %1 (mostly ROM).").arg(i) , "fullpath");
671                 cmdparser->addOption(*_opt_carts[i]);
672                 _cl.clear();
673         }
674 #endif
675 }
676 void SetBubbleOptions(QCommandLineParser *cmdparser)
677 {
678 #if defined(USE_BUBBLE)
679         for(int i = 0; i < USE_BUBBLE; i++) {
680                 QString sfdType1 = QString::fromUtf8("bub%1").arg(i);
681                 QString sfdType2 = QString::fromUtf8("vBubble%1").arg(i);
682                 QStringList _cl;
683                 _cl.append(sfdType1);
684                 _cl.append(sfdType2);
685                 _opt_bubbles[i] = new QCommandLineOption(_cl,QCoreApplication::translate("main", "Set virtual bubble cartridge %1.").arg(i) , "[B88_SLOT@]fullpath");
686                 cmdparser->addOption(*_opt_bubbles[i]);
687         }
688 #endif
689 }
690
691 void SetLDOptions(QCommandLineParser *cmdparser)
692 {
693 #if defined(USE_LASER_DISC)
694         for(int i = 0; i < USE_LASER_DISC; i++) {
695                 QString sfdType1 = QString::fromUtf8("ld%1").arg(i);
696                 QString sfdType2 = QString::fromUtf8("vLaserDisc%1").arg(i);
697                 QStringList _cl;
698                 _cl.append(sfdType1);
699                 _cl.append(sfdType2);
700                 _opt_lds[i] = new QCommandLineOption(_cl, QCoreApplication::translate("main", "Set virtual laser disc %1 (mostly movie file).").arg(i) , "fullpath");
701                 cmdparser->addOption(*_opt_lds[i]);
702                 _cl.clear();
703         }
704 #endif
705 }
706 void SetCDOptions(QCommandLineParser *cmdparser)
707 {
708 #if defined(USE_COMPACT_DISC)
709         for(int i = 0; i < USE_COMPACT_DISC; i++) {
710                 QString sfdType1 = QString::fromUtf8("cd%1").arg(i);
711                 QString sfdType2 = QString::fromUtf8("vCompactDisc%1").arg(i);
712                 QStringList _cl;
713                 _cl.append(sfdType1);
714                 _cl.append(sfdType2);
715                 _opt_cds[i] = new QCommandLineOption(_cl,QCoreApplication::translate("main", "Set virtual compact disc %1.").arg(i) , "fullpath");
716                 cmdparser->addOption(*_opt_cds[i]);
717                 _cl.clear();
718         }
719 #endif
720 }
721
722 void SetCmtOptions(QCommandLineParser *cmdparser)
723 {
724 #if defined(USE_TAPE)
725         for(int i = 0; i < USE_TAPE; i++) {
726                 QString sfdType1 = QString::fromUtf8("cmt%1").arg(i);
727                 QString sfdType2 = QString::fromUtf8("tape%1").arg(i);
728                 QString sfdType3 = QString::fromUtf8("vCmt%1").arg(i);
729                 QString sfdType4 = QString::fromUtf8("vTape%1").arg(i);
730                 QStringList _cl;
731                 _cl.append(sfdType1);
732                 _cl.append(sfdType2);
733                 _cl.append(sfdType3);
734                 _cl.append(sfdType4);
735                 _opt_cmts[i] = new QCommandLineOption(_cl, QCoreApplication::translate("main", "Set virtual casette tape %1.").arg(i) , "fullpath");
736                 cmdparser->addOption(*_opt_cmts[i]);
737                 _cl.clear();
738         }
739 #endif
740 }
741
742 void SetQuickDiskOptions(QCommandLineParser *cmdparser)
743 {
744 #if defined(USE_QUICK_DISK)
745         for(int i = 0; i < USE_QUICK_DISK; i++) {
746                 QString sfdType1 = QString::fromUtf8("qd%1").arg(i);
747                 QString sfdType2 = QString::fromUtf8("vQuickDisk%1").arg(i);
748
749                 QStringList _cl;
750                 _cl.append(sfdType1);
751                 _cl.append(sfdType2);
752                 _opt_qds[i] = new QCommandLineOption(_cl, QCoreApplication::translate("main", "Set virtual quick disk %1.").arg(i) , "fullpath");
753                 cmdparser->addOption(*_opt_qds[i]);
754                 _cl.clear();
755         }
756 #endif
757 }
758
759
760 void SetProcCmdFD(QCommandLineParser *cmdparser, QStringList *_l)
761 {
762 #if defined(USE_FLOPPY_DISK)
763         for(int i = 0; i < USE_FLOPPY_DISK; i++) {
764                 if(_opt_fds[i] != NULL) {
765                         if(cmdparser->isSet(*_opt_fds[i])) {
766                                 QString sfdType = QString::fromUtf8("vFloppyDisk%1").arg(i);
767                                 QString medianame = cmdparser->value(*_opt_fds[i]);
768                                 _l->append(sfdType);
769                                 _l->append(medianame);
770                         }
771                 }
772         }
773 #endif
774 }
775
776 void SetProcCmdHDD(QCommandLineParser *cmdparser, QStringList *_l)
777 {
778 #if defined(USE_HARD_DISK)
779         for(int i = 0; i < USE_HARD_DISK; i++) {
780                 if(_opt_hdds[i] != NULL) {
781                         if(cmdparser->isSet(*_opt_hdds[i])) {
782                                 QString sfdType = QString::fromUtf8("vHardDisk%1").arg(i);
783                                 QString medianame = cmdparser->value(*_opt_hdds[i]);
784                                 _l->append(sfdType);
785                                 _l->append(medianame);
786                         }
787                 }
788         }
789 #endif
790 }
791
792 void SetProcCmdQuickDisk(QCommandLineParser *cmdparser, QStringList *_l)
793 {
794 #if defined(USE_QUICK_DISK)
795         for(int i = 0; i < USE_QUICK_DISK; i++) {
796                 if(_opt_qds[i] != NULL) {
797                         if(cmdparser->isSet(*_opt_qds[i])) {
798                                 QString sfdType = QString::fromUtf8("vQuickDisk%1").arg(i);
799                                 QString medianame = cmdparser->value(*_opt_qds[i]);
800                                 _l->append(sfdType);
801                                 _l->append(medianame);
802                         }
803                 }
804         }
805 #endif
806 }
807
808 void SetProcCmdCmt(QCommandLineParser *cmdparser, QStringList *_l)
809 {
810 #if defined(USE_TAPE)
811         for(int i = 0; i < USE_TAPE; i++) {
812                 if(_opt_cmts[i] != NULL) {
813                         if(cmdparser->isSet(*_opt_cmts[i])) {
814                                 QString sfdType = QString::fromUtf8("vCmt%1").arg(i);
815                                 QString medianame = cmdparser->value(*_opt_cmts[i]);
816                                 _l->append(sfdType);
817                                 _l->append(medianame);
818                         }
819                 }
820         }
821 #endif
822 }
823
824 void SetProcCmdBinary(QCommandLineParser *cmdparser, QStringList *_l)
825 {
826 #if defined(USE_BINARY_FILE)
827         for(int i = 0; i < USE_BINARY_FILE; i++) {
828                 if(_opt_binaries[i] != NULL) {
829                         if(cmdparser->isSet(*_opt_binaries[i])) {
830                                 QString sfdType = QString::fromUtf8("vBinary%1").arg(i);
831                                 QString medianame = cmdparser->value(*_opt_binaries[i]);
832                                 _l->append(sfdType);
833                                 _l->append(medianame);
834                         }
835                 }
836         }
837 #endif
838 }
839
840 void SetProcCmdBubble(QCommandLineParser *cmdparser, QStringList *_l)
841 {
842 #if defined(USE_BUBBLE)
843         for(int i = 0; i < USE_BUBBLE; i++) {
844                 if(_opt_bubbles[i] != NULL) {
845                         if(cmdparser->isSet(*_opt_bubbles[i])) {
846                                 QString sfdType = QString::fromUtf8("vBubble%1").arg(i);
847                                 QString medianame = cmdparser->value(*_opt_bubbles[i]);
848                                 _l->append(sfdType);
849                                 _l->append(medianame);
850                         }
851                 }
852         }
853 #endif
854 }
855
856 void SetProcCmdCart(QCommandLineParser *cmdparser, QStringList *_l)
857 {
858 #if defined(USE_CART)
859         for(int i = 0; i < USE_CART; i++) {
860                 if(_opt_carts[i] != NULL) {
861                         if(cmdparser->isSet(*_opt_carts[i])) {
862                                 QString sfdType = QString::fromUtf8("vCart%1").arg(i);
863                                 QString medianame = cmdparser->value(*_opt_carts[i]);
864                                 _l->append(sfdType);
865                                 _l->append(medianame);
866                         }
867                 }
868         }
869 #endif
870 }
871
872 void SetProcCmdLD(QCommandLineParser *cmdparser, QStringList *_l)
873 {
874 #if defined(USE_LASER_DISC)
875         for(int i = 0; i < USE_LASER_DISC; i++) {
876                 if(_opt_lds[i] != NULL) {
877                         if(cmdparser->isSet(*_opt_lds[i])) {
878                                 QString sfdType = QString::fromUtf8("vLD%1").arg(i);
879                                 QString medianame = cmdparser->value(*_opt_lds[i]);
880                                 _l->append(sfdType);
881                                 _l->append(medianame);
882                         }
883                 }
884         }
885 #endif
886 }
887
888 void SetProcCmdCD(QCommandLineParser *cmdparser, QStringList *_l)
889 {
890 #if defined(USE_COMPACT_DISC)
891         for(int i = 0; i < USE_COMPACT_DISC; i++) {
892                 if(_opt_cds[i] != NULL) {
893                         if(cmdparser->isSet(*_opt_cds[i])) {
894                                 QString sfdType = QString::fromUtf8("vCD%1").arg(i);
895                                 QString medianame = cmdparser->value(*_opt_cds[i]);
896                                 _l->append(sfdType);
897                                 _l->append(medianame);
898                         }
899                 }
900         }
901 #endif
902 }
903
904 extern void SetOptions_Sub(QCommandLineParser *cmdparser);
905 void SetOptions(QCommandLineParser *cmdparser)
906 {
907         QString emudesc = QString::fromUtf8("Emulator for ");
908         emudesc = emudesc + QString::fromUtf8(DEVICE_NAME);
909     cmdparser->setApplicationDescription(emudesc);
910     cmdparser->addHelpOption();
911     //cmdparser->addVersionOption();
912
913         SetOptions_Sub(cmdparser);
914         QStringList _cl;
915         
916         for(int i = 0; i < 8; i++) {
917                 _opt_fds[i] = NULL;
918                 _opt_hdds[i] = NULL;
919                 _opt_qds[i] = NULL;
920                 _opt_bubbles[i] = NULL;
921                 _opt_binaries[i] = NULL;
922                 _opt_carts[i] = NULL;
923         }
924         for(int i = 0; i < 2; i++) {
925                 _opt_cmts[i] = NULL;
926                 _opt_lds[i] = NULL;
927                 _opt_cds[i] = NULL;
928         }               
929
930         SetFDOptions(cmdparser);
931         SetHDDOptions(cmdparser);
932         //SetBinaryOptions(cmdparser); // Temporally disabled.
933         SetCmtOptions(cmdparser);
934         SetCartOptions(cmdparser);
935         SetBubbleOptions(cmdparser); // Temporally disabled.
936         SetQuickDiskOptions(cmdparser);
937         SetLDOptions(cmdparser); // Temporally disabled.
938         SetCDOptions(cmdparser);
939         
940     cmdparser->addOption(*_opt_envver);
941     cmdparser->addOption(*_opt_dump_envver);
942 }
943
944 extern void ProcessCmdLine_Sub(QCommandLineParser *cmdparser, QStringList *_l);
945
946 void ProcessCmdLine(QCommandLineParser *cmdparser, QStringList *_l)
947 {
948         char homedir[PATH_MAX];
949         std::string delim;
950 #if defined(Q_OS_WIN)
951         delim = "\\";
952 #else
953         delim = "/";
954 #endif
955         ProcessCmdLine_Sub(cmdparser, _l);      
956         {
957                 char tmps[128];
958                 std::string localstr;
959                 memset(tmps, 0x00, 128);
960                 if(cmdparser->isSet(*_opt_cfgfile)) {
961                         strncpy(tmps, cmdparser->value(*_opt_cfgfile).toLocal8Bit().constData(), 127);
962                 }
963                 if(strlen(tmps) <= 0){
964                         snprintf(tmps, sizeof(tmps), _T("%s.ini"), _T(CONFIG_NAME));
965                 }
966                 localstr = tmps;
967                 localstr = cpp_confdir + localstr;
968                 load_config(localstr.c_str());
969                 config_fullpath = localstr;
970         }
971         if(cmdparser->isSet(*_opt_opengl)) {
972                 char tmps[128] = {0};
973                 strncpy(tmps, cmdparser->value(*_opt_opengl).toLocal8Bit().constData(), 128 - 1);
974                 if(strlen(tmps) > 0) {
975                         QString render = QString::fromLocal8Bit(tmps).toUpper();
976                         if((render == QString::fromUtf8("GL2")) ||
977                            (render == QString::fromUtf8("GLV2"))) {
978                                 config.render_platform = CONFIG_RENDER_PLATFORM_OPENGL_MAIN;
979                                 config.render_major_version = 2;
980                                 config.render_minor_version = 0;
981                                 GuiMain->setAttribute(Qt::AA_UseDesktopOpenGL, true);
982                                 GuiMain->setAttribute(Qt::AA_UseOpenGLES, false);
983                         } else if((render == QString::fromUtf8("GL3")) ||
984                                           (render == QString::fromUtf8("GLV3")) ||
985                                           (render == QString::fromUtf8("OPENGLV3")) ||
986                                           (render == QString::fromUtf8("OPENGL")) ||
987                                           (render == QString::fromUtf8("GL"))) {
988                                 config.render_platform = CONFIG_RENDER_PLATFORM_OPENGL_MAIN;
989                                 config.render_major_version = 3;
990                                 config.render_minor_version = 0;
991                                 GuiMain->setAttribute(Qt::AA_UseDesktopOpenGL, true);
992                                 GuiMain->setAttribute(Qt::AA_UseOpenGLES, false);
993                         } else if((render == QString::fromUtf8("GLES2")) ||
994                                          (render == QString::fromUtf8("GLESV2")) ||
995                                          (render == QString::fromUtf8("GLES3")) ||
996                                          (render == QString::fromUtf8("GLESV3")) ||
997                                          (render == QString::fromUtf8("GLES"))) {
998                                 config.render_platform = CONFIG_RENDER_PLATFORM_OPENGL_ES;
999                                 config.render_major_version = 2;
1000                                 config.render_minor_version = 1;
1001                                 GuiMain->setAttribute(Qt::AA_UseDesktopOpenGL, false);
1002                                 GuiMain->setAttribute(Qt::AA_UseOpenGLES, true);
1003                         } else if((render == QString::fromUtf8("GL4")) ||
1004                                          (render == QString::fromUtf8("GL43")) ||
1005                                          (render == QString::fromUtf8("GL4.3")) ||
1006                                          (render == QString::fromUtf8("GL4_3")) ||
1007                                          (render == QString::fromUtf8("GL4_CORE"))) {
1008                                 config.render_platform = CONFIG_RENDER_PLATFORM_OPENGL_CORE;
1009                                 config.render_major_version = 4;
1010                                 config.render_minor_version = 3;
1011                                 GuiMain->setAttribute(Qt::AA_UseDesktopOpenGL, true);
1012                                 GuiMain->setAttribute(Qt::AA_UseOpenGLES, false);
1013                         }
1014                 }
1015         }
1016         SetProcCmdFD(cmdparser, _l);
1017         SetProcCmdHDD(cmdparser, _l);
1018         SetProcCmdQuickDisk(cmdparser, _l);
1019         SetProcCmdCmt(cmdparser, _l);
1020         SetProcCmdCart(cmdparser, _l);
1021         SetProcCmdBinary(cmdparser, _l);
1022         SetProcCmdBubble(cmdparser, _l);
1023         SetProcCmdLD(cmdparser, _l);
1024         SetProcCmdCD(cmdparser, _l);
1025
1026 #if 0
1027         memset(homedir, 0x00, PATH_MAX);
1028         if(cmdparser->isSet(*_opt_homedir)) {
1029                 strncpy(homedir, cmdparser->value(*_opt_homedir).toLocal8Bit().constData(), PATH_MAX - 1);
1030                 cpp_homedir = homedir;
1031                 size_t _len = cpp_homedir.length() - 1;
1032                 size_t _pos = cpp_homedir.rfind(delim);
1033                 if((_pos < _len) ||
1034                    (_pos == std::string::npos)) {
1035                         cpp_homedir.append(delim);
1036                 }
1037         } else {
1038                 cpp_homedir.copy(homedir, PATH_MAX - 1, 0);
1039         }
1040
1041         if(cmdparser->isSet(*_opt_cfgdir)) {
1042                 char tmps[PATH_MAX];
1043                 std::string tmpstr;
1044                 memset(tmps, 0x00, PATH_MAX);
1045                 strncpy(tmps, cmdparser->value(*_opt_cfgdir).toLocal8Bit().constData(), PATH_MAX - 1);
1046                 cpp_confdir = tmps;
1047                 size_t _len = cpp_confdir.length() - 1;
1048                 size_t _pos = cpp_confdir.rfind(delim);
1049                 if((_pos < _len) ||
1050                    (_pos == std::string::npos)) {
1051                         cpp_confdir.append(delim);
1052                 }
1053         }
1054         
1055         {
1056                 char tmps[128];
1057                 std::string localstr;
1058                 memset(tmps, 0x00, 128);
1059                 if(cmdparser->isSet(*_opt_cfgfile)) {
1060                         strncpy(tmps, cmdparser->value(*_opt_cfgfile).toLocal8Bit().constData(), 127);
1061                 }
1062                 if(strlen(tmps) <= 0){
1063                         snprintf(tmps, sizeof(tmps), _T("%s.ini"), _T(CONFIG_NAME));
1064                 }
1065                 localstr = tmps;
1066                 localstr = cpp_confdir + localstr;
1067                 load_config(localstr.c_str());
1068                 config_fullpath = localstr;
1069         }
1070         if(cmdparser->isSet(*_opt_resdir)) {
1071                 char tmps[PATH_MAX];
1072                 std::string tmpstr;
1073                 memset(tmps, 0x00, PATH_MAX);
1074                 strncpy(tmps, cmdparser->value(*_opt_resdir).toLocal8Bit().constData(), PATH_MAX - 1);
1075                 sRssDir = tmps;
1076                 size_t _len = sRssDir.length() - 1;
1077                 size_t _pos = sRssDir.rfind(delim);
1078                 if((_pos < _len) ||
1079                    (_pos == std::string::npos)) {
1080                         sRssDir.append(delim);
1081                 }
1082         }
1083         if(cmdparser->isSet(*_opt_opengl)) {
1084                 char tmps[128] = {0};
1085                 strncpy(tmps, cmdparser->value(*_opt_opengl).toLocal8Bit().constData(), 128 - 1);
1086                 if(strlen(tmps) > 0) {
1087                         QString render = QString::fromLocal8Bit(tmps).toUpper();
1088                         if((render == QString::fromUtf8("GL2")) ||
1089                            (render == QString::fromUtf8("GLV2"))) {
1090                                 config.render_platform = CONFIG_RENDER_PLATFORM_OPENGL_MAIN;
1091                                 config.render_major_version = 2;
1092                                 config.render_minor_version = 0;
1093                                 GuiMain->setAttribute(Qt::AA_UseDesktopOpenGL, true);
1094                                 GuiMain->setAttribute(Qt::AA_UseOpenGLES, false);
1095                         } else if((render == QString::fromUtf8("GL3")) ||
1096                                           (render == QString::fromUtf8("GLV3")) ||
1097                                           (render == QString::fromUtf8("OPENGLV3")) ||
1098                                           (render == QString::fromUtf8("OPENGL")) ||
1099                                           (render == QString::fromUtf8("GL"))) {
1100                                 config.render_platform = CONFIG_RENDER_PLATFORM_OPENGL_MAIN;
1101                                 config.render_major_version = 3;
1102                                 config.render_minor_version = 0;
1103                                 GuiMain->setAttribute(Qt::AA_UseDesktopOpenGL, true);
1104                                 GuiMain->setAttribute(Qt::AA_UseOpenGLES, false);
1105                         } else if((render == QString::fromUtf8("GLES2")) ||
1106                                          (render == QString::fromUtf8("GLESV2")) ||
1107                                          (render == QString::fromUtf8("GLES3")) ||
1108                                          (render == QString::fromUtf8("GLESV3")) ||
1109                                          (render == QString::fromUtf8("GLES"))) {
1110                                 config.render_platform = CONFIG_RENDER_PLATFORM_OPENGL_ES;
1111                                 config.render_major_version = 2;
1112                                 config.render_minor_version = 1;
1113                                 GuiMain->setAttribute(Qt::AA_UseDesktopOpenGL, false);
1114                                 GuiMain->setAttribute(Qt::AA_UseOpenGLES, true);
1115                         } else if((render == QString::fromUtf8("GL4")) ||
1116                                          (render == QString::fromUtf8("GL43")) ||
1117                                          (render == QString::fromUtf8("GL4.3")) ||
1118                                          (render == QString::fromUtf8("GL4_3")) ||
1119                                          (render == QString::fromUtf8("GL4_CORE"))) {
1120                                 config.render_platform = CONFIG_RENDER_PLATFORM_OPENGL_CORE;
1121                                 config.render_major_version = 4;
1122                                 config.render_minor_version = 3;
1123                                 GuiMain->setAttribute(Qt::AA_UseDesktopOpenGL, true);
1124                                 GuiMain->setAttribute(Qt::AA_UseOpenGLES, false);
1125                         }
1126                 }
1127         }
1128         if(cmdparser->isSet(*_opt_envver)) {
1129                 QStringList nList = cmdparser->values(*_opt_envver);
1130                 QString tv;
1131                 //QProcessEnvironment ev = QProcessEnvironment::systemEnvironment();
1132                 QProcessEnvironment ev = _envvers;
1133                 if(nList.size() > 0) {
1134                         for(int i = 0; i < nList.size(); i++) {
1135                                 tv = nList.at(i);
1136                                 if(tv.indexOf(QString::fromUtf8("-")) == 0) {
1137                                         // Delete var
1138                                         int n1 = tv.indexOf(QString::fromUtf8("="));
1139                                         if(n1 > 0) {
1140                                                 tv = tv.left(n1).right(n1 - 1);
1141                                         } else {
1142                                                 tv = tv.right(tv.length() - 1);
1143                                         }
1144                                         printf("DEBUG: DEL ENV:%s\n", tv.toLocal8Bit().constData());
1145                                         ev.remove(tv);
1146                                 } else if(tv.indexOf(QString::fromUtf8("=")) > 0) {
1147                                         // Delete var
1148                                         int n1 = tv.indexOf(QString::fromUtf8("="));
1149                                         QString skey;
1150                                         QString sval;
1151                                         skey = tv.left(n1);
1152                                         if((tv.length() - n1) < 1) {
1153                                                 sval = QString::fromUtf8("");
1154                                         } else {
1155                                                 sval = tv.right(tv.length() - n1 - 1);
1156                                         }
1157                                         printf("DEBUG: SET ENV:%s to %s\n", skey.toLocal8Bit().constData(), sval.toLocal8Bit().constData());
1158                                         if(skey.length() > 0) ev.insert(skey, sval);
1159                                 } else if(tv.indexOf(QString::fromUtf8("=")) < 0) {
1160                                         printf("DEBUG: SET ENV:%s to (NULL)\n", tv.toLocal8Bit().constData());
1161                                         if(tv.length() > 0) ev.insert(tv, QString::fromUtf8(""));
1162                                 }
1163                         }
1164                         _envvers.swap(ev);
1165                 }
1166         }
1167         _b_dump_envver = false;
1168         if(cmdparser->isSet(*_opt_dump_envver)) {
1169                 _b_dump_envver = true;
1170         }
1171 #endif
1172         uint32_t dipsw_onbits = 0x0000000;
1173         uint32_t dipsw_offmask = 0xffffffff;
1174         if(cmdparser->isSet(*_opt_dipsw_off)) {
1175                 QStringList bitList = cmdparser->values(*_opt_dipsw_off);
1176                 QString tv;
1177                 bool num_ok;
1178                 for(int i = 0; i < bitList.size(); i++) {
1179                         tv = bitList.at(i);
1180                         int _bit = tv.toInt(&num_ok);
1181                         if(num_ok) {
1182                                 if((_bit >= 0) && (_bit < 32)) {
1183                                         dipsw_offmask &= (uint32_t)(~(1 << _bit));
1184                                 }
1185                         }
1186                 }
1187         }
1188         if(cmdparser->isSet(*_opt_dipsw_on)) {
1189                 QStringList bitList = cmdparser->values(*_opt_dipsw_on);
1190                 QString tv;
1191                 bool num_ok;
1192                 for(int i = 0; i < bitList.size(); i++) {
1193                         tv = bitList.at(i);
1194                         int _bit = tv.toInt(&num_ok);
1195                         if(num_ok) {
1196                                 if((_bit >= 0) && (_bit < 32)) {
1197                                         dipsw_onbits |= (uint32_t)(1 << _bit);
1198                                 }
1199                         }
1200                 }
1201         }
1202         config.dipswitch &= dipsw_offmask;
1203         config.dipswitch |= dipsw_onbits;
1204 }
1205
1206 void OpeningMessage(std::string archstr)
1207 {
1208         csp_logger->set_emu_vm_name(DEVICE_NAME); // Write to syslog, console
1209         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Start Common Source Project '%s'", my_procname.c_str());
1210         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "(C) Toshiya Takeda / Qt Version K.Ohta");
1211         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Architecture: %s", archstr.c_str());
1212         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Use -h or --help for help.");
1213         
1214         //csp_logger->debug_log(AGAR_LOG_INFO, " -? is print help(s).");
1215         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Home = %s, Resource directory = %s",
1216                                                   cpp_homedir.c_str(),
1217                                                   sRssDir.c_str()); // Debug
1218         
1219         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Config dir = %s, config_file = %s",
1220                                                   cpp_confdir.c_str(),
1221                                                   config_fullpath.c_str()); // Debug
1222         
1223         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "DIPSW VALUE IS 0x%08x", config.dipswitch);
1224         if(virtualMediaList.size() >= 2) {
1225                 for(int i = 0; i < virtualMediaList.size(); i += 2) {
1226                         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Virtual media %d, type %s, name %s",
1227                                                                   i / 2,
1228                                                                   virtualMediaList.at(i).toLocal8Bit().constData(),
1229                                                                   virtualMediaList.at(i + 1).toLocal8Bit().constData());
1230                 }
1231         }
1232 }
1233
1234 void SetupSDL(void)
1235 {
1236         QStringList _el = _envvers.toStringList();
1237         if(_el.size() > 0) {
1238                 for(int i = 0; i < _el.size(); i++) {
1239                         QString _s = _el.at(i);
1240                         if(_s.startsWith("SDL_")) {
1241                                 QString skey, svar;
1242                                 int _nl;
1243                                 _nl = _s.indexOf('=');
1244                                 if(_nl >= 0) {
1245                                         skey = _s.left(_nl);
1246                                         svar = _s.right(_s.length() - _nl);
1247                                 } else {
1248                                         skey = _s;
1249                                         svar = QString::fromUtf8("");
1250                                 }
1251                                 SDL_setenv(skey.toLocal8Bit().constData(), svar.toLocal8Bit().constData(), 1);
1252                                 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Note: SDL ENVIROMENT : %s to %s.",
1253                                                                           skey.toLocal8Bit().constData(),
1254                                                                           svar.toLocal8Bit().constData());
1255                         }
1256                 }
1257         }
1258 #if defined(USE_SDL2)
1259         SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
1260 #else
1261         SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);
1262 #endif
1263         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Audio and JOYSTICK subsystem was initialised.");
1264 }
1265
1266
1267 void SetupLogger(QObject *parent, std::string emustr, int _size)
1268 {
1269
1270         csp_logger = new CSP_Logger(parent, config.log_to_syslog, config.log_to_console, emustr.c_str()); // Write to syslog, console
1271         csp_logger->set_log_stdout(CSP_LOG_DEBUG, true);
1272         csp_logger->set_log_stdout(CSP_LOG_INFO, true);
1273         csp_logger->set_log_stdout(CSP_LOG_WARN, true);
1274         
1275         csp_logger->set_state_log(0, config.state_log_to_recording);
1276         csp_logger->set_state_log(1, config.state_log_to_syslog);
1277         csp_logger->set_state_log(2, config.state_log_to_console);
1278         
1279         for(int ii = 0; ii < _size; ii++) {
1280                 for(int jj = 0; jj < 8; jj++) {
1281                         csp_logger->set_device_node_log(ii, 1, jj, config.dev_log_to_syslog[ii][jj]);
1282                         csp_logger->set_device_node_log(ii, 2, jj, config.dev_log_to_console[ii][jj]);
1283                         csp_logger->set_device_node_log(ii, 0, jj, config.dev_log_recording[ii][jj]);
1284                 }
1285         }
1286 }
1287
1288 int MainLoop(int argc, char *argv[])
1289 {
1290
1291         std::string archstr;
1292         std::string emustr("emu");
1293         std::string cfgstr(CONFIG_NAME);
1294         std::string delim;
1295         QString emudesc;
1296
1297         setup_logs();
1298 #if defined(Q_OS_WIN)
1299         delim = "\\";
1300 #else
1301         delim = "/";
1302 #endif
1303         
1304         GuiMain = new QApplication(argc, argv);
1305         GuiMain->setObjectName(QString::fromUtf8("Gui_Main"));
1306         _envvers = QProcessEnvironment::systemEnvironment();
1307         
1308     QCommandLineParser cmdparser;
1309
1310         virtualMediaList.clear();
1311         SetOptions(&cmdparser);
1312
1313         cmdparser.process(QCoreApplication::arguments());
1314
1315         ProcessCmdLine(&cmdparser, &virtualMediaList);
1316
1317         emustr = emustr + cfgstr;
1318
1319         SetupLogger(GuiMain, emustr, CSP_LOG_TYPE_VM_DEVICE_END - CSP_LOG_TYPE_VM_DEVICE_0 + 1);
1320
1321         
1322         archstr = "Generic";
1323 #if defined(__x86_64__)
1324         archstr = "amd64";
1325 #endif
1326 #if defined(__i386__)
1327         archstr = "ia32";
1328 #endif
1329         OpeningMessage(archstr);
1330         SetupSDL();
1331         /*
1332          * Into Qt's Loop.
1333          */
1334
1335         //SetupTranslators();
1336         QTranslator local_translator;
1337         QLocale s_locale;
1338         if(local_translator.load(s_locale, QLatin1String("csp_qt_machine"), QLatin1String("_"), QLatin1String(":/"))) {
1339                 GuiMain->installTranslator(&local_translator);
1340         }
1341         QTranslator s_translator;
1342         if(s_translator.load(s_locale, QLatin1String("csp_qt_gui"), QLatin1String("_"), QLatin1String(":/"))) {
1343                 GuiMain->installTranslator(&s_translator);
1344         }
1345         QTranslator common_translator;
1346         if(common_translator.load(s_locale, QLatin1String("csp_qt_common"), QLatin1String("_"), QLatin1String(":/"))) {
1347                 GuiMain->installTranslator(&common_translator);
1348         }
1349         QTranslator debugger_translator;
1350         if(debugger_translator.load(s_locale, QLatin1String("csp_qt_debugger"), QLatin1String("_"), QLatin1String(":/"))) {
1351                 GuiMain->installTranslator(&debugger_translator);
1352         }
1353         //QProcessEnvironment::systemEnvironment() = _envvers;
1354         if(_b_dump_envver) {
1355                 //QProcessEnvironment ev = QProcessEnvironment::systemEnvironment();
1356                 QProcessEnvironment ev = _envvers;
1357                 QStringList el = _envvers.toStringList();
1358                 if(el.size() > 0) {
1359                         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "Environment Variables:");
1360                         for(int i = 0; i < el.size(); i++) {
1361                                 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "#%d : %s", i, el.at(i).toLocal8Bit().constData());
1362                         }
1363                 }
1364         }
1365         
1366         USING_FLAGS_EXT *using_flags = new USING_FLAGS_EXT(&config);
1367         // initialize emulation core
1368         rMainWindow = new META_MainWindow(using_flags, csp_logger);
1369         rMainWindow->connect(rMainWindow, SIGNAL(sig_quit_all(void)), rMainWindow, SLOT(deleteLater(void)));
1370         rMainWindow->setCoreApplication(GuiMain);
1371         rMainWindow->getWindow()->show();
1372                         
1373         // main loop
1374         rMainWindow->LaunchEmuThread();
1375 #if defined(USE_JOYSTICK)
1376         rMainWindow->LaunchJoyThread();
1377 #endif  
1378         GLDrawClass *pgl = rMainWindow->getGraphicsView();
1379         pgl->set_emu_launched();
1380         pgl->setFixedSize(pgl->width(), pgl->height());
1381         rMainWindow->retranselateUi_Depended_OSD();
1382         QObject::connect(emu->get_osd(), SIGNAL(sig_update_device_node_name(int, const _TCHAR *)),
1383                                          rMainWindow, SLOT(do_update_device_node_name(int, const _TCHAR *)));
1384         for(int i = 0; i < (CSP_LOG_TYPE_VM_DEVICE_END - CSP_LOG_TYPE_VM_DEVICE_0 + 1); i++) {
1385                 rMainWindow->do_update_device_node_name(i, using_flags->get_vm_node_name(i));
1386         }
1387         csp_logger->set_osd(emu->get_osd());
1388         csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GENERAL, "InitInstance() OK.");
1389         
1390         QObject::connect(GuiMain, SIGNAL(lastWindowClosed()),
1391                                          rMainWindow, SLOT(on_actionExit_triggered()));
1392
1393         GuiMain->exec();
1394         return 0;
1395 }
1396
1397 void Ui_MainWindow::do_update_inner_fd(int drv, QStringList base, class Action_Control **action_select_media_list,
1398                                        QStringList lst, int num, bool use_d88_menus)
1399 {
1400 #if defined(USE_FLOPPY_DISK)
1401         if(use_d88_menus) {
1402                 for(int ii = 0; ii < using_flags->get_max_d88_banks(); ii++) {
1403                         if(ii < emu->d88_file[drv].bank_num) {
1404                                 base << lst.value(ii);
1405                                 action_select_media_list[ii]->setText(lst.value(ii));
1406                                 action_select_media_list[ii]->setVisible(true);
1407                                 if(ii == num) action_select_media_list[ii]->setChecked(true);
1408                         } else {
1409                                 if(action_select_media_list[ii] != NULL) {
1410                                         action_select_media_list[ii]->setText(QString::fromUtf8(""));
1411                                         action_select_media_list[ii]->setVisible(false);
1412                                 }
1413                         }
1414                 }
1415         }
1416 #endif  
1417 }
1418
1419 void Ui_MainWindow::do_update_inner_bubble(int drv, QStringList base, class Action_Control **action_select_media_list,
1420                                        QStringList lst, int num, bool use_d88_menus)
1421 {
1422 #if defined(USE_BUBBLE) 
1423         if(use_d88_menus) {
1424                 for(int ii = 0; ii < using_flags->get_max_b77_banks(); ii++) {
1425                         if(ii < emu->b77_file[drv].bank_num) {
1426                                 base << lst.value(ii);
1427                                 action_select_media_list[ii]->setText(lst.value(ii));
1428                                 action_select_media_list[ii]->setVisible(true);
1429                                 if(ii == num) action_select_media_list[ii]->setChecked(true);
1430                         } else {
1431                                 if(action_select_media_list[ii] != NULL) {
1432                                         action_select_media_list[ii]->setText(QString::fromUtf8(""));
1433                                         action_select_media_list[ii]->setVisible(false);
1434                                 }
1435                         }
1436                 }
1437         }
1438 #endif  
1439 }
1440
1441 int Ui_MainWindow::GetBubbleBankNum(int drv)
1442 {
1443 #if MAX_BUBBLE
1444         if((emu != NULL) && (drv >= 0) && (drv < MAX_BUBBLE)) {
1445                 return emu->b77_file[drv].bank_num;
1446         }
1447 #endif
1448         return 0;
1449 }
1450
1451 int Ui_MainWindow::GetBubbleCurrentBankNum(int drv)
1452 {
1453 #if MAX_BUBBLE
1454         if((emu != NULL) && (drv >= 0) && (drv < MAX_BUBBLE)) {
1455                 return emu->b77_file[drv].cur_bank;
1456         }
1457 #endif
1458         return 0;
1459 }
1460
1461 bool Ui_MainWindow::GetBubbleCasetteIsProtected(int drv)
1462 {
1463 #if MAX_BUBBLE
1464         if(emu != NULL) {
1465                 if((drv >= 0) && (drv < MAX_BUBBLE)) {
1466                         return emu->is_bubble_casette_protected(drv);
1467                 }
1468         }
1469 #endif
1470         return false;
1471 }
1472
1473 QString Ui_MainWindow::GetBubbleB77FileName(int drv)
1474 {
1475         QString ans = QString::fromUtf8("");
1476 #if MAX_BUBBLE
1477         if(emu != NULL) {
1478                 if((drv < MAX_BUBBLE) && (drv >= 0)) {
1479                         ans = QString::fromLocal8Bit(emu->b77_file[drv].path);
1480                 }
1481         }
1482 #endif
1483         return ans;
1484 }
1485
1486 QString Ui_MainWindow::GetBubbleB77BubbleName(int drv, int num)
1487 {
1488         QString ans = QString::fromUtf8("");
1489 #if MAX_BUBBLE
1490         if(emu != NULL) {
1491                 if((drv < MAX_BUBBLE) && (drv >= 0)) {
1492                         if((num >= 0) && (num < MAX_B77_BANKS)) {
1493                                 ans = QString::fromLocal8Bit(emu->b77_file[drv].bubble_name[num]);
1494                         }
1495                 }
1496         }
1497 #endif
1498         return ans;
1499 }
1500
1501 #ifdef USE_DEBUGGER
1502 #include <../debugger/qt_debugger.h>
1503
1504 void Ui_MainWindow::OnOpenDebugger(int no)
1505 {
1506         if((no < 0) || (no > 7)) return;
1507         //emu->open_debugger(no);
1508         VM *vm = emu->get_vm();
1509
1510         if((emu->now_debugging ) || (emu->hDebugger != NULL)) /* OnCloseDebugger(); */ return;
1511         
1512         if(!(emu->now_debugging && emu->debugger_thread_param.cpu_index == no)) {
1513                 //emu->close_debugger();
1514                 if(vm->get_cpu(no) != NULL && vm->get_cpu(no)->get_debugger() != NULL) {
1515                         QString windowName = QString::fromUtf8(vm->get_cpu(no)->get_device_name());
1516                         windowName = QString::fromUtf8("Debugger ") + windowName;
1517                         emu->hDebugger = new CSP_Debugger(this);
1518                         QString objNameStr = QString("EmuDebugThread");
1519                         emu->hDebugger->setObjectName(objNameStr);
1520                         emu->hDebugger->debugger_thread_param.osd = emu->get_osd();
1521                         emu->hDebugger->debugger_thread_param.vm = vm;
1522                         emu->hDebugger->debugger_thread_param.cpu_index = no;
1523                         emu->stop_record_sound();
1524                         emu->stop_record_video();
1525                         //emu->now_debugging = true;
1526                         connect(this, SIGNAL(quit_debugger_thread()), emu->hDebugger, SLOT(doExit()));
1527                         connect(this, SIGNAL(destroyed()), emu->hDebugger, SLOT(do_destroy_thread()));
1528                         //connect(this, SIGNAL(quit_debugger_thread()), emu->hDebugger, SLOT(close()));
1529                         connect(emu->hDebugger, SIGNAL(sig_finished()), this, SLOT(OnCloseDebugger()));
1530                         connect(emu->hDebugger, SIGNAL(sig_put_string(QString)), emu->hDebugger, SLOT(put_string(QString)));
1531                         emu->hDebugger->show();
1532                         emu->hDebugger->run();
1533                         emu->hDebugger->setWindowTitle(windowName);
1534                 }
1535         }
1536 }
1537
1538 void Ui_MainWindow::OnCloseDebugger(void )
1539 {
1540
1541 //      emu->close_debugger();
1542         if(emu->now_debugging) {
1543                 if(emu->hDebugger->debugger_thread_param.running) {
1544                         emit quit_debugger_thread();
1545                         //emu->hDebugger->wait();
1546                 }
1547         }
1548         if(emu != NULL) {
1549                 if(emu->hDebugger != NULL) {
1550                         delete emu->hDebugger;
1551                         emu->hDebugger = NULL;
1552                 }
1553                 emu->now_debugging = false;
1554         }
1555 }
1556 #endif
1557