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