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 "osdep/keycodes.h"
27 #include "osdep/resource.h"
28 #include "input/input.h"
29 #include "input/mouse.h"
31 #include "video_out.h"
33 #include "w32_common.h"
36 extern int enable_mouse_movements
;
38 #ifndef MONITOR_DEFAULTTOPRIMARY
39 #define MONITOR_DEFAULTTOPRIMARY 1
42 static const char classname
[] = "MPlayer - The Movie Player";
45 static int depthonscreen
;
46 // last non-fullscreen extends
47 static int prev_width
;
48 static int prev_height
;
52 static uint32_t o_dwidth
;
53 static uint32_t o_dheight
;
55 static HINSTANCE hInstance
;
56 #define vo_window vo_w32_window
58 /** HDC used when rendering to a device instead of window */
60 static int event_flags
;
63 static HMONITOR (WINAPI
* myMonitorFromWindow
)(HWND
, DWORD
);
64 static BOOL (WINAPI
* myGetMonitorInfo
)(HMONITOR
, LPMONITORINFO
);
65 static BOOL (WINAPI
* myEnumDisplayMonitors
)(HDC
, LPCRECT
, MONITORENUMPROC
, LPARAM
);
67 static const struct keymap vk_map
[] = {
69 {VK_ESCAPE
, KEY_ESC
}, {VK_BACK
, KEY_BS
}, {VK_TAB
, KEY_TAB
}, {VK_CONTROL
, KEY_CTRL
},
72 {VK_LEFT
, KEY_LEFT
}, {VK_UP
, KEY_UP
}, {VK_RIGHT
, KEY_RIGHT
}, {VK_DOWN
, KEY_DOWN
},
75 {VK_INSERT
, KEY_INSERT
}, {VK_DELETE
, KEY_DELETE
}, {VK_HOME
, KEY_HOME
}, {VK_END
, KEY_END
},
76 {VK_PRIOR
, KEY_PAGE_UP
}, {VK_NEXT
, KEY_PAGE_DOWN
},
79 {VK_F1
, KEY_F
+1}, {VK_F2
, KEY_F
+2}, {VK_F3
, KEY_F
+3}, {VK_F4
, KEY_F
+4},
80 {VK_F5
, KEY_F
+5}, {VK_F6
, KEY_F
+6}, {VK_F7
, KEY_F
+7}, {VK_F8
, KEY_F
+8},
81 {VK_F9
, KEY_F
+9}, {VK_F10
, KEY_F
+10}, {VK_F11
, KEY_F
+11}, {VK_F1
, KEY_F
+12},
83 {VK_NUMPAD0
, KEY_KP0
}, {VK_NUMPAD1
, KEY_KP1
}, {VK_NUMPAD2
, KEY_KP2
},
84 {VK_NUMPAD3
, KEY_KP3
}, {VK_NUMPAD4
, KEY_KP4
}, {VK_NUMPAD5
, KEY_KP5
},
85 {VK_NUMPAD6
, KEY_KP6
}, {VK_NUMPAD7
, KEY_KP7
}, {VK_NUMPAD8
, KEY_KP8
},
86 {VK_NUMPAD9
, KEY_KP9
}, {VK_DECIMAL
, KEY_KPDEC
},
91 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
96 case WM_ERASEBKGND
: // no need to erase background seperately
99 event_flags
|= VO_EVENT_EXPOSE
;
104 ClientToScreen(vo_window
, &p
);
109 event_flags
|= VO_EVENT_RESIZE
;
110 GetClientRect(vo_window
, &r
);
112 vo_dheight
= r
.bottom
;
114 case WM_WINDOWPOSCHANGING
:
115 if (vo_keepaspect
&& !vo_fs
&& WinID
< 0) {
116 WINDOWPOS
*wpos
= lParam
;
117 int xborder
, yborder
;
121 AdjustWindowRect(&r
, GetWindowLong(vo_window
, GWL_STYLE
), 0);
122 xborder
= (r
.right
- r
.left
) - wpos
->cx
;
123 yborder
= (r
.bottom
- r
.top
) - wpos
->cy
;
124 wpos
->cx
-= xborder
; wpos
->cy
-= yborder
;
125 aspect_fit(global_vo
, &wpos
->cx
, &wpos
->cy
, wpos
->cx
, wpos
->cy
);
126 wpos
->cx
+= xborder
; wpos
->cy
+= yborder
;
130 mplayer_put_key(KEY_CLOSE_WIN
);
135 case SC_MONITORPOWER
:
136 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: killing screensaver\n");
141 mpkey
= lookup_keymap_table(vk_map
, wParam
);
143 mplayer_put_key(mpkey
);
146 mplayer_put_key(wParam
);
149 if (!vo_nomouse_input
&& (vo_fs
|| (wParam
& MK_CONTROL
))) {
150 mplayer_put_key(MOUSE_BTN0
);
155 SendMessage(hWnd
, WM_NCLBUTTONDOWN
, HTCAPTION
, 0);
160 if (!vo_nomouse_input
)
161 mplayer_put_key(MOUSE_BTN1
);
164 if (!vo_nomouse_input
)
165 mplayer_put_key(MOUSE_BTN2
);
168 if (enable_mouse_movements
) {
170 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i",
171 GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
172 mp_input_queue_cmd(global_vo
->input_ctx
, mp_input_parse_cmd(cmd_str
));
176 if (!vo_nomouse_input
) {
177 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
179 mplayer_put_key(MOUSE_BTN3
);
181 mplayer_put_key(MOUSE_BTN4
);
186 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
190 * \brief Dispatch incoming window events and handle them.
192 * This function should be placed inside libvo's function "check_events".
194 * Global libvo variables changed:
195 * vo_dwidth: new window client area width
196 * vo_dheight: new window client area height
198 * \return int with these flags possibly set, take care to handle in the right order
199 * if it matters in your driver:
201 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
202 * driver render context accordingly.
203 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
204 * the window if the movie is paused.
206 int vo_w32_check_events(void) {
209 while (PeekMessage(&msg
, 0, 0, 0, PM_REMOVE
)) {
210 TranslateMessage(&msg
);
211 DispatchMessage(&msg
);
215 GetClientRect(vo_window
, &r
);
216 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
) {
217 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
218 event_flags
|= VO_EVENT_RESIZE
;
220 GetClientRect(WinID
, &r
);
221 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
)
222 MoveWindow(vo_window
, 0, 0, r
.right
, r
.bottom
, FALSE
);
228 static BOOL CALLBACK
mon_enum(HMONITOR hmon
, HDC hdc
, LPRECT r
, LPARAM p
) {
229 // this defaults to the last screen if specified number does not exist
230 xinerama_x
= r
->left
;
232 vo_screenwidth
= r
->right
- r
->left
;
233 vo_screenheight
= r
->bottom
- r
->top
;
234 if (mon_cnt
== xinerama_screen
)
241 * \brief Update screen information.
243 * This function should be called in libvo's "control" callback
244 * with parameter VOCTRL_UPDATE_SCREENINFO.
245 * Note that this also enables the new API where geometry and aspect
246 * calculations are done in video_out.c:config_video_out
248 * Global libvo variables changed:
254 void w32_update_xinerama_info(void) {
255 xinerama_x
= xinerama_y
= 0;
256 if (xinerama_screen
< -1) {
258 xinerama_x
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
259 xinerama_y
= GetSystemMetrics(SM_YVIRTUALSCREEN
);
260 tmp
= GetSystemMetrics(SM_CXVIRTUALSCREEN
);
261 if (tmp
) vo_screenwidth
= tmp
;
262 tmp
= GetSystemMetrics(SM_CYVIRTUALSCREEN
);
263 if (tmp
) vo_screenheight
= tmp
;
264 } else if (xinerama_screen
== -1 && myMonitorFromWindow
&& myGetMonitorInfo
) {
266 HMONITOR m
= myMonitorFromWindow(vo_window
, MONITOR_DEFAULTTOPRIMARY
);
267 mi
.cbSize
= sizeof(mi
);
268 myGetMonitorInfo(m
, &mi
);
269 xinerama_x
= mi
.rcMonitor
.left
;
270 xinerama_y
= mi
.rcMonitor
.top
;
271 vo_screenwidth
= mi
.rcMonitor
.right
- mi
.rcMonitor
.left
;
272 vo_screenheight
= mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
;
273 } else if (xinerama_screen
> 0 && myEnumDisplayMonitors
) {
275 myEnumDisplayMonitors(NULL
, NULL
, mon_enum
, 0);
277 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
280 static void updateScreenProperties(void) {
282 dm
.dmSize
= sizeof dm
;
283 dm
.dmDriverExtra
= 0;
284 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
285 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS
, &dm
)) {
286 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to enumerate display settings!\n");
290 vo_screenwidth
= dm
.dmPelsWidth
;
291 vo_screenheight
= dm
.dmPelsHeight
;
292 depthonscreen
= dm
.dmBitsPerPel
;
293 w32_update_xinerama_info();
296 static void changeMode(void) {
298 dm
.dmSize
= sizeof dm
;
299 dm
.dmDriverExtra
= 0;
301 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
302 dm
.dmBitsPerPel
= depthonscreen
;
303 dm
.dmPelsWidth
= vo_screenwidth
;
304 dm
.dmPelsHeight
= vo_screenheight
;
308 int bestScore
= INT_MAX
;
310 for (i
= 0; EnumDisplaySettings(0, i
, &dm
); ++i
) {
311 int score
= (dm
.dmPelsWidth
- o_dwidth
) * (dm
.dmPelsHeight
- o_dheight
);
312 if (dm
.dmBitsPerPel
!= depthonscreen
) continue;
313 if (dm
.dmPelsWidth
< o_dwidth
) continue;
314 if (dm
.dmPelsHeight
< o_dheight
) continue;
316 if (score
< bestScore
) {
323 EnumDisplaySettings(0, bestMode
, &dm
);
325 ChangeDisplaySettings(&dm
, CDS_FULLSCREEN
);
329 static void resetMode(void) {
331 ChangeDisplaySettings(0, 0);
334 static int createRenderingContext(void) {
335 HWND layer
= HWND_NOTOPMOST
;
336 PIXELFORMATDESCRIPTOR pfd
;
337 HDC vo_hdc
= vo_w32_get_dc(vo_window
);
341 int style
= (vo_border
&& !vo_fs
) ?
342 (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
;
344 if (vo_fs
|| vo_ontop
) layer
= HWND_TOPMOST
;
347 while (ShowCursor(0) >= 0) /**/ ;
350 while (ShowCursor(1) < 0) /**/ ;
352 updateScreenProperties();
353 ShowWindow(vo_window
, SW_HIDE
);
354 SetWindowLong(vo_window
, GWL_STYLE
, style
);
356 prev_width
= vo_dwidth
;
357 prev_height
= vo_dheight
;
360 vo_dwidth
= vo_screenwidth
;
361 vo_dheight
= vo_screenheight
;
365 // make sure there are no "stale" resize events
366 // that would set vo_d* to wrong values
367 vo_w32_check_events();
368 vo_dwidth
= prev_width
;
369 vo_dheight
= prev_height
;
372 // HACK around what probably is a windows focus bug:
373 // when pressing 'f' on the console, then 'f' again to
374 // return to windowed mode, any input into the video
375 // window is lost forever.
379 r
.right
= r
.left
+ vo_dwidth
;
381 r
.bottom
= r
.top
+ vo_dheight
;
382 AdjustWindowRect(&r
, style
, 0);
383 SetWindowPos(vo_window
, layer
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_SHOWWINDOW
);
386 memset(&pfd
, 0, sizeof pfd
);
387 pfd
.nSize
= sizeof pfd
;
389 pfd
.dwFlags
= PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
;
390 pfd
.iPixelType
= PFD_TYPE_RGBA
;
392 pfd
.iLayerType
= PFD_MAIN_PLANE
;
393 pf
= ChoosePixelFormat(vo_hdc
, &pfd
);
395 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to select a valid pixel format!\n");
396 ReleaseDC(vo_window
, vo_hdc
);
400 SetPixelFormat(vo_hdc
, pf
, &pfd
);
402 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth
, vo_screenheight
, depthonscreen
);
404 vo_w32_release_dc(vo_window
, vo_hdc
);
409 * \brief Configure and show window on the screen.
411 * This function should be called in libvo's "config" callback.
412 * It configures a window and shows it on the screen.
414 * Global libvo variables changed:
418 * \return 1 - Success, 0 - Failure
420 int vo_w32_config(uint32_t width
, uint32_t height
, uint32_t flags
) {
421 // store original size for videomode switching
426 // the desired size is ignored in wid mode, it always matches the window size.
427 prev_width
= vo_dwidth
= width
;
428 prev_height
= vo_dheight
= height
;
433 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
434 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
435 return createRenderingContext();
439 * \brief return the name of the selected device if it is indepedant
441 static char *get_display_name(void) {
443 disp
.cb
= sizeof(disp
);
444 EnumDisplayDevices(NULL
, vo_adapter_num
, &disp
, 0);
445 if (disp
.StateFlags
& DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
)
447 return disp
.DeviceName
;
451 * \brief Initialize w32_common framework.
453 * The first function that should be called from the w32_common framework.
454 * It handles window creation on the screen with proper title and attributes.
455 * It also initializes the framework's internal variables. The function should
456 * be called after your own preinit initialization and you shouldn't do any
457 * window management on your own.
459 * Global libvo variables changed:
465 * \return 1 = Success, 0 = Failure
467 int vo_w32_init(void) {
469 HICON mplayerIcon
= NULL
;
470 HICON mplayerSmallIcon
= NULL
;
476 hInstance
= GetModuleHandle(NULL
);
478 mplayerIcon
= (HICON
)LoadImage(hInstance
, MAKEINTRESOURCE(IDI_APPICON
), IMAGE_ICON
, 0, 0, LR_DEFAULTSIZE
| LR_SHARED
);
479 mplayerSmallIcon
= (HICON
)LoadImage(hInstance
, MAKEINTRESOURCE(IDI_APPICON
), IMAGE_ICON
, 16, 16, LR_SHARED
);
482 WNDCLASSEX wcex
= { sizeof wcex
, CS_OWNDC
| CS_DBLCLKS
, WndProc
, 0, 0, hInstance
, mplayerIcon
, LoadCursor(0, IDC_ARROW
), NULL
, 0, classname
, mplayerSmallIcon
};
484 if (!RegisterClassEx(&wcex
)) {
485 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to register window class!\n");
493 GetClientRect(WinID
, &r
);
494 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
495 vo_window
= CreateWindowEx(WS_EX_NOPARENTNOTIFY
, classname
, classname
,
496 WS_CHILD
| WS_VISIBLE
,
497 0, 0, vo_dwidth
, vo_dheight
, WinID
, 0, hInstance
, 0);
498 EnableWindow(vo_window
, 0);
500 vo_window
= CreateWindowEx(0, classname
, classname
,
501 vo_border
? (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
,
502 CW_USEDEFAULT
, 0, 100, 100, 0, 0, hInstance
, 0);
504 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to create window!\n");
508 myMonitorFromWindow
= NULL
;
509 myGetMonitorInfo
= NULL
;
510 myEnumDisplayMonitors
= NULL
;
511 user32
= GetModuleHandle("user32.dll");
513 myMonitorFromWindow
= (void *)GetProcAddress(user32
, "MonitorFromWindow");
514 myGetMonitorInfo
= GetProcAddress(user32
, "GetMonitorInfoA");
515 myEnumDisplayMonitors
= GetProcAddress(user32
, "EnumDisplayMonitors");
518 dev
= get_display_name();
519 if (dev
) dev_hdc
= CreateDC(dev
, NULL
, NULL
, NULL
);
520 updateScreenProperties();
526 * \brief Toogle fullscreen / windowed mode.
528 * Should be called on VOCTRL_FULLSCREEN event. The window is
529 * always resized after this call, so the rendering context
530 * should be reinitialized with the new dimensions.
531 * It is unspecified if vo_check_events will create a resize
532 * event in addition or not.
534 * Global libvo variables changed:
540 void vo_w32_fullscreen(void) {
543 createRenderingContext();
547 * \brief Toogle window border attribute.
549 * Should be called on VOCTRL_BORDER event.
551 * Global libvo variables changed:
554 void vo_w32_border(void) {
555 vo_border
= !vo_border
;
556 createRenderingContext();
560 * \brief Toogle window ontop attribute.
562 * Should be called on VOCTRL_ONTOP event.
564 * Global libvo variables changed:
567 void vo_w32_ontop( void )
569 vo_ontop
= !vo_ontop
;
571 createRenderingContext();
576 * \brief Uninitialize w32_common framework.
578 * Should be called last in video driver's uninit function. First release
579 * anything built on top of the created window e.g. rendering context inside
580 * and call vo_w32_uninit at the end.
582 void vo_w32_uninit(void) {
583 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: uninit\n");
587 if (dev_hdc
) DeleteDC(dev_hdc
);
589 DestroyWindow(vo_window
);
591 UnregisterClass(classname
, 0);
595 * \brief get a device context to draw in
597 * \param wnd window the DC should belong to if it makes sense
599 HDC
vo_w32_get_dc(HWND wnd
) {
600 if (dev_hdc
) return dev_hdc
;
605 * \brief release a device context
607 * \param wnd window the DC probably belongs to
609 void vo_w32_release_dc(HWND wnd
, HDC dc
) {