6 #include "osdep/keycodes.h"
7 #include "input/input.h"
8 #include "input/mouse.h"
10 #include "video_out.h"
12 #include "w32_common.h"
14 // To get "#define vo_ontop global_vo->opts->vo_ontop" etc
15 #include "old_vo_defines.h"
17 extern int enable_mouse_movements
;
19 #ifndef MONITOR_DEFAULTTOPRIMARY
20 #define MONITOR_DEFAULTTOPRIMARY 1
23 static const char classname
[] = "MPlayer - Media player for Win32";
26 static int depthonscreen
;
27 // last non-fullscreen extends
28 static int prev_width
;
29 static int prev_height
;
33 static uint32_t o_dwidth
;
34 static uint32_t o_dheight
;
36 static HINSTANCE hInstance
;
37 #define vo_window vo_w32_window
39 static int event_flags
;
42 static HMONITOR (WINAPI
* myMonitorFromWindow
)(HWND
, DWORD
);
43 static BOOL (WINAPI
* myGetMonitorInfo
)(HMONITOR
, LPMONITORINFO
);
44 static BOOL (WINAPI
* myEnumDisplayMonitors
)(HDC
, LPCRECT
, MONITORENUMPROC
, LPARAM
);
46 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
49 if (WinID
< 0 || message
== WM_PAINT
|| message
== WM_ERASEBKGND
||
52 case WM_ERASEBKGND
: // no need to erase background seperately
55 event_flags
|= VO_EVENT_EXPOSE
;
60 ClientToScreen(vo_window
, &p
);
65 event_flags
|= VO_EVENT_RESIZE
;
66 GetClientRect(vo_window
, &r
);
68 vo_dheight
= r
.bottom
;
70 case WM_WINDOWPOSCHANGING
:
71 if (vo_keepaspect
&& !vo_fs
) {
72 WINDOWPOS
*wpos
= lParam
;
75 GetClientRect(vo_window
, &r
);
76 GetWindowRect(vo_window
, &r2
);
77 xborder
= (r2
.right
- r2
.left
) - (r
.right
- r
.left
);
78 yborder
= (r2
.bottom
- r2
.top
) - (r
.bottom
- r
.top
);
79 wpos
->cx
-= xborder
; wpos
->cy
-= yborder
;
80 aspect_fit(global_vo
, &wpos
->cx
, &wpos
->cy
, wpos
->cx
, wpos
->cy
);
81 wpos
->cx
+= xborder
; wpos
->cy
+= yborder
;
85 mplayer_put_key(KEY_CLOSE_WIN
);
91 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: killing screensaver\n");
97 case VK_LEFT
: mplayer_put_key(KEY_LEFT
); break;
98 case VK_UP
: mplayer_put_key(KEY_UP
); break;
99 case VK_RIGHT
: mplayer_put_key(KEY_RIGHT
); break;
100 case VK_DOWN
: mplayer_put_key(KEY_DOWN
); break;
101 case VK_TAB
: mplayer_put_key(KEY_TAB
); break;
102 case VK_CONTROL
: mplayer_put_key(KEY_CTRL
); break;
103 case VK_BACK
: mplayer_put_key(KEY_BS
); break;
104 case VK_DELETE
: mplayer_put_key(KEY_DELETE
); break;
105 case VK_INSERT
: mplayer_put_key(KEY_INSERT
); break;
106 case VK_HOME
: mplayer_put_key(KEY_HOME
); break;
107 case VK_END
: mplayer_put_key(KEY_END
); break;
108 case VK_PRIOR
: mplayer_put_key(KEY_PAGE_UP
); break;
109 case VK_NEXT
: mplayer_put_key(KEY_PAGE_DOWN
); break;
110 case VK_ESCAPE
: mplayer_put_key(KEY_ESC
); break;
114 mplayer_put_key(wParam
);
117 if (!vo_nomouse_input
&& (vo_fs
|| (wParam
& MK_CONTROL
))) {
118 mplayer_put_key(MOUSE_BTN0
);
123 SendMessage(hWnd
, WM_NCLBUTTONDOWN
, HTCAPTION
, 0);
128 if (!vo_nomouse_input
)
129 mplayer_put_key(MOUSE_BTN1
);
132 if (!vo_nomouse_input
)
133 mplayer_put_key(MOUSE_BTN2
);
136 if (enable_mouse_movements
) {
138 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i",
139 GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
140 mp_input_queue_cmd(global_vo
->input_ctx
, mp_input_parse_cmd(cmd_str
));
144 if (!vo_nomouse_input
) {
145 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
147 mplayer_put_key(MOUSE_BTN3
);
149 mplayer_put_key(MOUSE_BTN4
);
153 } else switch (message
) {
157 case WM_LBUTTONDBLCLK
:
160 case WM_MBUTTONDBLCLK
:
163 case WM_RBUTTONDBLCLK
:
164 SendNotifyMessage(WinID
, message
, wParam
, lParam
);
167 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
170 int vo_w32_check_events(void) {
173 while (PeekMessage(&msg
, 0, 0, 0, PM_REMOVE
)) {
174 TranslateMessage(&msg
);
175 DispatchMessage(&msg
);
179 GetClientRect(WinID
, &r
);
180 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
)
181 MoveWindow(vo_window
, 0, 0, r
.right
, r
.bottom
, FALSE
);
187 static BOOL CALLBACK
mon_enum(HMONITOR hmon
, HDC hdc
, LPRECT r
, LPARAM p
) {
188 // this defaults to the last screen if specified number does not exist
189 xinerama_x
= r
->left
;
191 vo_screenwidth
= r
->right
- r
->left
;
192 vo_screenheight
= r
->bottom
- r
->top
;
193 if (mon_cnt
== xinerama_screen
)
199 void w32_update_xinerama_info(void) {
200 xinerama_x
= xinerama_y
= 0;
201 if (xinerama_screen
< -1) {
203 xinerama_x
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
204 xinerama_y
= GetSystemMetrics(SM_YVIRTUALSCREEN
);
205 tmp
= GetSystemMetrics(SM_CXVIRTUALSCREEN
);
206 if (tmp
) vo_screenwidth
= tmp
;
207 tmp
= GetSystemMetrics(SM_CYVIRTUALSCREEN
);
208 if (tmp
) vo_screenheight
= tmp
;
209 } else if (xinerama_screen
== -1 && myMonitorFromWindow
&& myGetMonitorInfo
) {
211 HMONITOR m
= myMonitorFromWindow(vo_window
, MONITOR_DEFAULTTOPRIMARY
);
212 mi
.cbSize
= sizeof(mi
);
213 myGetMonitorInfo(m
, &mi
);
214 xinerama_x
= mi
.rcMonitor
.left
;
215 xinerama_y
= mi
.rcMonitor
.top
;
216 vo_screenwidth
= mi
.rcMonitor
.right
- mi
.rcMonitor
.left
;
217 vo_screenheight
= mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
;
218 } else if (xinerama_screen
> 0 && myEnumDisplayMonitors
) {
220 myEnumDisplayMonitors(NULL
, NULL
, mon_enum
, 0);
222 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
225 static void updateScreenProperties(void) {
227 dm
.dmSize
= sizeof dm
;
228 dm
.dmDriverExtra
= 0;
229 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
230 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS
, &dm
)) {
231 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to enumerate display settings!\n");
235 vo_screenwidth
= dm
.dmPelsWidth
;
236 vo_screenheight
= dm
.dmPelsHeight
;
237 depthonscreen
= dm
.dmBitsPerPel
;
238 w32_update_xinerama_info();
241 static void changeMode(void) {
243 dm
.dmSize
= sizeof dm
;
244 dm
.dmDriverExtra
= 0;
246 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
247 dm
.dmBitsPerPel
= depthonscreen
;
248 dm
.dmPelsWidth
= vo_screenwidth
;
249 dm
.dmPelsHeight
= vo_screenheight
;
253 int bestScore
= INT_MAX
;
255 for (i
= 0; EnumDisplaySettings(0, i
, &dm
); ++i
) {
256 int score
= (dm
.dmPelsWidth
- o_dwidth
) * (dm
.dmPelsHeight
- o_dheight
);
257 if (dm
.dmBitsPerPel
!= depthonscreen
) continue;
258 if (dm
.dmPelsWidth
< o_dwidth
) continue;
259 if (dm
.dmPelsHeight
< o_dheight
) continue;
261 if (score
< bestScore
) {
268 EnumDisplaySettings(0, bestMode
, &dm
);
270 ChangeDisplaySettings(&dm
, CDS_FULLSCREEN
);
274 static void resetMode(void) {
276 ChangeDisplaySettings(0, 0);
279 static int createRenderingContext(void) {
280 HWND layer
= HWND_NOTOPMOST
;
281 PIXELFORMATDESCRIPTOR pfd
;
282 HDC vo_hdc
= GetDC(vo_window
);
286 int style
= (vo_border
&& !vo_fs
) ?
287 (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
;
289 if (vo_fs
|| vo_ontop
) layer
= HWND_TOPMOST
;
292 while (ShowCursor(0) >= 0) /**/ ;
295 while (ShowCursor(1) < 0) /**/ ;
297 updateScreenProperties();
298 ShowWindow(vo_window
, SW_HIDE
);
299 SetWindowLong(vo_window
, GWL_STYLE
, style
);
301 prev_width
= vo_dwidth
;
302 prev_height
= vo_dheight
;
305 vo_dwidth
= vo_screenwidth
;
306 vo_dheight
= vo_screenheight
;
310 // make sure there are no "stale" resize events
311 // that would set vo_d* to wrong values
312 vo_w32_check_events();
313 vo_dwidth
= prev_width
;
314 vo_dheight
= prev_height
;
317 // HACK around what probably is a windows focus bug:
318 // when pressing 'f' on the console, then 'f' again to
319 // return to windowed mode, any input into the video
320 // window is lost forever.
324 r
.right
= r
.left
+ vo_dwidth
;
326 r
.bottom
= r
.top
+ vo_dheight
;
327 AdjustWindowRect(&r
, style
, 0);
328 SetWindowPos(vo_window
, layer
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_SHOWWINDOW
);
331 memset(&pfd
, 0, sizeof pfd
);
332 pfd
.nSize
= sizeof pfd
;
334 pfd
.dwFlags
= PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
;
335 pfd
.iPixelType
= PFD_TYPE_RGBA
;
337 pfd
.iLayerType
= PFD_MAIN_PLANE
;
338 pf
= ChoosePixelFormat(vo_hdc
, &pfd
);
340 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to select a valid pixel format!\n");
341 ReleaseDC(vo_window
, vo_hdc
);
345 SetPixelFormat(vo_hdc
, pf
, &pfd
);
347 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth
, vo_screenheight
, depthonscreen
);
349 ReleaseDC(vo_window
, vo_hdc
);
353 int vo_w32_config(uint32_t width
, uint32_t height
, uint32_t flags
) {
354 // store original size for videomode switching
358 prev_width
= vo_dwidth
= width
;
359 prev_height
= vo_dheight
= height
;
363 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
364 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
365 return createRenderingContext();
368 int vo_w32_init(void) {
369 HICON mplayerIcon
= 0;
370 char exedir
[MAX_PATH
];
376 hInstance
= GetModuleHandle(0);
378 if (GetModuleFileName(0, exedir
, MAX_PATH
))
379 mplayerIcon
= ExtractIcon(hInstance
, exedir
, 0);
381 mplayerIcon
= LoadIcon(0, IDI_APPLICATION
);
384 WNDCLASSEX wcex
= { sizeof wcex
, CS_OWNDC
| CS_DBLCLKS
, WndProc
, 0, 0, hInstance
, mplayerIcon
, LoadCursor(0, IDC_ARROW
), NULL
, 0, classname
, mplayerIcon
};
386 if (!RegisterClassEx(&wcex
)) {
387 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to register window class!\n");
395 GetClientRect(WinID
, &r
);
396 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
397 vo_window
= CreateWindowEx(WS_EX_NOPARENTNOTIFY
, classname
, classname
,
398 WS_CHILD
| WS_VISIBLE
,
399 0, 0, vo_dwidth
, vo_dheight
, WinID
, 0, hInstance
, 0);
401 vo_window
= CreateWindowEx(0, classname
, classname
,
402 vo_border
? (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
,
403 CW_USEDEFAULT
, 0, 100, 100, 0, 0, hInstance
, 0);
405 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to create window!\n");
409 myMonitorFromWindow
= NULL
;
410 myGetMonitorInfo
= NULL
;
411 myEnumDisplayMonitors
= NULL
;
412 user32
= GetModuleHandle("user32.dll");
414 myMonitorFromWindow
= (void *)GetProcAddress(user32
, "MonitorFromWindow");
415 myGetMonitorInfo
= GetProcAddress(user32
, "GetMonitorInfoA");
416 myEnumDisplayMonitors
= GetProcAddress(user32
, "EnumDisplayMonitors");
418 updateScreenProperties();
423 void vo_w32_fullscreen(void) {
426 createRenderingContext();
429 void vo_w32_border(void) {
430 vo_border
= !vo_border
;
431 createRenderingContext();
434 void vo_w32_ontop( void )
436 vo_ontop
= !vo_ontop
;
438 createRenderingContext();
442 void vo_w32_uninit(void) {
443 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: uninit\n");
447 DestroyWindow(vo_window
);
449 UnregisterClass(classname
, 0);