Merge svn changes up to r30165
[mplayer/glamo.git] / libvo / w32_common.c
blob3b5f97de298753db6fb6a48b4688554f5f45c54f
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 "osdep/keycodes.h"
27 #include "input/input.h"
28 #include "input/mouse.h"
29 #include "mp_msg.h"
30 #include "video_out.h"
31 #include "aspect.h"
32 #include "w32_common.h"
33 #include "mp_fifo.h"
35 extern int enable_mouse_movements;
37 #ifndef MONITOR_DEFAULTTOPRIMARY
38 #define MONITOR_DEFAULTTOPRIMARY 1
39 #endif
41 static const char classname[] = "MPlayer - The Movie Player";
42 int vo_vm = 0;
44 static int depthonscreen;
45 // last non-fullscreen extends
46 static int prev_width;
47 static int prev_height;
48 static int prev_x;
49 static int prev_y;
51 static uint32_t o_dwidth;
52 static uint32_t o_dheight;
54 static HINSTANCE hInstance;
55 #define vo_window vo_w32_window
56 HWND vo_window = 0;
57 /** HDC used when rendering to a device instead of window */
58 static HDC dev_hdc;
59 static int event_flags;
60 static int mon_cnt;
62 static HMONITOR (WINAPI* myMonitorFromWindow)(HWND, DWORD);
63 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
64 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
66 static const struct keymap vk_map[] = {
67 // special keys
68 {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
70 // cursor keys
71 {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
73 // navigation block
74 {VK_INSERT, KEY_INSERT}, {VK_DELETE, KEY_DELETE}, {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
75 {VK_PRIOR, KEY_PAGE_UP}, {VK_NEXT, KEY_PAGE_DOWN},
77 // F-keys
78 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
79 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
80 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F1, KEY_F+12},
81 // numpad
82 {VK_NUMPAD0, KEY_KP0}, {VK_NUMPAD1, KEY_KP1}, {VK_NUMPAD2, KEY_KP2},
83 {VK_NUMPAD3, KEY_KP3}, {VK_NUMPAD4, KEY_KP4}, {VK_NUMPAD5, KEY_KP5},
84 {VK_NUMPAD6, KEY_KP6}, {VK_NUMPAD7, KEY_KP7}, {VK_NUMPAD8, KEY_KP8},
85 {VK_NUMPAD9, KEY_KP9}, {VK_DECIMAL, KEY_KPDEC},
87 {0, 0}
90 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
91 RECT r;
92 POINT p;
93 int mpkey;
94 switch (message) {
95 case WM_ERASEBKGND: // no need to erase background seperately
96 return 1;
97 case WM_PAINT:
98 event_flags |= VO_EVENT_EXPOSE;
99 break;
100 case WM_MOVE:
101 p.x = 0;
102 p.y = 0;
103 ClientToScreen(vo_window, &p);
104 vo_dx = p.x;
105 vo_dy = p.y;
106 break;
107 case WM_SIZE:
108 event_flags |= VO_EVENT_RESIZE;
109 GetClientRect(vo_window, &r);
110 vo_dwidth = r.right;
111 vo_dheight = r.bottom;
112 break;
113 case WM_WINDOWPOSCHANGING:
114 if (vo_keepaspect && !vo_fs && WinID < 0) {
115 WINDOWPOS *wpos = lParam;
116 int xborder, yborder;
117 r.left = r.top = 0;
118 r.right = wpos->cx;
119 r.bottom = wpos->cy;
120 AdjustWindowRect(&r, GetWindowLong(vo_window, GWL_STYLE), 0);
121 xborder = (r.right - r.left) - wpos->cx;
122 yborder = (r.bottom - r.top) - wpos->cy;
123 wpos->cx -= xborder; wpos->cy -= yborder;
124 aspect_fit(global_vo, &wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
125 wpos->cx += xborder; wpos->cy += yborder;
127 return 0;
128 case WM_CLOSE:
129 mplayer_put_key(KEY_CLOSE_WIN);
130 break;
131 case WM_SYSCOMMAND:
132 switch (wParam) {
133 case SC_SCREENSAVE:
134 case SC_MONITORPOWER:
135 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
136 return 0;
138 break;
139 case WM_KEYDOWN:
140 mpkey = lookup_keymap_table(vk_map, wParam);
141 if (mpkey)
142 mplayer_put_key(mpkey);
143 break;
144 case WM_CHAR:
145 mplayer_put_key(wParam);
146 break;
147 case WM_LBUTTONDOWN:
148 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
149 mplayer_put_key(MOUSE_BTN0);
150 break;
152 if (!vo_fs) {
153 ReleaseCapture();
154 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
155 return 0;
157 break;
158 case WM_MBUTTONDOWN:
159 if (!vo_nomouse_input)
160 mplayer_put_key(MOUSE_BTN1);
161 break;
162 case WM_RBUTTONDOWN:
163 if (!vo_nomouse_input)
164 mplayer_put_key(MOUSE_BTN2);
165 break;
166 case WM_MOUSEMOVE:
167 if (enable_mouse_movements) {
168 char cmd_str[40];
169 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i",
170 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
171 mp_input_queue_cmd(global_vo->input_ctx, mp_input_parse_cmd(cmd_str));
173 break;
174 case WM_MOUSEWHEEL:
175 if (!vo_nomouse_input) {
176 int x = GET_WHEEL_DELTA_WPARAM(wParam);
177 if (x > 0)
178 mplayer_put_key(MOUSE_BTN3);
179 else
180 mplayer_put_key(MOUSE_BTN4);
181 break;
185 return DefWindowProc(hWnd, message, wParam, lParam);
189 * \brief Dispatch incoming window events and handle them.
191 * This function should be placed inside libvo's function "check_events".
193 * Global libvo variables changed:
194 * vo_dwidth: new window client area width
195 * vo_dheight: new window client area height
197 * \return int with these flags possibly set, take care to handle in the right order
198 * if it matters in your driver:
200 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
201 * driver render context accordingly.
202 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
203 * the window if the movie is paused.
205 int vo_w32_check_events(void) {
206 MSG msg;
207 event_flags = 0;
208 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
209 TranslateMessage(&msg);
210 DispatchMessage(&msg);
212 if (WinID >= 0) {
213 RECT r;
214 GetClientRect(vo_window, &r);
215 if (r.right != vo_dwidth || r.bottom != vo_dheight) {
216 vo_dwidth = r.right; vo_dheight = r.bottom;
217 event_flags |= VO_EVENT_RESIZE;
219 GetClientRect(WinID, &r);
220 if (r.right != vo_dwidth || r.bottom != vo_dheight)
221 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
224 return event_flags;
227 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
228 // this defaults to the last screen if specified number does not exist
229 xinerama_x = r->left;
230 xinerama_y = r->top;
231 vo_screenwidth = r->right - r->left;
232 vo_screenheight = r->bottom - r->top;
233 if (mon_cnt == xinerama_screen)
234 return FALSE;
235 mon_cnt++;
236 return TRUE;
240 * \brief Update screen information.
242 * This function should be called in libvo's "control" callback
243 * with parameter VOCTRL_UPDATE_SCREENINFO.
244 * Note that this also enables the new API where geometry and aspect
245 * calculations are done in video_out.c:config_video_out
247 * Global libvo variables changed:
248 * xinerama_x
249 * xinerama_y
250 * vo_screenwidth
251 * vo_screenheight
253 void w32_update_xinerama_info(void) {
254 xinerama_x = xinerama_y = 0;
255 if (xinerama_screen < -1) {
256 int tmp;
257 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
258 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
259 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
260 if (tmp) vo_screenwidth = tmp;
261 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
262 if (tmp) vo_screenheight = tmp;
263 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
264 MONITORINFO mi;
265 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
266 mi.cbSize = sizeof(mi);
267 myGetMonitorInfo(m, &mi);
268 xinerama_x = mi.rcMonitor.left;
269 xinerama_y = mi.rcMonitor.top;
270 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
271 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
272 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
273 mon_cnt = 0;
274 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
276 aspect_save_screenres(vo_screenwidth, vo_screenheight);
279 static void updateScreenProperties(void) {
280 DEVMODE dm;
281 dm.dmSize = sizeof dm;
282 dm.dmDriverExtra = 0;
283 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
284 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
285 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
286 return;
289 vo_screenwidth = dm.dmPelsWidth;
290 vo_screenheight = dm.dmPelsHeight;
291 depthonscreen = dm.dmBitsPerPel;
292 w32_update_xinerama_info();
295 static void changeMode(void) {
296 DEVMODE dm;
297 dm.dmSize = sizeof dm;
298 dm.dmDriverExtra = 0;
300 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
301 dm.dmBitsPerPel = depthonscreen;
302 dm.dmPelsWidth = vo_screenwidth;
303 dm.dmPelsHeight = vo_screenheight;
305 if (vo_vm) {
306 int bestMode = -1;
307 int bestScore = INT_MAX;
308 int i;
309 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
310 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
311 if (dm.dmBitsPerPel != depthonscreen) continue;
312 if (dm.dmPelsWidth < o_dwidth) continue;
313 if (dm.dmPelsHeight < o_dheight) continue;
315 if (score < bestScore) {
316 bestScore = score;
317 bestMode = i;
321 if (bestMode != -1)
322 EnumDisplaySettings(0, bestMode, &dm);
324 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
328 static void resetMode(void) {
329 if (vo_vm)
330 ChangeDisplaySettings(0, 0);
333 static int createRenderingContext(void) {
334 HWND layer = HWND_NOTOPMOST;
335 PIXELFORMATDESCRIPTOR pfd;
336 HDC vo_hdc = vo_w32_get_dc(vo_window);
337 RECT r;
338 int pf;
339 if (WinID < 0) {
340 int style = (vo_border && !vo_fs) ?
341 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
343 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
344 if (vo_fs) {
345 changeMode();
346 while (ShowCursor(0) >= 0) /**/ ;
347 } else {
348 resetMode();
349 while (ShowCursor(1) < 0) /**/ ;
351 updateScreenProperties();
352 ShowWindow(vo_window, SW_HIDE);
353 SetWindowLong(vo_window, GWL_STYLE, style);
354 if (vo_fs) {
355 prev_width = vo_dwidth;
356 prev_height = vo_dheight;
357 prev_x = vo_dx;
358 prev_y = vo_dy;
359 vo_dwidth = vo_screenwidth;
360 vo_dheight = vo_screenheight;
361 vo_dx = xinerama_x;
362 vo_dy = xinerama_y;
363 } else {
364 // make sure there are no "stale" resize events
365 // that would set vo_d* to wrong values
366 vo_w32_check_events();
367 vo_dwidth = prev_width;
368 vo_dheight = prev_height;
369 vo_dx = prev_x;
370 vo_dy = prev_y;
371 // HACK around what probably is a windows focus bug:
372 // when pressing 'f' on the console, then 'f' again to
373 // return to windowed mode, any input into the video
374 // window is lost forever.
375 SetFocus(vo_window);
377 r.left = vo_dx;
378 r.right = r.left + vo_dwidth;
379 r.top = vo_dy;
380 r.bottom = r.top + vo_dheight;
381 AdjustWindowRect(&r, style, 0);
382 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
385 memset(&pfd, 0, sizeof pfd);
386 pfd.nSize = sizeof pfd;
387 pfd.nVersion = 1;
388 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
389 pfd.iPixelType = PFD_TYPE_RGBA;
390 pfd.cColorBits = 24;
391 pfd.iLayerType = PFD_MAIN_PLANE;
392 pf = ChoosePixelFormat(vo_hdc, &pfd);
393 if (!pf) {
394 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
395 ReleaseDC(vo_window, vo_hdc);
396 return 0;
399 SetPixelFormat(vo_hdc, pf, &pfd);
401 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, depthonscreen);
403 vo_w32_release_dc(vo_window, vo_hdc);
404 return 1;
408 * \brief Configure and show window on the screen.
410 * This function should be called in libvo's "config" callback.
411 * It configures a window and shows it on the screen.
413 * Global libvo variables changed:
414 * vo_fs
415 * vo_vm
417 * \return 1 - Success, 0 - Failure
419 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
420 // store original size for videomode switching
421 o_dwidth = width;
422 o_dheight = height;
424 if (WinID < 0) {
425 // the desired size is ignored in wid mode, it always matches the window size.
426 prev_width = vo_dwidth = width;
427 prev_height = vo_dheight = height;
428 prev_x = vo_dx;
429 prev_y = vo_dy;
432 vo_fs = flags & VOFLAG_FULLSCREEN;
433 vo_vm = flags & VOFLAG_MODESWITCHING;
434 return createRenderingContext();
438 * \brief return the name of the selected device if it is indepedant
440 static char *get_display_name(void) {
441 DISPLAY_DEVICE disp;
442 disp.cb = sizeof(disp);
443 EnumDisplayDevices(NULL, vo_adapter_num, &disp, 0);
444 if (disp.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
445 return NULL;
446 return disp.DeviceName;
450 * \brief Initialize w32_common framework.
452 * The first function that should be called from the w32_common framework.
453 * It handles window creation on the screen with proper title and attributes.
454 * It also initializes the framework's internal variables. The function should
455 * be called after your own preinit initialization and you shouldn't do any
456 * window management on your own.
458 * Global libvo variables changed:
459 * vo_w32_window
460 * vo_depthonscreen
461 * vo_screenwidth
462 * vo_screenheight
464 * \return 1 = Success, 0 = Failure
466 int vo_w32_init(void) {
467 HICON mplayerIcon = 0;
468 char exedir[MAX_PATH];
469 HINSTANCE user32;
470 char *dev;
472 if (vo_window)
473 return 1;
475 hInstance = GetModuleHandle(0);
477 if (GetModuleFileName(0, exedir, MAX_PATH))
478 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
479 if (!mplayerIcon)
480 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
483 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
485 if (!RegisterClassEx(&wcex)) {
486 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
487 return 0;
491 if (WinID >= 0)
493 RECT r;
494 GetClientRect(WinID, &r);
495 vo_dwidth = r.right; vo_dheight = r.bottom;
496 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
497 WS_CHILD | WS_VISIBLE,
498 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
499 EnableWindow(vo_window, 0);
500 } else
501 vo_window = CreateWindowEx(0, classname, classname,
502 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
503 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
504 if (!vo_window) {
505 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
506 return 0;
509 myMonitorFromWindow = NULL;
510 myGetMonitorInfo = NULL;
511 myEnumDisplayMonitors = NULL;
512 user32 = GetModuleHandle("user32.dll");
513 if (user32) {
514 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
515 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
516 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
518 dev_hdc = 0;
519 dev = get_display_name();
520 if (dev) dev_hdc = CreateDC(dev, NULL, NULL, NULL);
521 updateScreenProperties();
523 return 1;
527 * \brief Toogle fullscreen / windowed mode.
529 * Should be called on VOCTRL_FULLSCREEN event. The window is
530 * always resized after this call, so the rendering context
531 * should be reinitialized with the new dimensions.
532 * It is unspecified if vo_check_events will create a resize
533 * event in addition or not.
535 * Global libvo variables changed:
536 * vo_dwidth
537 * vo_dheight
538 * vo_fs
541 void vo_w32_fullscreen(void) {
542 vo_fs = !vo_fs;
544 createRenderingContext();
548 * \brief Toogle window border attribute.
550 * Should be called on VOCTRL_BORDER event.
552 * Global libvo variables changed:
553 * vo_border
555 void vo_w32_border(void) {
556 vo_border = !vo_border;
557 createRenderingContext();
561 * \brief Toogle window ontop attribute.
563 * Should be called on VOCTRL_ONTOP event.
565 * Global libvo variables changed:
566 * vo_ontop
568 void vo_w32_ontop( void )
570 vo_ontop = !vo_ontop;
571 if (!vo_fs) {
572 createRenderingContext();
577 * \brief Uninitialize w32_common framework.
579 * Should be called last in video driver's uninit function. First release
580 * anything built on top of the created window e.g. rendering context inside
581 * and call vo_w32_uninit at the end.
583 void vo_w32_uninit(void) {
584 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
585 resetMode();
586 ShowCursor(1);
587 depthonscreen = 0;
588 if (dev_hdc) DeleteDC(dev_hdc);
589 dev_hdc = 0;
590 DestroyWindow(vo_window);
591 vo_window = 0;
592 UnregisterClass(classname, 0);
596 * \brief get a device context to draw in
598 * \param wnd window the DC should belong to if it makes sense
600 HDC vo_w32_get_dc(HWND wnd) {
601 if (dev_hdc) return dev_hdc;
602 return GetDC(wnd);
606 * \brief release a device context
608 * \param wnd window the DC probably belongs to
610 void vo_w32_release_dc(HWND wnd, HDC dc) {
611 if (dev_hdc) return;
612 ReleaseDC(wnd, dc);