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
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 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
48 event_flags
|= VO_EVENT_EXPOSE
;
53 ClientToScreen(vo_window
, &p
);
58 event_flags
|= VO_EVENT_RESIZE
;
59 GetClientRect(vo_window
, &r
);
61 vo_dheight
= r
.bottom
;
63 case WM_WINDOWPOSCHANGING
:
66 mplayer_put_key(KEY_CLOSE_WIN
);
72 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: killing screensaver\n");
78 case VK_LEFT
: mplayer_put_key(KEY_LEFT
); break;
79 case VK_UP
: mplayer_put_key(KEY_UP
); break;
80 case VK_RIGHT
: mplayer_put_key(KEY_RIGHT
); break;
81 case VK_DOWN
: mplayer_put_key(KEY_DOWN
); break;
82 case VK_TAB
: mplayer_put_key(KEY_TAB
); break;
83 case VK_CONTROL
: mplayer_put_key(KEY_CTRL
); break;
84 case VK_BACK
: mplayer_put_key(KEY_BS
); break;
85 case VK_DELETE
: mplayer_put_key(KEY_DELETE
); break;
86 case VK_INSERT
: mplayer_put_key(KEY_INSERT
); break;
87 case VK_HOME
: mplayer_put_key(KEY_HOME
); break;
88 case VK_END
: mplayer_put_key(KEY_END
); break;
89 case VK_PRIOR
: mplayer_put_key(KEY_PAGE_UP
); break;
90 case VK_NEXT
: mplayer_put_key(KEY_PAGE_DOWN
); break;
91 case VK_ESCAPE
: mplayer_put_key(KEY_ESC
); break;
95 mplayer_put_key(wParam
);
98 if (!vo_nomouse_input
&& (vo_fs
|| (wParam
& MK_CONTROL
))) {
99 mplayer_put_key(MOUSE_BTN0
);
104 SendMessage(hWnd
, WM_NCLBUTTONDOWN
, HTCAPTION
, 0);
109 if (!vo_nomouse_input
)
110 mplayer_put_key(MOUSE_BTN1
);
113 if (!vo_nomouse_input
)
114 mplayer_put_key(MOUSE_BTN2
);
117 if (enable_mouse_movements
) {
119 snprintf(cmd_str
, sizeof(cmd_str
), "set_mouse_pos %i %i",
120 GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
121 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str
));
125 if (!vo_nomouse_input
) {
126 int x
= GET_WHEEL_DELTA_WPARAM(wParam
);
128 mplayer_put_key(MOUSE_BTN3
);
130 mplayer_put_key(MOUSE_BTN4
);
135 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
138 int vo_w32_check_events(void) {
141 while (PeekMessage(&msg
, 0, 0, 0, PM_REMOVE
)) {
142 TranslateMessage(&msg
);
143 DispatchMessage(&msg
);
147 GetClientRect(vo_window
, &r
);
148 if (r
.right
!= vo_dwidth
|| r
.bottom
!= vo_dheight
)
149 event_flags
|= VO_EVENT_RESIZE
;
151 vo_dheight
= r
.bottom
;
157 static BOOL CALLBACK
mon_enum(HMONITOR hmon
, HDC hdc
, LPRECT r
, LPARAM p
) {
158 // this defaults to the last screen if specified number does not exist
159 xinerama_x
= r
->left
;
161 vo_screenwidth
= r
->right
- r
->left
;
162 vo_screenheight
= r
->bottom
- r
->top
;
163 if (mon_cnt
== xinerama_screen
)
169 void w32_update_xinerama_info(void) {
170 xinerama_x
= xinerama_y
= 0;
171 if (xinerama_screen
< -1) {
173 xinerama_x
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
174 xinerama_y
= GetSystemMetrics(SM_YVIRTUALSCREEN
);
175 tmp
= GetSystemMetrics(SM_CXVIRTUALSCREEN
);
176 if (tmp
) vo_screenwidth
= tmp
;
177 tmp
= GetSystemMetrics(SM_CYVIRTUALSCREEN
);
178 if (tmp
) vo_screenheight
= tmp
;
179 } else if (xinerama_screen
== -1 && myMonitorFromWindow
&& myGetMonitorInfo
) {
181 HMONITOR m
= myMonitorFromWindow(vo_window
, MONITOR_DEFAULTTOPRIMARY
);
182 mi
.cbSize
= sizeof(mi
);
183 myGetMonitorInfo(m
, &mi
);
184 xinerama_x
= mi
.rcMonitor
.left
;
185 xinerama_y
= mi
.rcMonitor
.top
;
186 vo_screenwidth
= mi
.rcMonitor
.right
- mi
.rcMonitor
.left
;
187 vo_screenheight
= mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
;
188 } else if (xinerama_screen
> 0 && myEnumDisplayMonitors
) {
190 myEnumDisplayMonitors(NULL
, NULL
, mon_enum
, 0);
192 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
195 static void updateScreenProperties() {
197 dm
.dmSize
= sizeof dm
;
198 dm
.dmDriverExtra
= 0;
199 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
200 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS
, &dm
)) {
201 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to enumerate display settings!\n");
205 vo_screenwidth
= dm
.dmPelsWidth
;
206 vo_screenheight
= dm
.dmPelsHeight
;
207 vo_depthonscreen
= dm
.dmBitsPerPel
;
208 w32_update_xinerama_info();
211 static void changeMode(void) {
213 dm
.dmSize
= sizeof dm
;
214 dm
.dmDriverExtra
= 0;
216 dm
.dmFields
= DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
;
217 dm
.dmBitsPerPel
= vo_depthonscreen
;
218 dm
.dmPelsWidth
= vo_screenwidth
;
219 dm
.dmPelsHeight
= vo_screenheight
;
223 int bestScore
= INT_MAX
;
225 for (i
= 0; EnumDisplaySettings(0, i
, &dm
); ++i
) {
226 int score
= (dm
.dmPelsWidth
- o_dwidth
) * (dm
.dmPelsHeight
- o_dheight
);
227 if (dm
.dmBitsPerPel
!= vo_depthonscreen
) continue;
228 if (dm
.dmPelsWidth
< o_dwidth
) continue;
229 if (dm
.dmPelsHeight
< o_dheight
) continue;
231 if (score
< bestScore
) {
238 EnumDisplaySettings(0, bestMode
, &dm
);
240 ChangeDisplaySettings(&dm
, CDS_FULLSCREEN
);
244 static void resetMode(void) {
246 ChangeDisplaySettings(0, 0);
249 static int createRenderingContext(void) {
250 HWND layer
= HWND_NOTOPMOST
;
251 PIXELFORMATDESCRIPTOR pfd
;
252 HDC vo_hdc
= GetDC(vo_window
);
256 int style
= (vo_border
&& !vo_fs
) ?
257 (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
;
259 if (vo_fs
|| vo_ontop
) layer
= HWND_TOPMOST
;
262 while (ShowCursor(0) >= 0) /**/ ;
265 while (ShowCursor(1) < 0) /**/ ;
267 updateScreenProperties();
268 ShowWindow(vo_window
, SW_HIDE
);
269 SetWindowLong(vo_window
, GWL_STYLE
, style
);
271 prev_width
= vo_dwidth
;
272 prev_height
= vo_dheight
;
275 vo_dwidth
= vo_screenwidth
;
276 vo_dheight
= vo_screenheight
;
280 // make sure there are no "stale" resize events
281 // that would set vo_d* to wrong values
282 vo_w32_check_events();
283 vo_dwidth
= prev_width
;
284 vo_dheight
= prev_height
;
287 // HACK around what probably is a windows focus bug:
288 // when pressing 'f' on the console, then 'f' again to
289 // return to windowed mode, any input into the video
290 // window is lost forever.
294 r
.right
= r
.left
+ vo_dwidth
;
296 r
.bottom
= r
.top
+ vo_dheight
;
297 AdjustWindowRect(&r
, style
, 0);
298 SetWindowPos(vo_window
, layer
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_SHOWWINDOW
);
301 memset(&pfd
, 0, sizeof pfd
);
302 pfd
.nSize
= sizeof pfd
;
304 pfd
.dwFlags
= PFD_DRAW_TO_WINDOW
| PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
;
305 pfd
.iPixelType
= PFD_TYPE_RGBA
;
307 pfd
.iLayerType
= PFD_MAIN_PLANE
;
308 pf
= ChoosePixelFormat(vo_hdc
, &pfd
);
310 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to select a valid pixel format!\n");
311 ReleaseDC(vo_window
, vo_hdc
);
315 SetPixelFormat(vo_hdc
, pf
, &pfd
);
317 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth
, vo_screenheight
, vo_depthonscreen
);
319 ReleaseDC(vo_window
, vo_hdc
);
323 int vo_w32_config(uint32_t width
, uint32_t height
, uint32_t flags
) {
324 // store original size for videomode switching
328 prev_width
= vo_dwidth
= width
;
329 prev_height
= vo_dheight
= height
;
333 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
334 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
335 return createRenderingContext();
338 int vo_w32_init(void) {
339 HICON mplayerIcon
= 0;
340 char exedir
[MAX_PATH
];
346 hInstance
= GetModuleHandle(0);
348 if (GetModuleFileName(0, exedir
, MAX_PATH
))
349 mplayerIcon
= ExtractIcon(hInstance
, exedir
, 0);
351 mplayerIcon
= LoadIcon(0, IDI_APPLICATION
);
354 WNDCLASSEX wcex
= { sizeof wcex
, CS_OWNDC
, WndProc
, 0, 0, hInstance
, mplayerIcon
, LoadCursor(0, IDC_ARROW
), (HBRUSH
)GetStockObject(BLACK_BRUSH
), 0, classname
, mplayerIcon
};
356 if (!RegisterClassEx(&wcex
)) {
357 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to register window class!\n");
365 vo_window
= CreateWindowEx(0, classname
, classname
,
366 vo_border
? (WS_OVERLAPPEDWINDOW
| WS_SIZEBOX
) : WS_POPUP
,
367 CW_USEDEFAULT
, 0, 100, 100, 0, 0, hInstance
, 0);
369 mp_msg(MSGT_VO
, MSGL_ERR
, "vo: win32: unable to create window!\n");
374 myMonitorFromWindow
= NULL
;
375 myGetMonitorInfo
= NULL
;
376 myEnumDisplayMonitors
= NULL
;
377 user32
= GetModuleHandle("user32.dll");
379 myMonitorFromWindow
= GetProcAddress(user32
, "MonitorFromWindow");
380 myGetMonitorInfo
= GetProcAddress(user32
, "GetMonitorInfoA");
381 myEnumDisplayMonitors
= GetProcAddress(user32
, "EnumDisplayMonitors");
383 updateScreenProperties();
388 void vo_w32_fullscreen(void) {
391 createRenderingContext();
394 void vo_w32_border() {
395 vo_border
= !vo_border
;
396 createRenderingContext();
399 void vo_w32_ontop( void )
401 vo_ontop
= !vo_ontop
;
403 HWND layer
= HWND_NOTOPMOST
;
404 if (vo_ontop
) layer
= HWND_TOPMOST
;
405 SetWindowPos(vo_window
, layer
, (vo_screenwidth
- vo_dwidth
) / 2, (vo_screenheight
- vo_dheight
) / 2, vo_dwidth
, vo_dheight
, SWP_SHOWWINDOW
);
409 void vo_w32_uninit() {
410 mp_msg(MSGT_VO
, MSGL_V
, "vo: win32: uninit\n");
413 vo_depthonscreen
= 0;
415 DestroyWindow(vo_window
);
417 UnregisterClass(classname
, 0);