cache: don't modify argument when stream control fails
[mplayer.git] / libvo / w32_common.c
bloba8a59d02c9b34d24701e4162bb2e431eba4b7470
1 /*
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.
19 #include <stdio.h>
20 #include <limits.h>
21 #include <windows.h>
22 #include <windowsx.h>
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"
28 #include "mp_msg.h"
29 #include "video_out.h"
30 #include "aspect.h"
31 #include "w32_common.h"
32 #include "mp_fifo.h"
34 #ifndef WM_XBUTTONDOWN
35 # define WM_XBUTTONDOWN 0x020B
36 # define WM_XBUTTONUP 0x020C
37 # define WM_XBUTTONDBLCLK 0x020D
38 #endif
40 #ifndef MONITOR_DEFAULTTOPRIMARY
41 #define MONITOR_DEFAULTTOPRIMARY 1
42 #endif
44 static const char classname[] = "MPlayer - The Movie Player";
45 int vo_vm = 0;
47 static int depthonscreen;
48 // last non-fullscreen extends
49 static int prev_width;
50 static int prev_height;
51 static int prev_x;
52 static int prev_y;
54 static uint32_t o_dwidth;
55 static uint32_t o_dheight;
57 static HINSTANCE hInstance;
58 #define vo_window vo_w32_window
59 HWND vo_window = 0;
60 /** HDC used when rendering to a device instead of window */
61 static HDC dev_hdc;
62 static int event_flags;
63 static int mon_cnt;
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[] = {
70 // special keys
71 {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
73 // cursor keys
74 {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
76 // navigation block
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},
80 // F-keys
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},
84 // numpad
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},
90 {0, 0}
93 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
94 RECT r;
95 POINT p;
96 int mpkey;
97 switch (message) {
98 case WM_ERASEBKGND: // no need to erase background seperately
99 return 1;
100 case WM_PAINT:
101 event_flags |= VO_EVENT_EXPOSE;
102 break;
103 case WM_MOVE:
104 event_flags |= VO_EVENT_MOVE;
105 p.x = 0;
106 p.y = 0;
107 ClientToScreen(vo_window, &p);
108 vo_dx = p.x;
109 vo_dy = p.y;
110 break;
111 case WM_SIZE:
112 event_flags |= VO_EVENT_RESIZE;
113 GetClientRect(vo_window, &r);
114 vo_dwidth = r.right;
115 vo_dheight = r.bottom;
116 break;
117 case WM_WINDOWPOSCHANGING:
118 if (vo_keepaspect && !vo_fs && WinID < 0) {
119 WINDOWPOS *wpos = lParam;
120 int xborder, yborder;
121 r.left = r.top = 0;
122 r.right = wpos->cx;
123 r.bottom = wpos->cy;
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;
131 return 0;
132 case WM_CLOSE:
133 mplayer_put_key(KEY_CLOSE_WIN);
134 break;
135 case WM_SYSCOMMAND:
136 switch (wParam) {
137 case SC_SCREENSAVE:
138 case SC_MONITORPOWER:
139 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
140 return 0;
142 break;
143 case WM_KEYDOWN:
144 mpkey = lookup_keymap_table(vk_map, wParam);
145 if (mpkey)
146 mplayer_put_key(mpkey);
147 break;
148 case WM_CHAR:
149 mplayer_put_key(wParam);
150 break;
151 case WM_LBUTTONDOWN:
152 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
153 mplayer_put_key(MOUSE_BTN0);
154 break;
156 if (!vo_fs) {
157 ReleaseCapture();
158 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
159 return 0;
161 break;
162 case WM_MBUTTONDOWN:
163 if (!vo_nomouse_input)
164 mplayer_put_key(MOUSE_BTN1);
165 break;
166 case WM_RBUTTONDOWN:
167 if (!vo_nomouse_input)
168 mplayer_put_key(MOUSE_BTN2);
169 break;
170 case WM_MOUSEMOVE:
171 vo_mouse_movement(global_vo, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
172 break;
173 case WM_MOUSEWHEEL:
174 if (!vo_nomouse_input) {
175 int x = GET_WHEEL_DELTA_WPARAM(wParam);
176 if (x > 0)
177 mplayer_put_key(MOUSE_BTN3);
178 else
179 mplayer_put_key(MOUSE_BTN4);
181 break;
182 case WM_XBUTTONDOWN:
183 if (!vo_nomouse_input) {
184 int x = HIWORD(wParam);
185 if (x == 1)
186 mplayer_put_key(MOUSE_BTN5);
187 else // if (x == 2)
188 mplayer_put_key(MOUSE_BTN6);
190 break;
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) {
214 MSG msg;
215 event_flags = 0;
216 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
217 TranslateMessage(&msg);
218 DispatchMessage(&msg);
220 if (WinID >= 0) {
221 BOOL res;
222 RECT r;
223 POINT p;
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;
229 p.x = 0; p.y = 0;
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);
243 return event_flags;
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;
249 xinerama_y = r->top;
250 vo_screenwidth = r->right - r->left;
251 vo_screenheight = r->bottom - r->top;
252 if (mon_cnt == xinerama_screen)
253 return FALSE;
254 mon_cnt++;
255 return TRUE;
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:
267 * xinerama_x
268 * xinerama_y
269 * vo_screenwidth
270 * vo_screenheight
272 void w32_update_xinerama_info(void) {
273 xinerama_x = xinerama_y = 0;
274 if (xinerama_screen < -1) {
275 int tmp;
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) {
283 MONITORINFO mi;
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) {
292 mon_cnt = 0;
293 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
295 aspect_save_screenres(vo_screenwidth, vo_screenheight);
298 static void updateScreenProperties(void) {
299 DEVMODE dm;
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");
305 return;
308 vo_screenwidth = dm.dmPelsWidth;
309 vo_screenheight = dm.dmPelsHeight;
310 depthonscreen = dm.dmBitsPerPel;
311 w32_update_xinerama_info();
314 static void changeMode(void) {
315 DEVMODE dm;
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;
324 if (vo_vm) {
325 int bestMode = -1;
326 int bestScore = INT_MAX;
327 int i;
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) {
335 bestScore = score;
336 bestMode = i;
340 if (bestMode != -1)
341 EnumDisplaySettings(0, bestMode, &dm);
343 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
347 static void resetMode(void) {
348 if (vo_vm)
349 ChangeDisplaySettings(0, 0);
352 static int createRenderingContext(void) {
353 HWND layer = HWND_NOTOPMOST;
354 RECT r;
355 int style = (vo_border && !vo_fs) ?
356 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
358 if (WinID >= 0)
359 return 1;
361 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
362 if (vo_fs) {
363 changeMode();
364 while (ShowCursor(0) >= 0) /**/ ;
365 } else {
366 resetMode();
367 while (ShowCursor(1) < 0) /**/ ;
369 updateScreenProperties();
370 ShowWindow(vo_window, SW_HIDE);
371 SetWindowLong(vo_window, GWL_STYLE, style);
372 if (vo_fs) {
373 prev_width = vo_dwidth;
374 prev_height = vo_dheight;
375 prev_x = vo_dx;
376 prev_y = vo_dy;
377 vo_dwidth = vo_screenwidth;
378 vo_dheight = vo_screenheight;
379 vo_dx = xinerama_x;
380 vo_dy = xinerama_y;
381 } else {
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;
387 vo_dx = prev_x;
388 vo_dy = prev_y;
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.
393 SetFocus(vo_window);
395 r.left = vo_dx;
396 r.right = r.left + vo_dwidth;
397 r.top = vo_dy;
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);
401 return 1;
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:
411 * vo_fs
412 * vo_vm
414 * \return 1 - Success, 0 - Failure
416 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
417 PIXELFORMATDESCRIPTOR pfd;
418 int pf;
419 HDC vo_hdc = vo_w32_get_dc(vo_window);
421 memset(&pfd, 0, sizeof pfd);
422 pfd.nSize = sizeof pfd;
423 pfd.nVersion = 1;
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;
428 pfd.cColorBits = 24;
429 pfd.iLayerType = PFD_MAIN_PLANE;
430 pf = ChoosePixelFormat(vo_hdc, &pfd);
431 if (!pf) {
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);
434 return 0;
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)
442 return 1;
443 // store original size for videomode switching
444 o_dwidth = width;
445 o_dheight = height;
447 if (WinID < 0) {
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;
451 prev_x = vo_dx;
452 prev_y = vo_dy;
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) {
465 DISPLAY_DEVICE disp;
466 disp.cb = sizeof(disp);
467 EnumDisplayDevices(NULL, vo_adapter_num, &disp, 0);
468 if (disp.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
469 return NULL;
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:
483 * vo_w32_window
484 * vo_screenwidth
485 * vo_screenheight
487 * \return 1 = Success, 0 = Failure
489 int vo_w32_init(void) {
490 HICON mplayerIcon = 0;
491 char exedir[MAX_PATH];
492 HINSTANCE user32;
493 char *dev;
495 if (vo_window)
496 return 1;
498 hInstance = GetModuleHandle(0);
500 if (GetModuleFileName(0, exedir, MAX_PATH))
501 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
502 if (!mplayerIcon)
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");
510 return 0;
514 if (WinID >= 0)
516 RECT r;
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);
523 } else
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);
527 if (!vo_window) {
528 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
529 return 0;
532 myMonitorFromWindow = NULL;
533 myGetMonitorInfo = NULL;
534 myEnumDisplayMonitors = NULL;
535 user32 = GetModuleHandle("user32.dll");
536 if (user32) {
537 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
538 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
539 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
541 dev_hdc = 0;
542 dev = get_display_name();
543 if (dev) dev_hdc = CreateDC(dev, NULL, NULL, NULL);
544 free(dev);
545 updateScreenProperties();
547 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, depthonscreen);
549 return 1;
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:
562 * vo_dwidth
563 * vo_dheight
564 * vo_fs
567 void vo_w32_fullscreen(void) {
568 vo_fs = !vo_fs;
570 createRenderingContext();
574 * \brief Toogle window border attribute.
576 * Should be called on VOCTRL_BORDER event.
578 * Global libvo variables changed:
579 * vo_border
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:
592 * vo_ontop
594 void vo_w32_ontop( void )
596 vo_ontop = !vo_ontop;
597 if (!vo_fs) {
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");
611 resetMode();
612 ShowCursor(1);
613 depthonscreen = 0;
614 if (dev_hdc) DeleteDC(dev_hdc);
615 dev_hdc = 0;
616 DestroyWindow(vo_window);
617 vo_window = 0;
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;
628 return GetDC(wnd);
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) {
637 if (dev_hdc) return;
638 ReleaseDC(wnd, dc);