input: avoid printing --input=cmdlist/keylist output twice
[mplayer.git] / libvo / w32_common.c
blob475327ce5c637100494e3cebcee2f6a2ad3d7fbf
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 #define WIN_ID_TO_HWND(x) ((HWND)(uint32_t)(x))
46 static const char classname[] = "MPlayer - The Movie Player";
47 int vo_vm = 0;
49 static int depthonscreen;
50 // last non-fullscreen extends
51 static int prev_width;
52 static int prev_height;
53 static int prev_x;
54 static int prev_y;
56 static uint32_t o_dwidth;
57 static uint32_t o_dheight;
59 static HINSTANCE hInstance;
60 #define vo_window vo_w32_window
61 HWND vo_window = 0;
62 /** HDC used when rendering to a device instead of window */
63 static HDC dev_hdc;
64 static int event_flags;
65 static int mon_cnt;
67 static HMONITOR (WINAPI* myMonitorFromWindow)(HWND, DWORD);
68 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
69 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
71 static const struct mp_keymap vk_map[] = {
72 // special keys
73 {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
75 // cursor keys
76 {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
78 // navigation block
79 {VK_INSERT, KEY_INSERT}, {VK_DELETE, KEY_DELETE}, {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
80 {VK_PRIOR, KEY_PAGE_UP}, {VK_NEXT, KEY_PAGE_DOWN},
82 // F-keys
83 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
84 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
85 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F1, KEY_F+12},
86 // numpad
87 {VK_NUMPAD0, KEY_KP0}, {VK_NUMPAD1, KEY_KP1}, {VK_NUMPAD2, KEY_KP2},
88 {VK_NUMPAD3, KEY_KP3}, {VK_NUMPAD4, KEY_KP4}, {VK_NUMPAD5, KEY_KP5},
89 {VK_NUMPAD6, KEY_KP6}, {VK_NUMPAD7, KEY_KP7}, {VK_NUMPAD8, KEY_KP8},
90 {VK_NUMPAD9, KEY_KP9}, {VK_DECIMAL, KEY_KPDEC},
92 {0, 0}
95 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
96 RECT r;
97 POINT p;
98 int mpkey;
99 switch (message) {
100 case WM_ERASEBKGND: // no need to erase background seperately
101 return 1;
102 case WM_PAINT:
103 event_flags |= VO_EVENT_EXPOSE;
104 break;
105 case WM_MOVE:
106 event_flags |= VO_EVENT_MOVE;
107 p.x = 0;
108 p.y = 0;
109 ClientToScreen(vo_window, &p);
110 vo_dx = p.x;
111 vo_dy = p.y;
112 break;
113 case WM_SIZE:
114 event_flags |= VO_EVENT_RESIZE;
115 GetClientRect(vo_window, &r);
116 vo_dwidth = r.right;
117 vo_dheight = r.bottom;
118 break;
119 case WM_WINDOWPOSCHANGING:
120 if (vo_keepaspect && !vo_fs && WinID < 0) {
121 WINDOWPOS *wpos = (WINDOWPOS*)lParam;
122 int xborder, yborder;
123 r.left = r.top = 0;
124 r.right = wpos->cx;
125 r.bottom = wpos->cy;
126 AdjustWindowRect(&r, GetWindowLong(vo_window, GWL_STYLE), 0);
127 xborder = (r.right - r.left) - wpos->cx;
128 yborder = (r.bottom - r.top) - wpos->cy;
129 wpos->cx -= xborder; wpos->cy -= yborder;
130 aspect_fit(global_vo, &wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
131 wpos->cx += xborder; wpos->cy += yborder;
133 return 0;
134 case WM_CLOSE:
135 mplayer_put_key(KEY_CLOSE_WIN);
136 break;
137 case WM_SYSCOMMAND:
138 switch (wParam) {
139 case SC_SCREENSAVE:
140 case SC_MONITORPOWER:
141 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
142 return 0;
144 break;
145 case WM_KEYDOWN:
146 mpkey = lookup_keymap_table(vk_map, wParam);
147 if (mpkey)
148 mplayer_put_key(mpkey);
149 break;
150 case WM_CHAR:
151 mplayer_put_key(wParam);
152 break;
153 case WM_LBUTTONDOWN:
154 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
155 mplayer_put_key(MOUSE_BTN0);
156 break;
158 if (!vo_fs) {
159 ReleaseCapture();
160 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
161 return 0;
163 break;
164 case WM_MBUTTONDOWN:
165 if (!vo_nomouse_input)
166 mplayer_put_key(MOUSE_BTN1);
167 break;
168 case WM_RBUTTONDOWN:
169 if (!vo_nomouse_input)
170 mplayer_put_key(MOUSE_BTN2);
171 break;
172 case WM_MOUSEMOVE:
173 vo_mouse_movement(global_vo, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
174 break;
175 case WM_MOUSEWHEEL:
176 if (!vo_nomouse_input) {
177 int x = GET_WHEEL_DELTA_WPARAM(wParam);
178 if (x > 0)
179 mplayer_put_key(MOUSE_BTN3);
180 else
181 mplayer_put_key(MOUSE_BTN4);
183 break;
184 case WM_XBUTTONDOWN:
185 if (!vo_nomouse_input) {
186 int x = HIWORD(wParam);
187 if (x == 1)
188 mplayer_put_key(MOUSE_BTN5);
189 else // if (x == 2)
190 mplayer_put_key(MOUSE_BTN6);
192 break;
195 return DefWindowProc(hWnd, message, wParam, lParam);
199 * \brief Dispatch incoming window events and handle them.
201 * This function should be placed inside libvo's function "check_events".
203 * Global libvo variables changed:
204 * vo_dwidth: new window client area width
205 * vo_dheight: new window client area height
207 * \return int with these flags possibly set, take care to handle in the right order
208 * if it matters in your driver:
210 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
211 * driver render context accordingly.
212 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
213 * the window if the movie is paused.
215 int vo_w32_check_events(void) {
216 MSG msg;
217 event_flags = 0;
218 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
219 TranslateMessage(&msg);
220 DispatchMessage(&msg);
222 if (WinID >= 0) {
223 BOOL res;
224 RECT r;
225 POINT p;
226 res = GetClientRect(vo_window, &r);
227 if (res && (r.right != vo_dwidth || r.bottom != vo_dheight)) {
228 vo_dwidth = r.right; vo_dheight = r.bottom;
229 event_flags |= VO_EVENT_RESIZE;
231 p.x = 0; p.y = 0;
232 ClientToScreen(vo_window, &p);
233 if (p.x != vo_dx || p.y != vo_dy) {
234 vo_dx = p.x; vo_dy = p.y;
235 event_flags |= VO_EVENT_MOVE;
237 res = GetClientRect(WIN_ID_TO_HWND(WinID), &r);
238 if (res && (r.right != vo_dwidth || r.bottom != vo_dheight))
239 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
240 if (!IsWindow(WIN_ID_TO_HWND(WinID)))
241 // Window has probably been closed, e.g. due to program crash
242 mplayer_put_key(KEY_CLOSE_WIN);
245 return event_flags;
248 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
249 // this defaults to the last screen if specified number does not exist
250 xinerama_x = r->left;
251 xinerama_y = r->top;
252 vo_screenwidth = r->right - r->left;
253 vo_screenheight = r->bottom - r->top;
254 if (mon_cnt == xinerama_screen)
255 return FALSE;
256 mon_cnt++;
257 return TRUE;
261 * \brief Update screen information.
263 * This function should be called in libvo's "control" callback
264 * with parameter VOCTRL_UPDATE_SCREENINFO.
265 * Note that this also enables the new API where geometry and aspect
266 * calculations are done in video_out.c:config_video_out
268 * Global libvo variables changed:
269 * xinerama_x
270 * xinerama_y
271 * vo_screenwidth
272 * vo_screenheight
274 void w32_update_xinerama_info(void) {
275 xinerama_x = xinerama_y = 0;
276 if (xinerama_screen < -1) {
277 int tmp;
278 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
279 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
280 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
281 if (tmp) vo_screenwidth = tmp;
282 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
283 if (tmp) vo_screenheight = tmp;
284 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
285 MONITORINFO mi;
286 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
287 mi.cbSize = sizeof(mi);
288 myGetMonitorInfo(m, &mi);
289 xinerama_x = mi.rcMonitor.left;
290 xinerama_y = mi.rcMonitor.top;
291 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
292 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
293 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
294 mon_cnt = 0;
295 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
297 aspect_save_screenres(vo_screenwidth, vo_screenheight);
300 static void updateScreenProperties(void) {
301 DEVMODE dm;
302 dm.dmSize = sizeof dm;
303 dm.dmDriverExtra = 0;
304 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
305 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
306 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
307 return;
310 vo_screenwidth = dm.dmPelsWidth;
311 vo_screenheight = dm.dmPelsHeight;
312 depthonscreen = dm.dmBitsPerPel;
313 w32_update_xinerama_info();
316 static void changeMode(void) {
317 DEVMODE dm;
318 dm.dmSize = sizeof dm;
319 dm.dmDriverExtra = 0;
321 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
322 dm.dmBitsPerPel = depthonscreen;
323 dm.dmPelsWidth = vo_screenwidth;
324 dm.dmPelsHeight = vo_screenheight;
326 if (vo_vm) {
327 int bestMode = -1;
328 int bestScore = INT_MAX;
329 int i;
330 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
331 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
332 if (dm.dmBitsPerPel != depthonscreen) continue;
333 if (dm.dmPelsWidth < o_dwidth) continue;
334 if (dm.dmPelsHeight < o_dheight) continue;
336 if (score < bestScore) {
337 bestScore = score;
338 bestMode = i;
342 if (bestMode != -1)
343 EnumDisplaySettings(0, bestMode, &dm);
345 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
349 static void resetMode(void) {
350 if (vo_vm)
351 ChangeDisplaySettings(0, 0);
354 static int createRenderingContext(void) {
355 HWND layer = HWND_NOTOPMOST;
356 RECT r;
357 int style = (vo_border && !vo_fs) ?
358 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
360 if (WinID >= 0)
361 return 1;
363 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
364 if (vo_fs) {
365 changeMode();
366 while (ShowCursor(0) >= 0) /**/ ;
367 } else {
368 resetMode();
369 while (ShowCursor(1) < 0) /**/ ;
371 updateScreenProperties();
372 ShowWindow(vo_window, SW_HIDE);
373 SetWindowLong(vo_window, GWL_STYLE, style);
374 if (vo_fs) {
375 prev_width = vo_dwidth;
376 prev_height = vo_dheight;
377 prev_x = vo_dx;
378 prev_y = vo_dy;
379 vo_dwidth = vo_screenwidth;
380 vo_dheight = vo_screenheight;
381 vo_dx = xinerama_x;
382 vo_dy = xinerama_y;
383 } else {
384 // make sure there are no "stale" resize events
385 // that would set vo_d* to wrong values
386 vo_w32_check_events();
387 vo_dwidth = prev_width;
388 vo_dheight = prev_height;
389 vo_dx = prev_x;
390 vo_dy = prev_y;
391 // HACK around what probably is a windows focus bug:
392 // when pressing 'f' on the console, then 'f' again to
393 // return to windowed mode, any input into the video
394 // window is lost forever.
395 SetFocus(vo_window);
397 r.left = vo_dx;
398 r.right = r.left + vo_dwidth;
399 r.top = vo_dy;
400 r.bottom = r.top + vo_dheight;
401 AdjustWindowRect(&r, style, 0);
402 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
403 return 1;
407 * \brief Configure and show window on the screen.
409 * This function should be called in libvo's "config" callback.
410 * It configures a window and shows it on the screen.
412 * Global libvo variables changed:
413 * vo_fs
414 * vo_vm
416 * \return 1 - Success, 0 - Failure
418 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
419 PIXELFORMATDESCRIPTOR pfd;
420 int pf;
421 HDC vo_hdc = vo_w32_get_dc(vo_window);
423 memset(&pfd, 0, sizeof pfd);
424 pfd.nSize = sizeof pfd;
425 pfd.nVersion = 1;
426 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
427 if (flags & VOFLAG_STEREO)
428 pfd.dwFlags |= PFD_STEREO;
429 pfd.iPixelType = PFD_TYPE_RGBA;
430 pfd.cColorBits = 24;
431 pfd.iLayerType = PFD_MAIN_PLANE;
432 pf = ChoosePixelFormat(vo_hdc, &pfd);
433 if (!pf) {
434 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
435 vo_w32_release_dc(vo_window, vo_hdc);
436 return 0;
439 SetPixelFormat(vo_hdc, pf, &pfd);
440 vo_w32_release_dc(vo_window, vo_hdc);
442 // we already have a fully initialized window, so nothing needs to be done
443 if (flags & VOFLAG_HIDDEN)
444 return 1;
445 // store original size for videomode switching
446 o_dwidth = width;
447 o_dheight = height;
449 if (WinID < 0) {
450 // the desired size is ignored in wid mode, it always matches the window size.
451 prev_width = vo_dwidth = width;
452 prev_height = vo_dheight = height;
453 prev_x = vo_dx;
454 prev_y = vo_dy;
457 vo_fs = flags & VOFLAG_FULLSCREEN;
458 vo_vm = flags & VOFLAG_MODESWITCHING;
459 return createRenderingContext();
463 * \brief return the name of the selected device if it is indepedant
464 * \return pointer to string, must be freed.
466 static char *get_display_name(void) {
467 DISPLAY_DEVICE disp;
468 disp.cb = sizeof(disp);
469 EnumDisplayDevices(NULL, vo_adapter_num, &disp, 0);
470 if (disp.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
471 return NULL;
472 return strdup(disp.DeviceName);
476 * \brief Initialize w32_common framework.
478 * The first function that should be called from the w32_common framework.
479 * It handles window creation on the screen with proper title and attributes.
480 * It also initializes the framework's internal variables. The function should
481 * be called after your own preinit initialization and you shouldn't do any
482 * window management on your own.
484 * Global libvo variables changed:
485 * vo_w32_window
486 * vo_screenwidth
487 * vo_screenheight
489 * \return 1 = Success, 0 = Failure
491 int vo_w32_init(void) {
492 HICON mplayerIcon = 0;
493 char exedir[MAX_PATH];
494 HINSTANCE user32;
495 char *dev;
497 if (vo_window)
498 return 1;
500 hInstance = GetModuleHandle(0);
502 if (GetModuleFileName(0, exedir, MAX_PATH))
503 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
504 if (!mplayerIcon)
505 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
508 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
510 if (!RegisterClassEx(&wcex)) {
511 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
512 return 0;
516 if (WinID >= 0)
518 RECT r;
519 GetClientRect(WIN_ID_TO_HWND(WinID), &r);
520 vo_dwidth = r.right; vo_dheight = r.bottom;
521 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
522 WS_CHILD | WS_VISIBLE, 0, 0, vo_dwidth, vo_dheight,
523 WIN_ID_TO_HWND(WinID), 0, hInstance, 0);
524 EnableWindow(vo_window, 0);
525 } else
526 vo_window = CreateWindowEx(0, classname, classname,
527 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
528 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
529 if (!vo_window) {
530 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
531 return 0;
534 myMonitorFromWindow = NULL;
535 myGetMonitorInfo = NULL;
536 myEnumDisplayMonitors = NULL;
537 user32 = GetModuleHandle("user32.dll");
538 if (user32) {
539 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
540 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
541 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
543 dev_hdc = 0;
544 dev = get_display_name();
545 if (dev) dev_hdc = CreateDC(dev, NULL, NULL, NULL);
546 free(dev);
547 updateScreenProperties();
549 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, depthonscreen);
551 return 1;
555 * \brief Toogle fullscreen / windowed mode.
557 * Should be called on VOCTRL_FULLSCREEN event. The window is
558 * always resized after this call, so the rendering context
559 * should be reinitialized with the new dimensions.
560 * It is unspecified if vo_check_events will create a resize
561 * event in addition or not.
563 * Global libvo variables changed:
564 * vo_dwidth
565 * vo_dheight
566 * vo_fs
569 void vo_w32_fullscreen(void) {
570 vo_fs = !vo_fs;
572 createRenderingContext();
576 * \brief Toogle window border attribute.
578 * Should be called on VOCTRL_BORDER event.
580 * Global libvo variables changed:
581 * vo_border
583 void vo_w32_border(void) {
584 vo_border = !vo_border;
585 createRenderingContext();
589 * \brief Toogle window ontop attribute.
591 * Should be called on VOCTRL_ONTOP event.
593 * Global libvo variables changed:
594 * vo_ontop
596 void vo_w32_ontop( void )
598 vo_ontop = !vo_ontop;
599 if (!vo_fs) {
600 createRenderingContext();
605 * \brief Uninitialize w32_common framework.
607 * Should be called last in video driver's uninit function. First release
608 * anything built on top of the created window e.g. rendering context inside
609 * and call vo_w32_uninit at the end.
611 void vo_w32_uninit(void) {
612 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
613 resetMode();
614 ShowCursor(1);
615 depthonscreen = 0;
616 if (dev_hdc) DeleteDC(dev_hdc);
617 dev_hdc = 0;
618 DestroyWindow(vo_window);
619 vo_window = 0;
620 UnregisterClass(classname, 0);
624 * \brief get a device context to draw in
626 * \param wnd window the DC should belong to if it makes sense
628 HDC vo_w32_get_dc(HWND wnd) {
629 if (dev_hdc) return dev_hdc;
630 return GetDC(wnd);
634 * \brief release a device context
636 * \param wnd window the DC probably belongs to
638 void vo_w32_release_dc(HWND wnd, HDC dc) {
639 if (dev_hdc) return;
640 ReleaseDC(wnd, dc);