Merge svn changes up to r28310
[mplayer/glamo.git] / libvo / w32_common.c
blob36fc3445763d9c630c984c79380dd745b730135d
1 #include <stdio.h>
2 #include <limits.h>
3 #include <windows.h>
4 #include <windowsx.h>
6 // To get "#define vo_ontop global_vo->opts->vo_ontop" etc
7 #include "old_vo_defines.h"
8 #include "osdep/keycodes.h"
9 #include "input/input.h"
10 #include "input/mouse.h"
11 #include "mp_msg.h"
12 #include "video_out.h"
13 #include "aspect.h"
14 #include "w32_common.h"
15 #include "mp_fifo.h"
17 extern int enable_mouse_movements;
19 #ifndef MONITOR_DEFAULTTOPRIMARY
20 #define MONITOR_DEFAULTTOPRIMARY 1
21 #endif
23 static const char classname[] = "MPlayer - Media player for Win32";
24 int vo_vm = 0;
26 static int depthonscreen;
27 // last non-fullscreen extends
28 static int prev_width;
29 static int prev_height;
30 static int prev_x;
31 static int prev_y;
33 static uint32_t o_dwidth;
34 static uint32_t o_dheight;
36 static HINSTANCE hInstance;
37 #define vo_window vo_w32_window
38 HWND vo_window = 0;
39 static int event_flags;
40 static int mon_cnt;
42 static HMONITOR (WINAPI* myMonitorFromWindow)(HWND, DWORD);
43 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
44 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
46 static const struct keymap vk_map[] = {
47 // special keys
48 {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
50 // cursor keys
51 {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
53 // navigation block
54 {VK_INSERT, KEY_INSERT}, {VK_DELETE, KEY_DELETE}, {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
55 {VK_PRIOR, KEY_PAGE_UP}, {VK_NEXT, KEY_PAGE_DOWN},
57 // F-keys
58 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
59 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
60 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F1, KEY_F+12},
61 // numpad
62 {VK_NUMPAD0, KEY_KP0}, {VK_NUMPAD1, KEY_KP1}, {VK_NUMPAD2, KEY_KP2},
63 {VK_NUMPAD3, KEY_KP3}, {VK_NUMPAD4, KEY_KP4}, {VK_NUMPAD5, KEY_KP5},
64 {VK_NUMPAD6, KEY_KP6}, {VK_NUMPAD7, KEY_KP7}, {VK_NUMPAD8, KEY_KP8},
65 {VK_NUMPAD9, KEY_KP9}, {VK_DECIMAL, KEY_KPDEC},
67 {0, 0}
70 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
71 RECT r;
72 POINT p;
73 int mpkey;
74 switch (message) {
75 case WM_ERASEBKGND: // no need to erase background seperately
76 return 1;
77 case WM_PAINT:
78 event_flags |= VO_EVENT_EXPOSE;
79 break;
80 case WM_MOVE:
81 p.x = 0;
82 p.y = 0;
83 ClientToScreen(vo_window, &p);
84 vo_dx = p.x;
85 vo_dy = p.y;
86 break;
87 case WM_SIZE:
88 event_flags |= VO_EVENT_RESIZE;
89 GetClientRect(vo_window, &r);
90 vo_dwidth = r.right;
91 vo_dheight = r.bottom;
92 break;
93 case WM_WINDOWPOSCHANGING:
94 if (vo_keepaspect && !vo_fs) {
95 WINDOWPOS *wpos = lParam;
96 int xborder, yborder;
97 RECT r2;
98 GetClientRect(vo_window, &r);
99 GetWindowRect(vo_window, &r2);
100 xborder = (r2.right - r2.left) - (r.right - r.left);
101 yborder = (r2.bottom - r2.top) - (r.bottom - r.top);
102 wpos->cx -= xborder; wpos->cy -= yborder;
103 aspect_fit(global_vo, &wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
104 wpos->cx += xborder; wpos->cy += yborder;
106 return 0;
107 case WM_CLOSE:
108 mplayer_put_key(KEY_CLOSE_WIN);
109 break;
110 case WM_SYSCOMMAND:
111 switch (wParam) {
112 case SC_SCREENSAVE:
113 case SC_MONITORPOWER:
114 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
115 return 0;
117 break;
118 case WM_KEYDOWN:
119 mpkey = lookup_keymap_table(vk_map, wParam);
120 if (mpkey)
121 mplayer_put_key(mpkey);
122 break;
123 case WM_CHAR:
124 mplayer_put_key(wParam);
125 break;
126 case WM_LBUTTONDOWN:
127 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
128 mplayer_put_key(MOUSE_BTN0);
129 break;
131 if (!vo_fs) {
132 ReleaseCapture();
133 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
134 return 0;
136 break;
137 case WM_MBUTTONDOWN:
138 if (!vo_nomouse_input)
139 mplayer_put_key(MOUSE_BTN1);
140 break;
141 case WM_RBUTTONDOWN:
142 if (!vo_nomouse_input)
143 mplayer_put_key(MOUSE_BTN2);
144 break;
145 case WM_MOUSEMOVE:
146 if (enable_mouse_movements) {
147 char cmd_str[40];
148 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i",
149 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
150 mp_input_queue_cmd(global_vo->input_ctx, mp_input_parse_cmd(cmd_str));
152 break;
153 case WM_MOUSEWHEEL:
154 if (!vo_nomouse_input) {
155 int x = GET_WHEEL_DELTA_WPARAM(wParam);
156 if (x > 0)
157 mplayer_put_key(MOUSE_BTN3);
158 else
159 mplayer_put_key(MOUSE_BTN4);
160 break;
164 return DefWindowProc(hWnd, message, wParam, lParam);
168 * \brief Dispatch incoming window events and handle them.
170 * This function should be placed inside libvo's function "check_events".
172 * Global libvo variables changed:
173 * vo_dwidth: new window client area width
174 * vo_dheight: new window client area height
176 * \return int with these flags possibly set, take care to handle in the right order
177 * if it matters in your driver:
179 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
180 * driver render context accordingly.
181 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
182 * the window if the movie is paused.
184 int vo_w32_check_events(void) {
185 MSG msg;
186 event_flags = 0;
187 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
188 TranslateMessage(&msg);
189 DispatchMessage(&msg);
191 if (WinID >= 0) {
192 RECT r;
193 GetClientRect(vo_window, &r);
194 if (r.right != vo_dwidth || r.bottom != vo_dheight) {
195 vo_dwidth = r.right; vo_dheight = r.bottom;
196 event_flags |= VO_EVENT_RESIZE;
198 GetClientRect(WinID, &r);
199 if (r.right != vo_dwidth || r.bottom != vo_dheight)
200 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
203 return event_flags;
206 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
207 // this defaults to the last screen if specified number does not exist
208 xinerama_x = r->left;
209 xinerama_y = r->top;
210 vo_screenwidth = r->right - r->left;
211 vo_screenheight = r->bottom - r->top;
212 if (mon_cnt == xinerama_screen)
213 return FALSE;
214 mon_cnt++;
215 return TRUE;
219 * \brief Update screen information.
221 * This function should be called in libvo's "control" callback
222 * with parameter VOCTRL_UPDATE_SCREENINFO.
223 * Note that this also enables the new API where geometry and aspect
224 * calculations are done in video_out.c:config_video_out
226 * Global libvo variables changed:
227 * xinerama_x
228 * xinerama_y
229 * vo_screenwidth
230 * vo_screenheight
232 void w32_update_xinerama_info(void) {
233 xinerama_x = xinerama_y = 0;
234 if (xinerama_screen < -1) {
235 int tmp;
236 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
237 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
238 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
239 if (tmp) vo_screenwidth = tmp;
240 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
241 if (tmp) vo_screenheight = tmp;
242 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
243 MONITORINFO mi;
244 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
245 mi.cbSize = sizeof(mi);
246 myGetMonitorInfo(m, &mi);
247 xinerama_x = mi.rcMonitor.left;
248 xinerama_y = mi.rcMonitor.top;
249 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
250 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
251 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
252 mon_cnt = 0;
253 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
255 aspect_save_screenres(vo_screenwidth, vo_screenheight);
258 static void updateScreenProperties(void) {
259 DEVMODE dm;
260 dm.dmSize = sizeof dm;
261 dm.dmDriverExtra = 0;
262 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
263 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
264 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
265 return;
268 vo_screenwidth = dm.dmPelsWidth;
269 vo_screenheight = dm.dmPelsHeight;
270 depthonscreen = dm.dmBitsPerPel;
271 w32_update_xinerama_info();
274 static void changeMode(void) {
275 DEVMODE dm;
276 dm.dmSize = sizeof dm;
277 dm.dmDriverExtra = 0;
279 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
280 dm.dmBitsPerPel = depthonscreen;
281 dm.dmPelsWidth = vo_screenwidth;
282 dm.dmPelsHeight = vo_screenheight;
284 if (vo_vm) {
285 int bestMode = -1;
286 int bestScore = INT_MAX;
287 int i;
288 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
289 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
290 if (dm.dmBitsPerPel != depthonscreen) continue;
291 if (dm.dmPelsWidth < o_dwidth) continue;
292 if (dm.dmPelsHeight < o_dheight) continue;
294 if (score < bestScore) {
295 bestScore = score;
296 bestMode = i;
300 if (bestMode != -1)
301 EnumDisplaySettings(0, bestMode, &dm);
303 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
307 static void resetMode(void) {
308 if (vo_vm)
309 ChangeDisplaySettings(0, 0);
312 static int createRenderingContext(void) {
313 HWND layer = HWND_NOTOPMOST;
314 PIXELFORMATDESCRIPTOR pfd;
315 HDC vo_hdc = GetDC(vo_window);
316 RECT r;
317 int pf;
318 if (WinID < 0) {
319 int style = (vo_border && !vo_fs) ?
320 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
322 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
323 if (vo_fs) {
324 changeMode();
325 while (ShowCursor(0) >= 0) /**/ ;
326 } else {
327 resetMode();
328 while (ShowCursor(1) < 0) /**/ ;
330 updateScreenProperties();
331 ShowWindow(vo_window, SW_HIDE);
332 SetWindowLong(vo_window, GWL_STYLE, style);
333 if (vo_fs) {
334 prev_width = vo_dwidth;
335 prev_height = vo_dheight;
336 prev_x = vo_dx;
337 prev_y = vo_dy;
338 vo_dwidth = vo_screenwidth;
339 vo_dheight = vo_screenheight;
340 vo_dx = xinerama_x;
341 vo_dy = xinerama_y;
342 } else {
343 // make sure there are no "stale" resize events
344 // that would set vo_d* to wrong values
345 vo_w32_check_events();
346 vo_dwidth = prev_width;
347 vo_dheight = prev_height;
348 vo_dx = prev_x;
349 vo_dy = prev_y;
350 // HACK around what probably is a windows focus bug:
351 // when pressing 'f' on the console, then 'f' again to
352 // return to windowed mode, any input into the video
353 // window is lost forever.
354 SetFocus(vo_window);
356 r.left = vo_dx;
357 r.right = r.left + vo_dwidth;
358 r.top = vo_dy;
359 r.bottom = r.top + vo_dheight;
360 AdjustWindowRect(&r, style, 0);
361 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
364 memset(&pfd, 0, sizeof pfd);
365 pfd.nSize = sizeof pfd;
366 pfd.nVersion = 1;
367 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
368 pfd.iPixelType = PFD_TYPE_RGBA;
369 pfd.cColorBits = 24;
370 pfd.iLayerType = PFD_MAIN_PLANE;
371 pf = ChoosePixelFormat(vo_hdc, &pfd);
372 if (!pf) {
373 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
374 ReleaseDC(vo_window, vo_hdc);
375 return 0;
378 SetPixelFormat(vo_hdc, pf, &pfd);
380 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, depthonscreen);
382 ReleaseDC(vo_window, vo_hdc);
383 return 1;
387 * \brief Configure and show window on the screen.
389 * This function should be called in libvo's "config" callback.
390 * It configures a window and shows it on the screen.
392 * Global libvo variables changed:
393 * vo_fs
394 * vo_vm
396 * \return 1 - Success, 0 - Failure
398 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
399 // store original size for videomode switching
400 o_dwidth = width;
401 o_dheight = height;
403 if (WinID < 0) {
404 // the desired size is ignored in wid mode, it always matches the window size.
405 prev_width = vo_dwidth = width;
406 prev_height = vo_dheight = height;
407 prev_x = vo_dx;
408 prev_y = vo_dy;
411 vo_fs = flags & VOFLAG_FULLSCREEN;
412 vo_vm = flags & VOFLAG_MODESWITCHING;
413 return createRenderingContext();
417 * \brief Initialize w32_common framework.
419 * The first function that should be called from the w32_common framework.
420 * It handles window creation on the screen with proper title and attributes.
421 * It also initializes the framework's internal variables. The function should
422 * be called after your own preinit initialization and you shouldn't do any
423 * window management on your own.
425 * Global libvo variables changed:
426 * vo_w32_window
427 * vo_depthonscreen
428 * vo_screenwidth
429 * vo_screenheight
431 * \return 1 = Success, 0 = Failure
433 int vo_w32_init(void) {
434 HICON mplayerIcon = 0;
435 char exedir[MAX_PATH];
436 HINSTANCE user32;
438 if (vo_window)
439 return 1;
441 hInstance = GetModuleHandle(0);
443 if (GetModuleFileName(0, exedir, MAX_PATH))
444 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
445 if (!mplayerIcon)
446 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
449 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
451 if (!RegisterClassEx(&wcex)) {
452 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
453 return 0;
457 if (WinID >= 0)
459 RECT r;
460 GetClientRect(WinID, &r);
461 vo_dwidth = r.right; vo_dheight = r.bottom;
462 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
463 WS_CHILD | WS_VISIBLE,
464 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
465 EnableWindow(vo_window, 0);
466 } else
467 vo_window = CreateWindowEx(0, classname, classname,
468 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
469 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
470 if (!vo_window) {
471 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
472 return 0;
475 myMonitorFromWindow = NULL;
476 myGetMonitorInfo = NULL;
477 myEnumDisplayMonitors = NULL;
478 user32 = GetModuleHandle("user32.dll");
479 if (user32) {
480 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
481 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
482 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
484 updateScreenProperties();
486 return 1;
490 * \brief Toogle fullscreen / windowed mode.
492 * Should be called on VOCTRL_FULLSCREEN event. The window is
493 * always resized after this call, so the rendering context
494 * should be reinitialized with the new dimensions.
495 * It is unspecified if vo_check_events will create a resize
496 * event in addition or not.
498 * Global libvo variables changed:
499 * vo_dwidth
500 * vo_dheight
501 * vo_fs
504 void vo_w32_fullscreen(void) {
505 vo_fs = !vo_fs;
507 createRenderingContext();
511 * \brief Toogle window border attribute.
513 * Should be called on VOCTRL_BORDER event.
515 * Global libvo variables changed:
516 * vo_border
518 void vo_w32_border(void) {
519 vo_border = !vo_border;
520 createRenderingContext();
524 * \brief Toogle window ontop attribute.
526 * Should be called on VOCTRL_ONTOP event.
528 * Global libvo variables changed:
529 * vo_ontop
531 void vo_w32_ontop( void )
533 vo_ontop = !vo_ontop;
534 if (!vo_fs) {
535 createRenderingContext();
540 * \brief Uninitialize w32_common framework.
542 * Should be called last in video driver's uninit function. First release
543 * anything built on top of the created window e.g. rendering context inside
544 * and call vo_w32_uninit at the end.
546 void vo_w32_uninit(void) {
547 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
548 resetMode();
549 ShowCursor(1);
550 depthonscreen = 0;
551 DestroyWindow(vo_window);
552 vo_window = 0;
553 UnregisterClass(classname, 0);