6 // To get "#define vo_ontop global_vo->opts->vo_ontop" etc
7 #include "old_vo_defines.h"
8 #include "osdep/keycodes.h"
9 #include "input/input.h"
10 #include "input/mouse.h"
12 #include "video_out.h"
14 #include "w32_common.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 const struct keymap vk_map
[] = {
48 {VK_ESCAPE
, KEY_ESC
}, {VK_BACK
, KEY_BS
}, {VK_TAB
, KEY_TAB
}, {VK_CONTROL
, KEY_CTRL
},
51 {VK_LEFT
, KEY_LEFT
}, {VK_UP
, KEY_UP
}, {VK_RIGHT
, KEY_RIGHT
}, {VK_DOWN
, KEY_DOWN
},
54 {VK_INSERT
, KEY_INSERT
}, {VK_DELETE
, KEY_DELETE
}, {VK_HOME
, KEY_HOME
}, {VK_END
, KEY_END
},
55 {VK_PRIOR
, KEY_PAGE_UP
}, {VK_NEXT
, KEY_PAGE_DOWN
},
58 {VK_F1
, KEY_F
+1}, {VK_F2
, KEY_F
+2}, {VK_F3
, KEY_F
+3}, {VK_F4
, KEY_F
+4},
59 {VK_F5
, KEY_F
+5}, {VK_F6
, KEY_F
+6}, {VK_F7
, KEY_F
+7}, {VK_F8
, KEY_F
+8},
60 {VK_F9
, KEY_F
+9}, {VK_F10
, KEY_F
+10}, {VK_F11
, KEY_F
+11}, {VK_F1
, KEY_F
+12},
62 {VK_NUMPAD0
, KEY_KP0
}, {VK_NUMPAD1
, KEY_KP1
}, {VK_NUMPAD2
, KEY_KP2
},
63 {VK_NUMPAD3
, KEY_KP3
}, {VK_NUMPAD4
, KEY_KP4
}, {VK_NUMPAD5
, KEY_KP5
},
64 {VK_NUMPAD6
, KEY_KP6
}, {VK_NUMPAD7
, KEY_KP7
}, {VK_NUMPAD8
, KEY_KP8
},
65 {VK_NUMPAD9
, KEY_KP9
}, {VK_DECIMAL
, KEY_KPDEC
},
70 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
75 case WM_ERASEBKGND
: // no need to erase background seperately
78 event_flags
|= VO_EVENT_EXPOSE
;
83 ClientToScreen(vo_window
, &p
);
88 event_flags
|= VO_EVENT_RESIZE
;
89 GetClientRect(vo_window
, &r
);
91 vo_dheight
= r
.bottom
;
93 case WM_WINDOWPOSCHANGING
:
94 if (vo_keepaspect
&& !vo_fs
) {
95 WINDOWPOS
*wpos
= lParam
;
98 GetClientRect(vo_window
, &r
);
99 GetWindowRect(vo_window
, &r2
);
100 xborder
= (r2
.right
- r2
.left
) - (r
.right
- r
.left
);
101 yborder
= (r2
.bottom
- r2
.top
) - (r
.bottom
- r
.top
);
102 wpos
->cx
-= xborder
; wpos
->cy
-= yborder
;
103 aspect_fit(global_vo
, &wpos
->cx
, &wpos
->cy
, wpos
->cx
, wpos
->cy
);
104 wpos
->cx
+= xborder
; wpos
->cy
+= yborder
;
108 mplayer_put_key(KEY_CLOSE_WIN
);
113 case SC_MONITORPOWER
:
114 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: killing screensaver\n");
119 mpkey
= lookup_keymap_table(vk_map
, wParam
);
121 mplayer_put_key(mpkey
);
124 mplayer_put_key(wParam
);
127 if (!vo_nomouse_input
&& (vo_fs
|| (wParam
& MK_CONTROL
))) {
128 mplayer_put_key(MOUSE_BTN0
);
133 SendMessage(hWnd
, WM_NCLBUTTONDOWN
, HTCAPTION
, 0);
138 if (!vo_nomouse_input
)
139 mplayer_put_key(MOUSE_BTN1
);
142 if (!vo_nomouse_input
)
143 mplayer_put_key(MOUSE_BTN2
);
146 if (enable_mouse_movements
) {
148 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i",
149 GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
150 mp_input_queue_cmd(global_vo
->input_ctx
, mp_input_parse_cmd(cmd_str
));
154 if (!vo_nomouse_input
) {
155 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
157 mplayer_put_key(MOUSE_BTN3
);
159 mplayer_put_key(MOUSE_BTN4
);
164 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
168 * \brief Dispatch incoming window events and handle them.
170 * This function should be placed inside libvo's function "check_events".
172 * Global libvo variables changed:
173 * vo_dwidth: new window client area width
174 * vo_dheight: new window client area height
176 * \return int with these flags possibly set, take care to handle in the right order
177 * if it matters in your driver:
179 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
180 * driver render context accordingly.
181 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
182 * the window if the movie is paused.
184 int vo_w32_check_events(void) {
187 while (PeekMessage(&msg
, 0, 0, 0, PM_REMOVE
)) {
188 TranslateMessage(&msg
);
189 DispatchMessage(&msg
);
193 GetClientRect(vo_window
, &r
);
194 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
) {
195 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
196 event_flags
|= VO_EVENT_RESIZE
;
198 GetClientRect(WinID
, &r
);
199 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
)
200 MoveWindow(vo_window
, 0, 0, r
.right
, r
.bottom
, FALSE
);
206 static BOOL CALLBACK
mon_enum(HMONITOR hmon
, HDC hdc
, LPRECT r
, LPARAM p
) {
207 // this defaults to the last screen if specified number does not exist
208 xinerama_x
= r
->left
;
210 vo_screenwidth
= r
->right
- r
->left
;
211 vo_screenheight
= r
->bottom
- r
->top
;
212 if (mon_cnt
== xinerama_screen
)
219 * \brief Update screen information.
221 * This function should be called in libvo's "control" callback
222 * with parameter VOCTRL_UPDATE_SCREENINFO.
223 * Note that this also enables the new API where geometry and aspect
224 * calculations are done in video_out.c:config_video_out
226 * Global libvo variables changed:
232 void w32_update_xinerama_info(void) {
233 xinerama_x
= xinerama_y
= 0;
234 if (xinerama_screen
< -1) {
236 xinerama_x
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
237 xinerama_y
= GetSystemMetrics(SM_YVIRTUALSCREEN
);
238 tmp
= GetSystemMetrics(SM_CXVIRTUALSCREEN
);
239 if (tmp
) vo_screenwidth
= tmp
;
240 tmp
= GetSystemMetrics(SM_CYVIRTUALSCREEN
);
241 if (tmp
) vo_screenheight
= tmp
;
242 } else if (xinerama_screen
== -1 && myMonitorFromWindow
&& myGetMonitorInfo
) {
244 HMONITOR m
= myMonitorFromWindow(vo_window
, MONITOR_DEFAULTTOPRIMARY
);
245 mi
.cbSize
= sizeof(mi
);
246 myGetMonitorInfo(m
, &mi
);
247 xinerama_x
= mi
.rcMonitor
.left
;
248 xinerama_y
= mi
.rcMonitor
.top
;
249 vo_screenwidth
= mi
.rcMonitor
.right
- mi
.rcMonitor
.left
;
250 vo_screenheight
= mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
;
251 } else if (xinerama_screen
> 0 && myEnumDisplayMonitors
) {
253 myEnumDisplayMonitors(NULL
, NULL
, mon_enum
, 0);
255 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
258 static void updateScreenProperties(void) {
260 dm
.dmSize
= sizeof dm
;
261 dm
.dmDriverExtra
= 0;
262 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
263 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS
, &dm
)) {
264 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to enumerate display settings!\n");
268 vo_screenwidth
= dm
.dmPelsWidth
;
269 vo_screenheight
= dm
.dmPelsHeight
;
270 depthonscreen
= dm
.dmBitsPerPel
;
271 w32_update_xinerama_info();
274 static void changeMode(void) {
276 dm
.dmSize
= sizeof dm
;
277 dm
.dmDriverExtra
= 0;
279 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
280 dm
.dmBitsPerPel
= depthonscreen
;
281 dm
.dmPelsWidth
= vo_screenwidth
;
282 dm
.dmPelsHeight
= vo_screenheight
;
286 int bestScore
= INT_MAX
;
288 for (i
= 0; EnumDisplaySettings(0, i
, &dm
); ++i
) {
289 int score
= (dm
.dmPelsWidth
- o_dwidth
) * (dm
.dmPelsHeight
- o_dheight
);
290 if (dm
.dmBitsPerPel
!= depthonscreen
) continue;
291 if (dm
.dmPelsWidth
< o_dwidth
) continue;
292 if (dm
.dmPelsHeight
< o_dheight
) continue;
294 if (score
< bestScore
) {
301 EnumDisplaySettings(0, bestMode
, &dm
);
303 ChangeDisplaySettings(&dm
, CDS_FULLSCREEN
);
307 static void resetMode(void) {
309 ChangeDisplaySettings(0, 0);
312 static int createRenderingContext(void) {
313 HWND layer
= HWND_NOTOPMOST
;
314 PIXELFORMATDESCRIPTOR pfd
;
315 HDC vo_hdc
= GetDC(vo_window
);
319 int style
= (vo_border
&& !vo_fs
) ?
320 (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
;
322 if (vo_fs
|| vo_ontop
) layer
= HWND_TOPMOST
;
325 while (ShowCursor(0) >= 0) /**/ ;
328 while (ShowCursor(1) < 0) /**/ ;
330 updateScreenProperties();
331 ShowWindow(vo_window
, SW_HIDE
);
332 SetWindowLong(vo_window
, GWL_STYLE
, style
);
334 prev_width
= vo_dwidth
;
335 prev_height
= vo_dheight
;
338 vo_dwidth
= vo_screenwidth
;
339 vo_dheight
= vo_screenheight
;
343 // make sure there are no "stale" resize events
344 // that would set vo_d* to wrong values
345 vo_w32_check_events();
346 vo_dwidth
= prev_width
;
347 vo_dheight
= prev_height
;
350 // HACK around what probably is a windows focus bug:
351 // when pressing 'f' on the console, then 'f' again to
352 // return to windowed mode, any input into the video
353 // window is lost forever.
357 r
.right
= r
.left
+ vo_dwidth
;
359 r
.bottom
= r
.top
+ vo_dheight
;
360 AdjustWindowRect(&r
, style
, 0);
361 SetWindowPos(vo_window
, layer
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_SHOWWINDOW
);
364 memset(&pfd
, 0, sizeof pfd
);
365 pfd
.nSize
= sizeof pfd
;
367 pfd
.dwFlags
= PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
;
368 pfd
.iPixelType
= PFD_TYPE_RGBA
;
370 pfd
.iLayerType
= PFD_MAIN_PLANE
;
371 pf
= ChoosePixelFormat(vo_hdc
, &pfd
);
373 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to select a valid pixel format!\n");
374 ReleaseDC(vo_window
, vo_hdc
);
378 SetPixelFormat(vo_hdc
, pf
, &pfd
);
380 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth
, vo_screenheight
, depthonscreen
);
382 ReleaseDC(vo_window
, vo_hdc
);
387 * \brief Configure and show window on the screen.
389 * This function should be called in libvo's "config" callback.
390 * It configures a window and shows it on the screen.
392 * Global libvo variables changed:
396 * \return 1 - Success, 0 - Failure
398 int vo_w32_config(uint32_t width
, uint32_t height
, uint32_t flags
) {
399 // store original size for videomode switching
404 // the desired size is ignored in wid mode, it always matches the window size.
405 prev_width
= vo_dwidth
= width
;
406 prev_height
= vo_dheight
= height
;
411 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
412 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
413 return createRenderingContext();
417 * \brief Initialize w32_common framework.
419 * The first function that should be called from the w32_common framework.
420 * It handles window creation on the screen with proper title and attributes.
421 * It also initializes the framework's internal variables. The function should
422 * be called after your own preinit initialization and you shouldn't do any
423 * window management on your own.
425 * Global libvo variables changed:
431 * \return 1 = Success, 0 = Failure
433 int vo_w32_init(void) {
434 HICON mplayerIcon
= 0;
435 char exedir
[MAX_PATH
];
441 hInstance
= GetModuleHandle(0);
443 if (GetModuleFileName(0, exedir
, MAX_PATH
))
444 mplayerIcon
= ExtractIcon(hInstance
, exedir
, 0);
446 mplayerIcon
= LoadIcon(0, IDI_APPLICATION
);
449 WNDCLASSEX wcex
= { sizeof wcex
, CS_OWNDC
| CS_DBLCLKS
, WndProc
, 0, 0, hInstance
, mplayerIcon
, LoadCursor(0, IDC_ARROW
), NULL
, 0, classname
, mplayerIcon
};
451 if (!RegisterClassEx(&wcex
)) {
452 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to register window class!\n");
460 GetClientRect(WinID
, &r
);
461 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
462 vo_window
= CreateWindowEx(WS_EX_NOPARENTNOTIFY
, classname
, classname
,
463 WS_CHILD
| WS_VISIBLE
,
464 0, 0, vo_dwidth
, vo_dheight
, WinID
, 0, hInstance
, 0);
465 EnableWindow(vo_window
, 0);
467 vo_window
= CreateWindowEx(0, classname
, classname
,
468 vo_border
? (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
,
469 CW_USEDEFAULT
, 0, 100, 100, 0, 0, hInstance
, 0);
471 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to create window!\n");
475 myMonitorFromWindow
= NULL
;
476 myGetMonitorInfo
= NULL
;
477 myEnumDisplayMonitors
= NULL
;
478 user32
= GetModuleHandle("user32.dll");
480 myMonitorFromWindow
= (void *)GetProcAddress(user32
, "MonitorFromWindow");
481 myGetMonitorInfo
= GetProcAddress(user32
, "GetMonitorInfoA");
482 myEnumDisplayMonitors
= GetProcAddress(user32
, "EnumDisplayMonitors");
484 updateScreenProperties();
490 * \brief Toogle fullscreen / windowed mode.
492 * Should be called on VOCTRL_FULLSCREEN event. The window is
493 * always resized after this call, so the rendering context
494 * should be reinitialized with the new dimensions.
495 * It is unspecified if vo_check_events will create a resize
496 * event in addition or not.
498 * Global libvo variables changed:
504 void vo_w32_fullscreen(void) {
507 createRenderingContext();
511 * \brief Toogle window border attribute.
513 * Should be called on VOCTRL_BORDER event.
515 * Global libvo variables changed:
518 void vo_w32_border(void) {
519 vo_border
= !vo_border
;
520 createRenderingContext();
524 * \brief Toogle window ontop attribute.
526 * Should be called on VOCTRL_ONTOP event.
528 * Global libvo variables changed:
531 void vo_w32_ontop( void )
533 vo_ontop
= !vo_ontop
;
535 createRenderingContext();
540 * \brief Uninitialize w32_common framework.
542 * Should be called last in video driver's uninit function. First release
543 * anything built on top of the created window e.g. rendering context inside
544 * and call vo_w32_uninit at the end.
546 void vo_w32_uninit(void) {
547 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: uninit\n");
551 DestroyWindow(vo_window
);
553 UnregisterClass(classname
, 0);