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"
15 extern int enable_mouse_movements
;
17 #ifndef MONITOR_DEFAULTTOPRIMARY
18 #define MONITOR_DEFAULTTOPRIMARY 1
21 static const char classname
[] = "MPlayer - Media player for Win32";
24 // last non-fullscreen extends
25 static int prev_width
;
26 static int prev_height
;
30 static uint32_t o_dwidth
;
31 static uint32_t o_dheight
;
33 static HINSTANCE hInstance
;
34 #define vo_window vo_w32_window
36 static int event_flags
;
39 static HMONITOR (WINAPI
* myMonitorFromWindow
)(HWND
, DWORD
);
40 static BOOL (WINAPI
* myGetMonitorInfo
)(HMONITOR
, LPMONITORINFO
);
41 static BOOL (WINAPI
* myEnumDisplayMonitors
)(HDC
, LPCRECT
, MONITORENUMPROC
, LPARAM
);
43 static const struct keymap vk_map
[] = {
45 {VK_ESCAPE
, KEY_ESC
}, {VK_BACK
, KEY_BS
}, {VK_TAB
, KEY_TAB
}, {VK_CONTROL
, KEY_CTRL
},
48 {VK_LEFT
, KEY_LEFT
}, {VK_UP
, KEY_UP
}, {VK_RIGHT
, KEY_RIGHT
}, {VK_DOWN
, KEY_DOWN
},
51 {VK_INSERT
, KEY_INSERT
}, {VK_DELETE
, KEY_DELETE
}, {VK_HOME
, KEY_HOME
}, {VK_END
, KEY_END
},
52 {VK_PRIOR
, KEY_PAGE_UP
}, {VK_NEXT
, KEY_PAGE_DOWN
},
55 {VK_F1
, KEY_F
+1}, {VK_F2
, KEY_F
+2}, {VK_F3
, KEY_F
+3}, {VK_F4
, KEY_F
+4},
56 {VK_F5
, KEY_F
+5}, {VK_F6
, KEY_F
+6}, {VK_F7
, KEY_F
+7}, {VK_F8
, KEY_F
+8},
57 {VK_F9
, KEY_F
+9}, {VK_F10
, KEY_F
+10}, {VK_F11
, KEY_F
+11}, {VK_F1
, KEY_F
+12},
59 {VK_NUMPAD0
, KEY_KP0
}, {VK_NUMPAD1
, KEY_KP1
}, {VK_NUMPAD2
, KEY_KP2
},
60 {VK_NUMPAD3
, KEY_KP3
}, {VK_NUMPAD4
, KEY_KP4
}, {VK_NUMPAD5
, KEY_KP5
},
61 {VK_NUMPAD6
, KEY_KP6
}, {VK_NUMPAD7
, KEY_KP7
}, {VK_NUMPAD8
, KEY_KP8
},
62 {VK_NUMPAD9
, KEY_KP9
}, {VK_DECIMAL
, KEY_KPDEC
},
67 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
72 case WM_ERASEBKGND
: // no need to erase background seperately
75 event_flags
|= VO_EVENT_EXPOSE
;
80 ClientToScreen(vo_window
, &p
);
85 event_flags
|= VO_EVENT_RESIZE
;
86 GetClientRect(vo_window
, &r
);
88 vo_dheight
= r
.bottom
;
90 case WM_WINDOWPOSCHANGING
:
91 if (vo_keepaspect
&& !vo_fs
) {
92 WINDOWPOS
*wpos
= lParam
;
95 GetClientRect(vo_window
, &r
);
96 GetWindowRect(vo_window
, &r2
);
97 xborder
= (r2
.right
- r2
.left
) - (r
.right
- r
.left
);
98 yborder
= (r2
.bottom
- r2
.top
) - (r
.bottom
- r
.top
);
99 wpos
->cx
-= xborder
; wpos
->cy
-= yborder
;
100 aspect_fit(&wpos
->cx
, &wpos
->cy
, wpos
->cx
, wpos
->cy
);
101 wpos
->cx
+= xborder
; wpos
->cy
+= yborder
;
105 mplayer_put_key(KEY_CLOSE_WIN
);
110 case SC_MONITORPOWER
:
111 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: killing screensaver\n");
116 mpkey
= lookup_keymap_table(vk_map
, wParam
);
118 mplayer_put_key(mpkey
);
121 mplayer_put_key(wParam
);
124 if (!vo_nomouse_input
&& (vo_fs
|| (wParam
& MK_CONTROL
))) {
125 mplayer_put_key(MOUSE_BTN0
);
130 SendMessage(hWnd
, WM_NCLBUTTONDOWN
, HTCAPTION
, 0);
135 if (!vo_nomouse_input
)
136 mplayer_put_key(MOUSE_BTN1
);
139 if (!vo_nomouse_input
)
140 mplayer_put_key(MOUSE_BTN2
);
143 if (enable_mouse_movements
) {
145 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i",
146 GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
147 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str
));
151 if (!vo_nomouse_input
) {
152 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
154 mplayer_put_key(MOUSE_BTN3
);
156 mplayer_put_key(MOUSE_BTN4
);
161 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
165 * \brief Dispatch incoming window events and handle them.
167 * This function should be placed inside libvo's function "check_events".
169 * Global libvo variables changed:
170 * vo_dwidth: new window client area width
171 * vo_dheight: new window client area height
173 * \return int with these flags possibly set, take care to handle in the right order
174 * if it matters in your driver:
176 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
177 * driver render context accordingly.
178 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
179 * the window if the movie is paused.
181 int vo_w32_check_events(void) {
184 while (PeekMessage(&msg
, 0, 0, 0, PM_REMOVE
)) {
185 TranslateMessage(&msg
);
186 DispatchMessage(&msg
);
190 GetClientRect(vo_window
, &r
);
191 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
) {
192 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
193 event_flags
|= VO_EVENT_RESIZE
;
195 GetClientRect(WinID
, &r
);
196 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
)
197 MoveWindow(vo_window
, 0, 0, r
.right
, r
.bottom
, FALSE
);
203 static BOOL CALLBACK
mon_enum(HMONITOR hmon
, HDC hdc
, LPRECT r
, LPARAM p
) {
204 // this defaults to the last screen if specified number does not exist
205 xinerama_x
= r
->left
;
207 vo_screenwidth
= r
->right
- r
->left
;
208 vo_screenheight
= r
->bottom
- r
->top
;
209 if (mon_cnt
== xinerama_screen
)
216 * \brief Update screen information.
218 * This function should be called in libvo's "control" callback
219 * with parameter VOCTRL_UPDATE_SCREENINFO.
220 * Note that this also enables the new API where geometry and aspect
221 * calculations are done in video_out.c:config_video_out
223 * Global libvo variables changed:
229 void w32_update_xinerama_info(void) {
230 xinerama_x
= xinerama_y
= 0;
231 if (xinerama_screen
< -1) {
233 xinerama_x
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
234 xinerama_y
= GetSystemMetrics(SM_YVIRTUALSCREEN
);
235 tmp
= GetSystemMetrics(SM_CXVIRTUALSCREEN
);
236 if (tmp
) vo_screenwidth
= tmp
;
237 tmp
= GetSystemMetrics(SM_CYVIRTUALSCREEN
);
238 if (tmp
) vo_screenheight
= tmp
;
239 } else if (xinerama_screen
== -1 && myMonitorFromWindow
&& myGetMonitorInfo
) {
241 HMONITOR m
= myMonitorFromWindow(vo_window
, MONITOR_DEFAULTTOPRIMARY
);
242 mi
.cbSize
= sizeof(mi
);
243 myGetMonitorInfo(m
, &mi
);
244 xinerama_x
= mi
.rcMonitor
.left
;
245 xinerama_y
= mi
.rcMonitor
.top
;
246 vo_screenwidth
= mi
.rcMonitor
.right
- mi
.rcMonitor
.left
;
247 vo_screenheight
= mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
;
248 } else if (xinerama_screen
> 0 && myEnumDisplayMonitors
) {
250 myEnumDisplayMonitors(NULL
, NULL
, mon_enum
, 0);
252 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
255 static void updateScreenProperties(void) {
257 dm
.dmSize
= sizeof dm
;
258 dm
.dmDriverExtra
= 0;
259 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
260 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS
, &dm
)) {
261 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to enumerate display settings!\n");
265 vo_screenwidth
= dm
.dmPelsWidth
;
266 vo_screenheight
= dm
.dmPelsHeight
;
267 vo_depthonscreen
= dm
.dmBitsPerPel
;
268 w32_update_xinerama_info();
271 static void changeMode(void) {
273 dm
.dmSize
= sizeof dm
;
274 dm
.dmDriverExtra
= 0;
276 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
277 dm
.dmBitsPerPel
= vo_depthonscreen
;
278 dm
.dmPelsWidth
= vo_screenwidth
;
279 dm
.dmPelsHeight
= vo_screenheight
;
283 int bestScore
= INT_MAX
;
285 for (i
= 0; EnumDisplaySettings(0, i
, &dm
); ++i
) {
286 int score
= (dm
.dmPelsWidth
- o_dwidth
) * (dm
.dmPelsHeight
- o_dheight
);
287 if (dm
.dmBitsPerPel
!= vo_depthonscreen
) continue;
288 if (dm
.dmPelsWidth
< o_dwidth
) continue;
289 if (dm
.dmPelsHeight
< o_dheight
) continue;
291 if (score
< bestScore
) {
298 EnumDisplaySettings(0, bestMode
, &dm
);
300 ChangeDisplaySettings(&dm
, CDS_FULLSCREEN
);
304 static void resetMode(void) {
306 ChangeDisplaySettings(0, 0);
309 static int createRenderingContext(void) {
310 HWND layer
= HWND_NOTOPMOST
;
311 PIXELFORMATDESCRIPTOR pfd
;
312 HDC vo_hdc
= GetDC(vo_window
);
316 int style
= (vo_border
&& !vo_fs
) ?
317 (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
;
319 if (vo_fs
|| vo_ontop
) layer
= HWND_TOPMOST
;
322 while (ShowCursor(0) >= 0) /**/ ;
325 while (ShowCursor(1) < 0) /**/ ;
327 updateScreenProperties();
328 ShowWindow(vo_window
, SW_HIDE
);
329 SetWindowLong(vo_window
, GWL_STYLE
, style
);
331 prev_width
= vo_dwidth
;
332 prev_height
= vo_dheight
;
335 vo_dwidth
= vo_screenwidth
;
336 vo_dheight
= vo_screenheight
;
340 // make sure there are no "stale" resize events
341 // that would set vo_d* to wrong values
342 vo_w32_check_events();
343 vo_dwidth
= prev_width
;
344 vo_dheight
= prev_height
;
347 // HACK around what probably is a windows focus bug:
348 // when pressing 'f' on the console, then 'f' again to
349 // return to windowed mode, any input into the video
350 // window is lost forever.
354 r
.right
= r
.left
+ vo_dwidth
;
356 r
.bottom
= r
.top
+ vo_dheight
;
357 AdjustWindowRect(&r
, style
, 0);
358 SetWindowPos(vo_window
, layer
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_SHOWWINDOW
);
361 memset(&pfd
, 0, sizeof pfd
);
362 pfd
.nSize
= sizeof pfd
;
364 pfd
.dwFlags
= PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
;
365 pfd
.iPixelType
= PFD_TYPE_RGBA
;
367 pfd
.iLayerType
= PFD_MAIN_PLANE
;
368 pf
= ChoosePixelFormat(vo_hdc
, &pfd
);
370 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to select a valid pixel format!\n");
371 ReleaseDC(vo_window
, vo_hdc
);
375 SetPixelFormat(vo_hdc
, pf
, &pfd
);
377 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth
, vo_screenheight
, vo_depthonscreen
);
379 ReleaseDC(vo_window
, vo_hdc
);
384 * \brief Configure and show window on the screen.
386 * This function should be called in libvo's "config" callback.
387 * It configures a window and shows it on the screen.
389 * Global libvo variables changed:
393 * \return 1 - Success, 0 - Failure
395 int vo_w32_config(uint32_t width
, uint32_t height
, uint32_t flags
) {
396 // store original size for videomode switching
401 // the desired size is ignored in wid mode, it always matches the window size.
402 prev_width
= vo_dwidth
= width
;
403 prev_height
= vo_dheight
= height
;
408 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
409 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
410 return createRenderingContext();
414 * \brief Initialize w32_common framework.
416 * The first function that should be called from the w32_common framework.
417 * It handles window creation on the screen with proper title and attributes.
418 * It also initializes the framework's internal variables. The function should
419 * be called after your own preinit initialization and you shouldn't do any
420 * window management on your own.
422 * Global libvo variables changed:
428 * \return 1 = Success, 0 = Failure
430 int vo_w32_init(void) {
431 HICON mplayerIcon
= 0;
432 char exedir
[MAX_PATH
];
438 hInstance
= GetModuleHandle(0);
440 if (GetModuleFileName(0, exedir
, MAX_PATH
))
441 mplayerIcon
= ExtractIcon(hInstance
, exedir
, 0);
443 mplayerIcon
= LoadIcon(0, IDI_APPLICATION
);
446 WNDCLASSEX wcex
= { sizeof wcex
, CS_OWNDC
| CS_DBLCLKS
, WndProc
, 0, 0, hInstance
, mplayerIcon
, LoadCursor(0, IDC_ARROW
), NULL
, 0, classname
, mplayerIcon
};
448 if (!RegisterClassEx(&wcex
)) {
449 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to register window class!\n");
457 GetClientRect(WinID
, &r
);
458 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
459 vo_window
= CreateWindowEx(WS_EX_NOPARENTNOTIFY
, classname
, classname
,
460 WS_CHILD
| WS_VISIBLE
,
461 0, 0, vo_dwidth
, vo_dheight
, WinID
, 0, hInstance
, 0);
462 EnableWindow(vo_window
, 0);
464 vo_window
= CreateWindowEx(0, classname
, classname
,
465 vo_border
? (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
,
466 CW_USEDEFAULT
, 0, 100, 100, 0, 0, hInstance
, 0);
468 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to create window!\n");
472 myMonitorFromWindow
= NULL
;
473 myGetMonitorInfo
= NULL
;
474 myEnumDisplayMonitors
= NULL
;
475 user32
= GetModuleHandle("user32.dll");
477 myMonitorFromWindow
= (void *)GetProcAddress(user32
, "MonitorFromWindow");
478 myGetMonitorInfo
= GetProcAddress(user32
, "GetMonitorInfoA");
479 myEnumDisplayMonitors
= GetProcAddress(user32
, "EnumDisplayMonitors");
481 updateScreenProperties();
487 * \brief Toogle fullscreen / windowed mode.
489 * Should be called on VOCTRL_FULLSCREEN event. The window is
490 * always resized after this call, so the rendering context
491 * should be reinitialized with the new dimensions.
492 * It is unspecified if vo_check_events will create a resize
493 * event in addition or not.
495 * Global libvo variables changed:
501 void vo_w32_fullscreen(void) {
504 createRenderingContext();
508 * \brief Toogle window border attribute.
510 * Should be called on VOCTRL_BORDER event.
512 * Global libvo variables changed:
515 void vo_w32_border(void) {
516 vo_border
= !vo_border
;
517 createRenderingContext();
521 * \brief Toogle window ontop attribute.
523 * Should be called on VOCTRL_ONTOP event.
525 * Global libvo variables changed:
528 void vo_w32_ontop( void )
530 vo_ontop
= !vo_ontop
;
532 createRenderingContext();
537 * \brief Uninitialize w32_common framework.
539 * Should be called last in video driver's uninit function. First release
540 * anything built on top of the created window e.g. rendering context inside
541 * and call vo_w32_uninit at the end.
543 void vo_w32_uninit(void) {
544 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: uninit\n");
547 vo_depthonscreen
= 0;
548 DestroyWindow(vo_window
);
550 UnregisterClass(classname
, 0);