OSDN Git Service

a17abee1c92e1cb4b0aad133d15d478cd062438b
[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 "qt_glutil_gl2_0.h"
33 #include "agar_logger.h"
34
35 #include "menu_disk.h"
36 #include "menu_bubble.h"
37 #include "menu_flags.h"
38 #include "dialog_movie.h"
39 #include "../avio/movie_saver.h"
40 // emulation core
41
42 EMU* emu;
43 QApplication *GuiMain = NULL;
44
45 // Start to define MainWindow.
46 class META_MainWindow *rMainWindow;
47
48 // buttons
49 #ifdef MAX_BUTTONS
50 #define MAX_FONT_SIZE 32
51 #endif
52
53 // menu
54 std::string cpp_homedir;
55 std::string cpp_confdir;
56 std::string my_procname;
57 std::string sRssDir;
58 bool now_menuloop = false;
59 static int close_notified = 0;
60 // timing control
61
62 // screen
63 unsigned int desktop_width;
64 unsigned int desktop_height;
65 //int desktop_bpp;
66 int prev_window_mode = 0;
67 bool now_fullscreen = false;
68
69 int window_mode_count;
70
71 void Ui_MainWindow::do_set_mouse_enable(bool flag)
72 {
73 #ifdef USE_MOUSE
74         if(emu == NULL) return;
75         emu->lock_vm();
76         if(flag) {
77                 graphicsView->grabMouse();
78                 emu->enable_mouse();
79         } else {
80                 graphicsView->releaseMouse();
81                 emu->disable_mouse();
82         }
83         emu->unlock_vm();
84 #endif  
85 }
86
87 void Ui_MainWindow::do_toggle_mouse(void)
88 {
89 #ifdef USE_MOUSE
90         if(emu == NULL) return;
91         emu->lock_vm();
92         bool flag = emu->is_mouse_enabled();
93         if(!flag) {
94                 graphicsView->grabMouse();
95                 emu->enable_mouse();
96         } else {
97                 graphicsView->releaseMouse();
98                 emu->disable_mouse();
99         }
100         emu->unlock_vm();
101 #endif  
102 }
103
104
105 void Object_Menu_Control::do_save_as_movie(void)
106 {
107         int fps = config.video_frame_rate;
108         emit sig_start_record_movie(fps);
109 }
110
111 void Object_Menu_Control::do_stop_saving_movie(void)
112 {
113         emit sig_stop_record_movie();
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
123 void Ui_MainWindow::LaunchEmuThread(void)
124 {
125         QString objNameStr;
126         GLDrawClass *glv = this->getGraphicsView();
127
128         int drvs;
129         
130         hRunEmu = new EmuThreadClass(rMainWindow, emu, using_flags, this);
131         connect(hRunEmu, SIGNAL(message_changed(QString)), this, SLOT(message_status_bar(QString)));
132         connect(hRunEmu, SIGNAL(sig_is_enable_mouse(bool)), glv, SLOT(do_set_mouse_enabled(bool)));
133         connect(glv, SIGNAL(sig_key_down(uint32_t, uint32_t, bool)), hRunEmu, SLOT(do_key_down(uint32_t, uint32_t, bool)));
134         connect(glv, SIGNAL(sig_key_up(uint32_t, uint32_t)), hRunEmu, SLOT(do_key_up(uint32_t, uint32_t)));
135         connect(this, SIGNAL(sig_quit_widgets()), glv, SLOT(do_stop_run_vm()));
136
137         
138         //connect(hRunEmu, SIGNAL(sig_finished()), this, SLOT(delete_emu_thread()));
139         connect(this, SIGNAL(sig_vm_reset()), hRunEmu, SLOT(doReset()));
140         connect(this, SIGNAL(sig_vm_specialreset()), hRunEmu, SLOT(doSpecialReset()));
141         connect(this, SIGNAL(sig_vm_loadstate()), hRunEmu, SLOT(doLoadState()));
142         connect(this, SIGNAL(sig_vm_savestate()), hRunEmu, SLOT(doSaveState()));
143
144         connect(this, SIGNAL(sig_emu_update_config()), hRunEmu, SLOT(doUpdateConfig()));
145         connect(this, SIGNAL(sig_emu_update_volume_level(int, int)), hRunEmu, SLOT(doUpdateVolumeLevel(int, int)));
146         connect(this, SIGNAL(sig_emu_update_volume_balance(int, int)), hRunEmu, SLOT(doUpdateVolumeBalance(int, int)));
147         connect(this, SIGNAL(sig_emu_start_rec_sound()), hRunEmu, SLOT(doStartRecordSound()));
148         connect(this, SIGNAL(sig_emu_stop_rec_sound()), hRunEmu, SLOT(doStopRecordSound()));
149         connect(this, SIGNAL(sig_emu_set_display_size(int, int, int, int)), hRunEmu, SLOT(doSetDisplaySize(int, int, int, int)));
150         
151
152 #if defined(USE_FD1) || defined(USE_FD2) || defined(USE_FD3) || defined(USE_FD4) || \
153     defined(USE_FD5) || defined(USE_FD6) || defined(USE_FD7) || defined(USE_FD8)
154         connect(this, SIGNAL(sig_write_protect_disk(int, bool)), hRunEmu, SLOT(do_write_protect_disk(int, bool)));
155         connect(this, SIGNAL(sig_open_disk(int, QString, int)), hRunEmu, SLOT(do_open_disk(int, QString, int)));
156         connect(this, SIGNAL(sig_close_disk(int)), hRunEmu, SLOT(do_close_disk(int)));
157         connect(hRunEmu, SIGNAL(sig_update_recent_disk(int)), this, SLOT(do_update_recent_disk(int)));
158         connect(hRunEmu, SIGNAL(sig_change_osd_fd(int, QString)), this, SLOT(do_change_osd_fd(int, QString)));
159         drvs = 0;
160 # if defined(USE_FD1)
161         drvs = 1;
162 # endif
163 # if defined(USE_FD2)
164         drvs = 2;
165 # endif
166 # if defined(USE_FD3)
167         drvs = 3;
168 # endif
169 # if defined(USE_FD4)
170         drvs = 4;
171 # endif
172 # if defined(USE_FD5)
173         drvs = 5;
174 # endif
175 # if defined(USE_FD6)
176         drvs = 6;
177 # endif
178 # if defined(USE_FD7)
179         drvs = 7;
180 # endif
181 # if defined(USE_FD8)
182         drvs = 8;
183 # endif
184         for(int ii = 0; ii < drvs; ii++) {
185                 menu_fds[ii]->setEmu(emu);
186                 connect(menu_fds[ii], SIGNAL(sig_update_inner_fd(int ,QStringList , class Action_Control **, QStringList , int, bool)),
187                                 this, SLOT(do_update_inner_fd(int ,QStringList , class Action_Control **, QStringList , int, bool)));
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                 connect(menu_bubbles[ii],
243                                 SIGNAL(sig_update_inner_bubble(int ,QStringList , class Action_Control **, QStringList , int, bool)),
244                                 this,
245                                 SLOT(do_update_inner_bubble(int ,QStringList , class Action_Control **, QStringList , int, bool)));
246         }
247 #endif
248         
249         connect(this, SIGNAL(quit_emu_thread()), hRunEmu, SLOT(doExit()));
250         connect(hRunEmu, SIGNAL(sig_mouse_enable(bool)),
251                         this, SLOT(do_set_mouse_enable(bool)));
252 #ifdef USE_TAPE_BUTTON
253         hRunEmu->set_tape_play(false);
254 #endif
255 #ifdef USE_LED_DEVICE
256         connect(hRunEmu, SIGNAL(sig_send_data_led(quint32)), this, SLOT(do_recv_data_led(quint32)));
257 #endif
258 #ifdef USE_AUTO_KEY
259         connect(this, SIGNAL(sig_start_auto_key(QString)), hRunEmu, SLOT(do_start_auto_key(QString)));
260         connect(this, SIGNAL(sig_stop_auto_key()), hRunEmu, SLOT(do_stop_auto_key()));
261 #endif  
262         //connect(actionExit_Emulator, SIGNAL(triggered()), hRunEmu, SLOT(doExit()));
263         AGAR_DebugLog(AGAR_LOG_DEBUG, "EmuThread : Start.");
264         objNameStr = QString("EmuThreadClass");
265         hRunEmu->setObjectName(objNameStr);
266         
267         hDrawEmu = new DrawThreadClass(emu, emu->get_osd(), this);
268         emu->set_parent_handler(hRunEmu, hDrawEmu);
269         
270 #ifdef ONE_BOARD_MICRO_COMPUTER
271         QImageReader *reader = new QImageReader(":/background.png");
272         QImage *result = new QImage(reader->read()); // this acts as a default if the size is not matched
273         glv->updateBitmap(result);
274         emu->get_osd()->upload_bitmap(result);
275         delete result;
276         delete reader;
277         emu->get_osd()->set_buttons();
278 #endif
279         AGAR_DebugLog(AGAR_LOG_DEBUG, "DrawThread : Start.");
280         connect(hDrawEmu, SIGNAL(sig_draw_frames(int)), hRunEmu, SLOT(print_framerate(int)));
281         connect(hRunEmu, SIGNAL(window_title_changed(QString)), this, SLOT(do_set_window_title(QString)));
282         connect(hDrawEmu, SIGNAL(message_changed(QString)), this, SLOT(message_status_bar(QString)));
283         connect(actionCapture_Screen, SIGNAL(triggered()), glv, SLOT(do_save_frame_screen()));
284                 
285         connect(hRunEmu, SIGNAL(sig_draw_thread(bool)), hDrawEmu, SLOT(doDraw(bool)));
286         //connect(hRunEmu, SIGNAL(quit_draw_thread()), hDrawEmu, SLOT(doExit()));
287         connect(this, SIGNAL(quit_draw_thread()), hDrawEmu, SLOT(doExit()));
288
289         connect(glv, SIGNAL(sig_draw_timing(bool)), hRunEmu, SLOT(do_draw_timing(bool)));
290         connect(hDrawEmu, SIGNAL(sig_draw_timing(bool)), hRunEmu, SLOT(do_draw_timing(bool)));
291         
292         connect(glv, SIGNAL(do_notify_move_mouse(int, int)),
293                         hRunEmu, SLOT(moved_mouse(int, int)));
294         connect(glv, SIGNAL(do_notify_button_pressed(Qt::MouseButton)),
295                 hRunEmu, SLOT(button_pressed_mouse(Qt::MouseButton)));
296         connect(glv, SIGNAL(do_notify_button_released(Qt::MouseButton)),
297                         hRunEmu, SLOT(button_released_mouse(Qt::MouseButton)));
298 #ifdef USE_MOUSE
299         connect(glv, SIGNAL(sig_toggle_mouse(void)),
300                         this, SLOT(do_toggle_mouse(void)));
301 #endif
302         connect(hRunEmu, SIGNAL(sig_resize_screen(int, int)),
303                         glv, SLOT(resizeGL(int, int)));
304         
305         connect(glv, SIGNAL(sig_resize_uibar(int, int)),
306                         this, SLOT(resize_statusbar(int, int)));
307         connect(hRunEmu, SIGNAL(sig_resize_uibar(int, int)),
308                         this, SLOT(resize_statusbar(int, int)));
309
310         connect(emu->get_osd(), SIGNAL(sig_req_encueue_video(int, int, int)),
311                         hDrawEmu, SLOT(do_req_encueue_video(int, int, int)));
312         connect(hRunEmu, SIGNAL(sig_finished()), glv, SLOT(releaseKeyCode(void)));
313         connect(hRunEmu, SIGNAL(sig_finished()), this, SLOT(delete_emu_thread()));
314         objNameStr = QString("EmuDrawThread");
315         hDrawEmu->setObjectName(objNameStr);
316         hDrawEmu->start();
317         AGAR_DebugLog(AGAR_LOG_DEBUG, "DrawThread : Launch done.");
318
319         hSaveMovieThread = new MOVIE_SAVER(640, 400,  30, emu->get_osd());
320         
321         connect(actionStart_Record_Movie->binds, SIGNAL(sig_start_record_movie(int)), hRunEmu, SLOT(doStartRecordVideo(int)));
322         connect(this, SIGNAL(sig_start_saving_movie()),
323                         actionStart_Record_Movie->binds, SLOT(do_save_as_movie()));
324         connect(actionStart_Record_Movie, SIGNAL(triggered()), this, SLOT(do_start_saving_movie()));
325
326         connect(actionStop_Record_Movie->binds, SIGNAL(sig_stop_record_movie()), hRunEmu, SLOT(doStopRecordVideo()));
327         connect(this, SIGNAL(sig_stop_saving_movie()), actionStop_Record_Movie->binds, SLOT(do_stop_saving_movie()));
328         connect(actionStop_Record_Movie, SIGNAL(triggered()), this, SLOT(do_stop_saving_movie()));
329
330         connect(emu->get_osd(), SIGNAL(sig_save_as_movie(QString, int, int)),
331                         hSaveMovieThread, SLOT(do_open(QString, int, int)));
332         connect(emu->get_osd(), SIGNAL(sig_stop_saving_movie()), hSaveMovieThread, SLOT(do_close()));
333
334         actionStop_Record_Movie->setIcon(QIcon(":/icon_process_stop.png"));
335         actionStop_Record_Movie->setVisible(false);
336         
337         connect(this, SIGNAL(sig_movie_set_width(int)), hSaveMovieThread, SLOT(do_set_width(int)));
338         connect(this, SIGNAL(sig_movie_set_height(int)), hSaveMovieThread, SLOT(do_set_height(int)));
339  
340         connect(emu->get_osd(), SIGNAL(sig_movie_set_width(int)), hSaveMovieThread, SLOT(do_set_width(int)));
341         connect(emu->get_osd(), SIGNAL(sig_movie_set_height(int)), hSaveMovieThread, SLOT(do_set_height(int)));
342    
343         connect(emu->get_osd(), SIGNAL(sig_enqueue_audio(int16_t*, int)), hSaveMovieThread, SLOT(enqueue_audio(int16_t *, int)));
344         connect(emu->get_osd(), SIGNAL(sig_enqueue_video(QImage *)), hSaveMovieThread, SLOT(enqueue_video(QImage *)));
345         connect(glv->extfunc, SIGNAL(sig_push_image_to_movie(QImage *)), hSaveMovieThread, SLOT(enqueue_video(QImage *)));
346         connect(this, SIGNAL(sig_quit_movie_thread()), hSaveMovieThread, SLOT(do_exit()));
347
348         objNameStr = QString("EmuMovieThread");
349         hSaveMovieThread->setObjectName(objNameStr);
350         hSaveMovieThread->start();
351         AGAR_DebugLog(AGAR_LOG_DEBUG, "MovieThread : Launch done.");
352
353         connect(action_SetupMovie, SIGNAL(triggered()), this, SLOT(rise_movie_dialog()));
354
355
356         hRunEmu->start();
357         AGAR_DebugLog(AGAR_LOG_DEBUG, "EmuThread : Launch done.");
358         this->set_screen_aspect(config.window_stretch_type);
359         emit sig_movie_set_width(SCREEN_WIDTH);
360         emit sig_movie_set_height(SCREEN_HEIGHT);
361 }
362
363 void Ui_MainWindow::LaunchJoyThread(void)
364 {
365 #if defined(USE_JOYSTICK)
366         hRunJoy = new JoyThreadClass(emu, emu->get_osd(), using_flags);
367         connect(this, SIGNAL(quit_joy_thread()), hRunJoy, SLOT(doExit()));
368         hRunJoy->setObjectName("JoyThread");
369         hRunJoy->start();
370 #endif  
371 }
372
373 void Ui_MainWindow::StopJoyThread(void)
374 {
375 #if defined(USE_JOYSTICK)
376         emit quit_joy_thread();
377 #endif  
378 }
379
380 void Ui_MainWindow::delete_joy_thread(void)
381 {
382         //    delete hRunJoyThread;
383         //  delete hRunJoy;
384 }
385
386 void Ui_MainWindow::on_actionExit_triggered()
387 {
388         save_config(create_local_path(_T("%s.ini"), _T(CONFIG_NAME)));
389         OnMainWindowClosed();
390 }
391
392 void Ui_MainWindow::OnWindowRedraw(void)
393 {
394         if(emu) {
395                 //emu->update_screen();
396         }
397 }
398
399 void Ui_MainWindow::OnWindowMove(void)
400 {
401         if(emu) {
402                 emu->suspend();
403         }
404 }
405
406
407 #ifdef USE_POWER_OFF
408 bool Ui_MainWindow::GetPowerState(void)
409 {
410         if(close_notified == 0) return true;
411         return false;
412 }
413 #endif
414
415 void Ui_MainWindow::OnMainWindowClosed(void)
416 {
417         // notify power off
418 #ifdef USE_POWER_OFF
419         if(emu) {
420                 if(!close_notified) {
421                         emu->lock_vm();
422                         emu->notify_power_off();
423                         emu->unlock_vm();
424                         close_notified = 1;
425                         return; 
426                 }
427         }
428 #endif
429         if(statusUpdateTimer != NULL) statusUpdateTimer->stop();
430 #ifdef USE_LED_DEVICE
431         if(ledUpdateTimer != NULL) ledUpdateTimer->stop();
432 #endif
433         emit quit_draw_thread();
434         emit quit_joy_thread();
435         emit quit_emu_thread();
436         emit sig_quit_movie_thread();
437         emit sig_quit_widgets();
438         
439         if(hSaveMovieThread != NULL) {
440                 hSaveMovieThread->wait();
441                 delete hSaveMovieThread;
442         }
443    
444         if(hDrawEmu != NULL) {
445                 hDrawEmu->wait();
446                 delete hDrawEmu;
447         }
448         if(hRunEmu != NULL) {
449                 hRunEmu->wait();
450                 delete hRunEmu;
451         }
452 #if defined(USE_JOYSTICK)
453         if(hRunJoy != NULL) {
454                 hRunJoy->wait();
455                 delete hRunJoy;
456         }
457 #endif  
458         do_release_emu_resources();
459
460         // release window
461         if(now_fullscreen) {
462                 //ChangeDisplaySettings(NULL, 0);
463         }
464         now_fullscreen = false;
465         return;
466 }
467
468 extern "C" {
469
470 void LostFocus(QWidget *widget)
471 {
472         if(emu) {
473                 emu->key_lost_focus();
474         }
475 }
476  
477 }  // extern "C"
478
479 void Ui_MainWindow::do_release_emu_resources(void)
480 {
481         if(emu) {
482                 delete emu;
483                 emu = NULL;
484         }
485 }
486
487 extern void get_long_full_path_name(_TCHAR* src, _TCHAR* dst);
488 extern _TCHAR* get_parent_dir(_TCHAR* file);
489 extern void get_short_filename(_TCHAR *dst, _TCHAR *file, int maxlen);
490
491
492 static void setup_logs(void)
493 {
494         std::string archstr;
495         std::string delim;
496         int  bLogSYSLOG;
497         int  bLogSTDOUT;
498         char    *p;
499
500         my_procname = "emu";
501         my_procname = my_procname + CONFIG_NAME;
502 #if defined(Q_OS_WIN)
503         delim = "\\";
504 #else
505         delim = "/";
506 #endif
507 #if !defined(Q_OS_WIN)
508         p = SDL_getenv("HOME");
509         if(p == NULL) {
510                 p = SDL_getenv("PWD");
511                 if(p == NULL) {
512                         cpp_homedir = ".";
513                 } else {
514                         cpp_homedir = p;
515                 }
516                 std::string tmpstr;
517                 tmpstr = "Warning : Can't get HOME directory...Making conf on " +  cpp_homedir + delim;
518                 perror(tmpstr.c_str());
519         } else {
520                 cpp_homedir = p;
521         }
522 #else
523         cpp_homedir = ".";
524 #endif  
525         cpp_homedir = cpp_homedir + delim;
526 #ifdef _WINDOWS
527         cpp_confdir = cpp_homedir + my_procname + delim;
528 #else
529         cpp_confdir = cpp_homedir + ".config" + delim + my_procname + delim;
530 #endif
531         bLogSYSLOG = (int)0;
532         bLogSTDOUT = (int)1;
533         AGAR_OpenLog(bLogSYSLOG, bLogSTDOUT, DEVICE_NAME); // Write to syslog, console
534         
535         archstr = "Generic";
536 #if defined(__x86_64__)
537         archstr = "amd64";
538 #endif
539 #if defined(__i386__)
540         archstr = "ia32";
541 #endif
542         AGAR_DebugLog(AGAR_LOG_INFO, "Start Common Source Project '%s'", my_procname.c_str());
543         AGAR_DebugLog(AGAR_LOG_INFO, "(C) Toshiya Takeda / Qt Version K.Ohta");
544         AGAR_DebugLog(AGAR_LOG_INFO, "Architecture: %s", archstr.c_str());
545         
546         //AGAR_DebugLog(AGAR_LOG_INFO, " -? is print help(s).");
547         AGAR_DebugLog(AGAR_LOG_DEBUG, "Moduledir = %s home = %s", cpp_confdir.c_str(), cpp_homedir.c_str()); // Debug
548 #if !defined(Q_OS_CYGWIN)       
549         {
550                 QDir dir;
551                 dir.mkdir( QString::fromStdString(cpp_confdir));
552         }
553 #endif   
554    //AG_MkPath(cpp_confdir.c_str());
555    /* Gettext */
556 #ifndef RSSDIR
557 #if defined(_USE_AGAR) || defined(_USE_QT)
558         sRssDir = "/usr/local/share/";
559 #else
560         sRssDir = "." + delim;
561 #endif
562         sRssDir = sRssDir + "CommonSourceCodeProject" + delim + my_procname;
563 #else
564         sRssDir = RSSDIR;
565 #endif
566 }
567
568 int MainLoop(int argc, char *argv[])
569 {
570         char c;
571         char strbuf[2048];
572         bool flag;
573         char homedir[PATH_MAX];
574         int thread_ret;
575         int w, h;
576         setup_logs();
577         cpp_homedir.copy(homedir, PATH_MAX - 1, 0);
578         flag = FALSE;
579         /*
580          * Into Qt's Loop.
581          */
582 #if defined(USE_SDL2)
583         SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
584 #else
585         SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);
586         //SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO);
587 #endif
588         AGAR_DebugLog(AGAR_LOG_DEBUG, "Audio and JOYSTICK subsystem was initialised.");
589         GuiMain = new QApplication(argc, argv);
590
591         load_config(create_local_path(_T("%s.ini"), _T(CONFIG_NAME)));
592         
593         USING_FLAGS *using_flags = new USING_FLAGS;
594         rMainWindow = new META_MainWindow(using_flags);
595         rMainWindow->connect(rMainWindow, SIGNAL(sig_quit_all(void)), rMainWindow, SLOT(deleteLater(void)));
596         rMainWindow->setCoreApplication(GuiMain);
597         
598         AGAR_DebugLog(AGAR_LOG_DEBUG, "InitInstance() OK.");
599   
600         // disable ime
601         
602         // initialize emulation core
603         rMainWindow->getWindow()->show();
604         emu = new EMU(rMainWindow, rMainWindow->getGraphicsView(), using_flags);
605         using_flags->set_emu(emu);
606         using_flags->set_osd(emu->get_osd());
607
608 #ifdef SUPPORT_DRAG_DROP
609         // open command line path
610         //      if(szCmdLine[0]) {
611         //      if(szCmdLine[0] == _T('"')) {
612         //              int len = strlen(szCmdLine);
613         //              szCmdLine[len - 1] = _T('\0');
614         //              szCmdLine++;
615         //      }
616         //      _TCHAR path[_MAX_PATH];
617         //      get_long_full_path_name(szCmdLine, path);
618         //      open_any_file(path);
619         //}
620 #endif
621         
622         // set priority
623         
624         // main loop
625         GLDrawClass *pgl = rMainWindow->getGraphicsView();
626         pgl->setEmuPtr(emu);
627         pgl->setFixedSize(pgl->width(), pgl->height());
628         
629 #if defined(USE_JOYSTICK)
630         rMainWindow->LaunchJoyThread();
631 #endif  
632         rMainWindow->LaunchEmuThread();
633         QObject::connect(GuiMain, SIGNAL(lastWindowClosed()),
634                                          rMainWindow, SLOT(on_actionExit_triggered()));
635         GuiMain->exec();
636         return 0;
637 }
638
639 void Ui_MainWindow::do_update_inner_fd(int drv, QStringList base, class Action_Control **action_select_media_list,
640                                        QStringList lst, int num, bool use_d88_menus)
641 {
642 #if defined(USE_FD1)
643         if(use_d88_menus) {
644                 for(int ii = 0; ii < using_flags->get_max_d88_banks(); ii++) {
645                         if(ii < emu->d88_file[drv].bank_num) {
646                                 base << lst.value(ii);
647                                 action_select_media_list[ii]->setText(lst.value(ii));
648                                 action_select_media_list[ii]->setVisible(true);
649                                 if(ii == num) action_select_media_list[ii]->setChecked(true);
650                         } else {
651                                 if(action_select_media_list[ii] != NULL) {
652                                         action_select_media_list[ii]->setText(QString::fromUtf8(""));
653                                         action_select_media_list[ii]->setVisible(false);
654                                 }
655                         }
656                 }
657         }
658 #endif  
659 }
660
661 void Ui_MainWindow::do_update_inner_bubble(int drv, QStringList base, class Action_Control **action_select_media_list,
662                                        QStringList lst, int num, bool use_d88_menus)
663 {
664 #if defined(USE_BUBBLE1)        
665         if(use_d88_menus) {
666                 for(int ii = 0; ii < using_flags->get_max_b77_banks(); ii++) {
667                         if(ii < emu->b77_file[drv].bank_num) {
668                                 base << lst.value(ii);
669                                 action_select_media_list[ii]->setText(lst.value(ii));
670                                 action_select_media_list[ii]->setVisible(true);
671                                 if(ii == num) action_select_media_list[ii]->setChecked(true);
672                         } else {
673                                 if(action_select_media_list[ii] != NULL) {
674                                         action_select_media_list[ii]->setText(QString::fromUtf8(""));
675                                         action_select_media_list[ii]->setVisible(false);
676                                 }
677                         }
678                 }
679         }
680 #endif  
681 }
682
683 #ifdef USE_DEBUGGER
684 #include <../debugger/qt_debugger.h>
685
686 void Ui_MainWindow::OnOpenDebugger(int no)
687 {
688         if((no < 0) || (no > 3)) return;
689         //emu->open_debugger(no);
690         VM *vm = emu->get_vm();
691
692         if(emu->now_debugging)  this->OnCloseDebugger();
693         if(!(emu->now_debugging && emu->debugger_thread_param.cpu_index == no)) {
694                 //emu->close_debugger();
695                 if(vm->get_cpu(no) != NULL && vm->get_cpu(no)->get_debugger() != NULL) {
696                         
697                         emu->hDebugger = new CSP_Debugger(this);
698                         QString objNameStr = QString("EmuDebugThread");
699                         emu->hDebugger->setObjectName(objNameStr);
700                         emu->hDebugger->debugger_thread_param.osd = emu->get_osd();
701                         emu->hDebugger->debugger_thread_param.vm = vm;
702                         emu->hDebugger->debugger_thread_param.cpu_index = no;
703                         emu->stop_record_sound();
704                         emu->stop_record_video();
705                         emu->now_debugging = true;
706                         connect(this, SIGNAL(quit_debugger_thread()), emu->hDebugger, SLOT(doExit()));
707                         //connect(this, SIGNAL(quit_debugger_thread()), emu->hDebugger, SLOT(close()));
708                         connect(emu->hDebugger, SIGNAL(sig_finished()), this, SLOT(OnCloseDebugger()));
709                         connect(emu->hDebugger, SIGNAL(sig_put_string(QString)), emu->hDebugger, SLOT(put_string(QString)));
710                         emu->hDebugger->show();
711                         emu->hDebugger->run();
712                 }
713         }
714 }
715
716 void Ui_MainWindow::OnCloseDebugger(void )
717 {
718
719 //      emu->close_debugger();
720         if(emu->now_debugging) {
721                 if(emu->hDebugger->debugger_thread_param.running) {
722                         emit quit_debugger_thread();
723                         //emu->hDebugger->wait();
724                 }
725                 delete emu->hDebugger;
726                 emu->hDebugger = NULL;
727                 emu->now_debugging = false;
728         }
729 }
730 #endif