OSDN Git Service

[UI][VM][EMU][Qt][WIP] Apply upstream's OSD class.I don't test to build yet X-)
[csp-qt/common_source_project-fm7.git] / source / src / win32 / osd.h
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2015.11.20-
6
7         [ win32 dependent ]
8 */
9
10 #ifndef _WIN32_OSD_H_
11 #define _WIN32_OSD_H_
12
13 #define DIRECTSOUND_VERSION     0x900
14 #define DIRECT3D_VERSION        0x900
15 #define DIRECTINPUT_VERSION     0x500
16
17 #include <windows.h>
18 #include <windowsx.h>
19 #include <mmsystem.h>
20 #include <process.h>
21 #include <winsock.h>
22
23 #include <d3d9.h>
24 #include <d3dx9.h>
25 #include <d3d9types.h>
26 #include <vfw.h>
27 #include <dsound.h>
28 #include <dinput.h>
29 #include "../vm/vm.h"
30 //#include "../emu.h"
31 #include "../config.h"
32
33 #pragma comment(lib, "d3d9.lib")
34 #pragma comment(lib, "d3dx9.lib")
35 #pragma comment(lib, "dsound.lib")
36 #pragma comment(lib, "dinput.lib")
37 #pragma comment(lib, "dxguid.lib")
38
39 #if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
40 #pragma comment(lib, "strmiids.lib")
41 #include <dshow.h>
42 //#include <qedit.h>
43 EXTERN_C const CLSID CLSID_SampleGrabber;
44 EXTERN_C const CLSID CLSID_NullRenderer;
45 EXTERN_C const IID IID_ISampleGrabberCB;
46 MIDL_INTERFACE("0579154A-2B53-4994-B0D0-E773148EFF85")
47 ISampleGrabberCB : public IUnknown {
48 public:
49         virtual HRESULT STDMETHODCALLTYPE SampleCB( double SampleTime,IMediaSample *pSample) = 0;
50         virtual HRESULT STDMETHODCALLTYPE BufferCB( double SampleTime,BYTE *pBuffer,long BufferLen) = 0;
51 };
52 EXTERN_C const IID IID_ISampleGrabber;
53 MIDL_INTERFACE("6B652FFF-11FE-4fce-92AD-0266B5D7C78F")
54 ISampleGrabber : public IUnknown {
55 public:
56         virtual HRESULT STDMETHODCALLTYPE SetOneShot( BOOL OneShot) = 0;
57         virtual HRESULT STDMETHODCALLTYPE SetMediaType( const AM_MEDIA_TYPE *pType) = 0;
58         virtual HRESULT STDMETHODCALLTYPE GetConnectedMediaType( AM_MEDIA_TYPE *pType) = 0;
59         virtual HRESULT STDMETHODCALLTYPE SetBufferSamples( BOOL BufferThem) = 0;
60         virtual HRESULT STDMETHODCALLTYPE GetCurrentBuffer( /* [out][in] */ long *pBufferSize,/* [out] */ long *pBuffer) = 0;
61         virtual HRESULT STDMETHODCALLTYPE GetCurrentSample( /* [retval][out] */ IMediaSample **ppSample) = 0;
62         virtual HRESULT STDMETHODCALLTYPE SetCallback( ISampleGrabberCB *pCallback,long WhichMethodToCallback) = 0;
63 };
64 #endif
65 #ifdef USE_MOVIE_PLAYER
66 class CMySampleGrabberCB : public ISampleGrabberCB {
67 private:
68         VM *vm;
69 public:
70         CMySampleGrabberCB(VM *vm_ptr)
71         {
72                 vm = vm_ptr;
73         }
74         STDMETHODIMP_(ULONG) AddRef()
75         {
76                 return 2;
77         }
78         STDMETHODIMP_(ULONG) Release()
79         {
80                 return 1;
81         }
82         STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
83         {
84                 if(riid == IID_ISampleGrabberCB || riid == IID_IUnknown) {
85                         *ppv = (void *) static_cast<ISampleGrabberCB*>(this);
86                         return NOERROR;
87                 }
88                 return E_NOINTERFACE;
89         }
90         STDMETHODIMP SampleCB(double SampleTime, IMediaSample *pSample)
91         {
92                 return S_OK;
93         }
94         STDMETHODIMP BufferCB(double dblSampleTime, BYTE *pBuffer, long lBufferSize)
95         {
96                 vm->movie_sound_callback(pBuffer, lBufferSize);
97                 return S_OK;
98         }
99 };
100 #endif
101
102 #define WM_RESIZE  (WM_USER + 1)
103 #define WM_SOCKET0 (WM_USER + 2)
104 #define WM_SOCKET1 (WM_USER + 3)
105 #define WM_SOCKET2 (WM_USER + 4)
106 #define WM_SOCKET3 (WM_USER + 5)
107
108 #ifdef USE_SOCKET
109 #define SOCKET_MAX 4
110 #define SOCKET_BUFFER_MAX 0x100000
111 #endif
112
113 #ifdef USE_VIDEO_CAPTURE
114 #define MAX_CAPTURE_DEVS 8
115 #endif
116
117 // check memory leaks
118 #ifdef _DEBUG
119 #define _CRTDBG_MAP_ALLOC
120 #include <crtdbg.h>
121 #define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
122 #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
123 #endif
124
125 typedef struct screen_buffer_s {
126         int width, height;
127         HDC hdcDib;
128         HBITMAP hBmp, hOldBmp;
129         LPBYTE lpBuf;
130         scrntype* lpBmp;
131         LPBITMAPINFO lpDib;
132         inline scrntype* get_buffer(int y)
133         {
134                 return lpBmp + width * (height - y - 1);
135         }
136 } screen_buffer_t;
137
138 typedef struct {
139         PAVISTREAM pAVICompressed;
140         scrntype* lpBmp;
141         LPBITMAPINFOHEADER pbmInfoHeader;
142         DWORD dwAVIFileSize;
143         LONG lAVIFrames;
144         int frames;
145         int result;
146 } rec_video_thread_param_t;
147
148 class FIFO;
149 class FILEIO;
150
151 class OSD
152 {
153 protected:
154 //      VM* vm;
155 //      EMU* emu;
156         
157 private:
158         _TCHAR app_path[_MAX_PATH];
159         
160         // console
161         HANDLE hStdIn, hStdOut;
162         
163         // input
164         void initialize_input();
165         void release_input();
166         void key_down_sub(int code, bool repeat);
167         void key_up_sub(int code);
168         
169         LPDIRECTINPUT lpdi;
170         LPDIRECTINPUTDEVICE lpdikey;
171 //      LPDIRECTINPUTDEVICE lpdijoy;
172         bool dinput_key_ok;
173 //      bool dinput_joy_ok;
174         
175         uint8 keycode_conv[256];
176         uint8 key_status[256];  // windows key code mapping
177         uint8 key_dik_prev[256];
178 #ifdef USE_SHIFT_NUMPAD_KEY
179         uint8 key_converted[256];
180         bool key_shift_pressed, key_shift_released;
181 #endif
182         bool lost_focus;
183         
184         uint32 joy_status[2];   // joystick #1, #2 (b0 = up, b1 = down, b2 = left, b3 = right, b4- = buttons
185         int joy_num;
186         uint32 joy_mask[2];
187         
188         int mouse_status[3];    // x, y, button (b0 = left, b1 = right)
189         bool mouse_enabled;
190         
191 #ifdef USE_AUTO_KEY
192         FIFO* autokey_buffer;
193         int autokey_phase, autokey_shift;
194         int autokey_table[256];
195 #endif
196         
197         // printer
198         
199         // screen
200         void initialize_screen();
201         void release_screen();
202         void initialize_screen_buffer(screen_buffer_t *buffer, int width, int height, int mode);
203         void release_screen_buffer(screen_buffer_t *buffer);
204 #ifdef USE_CRT_FILTER
205         void apply_crt_fileter_to_screen_buffer(screen_buffer_t *source, screen_buffer_t *dest);
206         void apply_crt_filter_x3_y3(screen_buffer_t *source, screen_buffer_t *dest);
207         void apply_crt_filter_x3_y2(screen_buffer_t *source, screen_buffer_t *dest);
208         void apply_crt_filter_x2_y3(screen_buffer_t *source, screen_buffer_t *dest);
209         void apply_crt_filter_x2_y2(screen_buffer_t *source, screen_buffer_t *dest);
210         void apply_crt_filter_x1_y1(screen_buffer_t *source, screen_buffer_t *dest);
211 #endif
212 #ifdef USE_SCREEN_ROTATE
213         void rotate_screen_buffer(screen_buffer_t *source, screen_buffer_t *dest);
214 #endif
215         void stretch_screen_buffer(screen_buffer_t *source, screen_buffer_t *dest);
216         bool initialize_d3d9();
217         bool initialize_d3d9_surface(screen_buffer_t *buffer);
218         void release_d3d9();
219         void release_d3d9_surface();
220         void copy_to_d3d9_surface(screen_buffer_t *buffer);
221         int add_video_frames();
222         
223         screen_buffer_t vm_screen_buffer;
224 #ifdef USE_CRT_FILTER
225         screen_buffer_t filtered_screen_buffer;
226 #endif
227 #ifdef USE_SCREEN_ROTATE
228         screen_buffer_t rotated_screen_buffer;
229 #endif
230         screen_buffer_t stretched_screen_buffer;
231         screen_buffer_t shrinked_screen_buffer;
232         screen_buffer_t video_screen_buffer;
233         
234         screen_buffer_t* draw_screen_buffer;
235         
236         int host_window_width, host_window_height;
237         bool host_window_mode;
238         int base_window_width, base_window_height;
239         int vm_screen_width, vm_screen_height, vm_screen_width_aspect, vm_screen_height_aspect;
240         int draw_screen_width, draw_screen_height;
241         
242         LPDIRECT3D9 lpd3d9;
243         LPDIRECT3DDEVICE9 lpd3d9Device;
244         LPDIRECT3DSURFACE9 lpd3d9Surface;
245         LPDIRECT3DSURFACE9 lpd3d9OffscreenSurface;
246         
247         _TCHAR video_file_name[_MAX_PATH];
248         int rec_video_fps;
249         double rec_video_run_frames;
250         double rec_video_frames;
251         
252         LPBITMAPINFO lpDibRec;
253         PAVIFILE pAVIFile;
254         PAVISTREAM pAVIStream;
255         PAVISTREAM pAVICompressed;
256         AVICOMPRESSOPTIONS AVIOpts;
257         DWORD dwAVIFileSize;
258         LONG lAVIFrames;
259         HANDLE hVideoThread;
260         rec_video_thread_param_t rec_video_thread_param;
261         
262         bool first_draw_screen;
263         bool first_invalidate;
264         bool self_invalidate;
265         
266         // sound
267         void initialize_sound(int rate, int samples);
268         void release_sound();
269         
270         int sound_rate, sound_samples;
271         bool sound_ok, sound_started, now_mute;
272         
273         LPDIRECTSOUND lpds;
274         LPDIRECTSOUNDBUFFER lpdsPrimaryBuffer, lpdsSecondaryBuffer;
275         bool sound_first_half;
276         
277         _TCHAR sound_file_name[_MAX_PATH];
278         FILEIO* rec_sound_fio;
279         int rec_sound_bytes;
280         int rec_sound_buffer_ptr;
281         
282 #if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
283         // video device
284         void initialize_video();
285         void release_video();
286         
287         IGraphBuilder *pGraphBuilder;
288         IBaseFilter *pVideoBaseFilter;
289         IBaseFilter *pCaptureBaseFilter;
290         ICaptureGraphBuilder2 *pCaptureGraphBuilder2;
291         ISampleGrabber *pVideoSampleGrabber;
292         IBaseFilter *pSoundBaseFilter;
293         ISampleGrabber *pSoundSampleGrabber;
294         CMySampleGrabberCB *pSoundCallBack;
295         IMediaControl *pMediaControl;
296         IMediaSeeking *pMediaSeeking;
297         IMediaPosition *pMediaPosition;
298         IVideoWindow *pVideoWindow;
299         IBasicVideo *pBasicVideo;
300         IBasicAudio *pBasicAudio;
301         bool bTimeFormatFrame;
302         bool bVerticalReversed;
303         
304         screen_buffer_t dshow_screen_buffer;
305         int direct_show_width, direct_show_height;
306         bool direct_show_mute[2];
307 #endif
308 #ifdef USE_MOVIE_PLAYER
309         double movie_frame_rate;
310         int movie_sound_rate;
311 #endif
312 #ifdef USE_VIDEO_CAPTURE
313         void enum_capture_devs();
314         bool connect_capture_dev(int index, bool pin);
315         int cur_capture_dev_index;
316         int num_capture_devs;
317         _TCHAR capture_dev_name[MAX_CAPTURE_DEVS][256];
318 #endif
319         
320 #ifdef USE_SOCKET
321         // 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         
341         void initialize(int rate, int samples);
342         void release();
343         void power_off();
344         void suspend();
345         void restore();
346         _TCHAR* application_path()
347         {
348                 return app_path;
349         }
350         _TCHAR* bios_path(const _TCHAR* file_name);
351         void get_host_time(cur_time_t* time);
352         void sleep(uint32 ms);
353         void create_date_file_name(_TCHAR *name, int length, _TCHAR *extension);
354         
355         // common console
356         void open_console(_TCHAR* title);
357         void close_console();
358         unsigned int get_console_code_page();
359         bool is_console_active();
360         void set_console_text_attribute(unsigned short attr);
361         void write_console(_TCHAR* buffer, unsigned int length);
362         int read_console_input(_TCHAR* buffer);
363         
364         // common input
365         void update_input();
366         void key_down(int code, bool repeat);
367         void key_up(int code);
368         void key_lost_focus()
369         {
370                 lost_focus = true;
371         }
372 #ifdef ONE_BOARD_MICRO_COMPUTER
373         void press_button(int num);
374 #endif
375         void enable_mouse();
376         void disenable_mouse();
377         void toggle_mouse();
378         bool get_mouse_enabled()
379         {
380                 return mouse_enabled;
381         }
382 #ifdef USE_AUTO_KEY
383         void start_auto_key();
384         void stop_auto_key();
385         bool now_auto_key()
386         {
387                 return (autokey_phase != 0);
388         }
389 #endif
390         uint8* key_buffer()
391         {
392                 return key_status;
393         }
394         uint32* joy_buffer()
395         {
396                 return joy_status;
397         }
398         int* mouse_buffer()
399         {
400                 return mouse_status;
401         }
402         
403         // common printer
404         
405         // common screen
406         int get_window_width(int mode);
407         int get_window_height(int mode);
408         void set_window_size(int window_width, int window_height, bool window_mode);
409         void set_vm_screen_size(int width, int height, int width_aspect, int height_aspect, int window_width, int window_height);
410         scrntype* get_vm_screen_buffer(int y);
411         int draw_screen();
412 #ifdef ONE_BOARD_MICRO_COMPUTER
413         void reload_bitmap()
414         {
415                 first_invalidate = true;
416         }
417 #endif
418         void capture_screen();
419         bool start_rec_video(int fps);
420         void stop_rec_video();
421         void restart_rec_video();
422         void add_extra_frames(int extra_frames);
423         bool now_rec_video;
424 #ifdef USE_CRT_FILTER
425         bool screen_skip_line;
426 #endif
427         
428         // common sound
429         void update_sound(int* extra_frames);
430         void mute_sound();
431         void stop_sound();
432         void start_rec_sound();
433         void stop_rec_sound();
434         void restart_rec_sound();
435         bool now_rec_sound;
436         
437 #if defined(USE_MOVIE_PLAYER) || defined(USE_VIDEO_CAPTURE)
438         // common video device
439         void get_video_buffer();
440         void mute_video_dev(bool l, bool r);
441 #endif
442 #ifdef USE_MOVIE_PLAYER
443         bool open_movie_file(const _TCHAR* file_path);
444         void close_movie_file();
445         void play_movie();
446         void stop_movie();
447         void pause_movie();
448         double get_movie_frame_rate()
449         {
450                 return movie_frame_rate;
451         }
452         int get_movie_sound_rate()
453         {
454                 return movie_sound_rate;
455         }
456         void set_cur_movie_frame(int frame, bool relative);
457         uint32 get_cur_movie_frame();
458         bool now_movie_play, now_movie_pause;
459 #endif
460 #ifdef USE_VIDEO_CAPTURE
461         int get_cur_capture_dev_index()
462         {
463                 return cur_capture_dev_index;
464         }
465         int get_num_capture_devs()
466         {
467                 return num_capture_devs;
468         }
469         _TCHAR* get_capture_dev_name(int index)
470         {
471                 return capture_dev_name[index];
472         }
473         void open_capture_dev(int index, bool pin);
474         void close_capture_dev();
475         void show_capture_dev_filter();
476         void show_capture_dev_pin();
477         void show_capture_dev_source();
478         void set_capture_dev_channel(int ch);
479 #endif
480         
481 #ifdef USE_SOCKET
482         // common socket
483         int get_socket(int ch)
484         {
485                 return soc[ch];
486         }
487         void socket_connected(int ch);
488         void socket_disconnected(int ch);
489         void update_socket();
490         bool init_socket_tcp(int ch);
491         bool init_socket_udp(int ch);
492         bool connect_socket(int ch, uint32 ipaddr, int port);
493         void disconnect_socket(int ch);
494         bool listen_socket(int ch);
495         void send_data_tcp(int ch);
496         void send_data_udp(int ch, uint32 ipaddr, int port);
497         void send_data(int ch);
498         void recv_data(int ch);
499 #endif
500         
501         // win32 dependent
502         void update_screen(HDC hdc);
503         HWND main_window_handle;
504         HINSTANCE instance_handle;
505         bool vista_or_later;
506 };
507
508 #endif