OSDN Git Service

1ac3aa6218424321ab54b952cd7ed03765632945
[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 <SDL.h>
19 #include <ctime>
20
21 #include "../vm/vm.h"
22 //#include "../emu.h"
23 #include "../config.h"
24 #include "../fileio.h"
25 #include "../fifo.h"
26 #if !defined(Q_OS_WIN32)
27 #include "qt_input.h"
28 #endif
29
30 typedef struct {
31    Sint16 **pSoundBuf;
32    int *uBufSize;
33    int *nSndWritePos;
34    int *nSndDataLen;
35    SDL_sem **pSndApplySem;
36    Uint8 *iTotalVolume;
37    bool *bSndExit;
38    bool *bSoundDebug;
39 } sdl_snddata_t;
40
41
42 #if 0 // TODO
43 #if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
44 ISampleGrabberCB : public IUnknown {
45 public:
46         virtual HRESULT STDMETHODCALLTYPE SampleCB( double SampleTime,IMediaSample *pSample) = 0;
47         virtual HRESULT STDMETHODCALLTYPE BufferCB( double SampleTime,BYTE *pBuffer,long BufferLen) = 0;
48 };
49 EXTERN_C const IID IID_ISampleGrabber;
50 MIDL_INTERFACE("6B652FFF-11FE-4fce-92AD-0266B5D7C78F")
51 ISampleGrabber : public IUnknown {
52 public:
53         virtual HRESULT STDMETHODCALLTYPE SetOneShot( BOOL OneShot) = 0;
54         virtual HRESULT STDMETHODCALLTYPE SetMediaType( const AM_MEDIA_TYPE *pType) = 0;
55         virtual HRESULT STDMETHODCALLTYPE GetConnectedMediaType( AM_MEDIA_TYPE *pType) = 0;
56         virtual HRESULT STDMETHODCALLTYPE SetBufferSamples( BOOL BufferThem) = 0;
57         virtual HRESULT STDMETHODCALLTYPE GetCurrentBuffer( /* [out][in] */ long *pBufferSize,/* [out] */ long *pBuffer) = 0;
58         virtual HRESULT STDMETHODCALLTYPE GetCurrentSample( /* [retval][out] */ IMediaSample **ppSample) = 0;
59         virtual HRESULT STDMETHODCALLTYPE SetCallback( ISampleGrabberCB *pCallback,long WhichMethodToCallback) = 0;
60 };
61 #endif
62 #ifdef USE_MOVIE_PLAYER
63 class CMySampleGrabberCB : public ISampleGrabberCB {
64 private:
65         VM *vm;
66 public:
67         CMySampleGrabberCB(VM *vm_ptr)
68         {
69                 vm = vm_ptr;
70         }
71         STDMETHODIMP_(ULONG) AddRef()
72         {
73                 return 2;
74         }
75         STDMETHODIMP_(ULONG) Release()
76         {
77                 return 1;
78         }
79         STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
80         {
81                 if(riid == IID_ISampleGrabberCB || riid == IID_IUnknown) {
82                         *ppv = (void *) static_cast<ISampleGrabberCB*>(this);
83                         return NOERROR;
84                 }
85                 return E_NOINTERFACE;
86         }
87         STDMETHODIMP SampleCB(double SampleTime, IMediaSample *pSample)
88         {
89                 return S_OK;
90         }
91         STDMETHODIMP BufferCB(double dblSampleTime, BYTE *pBuffer, long lBufferSize)
92         {
93                 vm->movie_sound_callback(pBuffer, lBufferSize);
94                 return S_OK;
95         }
96 };
97 #endif
98 #endif
99
100 #define WM_RESIZE  (WM_USER + 1)
101 #define WM_SOCKET0 (WM_USER + 2)
102 #define WM_SOCKET1 (WM_USER + 3)
103 #define WM_SOCKET2 (WM_USER + 4)
104 #define WM_SOCKET3 (WM_USER + 5)
105
106 #ifdef USE_SOCKET
107 #define SOCKET_MAX 4
108 #define SOCKET_BUFFER_MAX 0x100000
109 #endif
110
111 #ifdef USE_VIDEO_CAPTURE
112 #define MAX_CAPTURE_DEVS 8
113 #endif
114
115 // check memory leaks
116 #ifdef _DEBUG
117 //#define _CRTDBG_MAP_ALLOC
118 //#include <crtdbg.h>
119 //#define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
120 //#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
121 #endif
122
123 typedef struct screen_buffer_s {
124         int width, height;
125         //HBITMAP hBmp, hOldBmp;
126         //LPBYTE lpBuf;
127         scrntype* lpBuf;
128         QImage pImage;
129 } screen_buffer_t;
130
131 typedef struct {
132         //PAVISTREAM pAVICompressed;
133         scrntype* lpBmp;
134         //LPBITMAPINFOHEADER pbmInfoHeader;
135         DWORD dwAVIFileSize;
136         UINT64 lAVIFrames;
137         int frames;
138         int result;
139 } rec_video_thread_param_t;
140
141 #include "qt_main.h"
142 #include "mainwidget.h"
143 #include "qt_gldraw.h"
144 #include "agar_logger.h"
145
146 class GLDrawClass;
147 class EmuThreadClass;
148 class DrawThreadClass;
149 class Ui_MainWindow;
150 class EMU;
151 class VM;
152 class FIFO;
153
154 QT_BEGIN_NAMESPACE
155 class OSD : public QThread
156 {
157         Q_OBJECT
158 protected:
159 //      VM* vm;
160 //      EMU* emu;
161         EmuThreadClass *parent_thread;
162         QSemaphore *VMSemaphore;
163         _TCHAR auto_key_str[2048];
164         sdl_snddata_t snddata;
165         private:
166         _TCHAR app_path[_MAX_PATH];
167         
168         // console
169         FILE *hStdIn, *hStdOut;
170         FIFO *osd_console_input;
171         bool osd_console_opened;
172         // input
173         void initialize_input();
174         void release_input();
175         void key_down_sub(int code, bool repeat);
176         void key_up_sub(int code);
177         scrntype *get_buffer(screen_buffer_t *p, int y);
178         bool dinput_key_ok;
179 //      bool dinput_joy_ok;
180         
181         uint8 keycode_conv[256];
182         uint8 key_status[256];  // windows key code mapping
183         uint8 key_dik_prev[256];
184 #ifdef USE_SHIFT_NUMPAD_KEY
185         uint8 key_converted[256];
186         bool key_shift_pressed, key_shift_released;
187 #endif
188         uint32_t modkey_status;
189         bool lost_focus;
190         
191         uint32 joy_status[2];   // joystick #1, #2 (b0 = up, b1 = down, b2 = left, b3 = right, b4- = buttons
192         int joy_num;
193         uint32 joy_mask[2];
194         
195         int mouse_status[3];    // x, y, button (b0 = left, b1 = right)
196         bool mouse_enabled;
197         int mouse_ptrx;
198         int mouse_ptry;
199         int mouse_button;
200         int mouse_oldx;
201         int mouse_oldy;
202         Qt::CursorShape mouse_shape;
203         
204 #ifdef USE_AUTO_KEY
205         FIFO* autokey_buffer;
206         int autokey_phase, autokey_shift;
207         int autokey_table[256];
208 #endif
209         
210         // printer
211         
212         // screen
213         void initialize_screen();
214         void release_screen();
215         void initialize_screen_buffer(screen_buffer_t *buffer, int width, int height, int mode);
216         void release_screen_buffer(screen_buffer_t *buffer);
217 #ifdef USE_CRT_FILTER
218         void apply_crt_fileter_to_screen_buffer(screen_buffer_t *source, screen_buffer_t *dest);
219         void apply_crt_filter_x3_y3(screen_buffer_t *source, screen_buffer_t *dest);
220         void apply_crt_filter_x3_y2(screen_buffer_t *source, screen_buffer_t *dest);
221         void apply_crt_filter_x2_y3(screen_buffer_t *source, screen_buffer_t *dest);
222         void apply_crt_filter_x2_y2(screen_buffer_t *source, screen_buffer_t *dest);
223         void apply_crt_filter_x1_y1(screen_buffer_t *source, screen_buffer_t *dest);
224 #endif
225 #ifdef USE_SCREEN_ROTATE
226         void rotate_screen_buffer(screen_buffer_t *source, screen_buffer_t *dest);
227 #endif
228         void stretch_screen_buffer(screen_buffer_t *source, screen_buffer_t *dest);
229         int add_video_frames();
230         
231         screen_buffer_t vm_screen_buffer;
232         screen_buffer_t video_screen_buffer;
233 #ifdef USE_CRT_FILTER
234         screen_buffer_t filtered_screen_buffer;
235 #endif  
236         screen_buffer_t* draw_screen_buffer;
237         
238         int host_window_width, host_window_height;
239         bool host_window_mode;
240         int base_window_width, base_window_height;
241         int vm_screen_width, vm_screen_height, vm_screen_width_aspect, vm_screen_height_aspect;
242         int draw_screen_width, draw_screen_height;
243         
244         
245         _TCHAR video_file_name[_MAX_PATH];
246         int rec_video_fps;
247         double rec_video_run_frames;
248         double rec_video_frames;
249         
250         //LPBITMAPINFO lpDibRec;
251         //PAVIFILE pAVIFile;
252         //PAVISTREAM pAVIStream;
253         //PAVISTREAM pAVICompressed;
254         //AVICOMPRESSOPTIONS AVIOpts;
255         DWORD dwAVIFileSize;
256         UINT64 lAVIFrames;
257         //HANDLE hVideoThread;
258         rec_video_thread_param_t rec_video_thread_param;
259         
260         bool first_draw_screen;
261         bool first_invalidate;
262         bool self_invalidate;
263         
264         // sound
265         void initialize_sound(int rate, int samples);
266         void release_sound();
267         
268         int sound_rate, sound_samples;
269         bool sound_ok, sound_started, now_mute;
270         bool sound_first_half;
271         
272         _TCHAR sound_file_name[_MAX_PATH];
273         FILEIO* rec_sound_fio;
274         int rec_sound_bytes;
275         int rec_sound_buffer_ptr;
276         
277         
278 #if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
279         // video device
280         void initialize_video();
281         void release_video();
282         
283         //IGraphBuilder *pGraphBuilder;
284         //IBaseFilter *pVideoBaseFilter;
285         //IBaseFilter *pCaptureBaseFilter;
286         //ICaptureGraphBuilder2 *pCaptureGraphBuilder2;
287         //ISampleGrabber *pVideoSampleGrabber;
288         //IBaseFilter *pSoundBaseFilter;
289         //ISampleGrabber *pSoundSampleGrabber;
290         //CMySampleGrabberCB *pSoundCallBack;
291         //IMediaControl *pMediaControl;
292         //IMediaSeeking *pMediaSeeking;
293         //IMediaPosition *pMediaPosition;
294         //IVideoWindow *pVideoWindow;
295         //IBasicVideo *pBasicVideo;
296         //IBasicAudio *pBasicAudio;
297         //bool bTimeFormatFrame;
298         //bool bVerticalReversed;
299         
300         screen_buffer_t dshow_screen_buffer;
301         int direct_show_width, direct_show_height;
302         bool direct_show_mute[2];
303 #endif
304 #ifdef USE_MOVIE_PLAYER
305         double movie_frame_rate;
306         int movie_sound_rate;
307 #endif
308 #ifdef USE_VIDEO_CAPTURE
309         void enum_capture_devs();
310         bool connect_capture_dev(int index, bool pin);
311         int cur_capture_dev_index;
312         int num_capture_devs;
313         _TCHAR capture_dev_name[MAX_CAPTURE_DEVS][256];
314 #endif
315         _TCHAR prn_file_name[_MAX_PATH];
316         FILEIO *prn_fio;
317         int prn_data, prn_wait_frames;
318         bool prn_strobe;
319
320         // socket
321 #ifdef USE_SOCKET
322         void initialize_socket();
323         void release_socket();
324         
325         int soc[SOCKET_MAX];
326         bool is_tcp[SOCKET_MAX];
327         //struct sockaddr_in udpaddr[SOCKET_MAX];
328         int socket_delay[SOCKET_MAX];
329         char recv_buffer[SOCKET_MAX][SOCKET_BUFFER_MAX];
330         int recv_r_ptr[SOCKET_MAX], recv_w_ptr[SOCKET_MAX];
331 #endif
332         
333 public:
334         OSD();
335         ~OSD();
336         
337         // common
338         VM* vm;
339         //EMU* emu;
340         class Ui_MainWindow *main_window_handle;
341         GLDrawClass *glv;
342         int host_cpus;
343         
344         void initialize(int rate, int samples);
345         void release();
346         void power_off();
347         void suspend();
348         void restore();
349         _TCHAR* application_path()
350         {
351                 return app_path;
352         }
353         _TCHAR* bios_path(const _TCHAR* file_name);
354         void get_host_time(cur_time_t* time);
355         void sleep(uint32 ms);
356         void create_date_file_name(_TCHAR *name, int length, const _TCHAR *extension);
357         
358         // common console
359         void open_console(_TCHAR* title);
360         void close_console();
361         unsigned int get_console_code_page();
362         bool is_console_active();
363         void set_console_text_attribute(unsigned short attr);
364         void write_console(_TCHAR* buffer, unsigned int length);
365         int read_console_input(_TCHAR* buffer);
366         
367         // common input
368         void update_input();
369         void key_down(int code, bool repeat);
370         void key_up(int code);
371         void key_lost_focus()
372         {
373                 lost_focus = true;
374         }
375 #ifdef ONE_BOARD_MICRO_COMPUTER
376         void press_button(int num);
377 #endif
378 # if !defined(Q_OS_WIN) && !defined(Q_OS_CYGWIN)
379         uint16_t GetAsyncKeyState(uint32_t vk);  // Win32 GetAsyncKeyState() wrappeer.
380 # endif
381         void key_modifiers(uint32 mod) {
382                 modkey_status = mod;
383         }
384         void enable_mouse();
385         void disenable_mouse();
386         void toggle_mouse();
387         bool get_mouse_enabled()
388         {
389                 return mouse_enabled;
390         }
391         //QImage *getPseudoVramClass(void) { return pPseudoVram;}
392         void set_mouse_pointer(int x, int y) {
393                 mouse_ptrx = x;
394                 mouse_ptry = y;
395         }
396         void set_mouse_button(int button) {
397                 mouse_button = button;
398         }
399         int get_mouse_button() {
400                 return mouse_button;
401         }
402 #ifdef USE_AUTO_KEY
403         void start_auto_key();
404         void stop_auto_key();
405         bool now_auto_key()
406         {
407                 return (autokey_phase != 0);
408         }
409 #endif
410         uint8* key_buffer()
411         {
412                 return key_status;
413         }
414         uint32* joy_buffer()
415         {
416                 return joy_status;
417         }
418         int* mouse_buffer()
419         {
420                 return mouse_status;
421         }
422         
423         // common printer
424         void reset_printer() {
425                 close_printer_file();
426                 prn_data = -1;
427                 prn_strobe = false;
428         }
429         void update_printer() {
430                 if(prn_fio->IsOpened() && --prn_wait_frames == 0) {
431                         close_printer_file();
432                 }
433         }
434         void printer_out(uint8 value) {
435                 prn_data = value;
436         }
437         void printer_strobe(bool value) {
438                 bool falling = (prn_strobe && !value);
439                 prn_strobe = value;
440         
441                 if(falling) {
442                         if(!prn_fio->IsOpened()) {
443                                 if(prn_data == -1) {
444                                         return;
445                                 }
446                                 open_printer_file();
447                         }
448                         prn_fio->Fputc(prn_data);
449                         // wait 10sec
450 #ifdef SUPPORT_VARIABLE_TIMING
451                         prn_wait_frames = (int)(vm->frame_rate() * 10.0 + 0.5);
452 #else
453                         prn_wait_frames = (int)(FRAMES_PER_SEC * 10.0 + 0.5);
454 #endif
455                 }
456         }
457         // printer
458         void initialize_printer();
459         void release_printer();
460         void open_printer_file() {
461                 create_date_file_name(prn_file_name, _MAX_PATH, _T("txt"));
462                 prn_fio->Fopen(bios_path(prn_file_name), FILEIO_WRITE_BINARY);
463         }
464
465         void close_printer_file() {
466                 if(prn_fio->IsOpened()) {
467                         // remove if the file size is less than 2 bytes
468                         bool remove = (prn_fio->Ftell() < 2);
469                         prn_fio->Fclose();
470                         if(remove) {
471                                 FILEIO::RemoveFile(bios_path(prn_file_name));
472                         }
473                 }
474         }
475         
476         // common screen
477         int get_window_width(int mode);
478         int get_window_height(int mode);
479         void set_window_size(int window_width, int window_height, bool window_mode);
480         void set_vm_screen_size(int width, int height, int width_aspect, int height_aspect, int window_width, int window_height);
481         scrntype* get_vm_screen_buffer(int y);
482         int draw_screen();
483 #ifdef ONE_BOARD_MICRO_COMPUTER
484         void reload_bitmap()
485         {
486                 first_invalidate = true;
487         }
488 #endif
489         void capture_screen();
490         bool start_rec_video(int fps);
491         void stop_rec_video();
492         void restart_rec_video();
493         void add_extra_frames(int extra_frames);
494         bool now_rec_video;
495 #ifdef USE_CRT_FILTER
496         bool screen_skip_line;
497 #endif
498
499         // common sound
500         void update_sound(int* extra_frames);
501         void mute_sound();
502         void stop_sound();
503         void start_rec_sound();
504         void stop_rec_sound();
505         void restart_rec_sound();
506         bool now_rec_sound;
507         
508 #if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
509         // common video device
510         void get_video_buffer();
511         void mute_video_dev(bool l, bool r);
512 #endif
513 #ifdef USE_MOVIE_PLAYER
514         bool open_movie_file(const _TCHAR* file_path);
515         void close_movie_file();
516         void play_movie();
517         void stop_movie();
518         void pause_movie();
519         double get_movie_frame_rate()
520         {
521                 return movie_frame_rate;
522         }
523         int get_movie_sound_rate()
524         {
525                 return movie_sound_rate;
526         }
527         void set_cur_movie_frame(int frame, bool relative);
528         uint32 get_cur_movie_frame();
529         bool now_movie_play, now_movie_pause;
530 #endif
531 #ifdef USE_VIDEO_CAPTURE
532         int get_cur_capture_dev_index()
533         {
534                 return cur_capture_dev_index;
535         }
536         int get_num_capture_devs()
537         {
538                 return num_capture_devs;
539         }
540         _TCHAR* get_capture_dev_name(int index)
541         {
542                 return capture_dev_name[index];
543         }
544         void open_capture_dev(int index, bool pin);
545         void close_capture_dev();
546         void show_capture_dev_filter();
547         void show_capture_dev_pin();
548         void show_capture_dev_source();
549         void set_capture_dev_channel(int ch);
550 #endif
551         
552 #ifdef USE_SOCKET
553         // common socket
554         int get_socket(int ch)
555         {
556                 return soc[ch];
557         }
558         void socket_connected(int ch);
559         void socket_disconnected(int ch);
560         void update_socket();
561         bool init_socket_tcp(int ch);
562         bool init_socket_udp(int ch);
563         bool connect_socket(int ch, uint32 ipaddr, int port);
564         void disconnect_socket(int ch);
565         bool listen_socket(int ch);
566         void send_data_tcp(int ch);
567         void send_data_udp(int ch, uint32 ipaddr, int port);
568         void send_data(int ch);
569         void recv_data(int ch);
570 #endif
571
572         // win32 dependent
573         void update_screen();
574         void set_parent_thread(EmuThreadClass *parent);
575         EmuThreadClass *get_parent_handler();
576         void set_draw_thread(DrawThreadClass *handler);
577         void lock_vm(void){
578                 VMSemaphore->acquire(1);
579         }
580         void unlock_vm(void){
581                 VMSemaphore->release(1);
582         }
583         void force_unlock_vm(void){
584                 while(VMSemaphore->available() < 1) VMSemaphore->release(1);
585         }
586 public slots:
587 #ifdef USE_AUTO_KEY
588         void set_auto_key_string(QByteArray);
589 #endif
590         void do_write_inputdata(QString s);
591         void do_close_debugger_console();
592         void do_close_debugger_thread();
593         
594 signals:
595         int sig_update_screen(screen_buffer_t *);
596         int sig_save_screen(const char *);
597         int sig_close_window(void);
598         int sig_resize_vm_screen(QImage *, int, int);
599         int sig_put_string_debugger(QString);
600         int sig_debugger_finished();
601 };
602 QT_END_NAMESPACE
603
604 #endif