OSDN Git Service

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