OSDN Git Service

[General][VM][FM7][CMake] Build at least eFM77AV40EX.
[csp-qt/common_source_project-fm7.git] / source / src / qt / osd.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : K.Ohta <whatisthis.sowhat _at_ gmail.com>
5         Date   : 2015.11.30-
6
7         [ Qt dependent ]
8 */
9
10 #include "emu.h"
11 #include <string>
12 #include <QDateTime>
13 #include <QDate>
14 #include <QTime>
15 #include <QString>
16 #include <QObject>
17 #include "emu_thread.h"
18 #include "draw_thread.h"
19 #include "qt_gldraw.h"
20 #include "agar_logger.h"
21
22 OSD::OSD() : QThread(0)
23 {
24         VMSemaphore = new QSemaphore(1);
25         locked_vm = false;
26 }
27
28 OSD::~OSD()
29 {
30         delete VMSemaphore;
31 }
32
33 extern std::string cpp_homedir;
34 extern std::string my_procname;
35
36 EmuThreadClass *OSD::get_parent_handler()
37 {
38         return parent_thread;
39 }
40
41 void OSD::set_parent_thread(EmuThreadClass *parent)
42 {
43         parent_thread = parent;
44 #ifdef USE_AUTO_KEY     
45         connect(parent, SIGNAL(sig_auto_key_string(QByteArray)), this, SLOT(set_auto_key_string(QByteArray)));
46 #endif  
47 }
48
49 void OSD::set_draw_thread(DrawThreadClass *handler)
50 {
51         this->moveToThread(handler);
52         connect(this, SIGNAL(sig_update_screen(bitmap_t *)), handler, SLOT(do_update_screen(bitmap_t *)));
53         connect(this, SIGNAL(sig_save_screen(const char *)), glv, SLOT(do_save_frame_screen(const char *)));
54         connect(this, SIGNAL(sig_close_window()), parent_thread, SLOT(doExit()));
55         connect(this, SIGNAL(sig_resize_vm_screen(QImage *, int, int)), glv, SLOT(do_set_texture_size(QImage *, int, int)));
56         connect(this, SIGNAL(sig_console_input_string(QString)), parent_thread, SLOT(do_call_debugger_command(QString)));
57         connect(parent_thread, SIGNAL(sig_debugger_input(QString)), this, SLOT(do_set_input_string(QString)));
58         connect(parent_thread, SIGNAL(sig_quit_debugger()), this, SLOT(do_close_debugger_thread()));
59 }
60
61 void OSD::initialize(int rate, int samples)
62 {
63         // get module path
64         QString tmp_path;
65         tmp_path = QString::fromStdString(cpp_homedir);
66         tmp_path = tmp_path + QString::fromStdString(my_procname);
67 #if defined(Q_OS_WIN)
68         const char *delim = "\\";
69 #else
70         const char *delim = "/";
71 #endif
72         tmp_path = tmp_path + QString::fromUtf8(delim);
73         memset(app_path, 0x00, sizeof(app_path));
74         strncpy(app_path, tmp_path.toUtf8().constData(), _MAX_PATH);
75         
76         //memset(console_string, 0x00, sizeof(console_string));
77         console_cmd_str.clear();
78         osd_console_opened = false;
79         osd_timer.start();
80         //CoInitialize(NULL);
81         initialize_input();
82         initialize_printer();
83         initialize_screen();
84         initialize_sound(rate, samples);
85 #if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
86         initialize_video();
87 #endif
88 #ifdef USE_SOCKET
89         initialize_socket();
90 #endif
91 }
92
93 void OSD::release()
94 {
95         release_input();
96         release_printer();
97         release_screen();
98         release_sound();
99 #if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
100         release_video();
101 #endif
102 #ifdef USE_SOCKET
103         release_socket();
104 #endif
105         //CoUninitialize();
106 }
107
108 void OSD::power_off()
109 {
110         emit sig_close_window();
111 }
112
113 void OSD::suspend()
114 {
115 #ifdef USE_MOVIE_PLAYER
116         if(now_movie_play && !now_movie_pause) {
117                 pause_movie();
118                 now_movie_pause = false;
119         }
120 #endif
121         mute_sound();
122 }
123
124 void OSD::restore()
125 {
126 #ifdef USE_MOVIE_PLAYER
127         if(now_movie_play && !now_movie_pause) {
128                 play_movie();
129         }
130 #endif
131 }
132
133 _TCHAR* OSD::bios_path(const _TCHAR* file_name)
134 {
135         static _TCHAR file_path[_MAX_PATH];
136         snprintf(file_path, _MAX_PATH, _T("%s%s"), app_path, file_name);
137         AGAR_DebugLog(AGAR_LOG_INFO, "BIOS PATH:%s", file_path);
138         return file_path;
139 }
140
141 void OSD::get_host_time(cur_time_t* time)
142 {
143         //SYSTEMTIME sTime;
144         //GetLocalTime(&sTime);
145         QDateTime nowTime = QDateTime::currentDateTime();
146         QDate d = nowTime.date();
147         QTime t = nowTime.time();
148
149         time->year = d.year();
150         time->month = d.month();
151         time->day = d.day();
152         time->day_of_week = d.dayOfWeek();
153         time->hour = t.hour();
154         time->minute = t.minute();
155         time->second = t.second();
156 }
157
158 void OSD::sleep(uint32_t ms)
159 {
160         QThread::msleep(ms);
161 }
162
163 void OSD::create_date_file_name(_TCHAR *name, int length, const _TCHAR *extension)
164 {
165         QDateTime nowTime = QDateTime::currentDateTime();
166         QString tmps = QString::fromUtf8("emu");
167         tmps = tmps + QString::fromUtf8(CONFIG_NAME);
168         tmps = tmps + QString::fromUtf8("_");
169         tmps = tmps + nowTime.toString(QString::fromUtf8("yyyy-MM-dd_hh-mm-ss.zzz."));
170         tmps = tmps + QString::fromUtf8(extension);
171         snprintf(name, length, _T("%s"), tmps.toLocal8Bit().constData());
172 }
173
174 void OSD::lock_vm(void)
175 {
176         locked_vm = true;
177         if(parent_thread != NULL) { 
178                 if(!parent_thread->now_debugging()) VMSemaphore->acquire(1);
179         } else {
180                 VMSemaphore->acquire(1);
181         }
182 }
183
184 void OSD::unlock_vm(void)
185 {
186         locked_vm = false;
187         if(parent_thread != NULL) { 
188                 if(!parent_thread->now_debugging()) VMSemaphore->release(1);
189         } else {
190                 VMSemaphore->release(1);
191         }
192 }
193
194 bool OSD::is_vm_locked(void)
195 {
196         return locked_vm;
197 }
198
199 void OSD::force_unlock_vm(void)
200 {
201         if(parent_thread == NULL) {
202                 while(VMSemaphore->available() < 1) VMSemaphore->release(1);
203                 return;
204         }
205         if(parent_thread->now_debugging()) return;
206         while(VMSemaphore->available() < 1) VMSemaphore->release(1);
207 }