OSDN Git Service

[Qt][General] Move main() to libCSPgui.so.1.1 .
[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 <QString>
14 #include <QTextCodec>
15 #include <QImage>
16 #include <QImageReader>
17 #include <QDir>
18
19 #include "common.h"
20 #include "fileio.h"
21 #include "emu.h"
22 #include "menuclasses.h"
23 #include "mainwidget.h"
24 #include "commonclasses.h"
25 #include "qt_main.h"
26 #include "emu_thread.h"
27 #include "joy_thread.h"
28 #include "draw_thread.h"
29
30 #include "qt_gldraw.h"
31 #include "agar_logger.h"
32
33 #include "menu_disk.h"
34 #include "menu_bubble.h"
35 #include "menu_flags.h"
36 // emulation core
37
38 EMU* emu;
39 QApplication *GuiMain = NULL;
40 extern USING_FLAGS *using_flags;
41
42 // Start to define MainWindow.
43 class META_MainWindow *rMainWindow;
44
45 // buttons
46 #ifdef MAX_BUTTONS
47 #define MAX_FONT_SIZE 32
48 #endif
49
50 // menu
51 std::string cpp_homedir;
52 std::string cpp_confdir;
53 std::string my_procname;
54 std::string sRssDir;
55 bool now_menuloop = false;
56 static int close_notified = 0;
57 // timing control
58
59 // screen
60 unsigned int desktop_width;
61 unsigned int desktop_height;
62 //int desktop_bpp;
63 int prev_window_mode = 0;
64 bool now_fullscreen = false;
65
66 int window_mode_count;
67
68 void Ui_MainWindow::do_set_mouse_enable(bool flag)
69 {
70 #ifdef USE_MOUSE
71         if(emu == NULL) return;
72         emu->lock_vm();
73         if(flag) {
74                 graphicsView->grabMouse();
75                 emu->enable_mouse();
76         } else {
77                 graphicsView->releaseMouse();
78                 emu->disable_mouse();
79         }
80         emu->unlock_vm();
81 #endif  
82 }
83
84 void Ui_MainWindow::do_toggle_mouse(void)
85 {
86 #ifdef USE_MOUSE
87         if(emu == NULL) return;
88         emu->lock_vm();
89         bool flag = emu->is_mouse_enabled();
90         if(!flag) {
91                 graphicsView->grabMouse();
92                 emu->enable_mouse();
93         } else {
94                 graphicsView->releaseMouse();
95                 emu->disable_mouse();
96         }
97         emu->unlock_vm();
98 #endif  
99 }
100
101 void Ui_MainWindow::LaunchEmuThread(void)
102 {
103         QString objNameStr;
104         GLDrawClass *glv = this->getGraphicsView();
105
106         int drvs;
107         
108         hRunEmu = new EmuThreadClass(rMainWindow, emu, this);
109         connect(hRunEmu, SIGNAL(message_changed(QString)), this, SLOT(message_status_bar(QString)));
110         connect(hRunEmu, SIGNAL(sig_is_enable_mouse(bool)), glv, SLOT(do_set_mouse_enabled(bool)));
111         connect(glv, SIGNAL(sig_key_down(uint32_t, uint32_t, bool)), hRunEmu, SLOT(do_key_down(uint32_t, uint32_t, bool)));
112         connect(glv, SIGNAL(sig_key_up(uint32_t, uint32_t)), hRunEmu, SLOT(do_key_up(uint32_t, uint32_t)));
113         
114         //connect(hRunEmu, SIGNAL(sig_finished()), this, SLOT(delete_emu_thread()));
115         connect(this, SIGNAL(sig_vm_reset()), hRunEmu, SLOT(doReset()));
116         connect(this, SIGNAL(sig_vm_specialreset()), hRunEmu, SLOT(doSpecialReset()));
117         connect(this, SIGNAL(sig_vm_loadstate()), hRunEmu, SLOT(doLoadState()));
118         connect(this, SIGNAL(sig_vm_savestate()), hRunEmu, SLOT(doSaveState()));
119
120         connect(this, SIGNAL(sig_emu_update_config()), hRunEmu, SLOT(doUpdateConfig()));
121         connect(this, SIGNAL(sig_emu_update_volume_level(int, int)), hRunEmu, SLOT(doUpdateVolumeLevel(int, int)));
122         connect(this, SIGNAL(sig_emu_update_volume_balance(int, int)), hRunEmu, SLOT(doUpdateVolumeBalance(int, int)));
123         connect(this, SIGNAL(sig_emu_start_rec_sound()), hRunEmu, SLOT(doStartRecordSound()));
124         connect(this, SIGNAL(sig_emu_stop_rec_sound()), hRunEmu, SLOT(doStopRecordSound()));
125         connect(this, SIGNAL(sig_emu_set_display_size(int, int, int, int)), hRunEmu, SLOT(doSetDisplaySize(int, int, int, int)));
126         
127
128 #if defined(USE_FD1) || defined(USE_FD2) || defined(USE_FD3) || defined(USE_FD4) || \
129     defined(USE_FD5) || defined(USE_FD6) || defined(USE_FD7) || defined(USE_FD8)
130         connect(this, SIGNAL(sig_write_protect_disk(int, bool)), hRunEmu, SLOT(do_write_protect_disk(int, bool)));
131         connect(this, SIGNAL(sig_open_disk(int, QString, int)), hRunEmu, SLOT(do_open_disk(int, QString, int)));
132         connect(this, SIGNAL(sig_close_disk(int)), hRunEmu, SLOT(do_close_disk(int)));
133         connect(hRunEmu, SIGNAL(sig_update_recent_disk(int)), this, SLOT(do_update_recent_disk(int)));
134         connect(hRunEmu, SIGNAL(sig_change_osd_fd(int, QString)), this, SLOT(do_change_osd_fd(int, QString)));
135         drvs = 0;
136 # if defined(USE_FD1)
137         drvs = 1;
138 # endif
139 # if defined(USE_FD2)
140         drvs = 2;
141 # endif
142 # if defined(USE_FD3)
143         drvs = 3;
144 # endif
145 # if defined(USE_FD4)
146         drvs = 4;
147 # endif
148 # if defined(USE_FD5)
149         drvs = 5;
150 # endif
151 # if defined(USE_FD6)
152         drvs = 6;
153 # endif
154 # if defined(USE_FD7)
155         drvs = 7;
156 # endif
157 # if defined(USE_FD8)
158         drvs = 8;
159 # endif
160         for(int ii = 0; ii < drvs; ii++) {
161                 menu_fds[ii]->setEmu(emu);
162         }
163 #endif
164 #if defined(USE_TAPE)
165         connect(this, SIGNAL(sig_play_tape(QString)), hRunEmu, SLOT(do_play_tape(QString)));
166         connect(this, SIGNAL(sig_rec_tape(QString)),  hRunEmu, SLOT(do_rec_tape(QString)));
167         connect(this, SIGNAL(sig_close_tape(void)),   hRunEmu, SLOT(do_close_tape(void)));
168         connect(hRunEmu, SIGNAL(sig_change_osd_cmt(QString)), this, SLOT(do_change_osd_cmt(QString)));
169 # if defined(USE_TAPE_BUTTON)
170         connect(this, SIGNAL(sig_cmt_push_play(void)), hRunEmu, SLOT(do_cmt_push_play(void)));
171         connect(this, SIGNAL(sig_cmt_push_stop(void)), hRunEmu, SLOT(do_cmt_push_stop(void)));
172         connect(this, SIGNAL(sig_cmt_push_fast_forward(void)), hRunEmu, SLOT(do_cmt_push_fast_forward(void)));
173         connect(this, SIGNAL(sig_cmt_push_fast_rewind(void)),  hRunEmu, SLOT(do_cmt_push_fast_rewind(void)));
174         connect(this, SIGNAL(sig_cmt_push_apss_forward(void)), hRunEmu, SLOT(do_cmt_push_apss_forward(void)));
175         connect(this, SIGNAL(sig_cmt_push_apss_rewind(void)),  hRunEmu, SLOT(do_cmt_push_apss_rewind(void)));
176 # endif
177 #endif
178 #if defined(USE_QD1)
179         connect(this, SIGNAL(sig_write_protect_quickdisk(int, bool)), hRunEmu, SLOT(do_write_protect_quickdisk(int, bool)));
180         connect(this, SIGNAL(sig_open_quickdisk(int, QString)), hRunEmu, SLOT(do_open_quickdisk(int, QString)));
181         connect(this, SIGNAL(sig_close_quickdisk(int)), hRunEmu, SLOT(do_close_quickdisk(int)));
182         connect(hRunEmu, SIGNAL(sig_change_osd_qd(int, QString)), this, SLOT(do_change_osd_qd(int, QString)));
183 #endif
184 #if defined(USE_CART1)
185         connect(this, SIGNAL(sig_open_cart(int, QString)), hRunEmu, SLOT(do_open_cart(int, QString)));
186         connect(this, SIGNAL(sig_close_cart(int)), hRunEmu, SLOT(do_close_cart(int)));
187 #endif
188 #if defined(USE_COMPACT_DISC)
189         connect(this, SIGNAL(sig_open_cdrom(QString)), hRunEmu, SLOT(do_open_cdrom(QString)));
190         connect(this, SIGNAL(sig_close_cdrom()), hRunEmu, SLOT(do_eject_cdrom()));
191         connect(hRunEmu, SIGNAL(sig_change_osd_cdrom(QString)), this, SLOT(do_change_osd_cdrom(QString)));
192 #endif  
193 #if defined(USE_LASER_DISK)
194         connect(this, SIGNAL(sig_open_laser_disk(QString)), hRunEmu, SLOT(do_open_laser_disk(QString)));
195         connect(this, SIGNAL(sig_close_laser_disk(void)), hRunEmu, SLOT(do_close_laser_disk(void)));
196 #endif
197 #if defined(USE_BINARY_FILE1)
198         connect(this, SIGNAL(sig_load_binary(int, QString)), hRunEmu, SLOT(do_load_binary(int, QString)));
199         connect(this, SIGNAL(sig_save_binary(int, QString)), hRunEmu, SLOT(do_save_binary(int, QString)));
200 #endif
201 #if defined(USE_BUBBLE1)
202         connect(this, SIGNAL(sig_write_protect_bubble(int, bool)), hRunEmu, SLOT(do_write_protect_bubble_casette(int, bool)));
203         connect(this, SIGNAL(sig_open_bubble(int, QString, int)), hRunEmu, SLOT(do_open_bubble_casette(int, QString, int)));
204         connect(this, SIGNAL(sig_close_bubble(int)), hRunEmu, SLOT(do_close_bubble_casette(int)));
205         connect(hRunEmu, SIGNAL(sig_update_recent_bubble(int)), this, SLOT(do_update_recent_bubble(int)));
206         connect(hRunEmu, SIGNAL(sig_change_osd_bubble(int, QString)), this, SLOT(do_change_osd_bubble(int, QString)));
207         drvs = 0;
208 # if defined(USE_BUBBLE1)
209         drvs = 1;
210 # endif
211 # if defined(USE_BUBBLE2)
212         drvs = 2;
213 # endif
214         for(int ii = 0; ii < drvs; ii++) {
215                 menu_bubbles[ii]->setEmu(emu);
216         }
217 #endif
218         
219         connect(this, SIGNAL(quit_emu_thread()), hRunEmu, SLOT(doExit()));
220         connect(hRunEmu, SIGNAL(sig_mouse_enable(bool)),
221                         this, SLOT(do_set_mouse_enable(bool)));
222 #ifdef USE_TAPE_BUTTON
223         hRunEmu->set_tape_play(false);
224 #endif
225 #ifdef USE_LED_DEVICE
226         connect(hRunEmu, SIGNAL(sig_send_data_led(quint32)), this, SLOT(do_recv_data_led(quint32)));
227 #endif
228 #ifdef USE_AUTO_KEY
229         connect(this, SIGNAL(sig_start_auto_key(QString)), hRunEmu, SLOT(do_start_auto_key(QString)));
230         connect(this, SIGNAL(sig_stop_auto_key()), hRunEmu, SLOT(do_stop_auto_key()));
231 #endif  
232         //connect(actionExit_Emulator, SIGNAL(triggered()), hRunEmu, SLOT(doExit()));
233         AGAR_DebugLog(AGAR_LOG_DEBUG, "EmuThread : Start.");
234         objNameStr = QString("EmuThreadClass");
235         hRunEmu->setObjectName(objNameStr);
236         
237         hDrawEmu = new DrawThreadClass(emu, emu->get_osd(), this);
238         emu->set_parent_handler(hRunEmu, hDrawEmu);
239         
240 #ifdef ONE_BOARD_MICRO_COMPUTER
241         QImageReader *reader = new QImageReader(":/background.png");
242         QImage *result = new QImage(reader->read()); // this acts as a default if the size is not matched
243         glv->updateBitmap(result);
244         delete result;
245         delete reader;
246 #endif
247         AGAR_DebugLog(AGAR_LOG_DEBUG, "DrawThread : Start.");
248         connect(hDrawEmu, SIGNAL(sig_draw_frames(int)), hRunEmu, SLOT(print_framerate(int)));
249         connect(hRunEmu, SIGNAL(window_title_changed(QString)), this, SLOT(do_set_window_title(QString)));
250         connect(hDrawEmu, SIGNAL(message_changed(QString)), this, SLOT(message_status_bar(QString)));
251         connect(actionCapture_Screen, SIGNAL(triggered()), glv, SLOT(do_save_frame_screen()));
252                 
253         connect(hRunEmu, SIGNAL(sig_draw_thread(bool)), hDrawEmu, SLOT(doDraw(bool)));
254         //connect(hRunEmu, SIGNAL(quit_draw_thread()), hDrawEmu, SLOT(doExit()));
255         connect(this, SIGNAL(quit_draw_thread()), hDrawEmu, SLOT(doExit()));
256
257         connect(glv, SIGNAL(sig_draw_timing(bool)), hRunEmu, SLOT(do_draw_timing(bool)));
258         connect(hDrawEmu, SIGNAL(sig_draw_timing(bool)), hRunEmu, SLOT(do_draw_timing(bool)));
259         
260         connect(glv, SIGNAL(do_notify_move_mouse(int, int)),
261                         hRunEmu, SLOT(moved_mouse(int, int)));
262         connect(glv, SIGNAL(do_notify_button_pressed(Qt::MouseButton)),
263                 hRunEmu, SLOT(button_pressed_mouse(Qt::MouseButton)));
264         connect(glv, SIGNAL(do_notify_button_released(Qt::MouseButton)),
265                         hRunEmu, SLOT(button_released_mouse(Qt::MouseButton)));
266 #ifdef USE_MOUSE
267         connect(glv, SIGNAL(sig_toggle_mouse(void)),
268                         this, SLOT(do_toggle_mouse(void)));
269 #endif
270         connect(hRunEmu, SIGNAL(sig_resize_screen(int, int)),
271                         glv, SLOT(resizeGL(int, int)));
272         
273         connect(glv, SIGNAL(sig_resize_uibar(int, int)),
274                         this, SLOT(resize_statusbar(int, int)));
275         connect(hRunEmu, SIGNAL(sig_resize_uibar(int, int)),
276                         this, SLOT(resize_statusbar(int, int)));
277         
278         connect(hRunEmu, SIGNAL(sig_finished()), glv, SLOT(releaseKeyCode(void)));
279         connect(hRunEmu, SIGNAL(sig_finished()), this, SLOT(delete_emu_thread()));
280         objNameStr = QString("EmuDrawThread");
281         hDrawEmu->setObjectName(objNameStr);
282         hDrawEmu->start();
283         AGAR_DebugLog(AGAR_LOG_DEBUG, "DrawThread : Launch done.");
284         
285         hRunEmu->start();
286         AGAR_DebugLog(AGAR_LOG_DEBUG, "EmuThread : Launch done.");
287         this->set_screen_aspect(config.window_stretch_type);
288 }
289
290 void Ui_MainWindow::LaunchJoyThread(void)
291 {
292 #if defined(USE_JOYSTICK)
293         hRunJoy = new JoyThreadClass(emu, emu->get_osd(), this);
294         connect(this, SIGNAL(quit_joy_thread()), hRunJoy, SLOT(doExit()));
295         hRunJoy->setObjectName("JoyThread");
296         hRunJoy->start();
297 #endif  
298 }
299
300 void Ui_MainWindow::StopJoyThread(void)
301 {
302 #if defined(USE_JOYSTICK)
303         emit quit_joy_thread();
304 #endif  
305 }
306
307 void Ui_MainWindow::delete_joy_thread(void)
308 {
309         //    delete hRunJoyThread;
310         //  delete hRunJoy;
311 }
312
313 void Ui_MainWindow::on_actionExit_triggered()
314 {
315         save_config(create_local_path(_T("%s.ini"), _T(CONFIG_NAME)));
316         OnMainWindowClosed();
317 }
318
319 void Ui_MainWindow::OnWindowRedraw(void)
320 {
321         if(emu) {
322                 //emu->update_screen();
323         }
324 }
325
326 void Ui_MainWindow::OnWindowMove(void)
327 {
328         if(emu) {
329                 emu->suspend();
330         }
331 }
332
333
334 #ifdef USE_POWER_OFF
335 bool Ui_MainWindow::GetPowerState(void)
336 {
337         if(close_notified == 0) return true;
338         return false;
339 }
340 #endif
341
342 void Ui_MainWindow::OnMainWindowClosed(void)
343 {
344         // notify power off
345 #ifdef USE_POWER_OFF
346         if(emu) {
347                 if(!close_notified) {
348                         emu->lock_vm();
349                         emu->notify_power_off();
350                         emu->unlock_vm();
351                         close_notified = 1;
352                         return; 
353                 }
354         }
355 #endif
356         if(statusUpdateTimer != NULL) statusUpdateTimer->stop();
357 #ifdef USE_LED_DEVICE
358         if(ledUpdateTimer != NULL) ledUpdateTimer->stop();
359 #endif
360         emit quit_draw_thread();
361         emit quit_joy_thread();
362         emit quit_emu_thread();
363         if(hDrawEmu != NULL) {
364                 hDrawEmu->wait();
365                 delete hDrawEmu;
366         }
367         if(hRunEmu != NULL) {
368                 hRunEmu->wait();
369                 delete hRunEmu;
370         }
371 #if defined(USE_JOYSTICK)
372         if(hRunJoy != NULL) {
373                 hRunJoy->wait();
374                 delete hRunJoy;
375         }
376 #endif  
377         do_release_emu_resources();
378
379         // release window
380         if(now_fullscreen) {
381                 //ChangeDisplaySettings(NULL, 0);
382         }
383         now_fullscreen = false;
384         return;
385 }
386
387 extern "C" {
388
389 void LostFocus(QWidget *widget)
390 {
391         if(emu) {
392                 emu->key_lost_focus();
393         }
394 }
395  
396 }  // extern "C"
397
398 void Ui_MainWindow::do_release_emu_resources(void)
399 {
400         if(emu) {
401                 delete emu;
402                 emu = NULL;
403         }
404 }
405
406 extern void get_long_full_path_name(_TCHAR* src, _TCHAR* dst);
407 extern _TCHAR* get_parent_dir(_TCHAR* file);
408 extern void get_short_filename(_TCHAR *dst, _TCHAR *file, int maxlen);
409
410
411 static void setup_logs(void)
412 {
413         std::string archstr;
414         std::string delim;
415         int  bLogSYSLOG;
416         int  bLogSTDOUT;
417         char    *p;
418
419         my_procname = "emu";
420         my_procname = my_procname + CONFIG_NAME;
421 #if defined(Q_OS_WIN)
422         delim = "\\";
423 #else
424         delim = "/";
425 #endif
426 #if !defined(Q_OS_WIN)
427         p = SDL_getenv("HOME");
428         if(p == NULL) {
429                 p = SDL_getenv("PWD");
430                 if(p == NULL) {
431                         cpp_homedir = ".";
432                 } else {
433                         cpp_homedir = p;
434                 }
435                 std::string tmpstr;
436                 tmpstr = "Warning : Can't get HOME directory...Making conf on " +  cpp_homedir + delim;
437                 perror(tmpstr.c_str());
438         } else {
439                 cpp_homedir = p;
440         }
441 #else
442         cpp_homedir = ".";
443 #endif  
444         cpp_homedir = cpp_homedir + delim;
445 #ifdef _WINDOWS
446         cpp_confdir = cpp_homedir + my_procname + delim;
447 #else
448         cpp_confdir = cpp_homedir + ".config" + delim + my_procname + delim;
449 #endif
450         bLogSYSLOG = (int)0;
451         bLogSTDOUT = (int)1;
452         AGAR_OpenLog(bLogSYSLOG, bLogSTDOUT, DEVICE_NAME); // Write to syslog, console
453         
454         archstr = "Generic";
455 #if defined(__x86_64__)
456         archstr = "amd64";
457 #endif
458 #if defined(__i386__)
459         archstr = "ia32";
460 #endif
461         AGAR_DebugLog(AGAR_LOG_INFO, "Start Common Source Project '%s'", my_procname.c_str());
462         AGAR_DebugLog(AGAR_LOG_INFO, "(C) Toshiya Takeda / Qt Version K.Ohta");
463         AGAR_DebugLog(AGAR_LOG_INFO, "Architecture: %s", archstr.c_str());
464         
465         //AGAR_DebugLog(AGAR_LOG_INFO, " -? is print help(s).");
466         AGAR_DebugLog(AGAR_LOG_DEBUG, "Moduledir = %s home = %s", cpp_confdir.c_str(), cpp_homedir.c_str()); // Debug
467 #if !defined(Q_OS_CYGWIN)       
468         {
469                 QDir dir;
470                 dir.mkdir( QString::fromStdString(cpp_confdir));
471         }
472 #endif   
473    //AG_MkPath(cpp_confdir.c_str());
474    /* Gettext */
475 #ifndef RSSDIR
476 #if defined(_USE_AGAR) || defined(_USE_QT)
477         sRssDir = "/usr/local/share/";
478 #else
479         sRssDir = "." + delim;
480 #endif
481         sRssDir = sRssDir + "CommonSourceCodeProject" + delim + my_procname;
482 #else
483         sRssDir = RSSDIR;
484 #endif
485 }
486
487 int MainLoop(int argc, char *argv[])
488 {
489         char c;
490         char strbuf[2048];
491         bool flag;
492         char homedir[PATH_MAX];
493         int thread_ret;
494         int w, h;
495         setup_logs();
496         cpp_homedir.copy(homedir, PATH_MAX - 1, 0);
497         flag = FALSE;
498         /*
499          * Into Qt's Loop.
500          */
501 #if defined(USE_SDL2)
502         SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
503 #else
504         SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);
505         //SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO);
506 #endif
507         AGAR_DebugLog(AGAR_LOG_DEBUG, "Audio and JOYSTICK subsystem was initialised.");
508         GuiMain = new QApplication(argc, argv);
509
510         load_config(create_local_path(_T("%s.ini"), _T(CONFIG_NAME)));
511         
512         rMainWindow = new META_MainWindow();
513         rMainWindow->connect(rMainWindow, SIGNAL(sig_quit_all(void)), rMainWindow, SLOT(deleteLater(void)));
514         rMainWindow->setCoreApplication(GuiMain);
515         
516         AGAR_DebugLog(AGAR_LOG_DEBUG, "InitInstance() OK.");
517   
518         // disable ime
519         
520         // initialize emulation core
521         rMainWindow->getWindow()->show();
522         emu = new EMU(rMainWindow, rMainWindow->getGraphicsView());
523 #ifdef SUPPORT_DRAG_DROP
524         // open command line path
525         //      if(szCmdLine[0]) {
526         //      if(szCmdLine[0] == _T('"')) {
527         //              int len = strlen(szCmdLine);
528         //              szCmdLine[len - 1] = _T('\0');
529         //              szCmdLine++;
530         //      }
531         //      _TCHAR path[_MAX_PATH];
532         //      get_long_full_path_name(szCmdLine, path);
533         //      open_any_file(path);
534         //}
535 #endif
536         
537         // set priority
538         
539         // main loop
540         GLDrawClass *pgl = rMainWindow->getGraphicsView();
541         pgl->setEmuPtr(emu);
542         pgl->setFixedSize(pgl->width(), pgl->height());
543         
544 #if defined(USE_JOYSTICK)
545         rMainWindow->LaunchJoyThread();
546 #endif  
547         rMainWindow->LaunchEmuThread();
548         QObject::connect(GuiMain, SIGNAL(lastWindowClosed()),
549                                          rMainWindow, SLOT(on_actionExit_triggered()));
550         GuiMain->exec();
551         return 0;
552 }
553
554 #ifdef USE_DEBUGGER
555 #include <../debugger/qt_debugger.h>
556
557 void Ui_MainWindow::OnOpenDebugger(int no)
558 {
559         if((no < 0) || (no > 3)) return;
560         //emu->open_debugger(no);
561         VM *vm = emu->get_vm();
562
563         if(emu->now_debugging)  this->OnCloseDebugger();
564         if(!(emu->now_debugging && emu->debugger_thread_param.cpu_index == no)) {
565                 //emu->close_debugger();
566                 if(vm->get_cpu(no) != NULL && vm->get_cpu(no)->get_debugger() != NULL) {
567                         
568                         emu->hDebugger = new CSP_Debugger(this);
569                         QString objNameStr = QString("EmuDebugThread");
570                         emu->hDebugger->setObjectName(objNameStr);
571                         emu->hDebugger->debugger_thread_param.osd = emu->get_osd();
572                         emu->hDebugger->debugger_thread_param.vm = vm;
573                         emu->hDebugger->debugger_thread_param.cpu_index = no;
574                         emu->stop_record_sound();
575                         emu->stop_record_video();
576                         emu->now_debugging = true;
577                         connect(this, SIGNAL(quit_debugger_thread()), emu->hDebugger, SLOT(doExit()));
578                         //connect(this, SIGNAL(quit_debugger_thread()), emu->hDebugger, SLOT(close()));
579                         connect(emu->hDebugger, SIGNAL(sig_finished()), this, SLOT(OnCloseDebugger()));
580                         connect(emu->hDebugger, SIGNAL(sig_put_string(QString)), emu->hDebugger, SLOT(put_string(QString)));
581                         emu->hDebugger->show();
582                         emu->hDebugger->run();
583                 }
584         }
585 }
586
587 void Ui_MainWindow::OnCloseDebugger(void )
588 {
589
590 //      emu->close_debugger();
591         if(emu->now_debugging) {
592                 if(emu->hDebugger->debugger_thread_param.running) {
593                         emit quit_debugger_thread();
594                         //emu->hDebugger->wait();
595                 }
596                 delete emu->hDebugger;
597                 emu->hDebugger = NULL;
598                 emu->now_debugging = false;
599         }
600 }
601 #endif