OSDN Git Service

[UI][Qt][OSD] Move JoyThread from qt/common/ to qt/gui/ . Update soname to libCSPgui...
[csp-qt/common_source_project-fm7.git] / source / src / qt / osd.h
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 #ifndef _QT_OSD_H_
11 #define _QT_OSD_H_
12
13
14 #include <QWidget>
15 #include <QThread>
16 #include <QMutex>
17 #include <QSemaphore>
18 #include <QPainter>
19 #include <QElapsedTimer>
20 #include <QString>
21
22 #include <SDL.h>
23 #include "simd_types.h"
24
25 #include <ctime>
26
27 //#include "../vm/vm.h"
28 //#include "../emu.h"
29 #include "../config.h"
30 #include "../fileio.h"
31 #include "../fifo.h"
32 #if !defined(Q_OS_WIN32)
33 #include "qt_input.h"
34 #endif
35 //#ifdef USE_SOCKET
36 #define SOCKET_MAX 4
37 #define SOCKET_BUFFER_MAX 0x100000
38 //#endif
39 #include "osd_types.h"
40
41
42 #define WM_RESIZE  (WM_USER + 1)
43 #define WM_SOCKET0 (WM_USER + 2)
44 #define WM_SOCKET1 (WM_USER + 3)
45 #define WM_SOCKET2 (WM_USER + 4)
46 #define WM_SOCKET3 (WM_USER + 5)
47
48 //#ifdef USE_VIDEO_CAPTURE
49 #define MAX_CAPTURE_DEVS 8
50 //#endif
51
52 #include "qt_main.h"
53 //#include "mainwidget.h"
54 #include "agar_logger.h"
55
56 class GLDrawClass;
57 class EmuThreadClass;
58 class DrawThreadClass;
59 class Ui_MainWindow;
60 class EMU;
61 class VM;
62 class FIFO;
63 class CSP_KeyTables;
64
65 QT_BEGIN_NAMESPACE
66 class OSD : public QThread
67 {
68         Q_OBJECT
69 protected:
70 //      VM* vm;
71 //      EMU* emu;
72         EmuThreadClass *parent_thread;
73         QSemaphore *VMSemaphore;
74         QSemaphore *DebugSemaphore;
75         sdl_snddata_t snddata;
76         private:
77         _TCHAR app_path[_MAX_PATH];
78         QElapsedTimer osd_timer;
79         bool locked_vm;
80         
81         // console
82         FILE *hStdIn, *hStdOut;
83         QString console_cmd_str;
84         bool osd_console_opened;
85         // input
86         void initialize_input();
87         void release_input();
88         void key_down_sub(int code, bool repeat);
89         void key_up_sub(int code);
90         CSP_KeyTables *key_table;
91         
92         scrntype_t *get_buffer(bitmap_t *p, int y);
93         bool dinput_key_ok;
94 //      bool dinput_joy_ok;
95         
96         uint8_t keycode_conv[256];
97         uint8_t key_status[256];        // windows key code mapping
98         uint8_t key_dik_prev[256];
99 //#ifdef USE_SHIFT_NUMPAD_KEY
100         uint8_t key_converted[256];
101         bool key_shift_pressed, key_shift_released;
102 //#endif
103         uint32_t modkey_status;
104         bool lost_focus;
105         uint32_t joy_status[4]; // joystick #1, #2 (b0 = up, b1 = down, b2 = left, b3 = right, b4- = buttons
106         int joy_num;
107         uint32_t joy_mask[4];
108         
109         int mouse_status[3];    // x, y, button (b0 = left, b1 = right)
110         bool mouse_enabled;
111         int mouse_ptrx;
112         int mouse_ptry;
113         int mouse_button;
114         int mouse_oldx;
115         int mouse_oldy;
116         Qt::CursorShape mouse_shape;
117         
118         // printer
119         
120         // screen
121         void initialize_screen();
122         void release_screen();
123         void initialize_screen_buffer(bitmap_t *buffer, int width, int height, int mode);
124         void release_screen_buffer(bitmap_t *buffer);
125 //#ifdef USE_SCREEN_ROTATE
126         void rotate_screen_buffer(bitmap_t *source, bitmap_t *dest);
127 //#endif
128         void stretch_screen_buffer(bitmap_t *source, bitmap_t *dest);
129         int add_video_frames();
130         
131         bitmap_t vm_screen_buffer;
132         bitmap_t video_screen_buffer;
133         bitmap_t* draw_screen_buffer;
134         int vm_window_width, vm_window_height;
135         int vm_window_width_aspect, vm_window_height_aspect;
136         
137         int host_window_width, host_window_height;
138         bool host_window_mode;
139         int base_window_width, base_window_height;
140         int vm_screen_width, vm_screen_height;
141         int draw_screen_width, draw_screen_height;
142         
143         
144         _TCHAR video_file_name[_MAX_PATH];
145         int rec_video_fps;
146         double rec_video_run_frames;
147         double rec_video_frames;
148         
149         //LPBITMAPINFO lpDibRec;
150         //PAVIFILE pAVIFile;
151         //PAVISTREAM pAVIStream;
152         //PAVISTREAM pAVICompressed;
153         //AVICOMPRESSOPTIONS AVIOpts;
154         uint64_t dwAVIFileSize;
155         uint64_t lAVIFrames;
156         //HANDLE hVideoThread;
157         rec_video_thread_param_t rec_video_thread_param;
158         
159         bool first_draw_screen;
160         bool first_invalidate;
161         bool self_invalidate;
162         
163         // sound
164         void initialize_sound(int rate, int samples);
165         void release_sound();
166         static void audio_callback(void *udata, Uint8 *stream, int len);
167         int sound_rate, sound_samples;
168         bool sound_ok, sound_started, now_mute;
169         bool sound_first_half;
170         
171         _TCHAR sound_file_name[_MAX_PATH];
172         FILEIO* rec_sound_fio;
173         int rec_sound_bytes;
174         int rec_sound_buffer_ptr;
175         
176         int sound_buffer_size;
177         int sound_data_len;
178         int sound_data_pos;
179         int sound_write_pos;
180         bool sound_exit;
181         bool sound_debug;
182         SDL_sem *snd_apply_sem;
183         Sint16 *sound_buf_ptr;
184         Uint8 snd_total_volume;
185 #if defined(USE_SDL2)   
186         SDL_AudioDeviceID audio_dev_id;
187 #else
188         int audio_dev_id;
189 #endif
190         SDL_AudioSpec snd_spec_req, snd_spec_presented;
191         
192 //#if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
193         // video device
194         void initialize_video();
195         void release_video();
196         
197         //IGraphBuilder *pGraphBuilder;
198         //IBaseFilter *pVideoBaseFilter;
199         //IBaseFilter *pCaptureBaseFilter;
200         //ICaptureGraphBuilder2 *pCaptureGraphBuilder2;
201         //ISampleGrabber *pVideoSampleGrabber;
202         //IBaseFilter *pSoundBaseFilter;
203         //ISampleGrabber *pSoundSampleGrabber;
204         //CMySampleGrabberCB *pSoundCallBack;
205         //IMediaControl *pMediaControl;
206         //IMediaSeeking *pMediaSeeking;
207         //IMediaPosition *pMediaPosition;
208         //IVideoWindow *pVideoWindow;
209         //IBasicVideo *pBasicVideo;
210         //IBasicAudio *pBasicAudio;
211         //bool bTimeFormatFrame;
212         //bool bVerticalReversed;
213         
214         bitmap_t dshow_screen_buffer;
215         int direct_show_width, direct_show_height;
216         bool direct_show_mute[2];
217 //#endif
218 //#ifdef USE_MOVIE_PLAYER
219         double movie_frame_rate;
220         int movie_sound_rate;
221 //#endif
222 //#ifdef USE_VIDEO_CAPTURE
223         void enum_capture_devs();
224         bool connect_capture_dev(int index, bool pin);
225         int cur_capture_dev_index;
226         int num_capture_devs;
227         _TCHAR capture_dev_name[MAX_CAPTURE_DEVS][256];
228 //#endif
229         _TCHAR prn_file_name[_MAX_PATH];
230         FILEIO *prn_fio;
231         int prn_data, prn_wait_frames;
232         bool prn_strobe;
233
234         // socket
235 //#ifdef USE_SOCKET
236         void initialize_socket();
237         void release_socket();
238         
239         int soc[SOCKET_MAX];
240         bool is_tcp[SOCKET_MAX];
241         //struct sockaddr_in udpaddr[SOCKET_MAX];
242         int socket_delay[SOCKET_MAX];
243         char recv_buffer[SOCKET_MAX][SOCKET_BUFFER_MAX];
244         int recv_r_ptr[SOCKET_MAX], recv_w_ptr[SOCKET_MAX];
245 //#endif
246         
247 public:
248         OSD();
249         ~OSD();
250         
251         // common
252         VM* vm;
253         //EMU* emu;
254         class Ui_MainWindow *main_window_handle;
255         GLDrawClass *glv;
256         int host_cpus;
257 //#ifdef USE_AUTO_KEY
258         bool now_auto_key;
259 //#endif        
260         
261         void initialize(int rate, int samples);
262         void release();
263         void power_off();
264         void suspend();
265         void restore();
266         _TCHAR* application_path()
267         {
268                 return app_path;
269         }
270         _TCHAR* bios_path(const _TCHAR* file_name);
271         void get_host_time(cur_time_t* time);
272         void sleep(uint32_t ms);
273         void create_date_file_name(_TCHAR *name, int length, const _TCHAR *extension);
274         
275         // common console
276         void open_console(_TCHAR* title);
277         void close_console();
278         unsigned int get_console_code_page();
279         bool is_console_active();
280         void set_console_text_attribute(unsigned short attr);
281         void write_console(_TCHAR* buffer, unsigned int length);
282         int read_console_input(_TCHAR* buffer);
283         bool is_console_key_pressed(uint32_t ch);
284         
285         // common input
286         void update_input();
287         void key_down(int code, bool repeat);
288         void key_up(int code);
289         void key_down_native(int code, bool repeat);
290         void key_up_native(int code);
291         void key_lost_focus()
292         {
293                 lost_focus = true;
294         }
295 //#ifdef ONE_BOARD_MICRO_COMPUTER
296         void press_button(int num);
297 //#endif
298 # if !defined(Q_OS_WIN) && !defined(Q_OS_CYGWIN)
299         uint16_t GetAsyncKeyState(uint32_t vk);  // Win32 GetAsyncKeyState() wrappeer.
300 # endif
301         void key_modifiers(uint32_t mod) {
302                 modkey_status = mod;
303         }
304         void enable_mouse();
305         void disable_mouse();
306         void toggle_mouse();
307         bool is_mouse_enabled()
308         {
309                 return mouse_enabled;
310         }
311         //QImage *getPseudoVramClass(void) { return pPseudoVram;}
312         void set_mouse_pointer(int x, int y) {
313                 mouse_ptrx = x;
314                 mouse_ptry = y;
315         }
316         void set_mouse_button(int button) {
317                 mouse_button = button;
318         }
319         int get_mouse_button() {
320                 return mouse_button;
321         }
322         void modify_key_buffer(int code, uint8_t val)
323         {
324                 key_status[code] = val;
325         }
326         uint8_t* get_key_buffer()
327         {
328                 return key_status;
329         }
330         uint32_t* get_joy_buffer()
331         {
332                 return joy_status;
333         }
334         int* get_mouse_buffer()
335         {
336                 return mouse_status;
337         }
338         
339         // common printer
340         void reset_printer() {
341                 close_printer_file();
342                 prn_data = -1;
343                 prn_strobe = false;
344         }
345         void update_printer() {
346                 if(prn_fio->IsOpened() && --prn_wait_frames == 0) {
347                         close_printer_file();
348                 }
349         }
350         void printer_out(uint8_t value) {
351                 prn_data = value;
352         }
353         void printer_strobe(bool value);
354         // printer
355         void initialize_printer();
356         void release_printer();
357         void open_printer_file() {
358                 create_date_file_name(prn_file_name, _MAX_PATH, _T("txt"));
359                 prn_fio->Fopen(bios_path(prn_file_name), FILEIO_WRITE_BINARY);
360         }
361
362         void close_printer_file() {
363                 if(prn_fio->IsOpened()) {
364                         // remove if the file size is less than 2 bytes
365                         bool remove = (prn_fio->Ftell() < 2);
366                         prn_fio->Fclose();
367                         if(remove) {
368                                 FILEIO::RemoveFile(bios_path(prn_file_name));
369                         }
370                 }
371         }
372         
373         // common screen
374         int get_window_mode_width(int mode);
375         int get_window_mode_height(int mode);
376         void set_host_window_size(int window_width, int window_height, bool window_mode);
377         void set_vm_screen_size(int width, int height, int width_aspect, int height_aspect, int window_width, int window_height);
378         int get_vm_window_width()
379         {
380                 return vm_window_width;
381         }
382         int get_vm_window_height()
383         {
384                 return vm_window_height;
385         }
386         int get_vm_window_width_aspect()
387         {
388                 return vm_window_width_aspect;
389         }
390         int get_vm_window_height_aspect()
391         {
392                 return vm_window_height_aspect;
393         }
394         scrntype_t* get_vm_screen_buffer(int y);
395         int draw_screen();
396 //#ifdef ONE_BOARD_MICRO_COMPUTER
397         void reload_bitmap()
398         {
399                 first_invalidate = true;
400         }
401 //#endif
402         void capture_screen();
403         bool start_record_video(int fps);
404         void stop_record_video();
405         void restart_record_video();
406         void add_extra_frames(int extra_frames);
407         bool now_record_video;
408 //#ifdef USE_CRT_FILTER
409         bool screen_skip_line;
410 //#endif
411
412         // common sound
413         void update_sound(int* extra_frames);
414         void mute_sound();
415         void stop_sound();
416         void start_record_sound();
417         void stop_record_sound();
418         void restart_record_sound();
419         bool now_record_sound;
420         
421 //#if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
422         // common video device
423         void get_video_buffer();
424         void mute_video_dev(bool l, bool r);
425 //#endif
426 //#ifdef USE_MOVIE_PLAYER
427         bool open_movie_file(const _TCHAR* file_path);
428         void close_movie_file();
429         void play_movie();
430         void stop_movie();
431         void pause_movie();
432         double get_movie_frame_rate()
433         {
434                 return movie_frame_rate;
435         }
436         int get_movie_sound_rate()
437         {
438                 return movie_sound_rate;
439         }
440         void set_cur_movie_frame(int frame, bool relative);
441         uint32_t get_cur_movie_frame();
442         bool now_movie_play, now_movie_pause;
443 //#endif
444 //#ifdef USE_VIDEO_CAPTURE
445         int get_cur_capture_dev_index()
446         {
447                 return cur_capture_dev_index;
448         }
449         int get_num_capture_devs()
450         {
451                 return num_capture_devs;
452         }
453         _TCHAR* get_capture_dev_name(int index)
454         {
455                 return capture_dev_name[index];
456         }
457         void open_capture_dev(int index, bool pin);
458         void close_capture_dev();
459         void show_capture_dev_filter();
460         void show_capture_dev_pin();
461         void show_capture_dev_source();
462         void set_capture_dev_channel(int ch);
463 //#endif
464         
465         // common printer
466 //#ifdef USE_PRINTER
467         void create_bitmap(bitmap_t *bitmap, int width, int height);
468         void release_bitmap(bitmap_t *bitmap);
469         void create_font(font_t *font, const _TCHAR *family, int width, int height, int rotate, bool bold, bool italic);
470         void release_font(font_t *font);
471         void create_pen(pen_t *pen, int width, uint8_t r, uint8_t g, uint8_t b);
472         void release_pen(pen_t *pen);
473
474         void clear_bitmap(bitmap_t *bitmap, uint8_t r, uint8_t g, uint8_t b);
475         int get_text_width(bitmap_t *bitmap, font_t *font, const char *text);
476         
477         void draw_text_to_bitmap(bitmap_t *bitmap, font_t *font, int x, int y, const _TCHAR *text, uint8_t r, uint8_t g, uint8_t b);
478         void draw_line_to_bitmap(bitmap_t *bitmap, pen_t *pen, int sx, int sy, int ex, int ey);
479         void draw_rectangle_to_bitmap(bitmap_t *bitmap, int x, int y, int width, int height, uint8_t r, uint8_t g, uint8_t b);
480         void draw_point_to_bitmap(bitmap_t *bitmap, int x, int y, uint8_t r, uint8_t g, uint8_t b);
481
482         void stretch_bitmap(bitmap_t *dest, int dest_x, int dest_y, int dest_width, int dest_height, bitmap_t *source, int source_x, int source_y, int source_width, int source_height);
483 //#endif
484         void write_bitmap_to_file(bitmap_t *bitmap, const _TCHAR *file_path);
485
486         // common socket
487 //#ifdef USE_SOCKET
488         int get_socket(int ch)
489         {
490                 return soc[ch];
491         }
492         void notify_socket_connected(int ch);
493         void notify_socket_disconnected(int ch);
494         void update_socket();
495         bool initialize_socket_tcp(int ch);
496         bool initialize_socket_udp(int ch);
497         bool connect_socket(int ch, uint32_t ipaddr, int port);
498         void disconnect_socket(int ch);
499         bool listen_socket(int ch);
500         void send_socket_data_tcp(int ch);
501         void send_socket_data_udp(int ch, uint32_t ipaddr, int port);
502         void send_socket_data(int ch);
503         void recv_socket_data(int ch);
504 //#endif
505
506         // win32 dependent
507         void update_screen();
508         void set_parent_thread(EmuThreadClass *parent);
509         EmuThreadClass *get_parent_handler();
510         void set_draw_thread(DrawThreadClass *handler);
511         _TCHAR *console_input_string(void);
512         void clear_console_input_string(void);
513         void lock_vm(void);
514         void unlock_vm(void);
515         void force_unlock_vm(void);
516         bool is_vm_locked(void);
517
518 public slots:
519         void do_write_inputdata(QString s);
520         void do_set_input_string(QString s);
521         void close_debugger_console();
522         void do_close_debugger_thread();
523         void do_assign_js_setting(int jsnum, int axis_idx, int assigned_value);
524         
525 signals:
526         int sig_update_screen(bitmap_t *);
527         int sig_save_screen(const char *);
528         int sig_close_window(void);
529         int sig_resize_vm_screen(QImage *, int, int);
530         int sig_put_string_debugger(QString);
531         int sig_console_input_string(QString);
532         int sig_debugger_finished();
533 };
534 QT_END_NAMESPACE
535
536 #endif