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 // To get "#define vo_ontop global_vo->opts->vo_ontop" etc
25 #include "old_vo_defines.h"
26 #include "input/keycodes.h"
27 #include "input/input.h"
29 #include "video_out.h"
31 #include "w32_common.h"
34 #ifndef WM_XBUTTONDOWN
35 # define WM_XBUTTONDOWN 0x020B
36 # define WM_XBUTTONUP 0x020C
37 # define WM_XBUTTONDBLCLK 0x020D
40 #ifndef MONITOR_DEFAULTTOPRIMARY
41 #define MONITOR_DEFAULTTOPRIMARY 1
44 static const char classname
[] = "MPlayer - The Movie Player";
47 static int depthonscreen
;
48 // last non-fullscreen extends
49 static int prev_width
;
50 static int prev_height
;
54 static uint32_t o_dwidth
;
55 static uint32_t o_dheight
;
57 static HINSTANCE hInstance
;
58 #define vo_window vo_w32_window
60 /** HDC used when rendering to a device instead of window */
62 static int event_flags
;
65 static HMONITOR (WINAPI
* myMonitorFromWindow
)(HWND
, DWORD
);
66 static BOOL (WINAPI
* myGetMonitorInfo
)(HMONITOR
, LPMONITORINFO
);
67 static BOOL (WINAPI
* myEnumDisplayMonitors
)(HDC
, LPCRECT
, MONITORENUMPROC
, LPARAM
);
69 static const struct mp_keymap vk_map
[] = {
71 {VK_ESCAPE
, KEY_ESC
}, {VK_BACK
, KEY_BS
}, {VK_TAB
, KEY_TAB
}, {VK_CONTROL
, KEY_CTRL
},
74 {VK_LEFT
, KEY_LEFT
}, {VK_UP
, KEY_UP
}, {VK_RIGHT
, KEY_RIGHT
}, {VK_DOWN
, KEY_DOWN
},
77 {VK_INSERT
, KEY_INSERT
}, {VK_DELETE
, KEY_DELETE
}, {VK_HOME
, KEY_HOME
}, {VK_END
, KEY_END
},
78 {VK_PRIOR
, KEY_PAGE_UP
}, {VK_NEXT
, KEY_PAGE_DOWN
},
81 {VK_F1
, KEY_F
+1}, {VK_F2
, KEY_F
+2}, {VK_F3
, KEY_F
+3}, {VK_F4
, KEY_F
+4},
82 {VK_F5
, KEY_F
+5}, {VK_F6
, KEY_F
+6}, {VK_F7
, KEY_F
+7}, {VK_F8
, KEY_F
+8},
83 {VK_F9
, KEY_F
+9}, {VK_F10
, KEY_F
+10}, {VK_F11
, KEY_F
+11}, {VK_F1
, KEY_F
+12},
85 {VK_NUMPAD0
, KEY_KP0
}, {VK_NUMPAD1
, KEY_KP1
}, {VK_NUMPAD2
, KEY_KP2
},
86 {VK_NUMPAD3
, KEY_KP3
}, {VK_NUMPAD4
, KEY_KP4
}, {VK_NUMPAD5
, KEY_KP5
},
87 {VK_NUMPAD6
, KEY_KP6
}, {VK_NUMPAD7
, KEY_KP7
}, {VK_NUMPAD8
, KEY_KP8
},
88 {VK_NUMPAD9
, KEY_KP9
}, {VK_DECIMAL
, KEY_KPDEC
},
93 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
98 case WM_ERASEBKGND
: // no need to erase background seperately
101 event_flags
|= VO_EVENT_EXPOSE
;
104 event_flags
|= VO_EVENT_MOVE
;
107 ClientToScreen(vo_window
, &p
);
112 event_flags
|= VO_EVENT_RESIZE
;
113 GetClientRect(vo_window
, &r
);
115 vo_dheight
= r
.bottom
;
117 case WM_WINDOWPOSCHANGING
:
118 if (vo_keepaspect
&& !vo_fs
&& WinID
< 0) {
119 WINDOWPOS
*wpos
= lParam
;
120 int xborder
, yborder
;
124 AdjustWindowRect(&r
, GetWindowLong(vo_window
, GWL_STYLE
), 0);
125 xborder
= (r
.right
- r
.left
) - wpos
->cx
;
126 yborder
= (r
.bottom
- r
.top
) - wpos
->cy
;
127 wpos
->cx
-= xborder
; wpos
->cy
-= yborder
;
128 aspect_fit(global_vo
, &wpos
->cx
, &wpos
->cy
, wpos
->cx
, wpos
->cy
);
129 wpos
->cx
+= xborder
; wpos
->cy
+= yborder
;
133 mplayer_put_key(KEY_CLOSE_WIN
);
138 case SC_MONITORPOWER
:
139 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: killing screensaver\n");
144 mpkey
= lookup_keymap_table(vk_map
, wParam
);
146 mplayer_put_key(mpkey
);
149 mplayer_put_key(wParam
);
152 if (!vo_nomouse_input
&& (vo_fs
|| (wParam
& MK_CONTROL
))) {
153 mplayer_put_key(MOUSE_BTN0
);
158 SendMessage(hWnd
, WM_NCLBUTTONDOWN
, HTCAPTION
, 0);
163 if (!vo_nomouse_input
)
164 mplayer_put_key(MOUSE_BTN1
);
167 if (!vo_nomouse_input
)
168 mplayer_put_key(MOUSE_BTN2
);
171 vo_mouse_movement(global_vo
, GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
174 if (!vo_nomouse_input
) {
175 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
177 mplayer_put_key(MOUSE_BTN3
);
179 mplayer_put_key(MOUSE_BTN4
);
183 if (!vo_nomouse_input
) {
184 int x
= HIWORD(wParam
);
186 mplayer_put_key(MOUSE_BTN5
);
188 mplayer_put_key(MOUSE_BTN6
);
193 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
197 * \brief Dispatch incoming window events and handle them.
199 * This function should be placed inside libvo's function "check_events".
201 * Global libvo variables changed:
202 * vo_dwidth: new window client area width
203 * vo_dheight: new window client area height
205 * \return int with these flags possibly set, take care to handle in the right order
206 * if it matters in your driver:
208 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
209 * driver render context accordingly.
210 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
211 * the window if the movie is paused.
213 int vo_w32_check_events(void) {
216 while (PeekMessage(&msg
, 0, 0, 0, PM_REMOVE
)) {
217 TranslateMessage(&msg
);
218 DispatchMessage(&msg
);
224 res
= GetClientRect(vo_window
, &r
);
225 if (res
&& (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
)) {
226 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
227 event_flags
|= VO_EVENT_RESIZE
;
230 ClientToScreen(vo_window
, &p
);
231 if (p
.x
!= vo_dx
|| p
.y
!= vo_dy
) {
232 vo_dx
= p
.x
; vo_dy
= p
.y
;
233 event_flags
|= VO_EVENT_MOVE
;
235 res
= GetClientRect(WinID
, &r
);
236 if (res
&& (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
))
237 MoveWindow(vo_window
, 0, 0, r
.right
, r
.bottom
, FALSE
);
238 if (!IsWindow(WinID
))
239 // Window has probably been closed, e.g. due to program crash
240 mplayer_put_key(KEY_CLOSE_WIN
);
246 static BOOL CALLBACK
mon_enum(HMONITOR hmon
, HDC hdc
, LPRECT r
, LPARAM p
) {
247 // this defaults to the last screen if specified number does not exist
248 xinerama_x
= r
->left
;
250 vo_screenwidth
= r
->right
- r
->left
;
251 vo_screenheight
= r
->bottom
- r
->top
;
252 if (mon_cnt
== xinerama_screen
)
259 * \brief Update screen information.
261 * This function should be called in libvo's "control" callback
262 * with parameter VOCTRL_UPDATE_SCREENINFO.
263 * Note that this also enables the new API where geometry and aspect
264 * calculations are done in video_out.c:config_video_out
266 * Global libvo variables changed:
272 void w32_update_xinerama_info(void) {
273 xinerama_x
= xinerama_y
= 0;
274 if (xinerama_screen
< -1) {
276 xinerama_x
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
277 xinerama_y
= GetSystemMetrics(SM_YVIRTUALSCREEN
);
278 tmp
= GetSystemMetrics(SM_CXVIRTUALSCREEN
);
279 if (tmp
) vo_screenwidth
= tmp
;
280 tmp
= GetSystemMetrics(SM_CYVIRTUALSCREEN
);
281 if (tmp
) vo_screenheight
= tmp
;
282 } else if (xinerama_screen
== -1 && myMonitorFromWindow
&& myGetMonitorInfo
) {
284 HMONITOR m
= myMonitorFromWindow(vo_window
, MONITOR_DEFAULTTOPRIMARY
);
285 mi
.cbSize
= sizeof(mi
);
286 myGetMonitorInfo(m
, &mi
);
287 xinerama_x
= mi
.rcMonitor
.left
;
288 xinerama_y
= mi
.rcMonitor
.top
;
289 vo_screenwidth
= mi
.rcMonitor
.right
- mi
.rcMonitor
.left
;
290 vo_screenheight
= mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
;
291 } else if (xinerama_screen
> 0 && myEnumDisplayMonitors
) {
293 myEnumDisplayMonitors(NULL
, NULL
, mon_enum
, 0);
295 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
298 static void updateScreenProperties(void) {
300 dm
.dmSize
= sizeof dm
;
301 dm
.dmDriverExtra
= 0;
302 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
303 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS
, &dm
)) {
304 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to enumerate display settings!\n");
308 vo_screenwidth
= dm
.dmPelsWidth
;
309 vo_screenheight
= dm
.dmPelsHeight
;
310 depthonscreen
= dm
.dmBitsPerPel
;
311 w32_update_xinerama_info();
314 static void changeMode(void) {
316 dm
.dmSize
= sizeof dm
;
317 dm
.dmDriverExtra
= 0;
319 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
320 dm
.dmBitsPerPel
= depthonscreen
;
321 dm
.dmPelsWidth
= vo_screenwidth
;
322 dm
.dmPelsHeight
= vo_screenheight
;
326 int bestScore
= INT_MAX
;
328 for (i
= 0; EnumDisplaySettings(0, i
, &dm
); ++i
) {
329 int score
= (dm
.dmPelsWidth
- o_dwidth
) * (dm
.dmPelsHeight
- o_dheight
);
330 if (dm
.dmBitsPerPel
!= depthonscreen
) continue;
331 if (dm
.dmPelsWidth
< o_dwidth
) continue;
332 if (dm
.dmPelsHeight
< o_dheight
) continue;
334 if (score
< bestScore
) {
341 EnumDisplaySettings(0, bestMode
, &dm
);
343 ChangeDisplaySettings(&dm
, CDS_FULLSCREEN
);
347 static void resetMode(void) {
349 ChangeDisplaySettings(0, 0);
352 static int createRenderingContext(void) {
353 HWND layer
= HWND_NOTOPMOST
;
355 int style
= (vo_border
&& !vo_fs
) ?
356 (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
;
361 if (vo_fs
|| vo_ontop
) layer
= HWND_TOPMOST
;
364 while (ShowCursor(0) >= 0) /**/ ;
367 while (ShowCursor(1) < 0) /**/ ;
369 updateScreenProperties();
370 ShowWindow(vo_window
, SW_HIDE
);
371 SetWindowLong(vo_window
, GWL_STYLE
, style
);
373 prev_width
= vo_dwidth
;
374 prev_height
= vo_dheight
;
377 vo_dwidth
= vo_screenwidth
;
378 vo_dheight
= vo_screenheight
;
382 // make sure there are no "stale" resize events
383 // that would set vo_d* to wrong values
384 vo_w32_check_events();
385 vo_dwidth
= prev_width
;
386 vo_dheight
= prev_height
;
389 // HACK around what probably is a windows focus bug:
390 // when pressing 'f' on the console, then 'f' again to
391 // return to windowed mode, any input into the video
392 // window is lost forever.
396 r
.right
= r
.left
+ vo_dwidth
;
398 r
.bottom
= r
.top
+ vo_dheight
;
399 AdjustWindowRect(&r
, style
, 0);
400 SetWindowPos(vo_window
, layer
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_SHOWWINDOW
);
405 * \brief Configure and show window on the screen.
407 * This function should be called in libvo's "config" callback.
408 * It configures a window and shows it on the screen.
410 * Global libvo variables changed:
414 * \return 1 - Success, 0 - Failure
416 int vo_w32_config(uint32_t width
, uint32_t height
, uint32_t flags
) {
417 PIXELFORMATDESCRIPTOR pfd
;
419 HDC vo_hdc
= vo_w32_get_dc(vo_window
);
421 memset(&pfd
, 0, sizeof pfd
);
422 pfd
.nSize
= sizeof pfd
;
424 pfd
.dwFlags
= PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
;
425 if (flags
& VOFLAG_STEREO
)
426 pfd
.dwFlags
|= PFD_STEREO
;
427 pfd
.iPixelType
= PFD_TYPE_RGBA
;
429 pfd
.iLayerType
= PFD_MAIN_PLANE
;
430 pf
= ChoosePixelFormat(vo_hdc
, &pfd
);
432 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to select a valid pixel format!\n");
433 vo_w32_release_dc(vo_window
, vo_hdc
);
437 SetPixelFormat(vo_hdc
, pf
, &pfd
);
438 vo_w32_release_dc(vo_window
, vo_hdc
);
440 // we already have a fully initialized window, so nothing needs to be done
441 if (flags
& VOFLAG_HIDDEN
)
443 // store original size for videomode switching
448 // the desired size is ignored in wid mode, it always matches the window size.
449 prev_width
= vo_dwidth
= width
;
450 prev_height
= vo_dheight
= height
;
455 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
456 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
457 return createRenderingContext();
461 * \brief return the name of the selected device if it is indepedant
462 * \return pointer to string, must be freed.
464 static char *get_display_name(void) {
466 disp
.cb
= sizeof(disp
);
467 EnumDisplayDevices(NULL
, vo_adapter_num
, &disp
, 0);
468 if (disp
.StateFlags
& DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
)
470 return strdup(disp
.DeviceName
);
474 * \brief Initialize w32_common framework.
476 * The first function that should be called from the w32_common framework.
477 * It handles window creation on the screen with proper title and attributes.
478 * It also initializes the framework's internal variables. The function should
479 * be called after your own preinit initialization and you shouldn't do any
480 * window management on your own.
482 * Global libvo variables changed:
487 * \return 1 = Success, 0 = Failure
489 int vo_w32_init(void) {
490 HICON mplayerIcon
= 0;
491 char exedir
[MAX_PATH
];
498 hInstance
= GetModuleHandle(0);
500 if (GetModuleFileName(0, exedir
, MAX_PATH
))
501 mplayerIcon
= ExtractIcon(hInstance
, exedir
, 0);
503 mplayerIcon
= LoadIcon(0, IDI_APPLICATION
);
506 WNDCLASSEX wcex
= { sizeof wcex
, CS_OWNDC
| CS_DBLCLKS
, WndProc
, 0, 0, hInstance
, mplayerIcon
, LoadCursor(0, IDC_ARROW
), NULL
, 0, classname
, mplayerIcon
};
508 if (!RegisterClassEx(&wcex
)) {
509 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to register window class!\n");
517 GetClientRect(WinID
, &r
);
518 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
519 vo_window
= CreateWindowEx(WS_EX_NOPARENTNOTIFY
, classname
, classname
,
520 WS_CHILD
| WS_VISIBLE
,
521 0, 0, vo_dwidth
, vo_dheight
, WinID
, 0, hInstance
, 0);
522 EnableWindow(vo_window
, 0);
524 vo_window
= CreateWindowEx(0, classname
, classname
,
525 vo_border
? (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
,
526 CW_USEDEFAULT
, 0, 100, 100, 0, 0, hInstance
, 0);
528 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to create window!\n");
532 myMonitorFromWindow
= NULL
;
533 myGetMonitorInfo
= NULL
;
534 myEnumDisplayMonitors
= NULL
;
535 user32
= GetModuleHandle("user32.dll");
537 myMonitorFromWindow
= (void *)GetProcAddress(user32
, "MonitorFromWindow");
538 myGetMonitorInfo
= GetProcAddress(user32
, "GetMonitorInfoA");
539 myEnumDisplayMonitors
= GetProcAddress(user32
, "EnumDisplayMonitors");
542 dev
= get_display_name();
543 if (dev
) dev_hdc
= CreateDC(dev
, NULL
, NULL
, NULL
);
545 updateScreenProperties();
547 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth
, vo_screenheight
, depthonscreen
);
553 * \brief Toogle fullscreen / windowed mode.
555 * Should be called on VOCTRL_FULLSCREEN event. The window is
556 * always resized after this call, so the rendering context
557 * should be reinitialized with the new dimensions.
558 * It is unspecified if vo_check_events will create a resize
559 * event in addition or not.
561 * Global libvo variables changed:
567 void vo_w32_fullscreen(void) {
570 createRenderingContext();
574 * \brief Toogle window border attribute.
576 * Should be called on VOCTRL_BORDER event.
578 * Global libvo variables changed:
581 void vo_w32_border(void) {
582 vo_border
= !vo_border
;
583 createRenderingContext();
587 * \brief Toogle window ontop attribute.
589 * Should be called on VOCTRL_ONTOP event.
591 * Global libvo variables changed:
594 void vo_w32_ontop( void )
596 vo_ontop
= !vo_ontop
;
598 createRenderingContext();
603 * \brief Uninitialize w32_common framework.
605 * Should be called last in video driver's uninit function. First release
606 * anything built on top of the created window e.g. rendering context inside
607 * and call vo_w32_uninit at the end.
609 void vo_w32_uninit(void) {
610 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: uninit\n");
614 if (dev_hdc
) DeleteDC(dev_hdc
);
616 DestroyWindow(vo_window
);
618 UnregisterClass(classname
, 0);
622 * \brief get a device context to draw in
624 * \param wnd window the DC should belong to if it makes sense
626 HDC
vo_w32_get_dc(HWND wnd
) {
627 if (dev_hdc
) return dev_hdc
;
632 * \brief release a device context
634 * \param wnd window the DC probably belongs to
636 void vo_w32_release_dc(HWND wnd
, HDC dc
) {