2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "osdep/keycodes.h"
25 #include "input/input.h"
26 #include "input/mouse.h"
28 #include "video_out.h"
30 #include "w32_common.h"
33 extern int enable_mouse_movements
;
35 #ifndef MONITOR_DEFAULTTOPRIMARY
36 #define MONITOR_DEFAULTTOPRIMARY 1
39 static const char classname
[] = "MPlayer - Media player for Win32";
42 // last non-fullscreen extends
43 static int prev_width
;
44 static int prev_height
;
48 static uint32_t o_dwidth
;
49 static uint32_t o_dheight
;
51 static HINSTANCE hInstance
;
52 #define vo_window vo_w32_window
54 static int event_flags
;
57 static HMONITOR (WINAPI
* myMonitorFromWindow
)(HWND
, DWORD
);
58 static BOOL (WINAPI
* myGetMonitorInfo
)(HMONITOR
, LPMONITORINFO
);
59 static BOOL (WINAPI
* myEnumDisplayMonitors
)(HDC
, LPCRECT
, MONITORENUMPROC
, LPARAM
);
61 static const struct keymap vk_map
[] = {
63 {VK_ESCAPE
, KEY_ESC
}, {VK_BACK
, KEY_BS
}, {VK_TAB
, KEY_TAB
}, {VK_CONTROL
, KEY_CTRL
},
66 {VK_LEFT
, KEY_LEFT
}, {VK_UP
, KEY_UP
}, {VK_RIGHT
, KEY_RIGHT
}, {VK_DOWN
, KEY_DOWN
},
69 {VK_INSERT
, KEY_INSERT
}, {VK_DELETE
, KEY_DELETE
}, {VK_HOME
, KEY_HOME
}, {VK_END
, KEY_END
},
70 {VK_PRIOR
, KEY_PAGE_UP
}, {VK_NEXT
, KEY_PAGE_DOWN
},
73 {VK_F1
, KEY_F
+1}, {VK_F2
, KEY_F
+2}, {VK_F3
, KEY_F
+3}, {VK_F4
, KEY_F
+4},
74 {VK_F5
, KEY_F
+5}, {VK_F6
, KEY_F
+6}, {VK_F7
, KEY_F
+7}, {VK_F8
, KEY_F
+8},
75 {VK_F9
, KEY_F
+9}, {VK_F10
, KEY_F
+10}, {VK_F11
, KEY_F
+11}, {VK_F1
, KEY_F
+12},
77 {VK_NUMPAD0
, KEY_KP0
}, {VK_NUMPAD1
, KEY_KP1
}, {VK_NUMPAD2
, KEY_KP2
},
78 {VK_NUMPAD3
, KEY_KP3
}, {VK_NUMPAD4
, KEY_KP4
}, {VK_NUMPAD5
, KEY_KP5
},
79 {VK_NUMPAD6
, KEY_KP6
}, {VK_NUMPAD7
, KEY_KP7
}, {VK_NUMPAD8
, KEY_KP8
},
80 {VK_NUMPAD9
, KEY_KP9
}, {VK_DECIMAL
, KEY_KPDEC
},
85 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
90 case WM_ERASEBKGND
: // no need to erase background seperately
93 event_flags
|= VO_EVENT_EXPOSE
;
98 ClientToScreen(vo_window
, &p
);
103 event_flags
|= VO_EVENT_RESIZE
;
104 GetClientRect(vo_window
, &r
);
106 vo_dheight
= r
.bottom
;
108 case WM_WINDOWPOSCHANGING
:
109 if (vo_keepaspect
&& !vo_fs
) {
110 WINDOWPOS
*wpos
= lParam
;
111 int xborder
, yborder
;
113 GetClientRect(vo_window
, &r
);
114 GetWindowRect(vo_window
, &r2
);
115 xborder
= (r2
.right
- r2
.left
) - (r
.right
- r
.left
);
116 yborder
= (r2
.bottom
- r2
.top
) - (r
.bottom
- r
.top
);
117 wpos
->cx
-= xborder
; wpos
->cy
-= yborder
;
118 aspect_fit(&wpos
->cx
, &wpos
->cy
, wpos
->cx
, wpos
->cy
);
119 wpos
->cx
+= xborder
; wpos
->cy
+= yborder
;
123 mplayer_put_key(KEY_CLOSE_WIN
);
128 case SC_MONITORPOWER
:
129 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: killing screensaver\n");
134 mpkey
= lookup_keymap_table(vk_map
, wParam
);
136 mplayer_put_key(mpkey
);
139 mplayer_put_key(wParam
);
142 if (!vo_nomouse_input
&& (vo_fs
|| (wParam
& MK_CONTROL
))) {
143 mplayer_put_key(MOUSE_BTN0
);
148 SendMessage(hWnd
, WM_NCLBUTTONDOWN
, HTCAPTION
, 0);
153 if (!vo_nomouse_input
)
154 mplayer_put_key(MOUSE_BTN1
);
157 if (!vo_nomouse_input
)
158 mplayer_put_key(MOUSE_BTN2
);
161 if (enable_mouse_movements
) {
163 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i",
164 GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
165 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str
));
169 if (!vo_nomouse_input
) {
170 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
172 mplayer_put_key(MOUSE_BTN3
);
174 mplayer_put_key(MOUSE_BTN4
);
179 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
183 * \brief Dispatch incoming window events and handle them.
185 * This function should be placed inside libvo's function "check_events".
187 * Global libvo variables changed:
188 * vo_dwidth: new window client area width
189 * vo_dheight: new window client area height
191 * \return int with these flags possibly set, take care to handle in the right order
192 * if it matters in your driver:
194 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
195 * driver render context accordingly.
196 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
197 * the window if the movie is paused.
199 int vo_w32_check_events(void) {
202 while (PeekMessage(&msg
, 0, 0, 0, PM_REMOVE
)) {
203 TranslateMessage(&msg
);
204 DispatchMessage(&msg
);
208 GetClientRect(vo_window
, &r
);
209 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
) {
210 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
211 event_flags
|= VO_EVENT_RESIZE
;
213 GetClientRect(WinID
, &r
);
214 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
)
215 MoveWindow(vo_window
, 0, 0, r
.right
, r
.bottom
, FALSE
);
221 static BOOL CALLBACK
mon_enum(HMONITOR hmon
, HDC hdc
, LPRECT r
, LPARAM p
) {
222 // this defaults to the last screen if specified number does not exist
223 xinerama_x
= r
->left
;
225 vo_screenwidth
= r
->right
- r
->left
;
226 vo_screenheight
= r
->bottom
- r
->top
;
227 if (mon_cnt
== xinerama_screen
)
234 * \brief Update screen information.
236 * This function should be called in libvo's "control" callback
237 * with parameter VOCTRL_UPDATE_SCREENINFO.
238 * Note that this also enables the new API where geometry and aspect
239 * calculations are done in video_out.c:config_video_out
241 * Global libvo variables changed:
247 void w32_update_xinerama_info(void) {
248 xinerama_x
= xinerama_y
= 0;
249 if (xinerama_screen
< -1) {
251 xinerama_x
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
252 xinerama_y
= GetSystemMetrics(SM_YVIRTUALSCREEN
);
253 tmp
= GetSystemMetrics(SM_CXVIRTUALSCREEN
);
254 if (tmp
) vo_screenwidth
= tmp
;
255 tmp
= GetSystemMetrics(SM_CYVIRTUALSCREEN
);
256 if (tmp
) vo_screenheight
= tmp
;
257 } else if (xinerama_screen
== -1 && myMonitorFromWindow
&& myGetMonitorInfo
) {
259 HMONITOR m
= myMonitorFromWindow(vo_window
, MONITOR_DEFAULTTOPRIMARY
);
260 mi
.cbSize
= sizeof(mi
);
261 myGetMonitorInfo(m
, &mi
);
262 xinerama_x
= mi
.rcMonitor
.left
;
263 xinerama_y
= mi
.rcMonitor
.top
;
264 vo_screenwidth
= mi
.rcMonitor
.right
- mi
.rcMonitor
.left
;
265 vo_screenheight
= mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
;
266 } else if (xinerama_screen
> 0 && myEnumDisplayMonitors
) {
268 myEnumDisplayMonitors(NULL
, NULL
, mon_enum
, 0);
270 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
273 static void updateScreenProperties(void) {
275 dm
.dmSize
= sizeof dm
;
276 dm
.dmDriverExtra
= 0;
277 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
278 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS
, &dm
)) {
279 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to enumerate display settings!\n");
283 vo_screenwidth
= dm
.dmPelsWidth
;
284 vo_screenheight
= dm
.dmPelsHeight
;
285 vo_depthonscreen
= dm
.dmBitsPerPel
;
286 w32_update_xinerama_info();
289 static void changeMode(void) {
291 dm
.dmSize
= sizeof dm
;
292 dm
.dmDriverExtra
= 0;
294 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
295 dm
.dmBitsPerPel
= vo_depthonscreen
;
296 dm
.dmPelsWidth
= vo_screenwidth
;
297 dm
.dmPelsHeight
= vo_screenheight
;
301 int bestScore
= INT_MAX
;
303 for (i
= 0; EnumDisplaySettings(0, i
, &dm
); ++i
) {
304 int score
= (dm
.dmPelsWidth
- o_dwidth
) * (dm
.dmPelsHeight
- o_dheight
);
305 if (dm
.dmBitsPerPel
!= vo_depthonscreen
) continue;
306 if (dm
.dmPelsWidth
< o_dwidth
) continue;
307 if (dm
.dmPelsHeight
< o_dheight
) continue;
309 if (score
< bestScore
) {
316 EnumDisplaySettings(0, bestMode
, &dm
);
318 ChangeDisplaySettings(&dm
, CDS_FULLSCREEN
);
322 static void resetMode(void) {
324 ChangeDisplaySettings(0, 0);
327 static int createRenderingContext(void) {
328 HWND layer
= HWND_NOTOPMOST
;
329 PIXELFORMATDESCRIPTOR pfd
;
330 HDC vo_hdc
= GetDC(vo_window
);
334 int style
= (vo_border
&& !vo_fs
) ?
335 (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
;
337 if (vo_fs
|| vo_ontop
) layer
= HWND_TOPMOST
;
340 while (ShowCursor(0) >= 0) /**/ ;
343 while (ShowCursor(1) < 0) /**/ ;
345 updateScreenProperties();
346 ShowWindow(vo_window
, SW_HIDE
);
347 SetWindowLong(vo_window
, GWL_STYLE
, style
);
349 prev_width
= vo_dwidth
;
350 prev_height
= vo_dheight
;
353 vo_dwidth
= vo_screenwidth
;
354 vo_dheight
= vo_screenheight
;
358 // make sure there are no "stale" resize events
359 // that would set vo_d* to wrong values
360 vo_w32_check_events();
361 vo_dwidth
= prev_width
;
362 vo_dheight
= prev_height
;
365 // HACK around what probably is a windows focus bug:
366 // when pressing 'f' on the console, then 'f' again to
367 // return to windowed mode, any input into the video
368 // window is lost forever.
372 r
.right
= r
.left
+ vo_dwidth
;
374 r
.bottom
= r
.top
+ vo_dheight
;
375 AdjustWindowRect(&r
, style
, 0);
376 SetWindowPos(vo_window
, layer
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_SHOWWINDOW
);
379 memset(&pfd
, 0, sizeof pfd
);
380 pfd
.nSize
= sizeof pfd
;
382 pfd
.dwFlags
= PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
;
383 pfd
.iPixelType
= PFD_TYPE_RGBA
;
385 pfd
.iLayerType
= PFD_MAIN_PLANE
;
386 pf
= ChoosePixelFormat(vo_hdc
, &pfd
);
388 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to select a valid pixel format!\n");
389 ReleaseDC(vo_window
, vo_hdc
);
393 SetPixelFormat(vo_hdc
, pf
, &pfd
);
395 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth
, vo_screenheight
, vo_depthonscreen
);
397 ReleaseDC(vo_window
, vo_hdc
);
402 * \brief Configure and show window on the screen.
404 * This function should be called in libvo's "config" callback.
405 * It configures a window and shows it on the screen.
407 * Global libvo variables changed:
411 * \return 1 - Success, 0 - Failure
413 int vo_w32_config(uint32_t width
, uint32_t height
, uint32_t flags
) {
414 // store original size for videomode switching
419 // the desired size is ignored in wid mode, it always matches the window size.
420 prev_width
= vo_dwidth
= width
;
421 prev_height
= vo_dheight
= height
;
426 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
427 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
428 return createRenderingContext();
432 * \brief Initialize w32_common framework.
434 * The first function that should be called from the w32_common framework.
435 * It handles window creation on the screen with proper title and attributes.
436 * It also initializes the framework's internal variables. The function should
437 * be called after your own preinit initialization and you shouldn't do any
438 * window management on your own.
440 * Global libvo variables changed:
446 * \return 1 = Success, 0 = Failure
448 int vo_w32_init(void) {
449 HICON mplayerIcon
= 0;
450 char exedir
[MAX_PATH
];
456 hInstance
= GetModuleHandle(0);
458 if (GetModuleFileName(0, exedir
, MAX_PATH
))
459 mplayerIcon
= ExtractIcon(hInstance
, exedir
, 0);
461 mplayerIcon
= LoadIcon(0, IDI_APPLICATION
);
464 WNDCLASSEX wcex
= { sizeof wcex
, CS_OWNDC
| CS_DBLCLKS
, WndProc
, 0, 0, hInstance
, mplayerIcon
, LoadCursor(0, IDC_ARROW
), NULL
, 0, classname
, mplayerIcon
};
466 if (!RegisterClassEx(&wcex
)) {
467 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to register window class!\n");
475 GetClientRect(WinID
, &r
);
476 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
477 vo_window
= CreateWindowEx(WS_EX_NOPARENTNOTIFY
, classname
, classname
,
478 WS_CHILD
| WS_VISIBLE
,
479 0, 0, vo_dwidth
, vo_dheight
, WinID
, 0, hInstance
, 0);
480 EnableWindow(vo_window
, 0);
482 vo_window
= CreateWindowEx(0, classname
, classname
,
483 vo_border
? (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
,
484 CW_USEDEFAULT
, 0, 100, 100, 0, 0, hInstance
, 0);
486 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to create window!\n");
490 myMonitorFromWindow
= NULL
;
491 myGetMonitorInfo
= NULL
;
492 myEnumDisplayMonitors
= NULL
;
493 user32
= GetModuleHandle("user32.dll");
495 myMonitorFromWindow
= (void *)GetProcAddress(user32
, "MonitorFromWindow");
496 myGetMonitorInfo
= GetProcAddress(user32
, "GetMonitorInfoA");
497 myEnumDisplayMonitors
= GetProcAddress(user32
, "EnumDisplayMonitors");
499 updateScreenProperties();
505 * \brief Toogle fullscreen / windowed mode.
507 * Should be called on VOCTRL_FULLSCREEN event. The window is
508 * always resized after this call, so the rendering context
509 * should be reinitialized with the new dimensions.
510 * It is unspecified if vo_check_events will create a resize
511 * event in addition or not.
513 * Global libvo variables changed:
519 void vo_w32_fullscreen(void) {
522 createRenderingContext();
526 * \brief Toogle window border attribute.
528 * Should be called on VOCTRL_BORDER event.
530 * Global libvo variables changed:
533 void vo_w32_border(void) {
534 vo_border
= !vo_border
;
535 createRenderingContext();
539 * \brief Toogle window ontop attribute.
541 * Should be called on VOCTRL_ONTOP event.
543 * Global libvo variables changed:
546 void vo_w32_ontop( void )
548 vo_ontop
= !vo_ontop
;
550 createRenderingContext();
555 * \brief Uninitialize w32_common framework.
557 * Should be called last in video driver's uninit function. First release
558 * anything built on top of the created window e.g. rendering context inside
559 * and call vo_w32_uninit at the end.
561 void vo_w32_uninit(void) {
562 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: uninit\n");
565 vo_depthonscreen
= 0;
566 DestroyWindow(vo_window
);
568 UnregisterClass(classname
, 0);