stream: show negative seek position value in error message
[mplayer.git] / libvo / w32_common.c
blob0c512f3d2d2c058dcf1344f2418694b4e65647d0
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 p.x = 0;
105 p.y = 0;
106 ClientToScreen(vo_window, &p);
107 vo_dx = p.x;
108 vo_dy = p.y;
109 break;
110 case WM_SIZE:
111 event_flags |= VO_EVENT_RESIZE;
112 GetClientRect(vo_window, &r);
113 vo_dwidth = r.right;
114 vo_dheight = r.bottom;
115 break;
116 case WM_WINDOWPOSCHANGING:
117 if (vo_keepaspect && !vo_fs && WinID < 0) {
118 WINDOWPOS *wpos = lParam;
119 int xborder, yborder;
120 r.left = r.top = 0;
121 r.right = wpos->cx;
122 r.bottom = wpos->cy;
123 AdjustWindowRect(&r, GetWindowLong(vo_window, GWL_STYLE), 0);
124 xborder = (r.right - r.left) - wpos->cx;
125 yborder = (r.bottom - r.top) - wpos->cy;
126 wpos->cx -= xborder; wpos->cy -= yborder;
127 aspect_fit(global_vo, &wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
128 wpos->cx += xborder; wpos->cy += yborder;
130 return 0;
131 case WM_CLOSE:
132 mplayer_put_key(KEY_CLOSE_WIN);
133 break;
134 case WM_SYSCOMMAND:
135 switch (wParam) {
136 case SC_SCREENSAVE:
137 case SC_MONITORPOWER:
138 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
139 return 0;
141 break;
142 case WM_KEYDOWN:
143 mpkey = lookup_keymap_table(vk_map, wParam);
144 if (mpkey)
145 mplayer_put_key(mpkey);
146 break;
147 case WM_CHAR:
148 mplayer_put_key(wParam);
149 break;
150 case WM_LBUTTONDOWN:
151 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
152 mplayer_put_key(MOUSE_BTN0);
153 break;
155 if (!vo_fs) {
156 ReleaseCapture();
157 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
158 return 0;
160 break;
161 case WM_MBUTTONDOWN:
162 if (!vo_nomouse_input)
163 mplayer_put_key(MOUSE_BTN1);
164 break;
165 case WM_RBUTTONDOWN:
166 if (!vo_nomouse_input)
167 mplayer_put_key(MOUSE_BTN2);
168 break;
169 case WM_MOUSEMOVE:
170 vo_mouse_movement(global_vo, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
171 break;
172 case WM_MOUSEWHEEL:
173 if (!vo_nomouse_input) {
174 int x = GET_WHEEL_DELTA_WPARAM(wParam);
175 if (x > 0)
176 mplayer_put_key(MOUSE_BTN3);
177 else
178 mplayer_put_key(MOUSE_BTN4);
180 break;
181 case WM_XBUTTONDOWN:
182 if (!vo_nomouse_input) {
183 int x = HIWORD(wParam);
184 if (x == 1)
185 mplayer_put_key(MOUSE_BTN5);
186 else // if (x == 2)
187 mplayer_put_key(MOUSE_BTN6);
189 break;
192 return DefWindowProc(hWnd, message, wParam, lParam);
196 * \brief Dispatch incoming window events and handle them.
198 * This function should be placed inside libvo's function "check_events".
200 * Global libvo variables changed:
201 * vo_dwidth: new window client area width
202 * vo_dheight: new window client area height
204 * \return int with these flags possibly set, take care to handle in the right order
205 * if it matters in your driver:
207 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
208 * driver render context accordingly.
209 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
210 * the window if the movie is paused.
212 int vo_w32_check_events(void) {
213 MSG msg;
214 event_flags = 0;
215 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
216 TranslateMessage(&msg);
217 DispatchMessage(&msg);
219 if (WinID >= 0) {
220 BOOL res;
221 RECT r;
222 res = GetClientRect(vo_window, &r);
223 if (res && (r.right != vo_dwidth || r.bottom != vo_dheight)) {
224 vo_dwidth = r.right; vo_dheight = r.bottom;
225 event_flags |= VO_EVENT_RESIZE;
227 res = GetClientRect(WinID, &r);
228 if (res && (r.right != vo_dwidth || r.bottom != vo_dheight))
229 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
230 if (!IsWindow(WinID))
231 // Window has probably been closed, e.g. due to program crash
232 mplayer_put_key(KEY_CLOSE_WIN);
235 return event_flags;
238 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
239 // this defaults to the last screen if specified number does not exist
240 xinerama_x = r->left;
241 xinerama_y = r->top;
242 vo_screenwidth = r->right - r->left;
243 vo_screenheight = r->bottom - r->top;
244 if (mon_cnt == xinerama_screen)
245 return FALSE;
246 mon_cnt++;
247 return TRUE;
251 * \brief Update screen information.
253 * This function should be called in libvo's "control" callback
254 * with parameter VOCTRL_UPDATE_SCREENINFO.
255 * Note that this also enables the new API where geometry and aspect
256 * calculations are done in video_out.c:config_video_out
258 * Global libvo variables changed:
259 * xinerama_x
260 * xinerama_y
261 * vo_screenwidth
262 * vo_screenheight
264 void w32_update_xinerama_info(void) {
265 xinerama_x = xinerama_y = 0;
266 if (xinerama_screen < -1) {
267 int tmp;
268 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
269 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
270 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
271 if (tmp) vo_screenwidth = tmp;
272 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
273 if (tmp) vo_screenheight = tmp;
274 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
275 MONITORINFO mi;
276 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
277 mi.cbSize = sizeof(mi);
278 myGetMonitorInfo(m, &mi);
279 xinerama_x = mi.rcMonitor.left;
280 xinerama_y = mi.rcMonitor.top;
281 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
282 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
283 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
284 mon_cnt = 0;
285 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
287 aspect_save_screenres(vo_screenwidth, vo_screenheight);
290 static void updateScreenProperties(void) {
291 DEVMODE dm;
292 dm.dmSize = sizeof dm;
293 dm.dmDriverExtra = 0;
294 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
295 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
296 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
297 return;
300 vo_screenwidth = dm.dmPelsWidth;
301 vo_screenheight = dm.dmPelsHeight;
302 depthonscreen = dm.dmBitsPerPel;
303 w32_update_xinerama_info();
306 static void changeMode(void) {
307 DEVMODE dm;
308 dm.dmSize = sizeof dm;
309 dm.dmDriverExtra = 0;
311 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
312 dm.dmBitsPerPel = depthonscreen;
313 dm.dmPelsWidth = vo_screenwidth;
314 dm.dmPelsHeight = vo_screenheight;
316 if (vo_vm) {
317 int bestMode = -1;
318 int bestScore = INT_MAX;
319 int i;
320 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
321 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
322 if (dm.dmBitsPerPel != depthonscreen) continue;
323 if (dm.dmPelsWidth < o_dwidth) continue;
324 if (dm.dmPelsHeight < o_dheight) continue;
326 if (score < bestScore) {
327 bestScore = score;
328 bestMode = i;
332 if (bestMode != -1)
333 EnumDisplaySettings(0, bestMode, &dm);
335 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
339 static void resetMode(void) {
340 if (vo_vm)
341 ChangeDisplaySettings(0, 0);
344 static int createRenderingContext(void) {
345 HWND layer = HWND_NOTOPMOST;
346 RECT r;
347 int style = (vo_border && !vo_fs) ?
348 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
350 if (WinID >= 0)
351 return 1;
353 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
354 if (vo_fs) {
355 changeMode();
356 while (ShowCursor(0) >= 0) /**/ ;
357 } else {
358 resetMode();
359 while (ShowCursor(1) < 0) /**/ ;
361 updateScreenProperties();
362 ShowWindow(vo_window, SW_HIDE);
363 SetWindowLong(vo_window, GWL_STYLE, style);
364 if (vo_fs) {
365 prev_width = vo_dwidth;
366 prev_height = vo_dheight;
367 prev_x = vo_dx;
368 prev_y = vo_dy;
369 vo_dwidth = vo_screenwidth;
370 vo_dheight = vo_screenheight;
371 vo_dx = xinerama_x;
372 vo_dy = xinerama_y;
373 } else {
374 // make sure there are no "stale" resize events
375 // that would set vo_d* to wrong values
376 vo_w32_check_events();
377 vo_dwidth = prev_width;
378 vo_dheight = prev_height;
379 vo_dx = prev_x;
380 vo_dy = prev_y;
381 // HACK around what probably is a windows focus bug:
382 // when pressing 'f' on the console, then 'f' again to
383 // return to windowed mode, any input into the video
384 // window is lost forever.
385 SetFocus(vo_window);
387 r.left = vo_dx;
388 r.right = r.left + vo_dwidth;
389 r.top = vo_dy;
390 r.bottom = r.top + vo_dheight;
391 AdjustWindowRect(&r, style, 0);
392 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
393 return 1;
397 * \brief Configure and show window on the screen.
399 * This function should be called in libvo's "config" callback.
400 * It configures a window and shows it on the screen.
402 * Global libvo variables changed:
403 * vo_fs
404 * vo_vm
406 * \return 1 - Success, 0 - Failure
408 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
409 PIXELFORMATDESCRIPTOR pfd;
410 int pf;
411 HDC vo_hdc = vo_w32_get_dc(vo_window);
413 memset(&pfd, 0, sizeof pfd);
414 pfd.nSize = sizeof pfd;
415 pfd.nVersion = 1;
416 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
417 if (flags & VOFLAG_STEREO)
418 pfd.dwFlags |= PFD_STEREO;
419 pfd.iPixelType = PFD_TYPE_RGBA;
420 pfd.cColorBits = 24;
421 pfd.iLayerType = PFD_MAIN_PLANE;
422 pf = ChoosePixelFormat(vo_hdc, &pfd);
423 if (!pf) {
424 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
425 vo_w32_release_dc(vo_window, vo_hdc);
426 return 0;
429 SetPixelFormat(vo_hdc, pf, &pfd);
430 vo_w32_release_dc(vo_window, vo_hdc);
432 // we already have a fully initialized window, so nothing needs to be done
433 if (flags & VOFLAG_HIDDEN)
434 return 1;
435 // store original size for videomode switching
436 o_dwidth = width;
437 o_dheight = height;
439 if (WinID < 0) {
440 // the desired size is ignored in wid mode, it always matches the window size.
441 prev_width = vo_dwidth = width;
442 prev_height = vo_dheight = height;
443 prev_x = vo_dx;
444 prev_y = vo_dy;
447 vo_fs = flags & VOFLAG_FULLSCREEN;
448 vo_vm = flags & VOFLAG_MODESWITCHING;
449 return createRenderingContext();
453 * \brief return the name of the selected device if it is indepedant
454 * \return pointer to string, must be freed.
456 static char *get_display_name(void) {
457 DISPLAY_DEVICE disp;
458 disp.cb = sizeof(disp);
459 EnumDisplayDevices(NULL, vo_adapter_num, &disp, 0);
460 if (disp.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
461 return NULL;
462 return strdup(disp.DeviceName);
466 * \brief Initialize w32_common framework.
468 * The first function that should be called from the w32_common framework.
469 * It handles window creation on the screen with proper title and attributes.
470 * It also initializes the framework's internal variables. The function should
471 * be called after your own preinit initialization and you shouldn't do any
472 * window management on your own.
474 * Global libvo variables changed:
475 * vo_w32_window
476 * vo_screenwidth
477 * vo_screenheight
479 * \return 1 = Success, 0 = Failure
481 int vo_w32_init(void) {
482 HICON mplayerIcon = 0;
483 char exedir[MAX_PATH];
484 HINSTANCE user32;
485 char *dev;
487 if (vo_window)
488 return 1;
490 hInstance = GetModuleHandle(0);
492 if (GetModuleFileName(0, exedir, MAX_PATH))
493 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
494 if (!mplayerIcon)
495 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
498 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
500 if (!RegisterClassEx(&wcex)) {
501 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
502 return 0;
506 if (WinID >= 0)
508 RECT r;
509 GetClientRect(WinID, &r);
510 vo_dwidth = r.right; vo_dheight = r.bottom;
511 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
512 WS_CHILD | WS_VISIBLE,
513 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
514 EnableWindow(vo_window, 0);
515 } else
516 vo_window = CreateWindowEx(0, classname, classname,
517 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
518 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
519 if (!vo_window) {
520 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
521 return 0;
524 myMonitorFromWindow = NULL;
525 myGetMonitorInfo = NULL;
526 myEnumDisplayMonitors = NULL;
527 user32 = GetModuleHandle("user32.dll");
528 if (user32) {
529 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
530 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
531 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
533 dev_hdc = 0;
534 dev = get_display_name();
535 if (dev) dev_hdc = CreateDC(dev, NULL, NULL, NULL);
536 free(dev);
537 updateScreenProperties();
539 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, depthonscreen);
541 return 1;
545 * \brief Toogle fullscreen / windowed mode.
547 * Should be called on VOCTRL_FULLSCREEN event. The window is
548 * always resized after this call, so the rendering context
549 * should be reinitialized with the new dimensions.
550 * It is unspecified if vo_check_events will create a resize
551 * event in addition or not.
553 * Global libvo variables changed:
554 * vo_dwidth
555 * vo_dheight
556 * vo_fs
559 void vo_w32_fullscreen(void) {
560 vo_fs = !vo_fs;
562 createRenderingContext();
566 * \brief Toogle window border attribute.
568 * Should be called on VOCTRL_BORDER event.
570 * Global libvo variables changed:
571 * vo_border
573 void vo_w32_border(void) {
574 vo_border = !vo_border;
575 createRenderingContext();
579 * \brief Toogle window ontop attribute.
581 * Should be called on VOCTRL_ONTOP event.
583 * Global libvo variables changed:
584 * vo_ontop
586 void vo_w32_ontop( void )
588 vo_ontop = !vo_ontop;
589 if (!vo_fs) {
590 createRenderingContext();
595 * \brief Uninitialize w32_common framework.
597 * Should be called last in video driver's uninit function. First release
598 * anything built on top of the created window e.g. rendering context inside
599 * and call vo_w32_uninit at the end.
601 void vo_w32_uninit(void) {
602 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
603 resetMode();
604 ShowCursor(1);
605 depthonscreen = 0;
606 if (dev_hdc) DeleteDC(dev_hdc);
607 dev_hdc = 0;
608 DestroyWindow(vo_window);
609 vo_window = 0;
610 UnregisterClass(classname, 0);
614 * \brief get a device context to draw in
616 * \param wnd window the DC should belong to if it makes sense
618 HDC vo_w32_get_dc(HWND wnd) {
619 if (dev_hdc) return dev_hdc;
620 return GetDC(wnd);
624 * \brief release a device context
626 * \param wnd window the DC probably belongs to
628 void vo_w32_release_dc(HWND wnd, HDC dc) {
629 if (dev_hdc) return;
630 ReleaseDC(wnd, dc);