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
},
57 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
62 case WM_ERASEBKGND
: // no need to erase background seperately
65 event_flags
|= VO_EVENT_EXPOSE
;
70 ClientToScreen(vo_window
, &p
);
75 event_flags
|= VO_EVENT_RESIZE
;
76 GetClientRect(vo_window
, &r
);
78 vo_dheight
= r
.bottom
;
80 case WM_WINDOWPOSCHANGING
:
81 if (vo_keepaspect
&& !vo_fs
) {
82 WINDOWPOS
*wpos
= lParam
;
85 GetClientRect(vo_window
, &r
);
86 GetWindowRect(vo_window
, &r2
);
87 xborder
= (r2
.right
- r2
.left
) - (r
.right
- r
.left
);
88 yborder
= (r2
.bottom
- r2
.top
) - (r
.bottom
- r
.top
);
89 wpos
->cx
-= xborder
; wpos
->cy
-= yborder
;
90 aspect_fit(&wpos
->cx
, &wpos
->cy
, wpos
->cx
, wpos
->cy
);
91 wpos
->cx
+= xborder
; wpos
->cy
+= yborder
;
95 mplayer_put_key(KEY_CLOSE_WIN
);
100 case SC_MONITORPOWER
:
101 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: killing screensaver\n");
106 mpkey
= lookup_keymap_table(vk_map
, wParam
);
108 mplayer_put_key(mpkey
);
111 mplayer_put_key(wParam
);
114 if (!vo_nomouse_input
&& (vo_fs
|| (wParam
& MK_CONTROL
))) {
115 mplayer_put_key(MOUSE_BTN0
);
120 SendMessage(hWnd
, WM_NCLBUTTONDOWN
, HTCAPTION
, 0);
125 if (!vo_nomouse_input
)
126 mplayer_put_key(MOUSE_BTN1
);
129 if (!vo_nomouse_input
)
130 mplayer_put_key(MOUSE_BTN2
);
133 if (enable_mouse_movements
) {
135 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i",
136 GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
137 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str
));
141 if (!vo_nomouse_input
) {
142 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
144 mplayer_put_key(MOUSE_BTN3
);
146 mplayer_put_key(MOUSE_BTN4
);
151 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
155 * \brief Dispatch incoming window events and handle them.
157 * This function should be placed inside libvo's function "check_events".
159 * Global libvo variables changed:
160 * vo_dwidth: new window client area width
161 * vo_dheight: new window client area height
163 * \return int with these flags possibly set, take care to handle in the right order
164 * if it matters in your driver:
166 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
167 * driver render context accordingly.
168 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
169 * the window if the movie is paused.
171 int vo_w32_check_events(void) {
174 while (PeekMessage(&msg
, 0, 0, 0, PM_REMOVE
)) {
175 TranslateMessage(&msg
);
176 DispatchMessage(&msg
);
180 GetClientRect(vo_window
, &r
);
181 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
) {
182 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
183 event_flags
|= VO_EVENT_RESIZE
;
185 GetClientRect(WinID
, &r
);
186 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
)
187 MoveWindow(vo_window
, 0, 0, r
.right
, r
.bottom
, FALSE
);
193 static BOOL CALLBACK
mon_enum(HMONITOR hmon
, HDC hdc
, LPRECT r
, LPARAM p
) {
194 // this defaults to the last screen if specified number does not exist
195 xinerama_x
= r
->left
;
197 vo_screenwidth
= r
->right
- r
->left
;
198 vo_screenheight
= r
->bottom
- r
->top
;
199 if (mon_cnt
== xinerama_screen
)
206 * \brief Update screen information.
208 * This function should be called in libvo's "control" callback
209 * with parameter VOCTRL_UPDATE_SCREENINFO.
210 * Note that this also enables the new API where geometry and aspect
211 * calculations are done in video_out.c:config_video_out
213 * Global libvo variables changed:
219 void w32_update_xinerama_info(void) {
220 xinerama_x
= xinerama_y
= 0;
221 if (xinerama_screen
< -1) {
223 xinerama_x
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
224 xinerama_y
= GetSystemMetrics(SM_YVIRTUALSCREEN
);
225 tmp
= GetSystemMetrics(SM_CXVIRTUALSCREEN
);
226 if (tmp
) vo_screenwidth
= tmp
;
227 tmp
= GetSystemMetrics(SM_CYVIRTUALSCREEN
);
228 if (tmp
) vo_screenheight
= tmp
;
229 } else if (xinerama_screen
== -1 && myMonitorFromWindow
&& myGetMonitorInfo
) {
231 HMONITOR m
= myMonitorFromWindow(vo_window
, MONITOR_DEFAULTTOPRIMARY
);
232 mi
.cbSize
= sizeof(mi
);
233 myGetMonitorInfo(m
, &mi
);
234 xinerama_x
= mi
.rcMonitor
.left
;
235 xinerama_y
= mi
.rcMonitor
.top
;
236 vo_screenwidth
= mi
.rcMonitor
.right
- mi
.rcMonitor
.left
;
237 vo_screenheight
= mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
;
238 } else if (xinerama_screen
> 0 && myEnumDisplayMonitors
) {
240 myEnumDisplayMonitors(NULL
, NULL
, mon_enum
, 0);
242 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
245 static void updateScreenProperties(void) {
247 dm
.dmSize
= sizeof dm
;
248 dm
.dmDriverExtra
= 0;
249 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
250 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS
, &dm
)) {
251 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to enumerate display settings!\n");
255 vo_screenwidth
= dm
.dmPelsWidth
;
256 vo_screenheight
= dm
.dmPelsHeight
;
257 vo_depthonscreen
= dm
.dmBitsPerPel
;
258 w32_update_xinerama_info();
261 static void changeMode(void) {
263 dm
.dmSize
= sizeof dm
;
264 dm
.dmDriverExtra
= 0;
266 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
267 dm
.dmBitsPerPel
= vo_depthonscreen
;
268 dm
.dmPelsWidth
= vo_screenwidth
;
269 dm
.dmPelsHeight
= vo_screenheight
;
273 int bestScore
= INT_MAX
;
275 for (i
= 0; EnumDisplaySettings(0, i
, &dm
); ++i
) {
276 int score
= (dm
.dmPelsWidth
- o_dwidth
) * (dm
.dmPelsHeight
- o_dheight
);
277 if (dm
.dmBitsPerPel
!= vo_depthonscreen
) continue;
278 if (dm
.dmPelsWidth
< o_dwidth
) continue;
279 if (dm
.dmPelsHeight
< o_dheight
) continue;
281 if (score
< bestScore
) {
288 EnumDisplaySettings(0, bestMode
, &dm
);
290 ChangeDisplaySettings(&dm
, CDS_FULLSCREEN
);
294 static void resetMode(void) {
296 ChangeDisplaySettings(0, 0);
299 static int createRenderingContext(void) {
300 HWND layer
= HWND_NOTOPMOST
;
301 PIXELFORMATDESCRIPTOR pfd
;
302 HDC vo_hdc
= GetDC(vo_window
);
306 int style
= (vo_border
&& !vo_fs
) ?
307 (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
;
309 if (vo_fs
|| vo_ontop
) layer
= HWND_TOPMOST
;
312 while (ShowCursor(0) >= 0) /**/ ;
315 while (ShowCursor(1) < 0) /**/ ;
317 updateScreenProperties();
318 ShowWindow(vo_window
, SW_HIDE
);
319 SetWindowLong(vo_window
, GWL_STYLE
, style
);
321 prev_width
= vo_dwidth
;
322 prev_height
= vo_dheight
;
325 vo_dwidth
= vo_screenwidth
;
326 vo_dheight
= vo_screenheight
;
330 // make sure there are no "stale" resize events
331 // that would set vo_d* to wrong values
332 vo_w32_check_events();
333 vo_dwidth
= prev_width
;
334 vo_dheight
= prev_height
;
337 // HACK around what probably is a windows focus bug:
338 // when pressing 'f' on the console, then 'f' again to
339 // return to windowed mode, any input into the video
340 // window is lost forever.
344 r
.right
= r
.left
+ vo_dwidth
;
346 r
.bottom
= r
.top
+ vo_dheight
;
347 AdjustWindowRect(&r
, style
, 0);
348 SetWindowPos(vo_window
, layer
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_SHOWWINDOW
);
351 memset(&pfd
, 0, sizeof pfd
);
352 pfd
.nSize
= sizeof pfd
;
354 pfd
.dwFlags
= PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
;
355 pfd
.iPixelType
= PFD_TYPE_RGBA
;
357 pfd
.iLayerType
= PFD_MAIN_PLANE
;
358 pf
= ChoosePixelFormat(vo_hdc
, &pfd
);
360 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to select a valid pixel format!\n");
361 ReleaseDC(vo_window
, vo_hdc
);
365 SetPixelFormat(vo_hdc
, pf
, &pfd
);
367 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth
, vo_screenheight
, vo_depthonscreen
);
369 ReleaseDC(vo_window
, vo_hdc
);
374 * \brief Configure and show window on the screen.
376 * This function should be called in libvo's "config" callback.
377 * It configures a window and shows it on the screen.
379 * Global libvo variables changed:
383 * \return 1 - Success, 0 - Failure
385 int vo_w32_config(uint32_t width
, uint32_t height
, uint32_t flags
) {
386 // store original size for videomode switching
391 // the desired size is ignored in wid mode, it always matches the window size.
392 prev_width
= vo_dwidth
= width
;
393 prev_height
= vo_dheight
= height
;
398 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
399 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
400 return createRenderingContext();
404 * \brief Initialize w32_common framework.
406 * The first function that should be called from the w32_common framework.
407 * It handles window creation on the screen with proper title and attributes.
408 * It also initializes the framework's internal variables. The function should
409 * be called after your own preinit initialization and you shouldn't do any
410 * window management on your own.
412 * Global libvo variables changed:
418 * \return 1 = Success, 0 = Failure
420 int vo_w32_init(void) {
421 HICON mplayerIcon
= 0;
422 char exedir
[MAX_PATH
];
428 hInstance
= GetModuleHandle(0);
430 if (GetModuleFileName(0, exedir
, MAX_PATH
))
431 mplayerIcon
= ExtractIcon(hInstance
, exedir
, 0);
433 mplayerIcon
= LoadIcon(0, IDI_APPLICATION
);
436 WNDCLASSEX wcex
= { sizeof wcex
, CS_OWNDC
| CS_DBLCLKS
, WndProc
, 0, 0, hInstance
, mplayerIcon
, LoadCursor(0, IDC_ARROW
), NULL
, 0, classname
, mplayerIcon
};
438 if (!RegisterClassEx(&wcex
)) {
439 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to register window class!\n");
447 GetClientRect(WinID
, &r
);
448 vo_dwidth
= r
.right
; vo_dheight
= r
.bottom
;
449 vo_window
= CreateWindowEx(WS_EX_NOPARENTNOTIFY
, classname
, classname
,
450 WS_CHILD
| WS_VISIBLE
,
451 0, 0, vo_dwidth
, vo_dheight
, WinID
, 0, hInstance
, 0);
452 EnableWindow(vo_window
, 0);
454 vo_window
= CreateWindowEx(0, classname
, classname
,
455 vo_border
? (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
,
456 CW_USEDEFAULT
, 0, 100, 100, 0, 0, hInstance
, 0);
458 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to create window!\n");
462 myMonitorFromWindow
= NULL
;
463 myGetMonitorInfo
= NULL
;
464 myEnumDisplayMonitors
= NULL
;
465 user32
= GetModuleHandle("user32.dll");
467 myMonitorFromWindow
= (void *)GetProcAddress(user32
, "MonitorFromWindow");
468 myGetMonitorInfo
= GetProcAddress(user32
, "GetMonitorInfoA");
469 myEnumDisplayMonitors
= GetProcAddress(user32
, "EnumDisplayMonitors");
471 updateScreenProperties();
477 * \brief Toogle fullscreen / windowed mode.
479 * Should be called on VOCTRL_FULLSCREEN event. The window is
480 * always resized after this call, so the rendering context
481 * should be reinitialized with the new dimensions.
482 * It is unspecified if vo_check_events will create a resize
483 * event in addition or not.
485 * Global libvo variables changed:
491 void vo_w32_fullscreen(void) {
494 createRenderingContext();
498 * \brief Toogle window border attribute.
500 * Should be called on VOCTRL_BORDER event.
502 * Global libvo variables changed:
505 void vo_w32_border(void) {
506 vo_border
= !vo_border
;
507 createRenderingContext();
511 * \brief Toogle window ontop attribute.
513 * Should be called on VOCTRL_ONTOP event.
515 * Global libvo variables changed:
518 void vo_w32_ontop( void )
520 vo_ontop
= !vo_ontop
;
522 createRenderingContext();
527 * \brief Uninitialize w32_common framework.
529 * Should be called last in video driver's uninit function. First release
530 * anything built on top of the created window e.g. rendering context inside
531 * and call vo_w32_uninit at the end.
533 void vo_w32_uninit(void) {
534 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: uninit\n");
537 vo_depthonscreen
= 0;
538 DestroyWindow(vo_window
);
540 UnregisterClass(classname
, 0);