Fix several return values
[mplayer/glamo.git] / libvo / w32_common.c
blob8063352d7ecb75116551dc218d13ec02df699114
1 #include <stdio.h>
2 #include <limits.h>
3 #include <windows.h>
4 #include <windowsx.h>
6 #include "osdep/keycodes.h"
7 #include "input/input.h"
8 #include "input/mouse.h"
9 #include "mp_msg.h"
10 #include "video_out.h"
11 #include "aspect.h"
12 #include "w32_common.h"
13 #include "mp_fifo.h"
15 extern int enable_mouse_movements;
17 #ifndef MONITOR_DEFAULTTOPRIMARY
18 #define MONITOR_DEFAULTTOPRIMARY 1
19 #endif
21 static const char classname[] = "MPlayer - Media player for Win32";
22 int vo_vm = 0;
24 // last non-fullscreen extends
25 static int prev_width;
26 static int prev_height;
27 static int prev_x;
28 static int prev_y;
30 static uint32_t o_dwidth;
31 static uint32_t o_dheight;
33 static HINSTANCE hInstance;
34 #define vo_window vo_w32_window
35 HWND vo_window = 0;
36 static int event_flags;
37 static int mon_cnt;
39 static HMONITOR (WINAPI* myMonitorFromWindow)(HWND, DWORD);
40 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
41 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
43 static const struct keymap vk_map[] = {
44 // special keys
45 {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
47 // cursor keys
48 {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
50 // navigation block
51 {VK_INSERT, KEY_INSERT}, {VK_DELETE, KEY_DELETE}, {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
52 {VK_PRIOR, KEY_PAGE_UP}, {VK_NEXT, KEY_PAGE_DOWN},
54 // F-keys
55 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
56 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
57 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F1, KEY_F+12},
58 // numpad
59 {VK_NUMPAD0, KEY_KP0}, {VK_NUMPAD1, KEY_KP1}, {VK_NUMPAD2, KEY_KP2},
60 {VK_NUMPAD3, KEY_KP3}, {VK_NUMPAD4, KEY_KP4}, {VK_NUMPAD5, KEY_KP5},
61 {VK_NUMPAD6, KEY_KP6}, {VK_NUMPAD7, KEY_KP7}, {VK_NUMPAD8, KEY_KP8},
62 {VK_NUMPAD9, KEY_KP9}, {VK_DECIMAL, KEY_KPDEC},
64 {0, 0}
67 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
68 RECT r;
69 POINT p;
70 int mpkey;
71 switch (message) {
72 case WM_ERASEBKGND: // no need to erase background seperately
73 return 1;
74 case WM_PAINT:
75 event_flags |= VO_EVENT_EXPOSE;
76 break;
77 case WM_MOVE:
78 p.x = 0;
79 p.y = 0;
80 ClientToScreen(vo_window, &p);
81 vo_dx = p.x;
82 vo_dy = p.y;
83 break;
84 case WM_SIZE:
85 event_flags |= VO_EVENT_RESIZE;
86 GetClientRect(vo_window, &r);
87 vo_dwidth = r.right;
88 vo_dheight = r.bottom;
89 break;
90 case WM_WINDOWPOSCHANGING:
91 if (vo_keepaspect && !vo_fs) {
92 WINDOWPOS *wpos = lParam;
93 int xborder, yborder;
94 RECT r2;
95 GetClientRect(vo_window, &r);
96 GetWindowRect(vo_window, &r2);
97 xborder = (r2.right - r2.left) - (r.right - r.left);
98 yborder = (r2.bottom - r2.top) - (r.bottom - r.top);
99 wpos->cx -= xborder; wpos->cy -= yborder;
100 aspect_fit(&wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
101 wpos->cx += xborder; wpos->cy += yborder;
103 return 0;
104 case WM_CLOSE:
105 mplayer_put_key(KEY_CLOSE_WIN);
106 break;
107 case WM_SYSCOMMAND:
108 switch (wParam) {
109 case SC_SCREENSAVE:
110 case SC_MONITORPOWER:
111 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
112 return 0;
114 break;
115 case WM_KEYDOWN:
116 mpkey = lookup_keymap_table(vk_map, wParam);
117 if (mpkey)
118 mplayer_put_key(mpkey);
119 break;
120 case WM_CHAR:
121 mplayer_put_key(wParam);
122 break;
123 case WM_LBUTTONDOWN:
124 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
125 mplayer_put_key(MOUSE_BTN0);
126 break;
128 if (!vo_fs) {
129 ReleaseCapture();
130 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
131 return 0;
133 break;
134 case WM_MBUTTONDOWN:
135 if (!vo_nomouse_input)
136 mplayer_put_key(MOUSE_BTN1);
137 break;
138 case WM_RBUTTONDOWN:
139 if (!vo_nomouse_input)
140 mplayer_put_key(MOUSE_BTN2);
141 break;
142 case WM_MOUSEMOVE:
143 if (enable_mouse_movements) {
144 char cmd_str[40];
145 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i",
146 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
147 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
149 break;
150 case WM_MOUSEWHEEL:
151 if (!vo_nomouse_input) {
152 int x = GET_WHEEL_DELTA_WPARAM(wParam);
153 if (x > 0)
154 mplayer_put_key(MOUSE_BTN3);
155 else
156 mplayer_put_key(MOUSE_BTN4);
157 break;
161 return DefWindowProc(hWnd, message, wParam, lParam);
165 * \brief Dispatch incoming window events and handle them.
167 * This function should be placed inside libvo's function "check_events".
169 * Global libvo variables changed:
170 * vo_dwidth: new window client area width
171 * vo_dheight: new window client area height
173 * \return int with these flags possibly set, take care to handle in the right order
174 * if it matters in your driver:
176 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
177 * driver render context accordingly.
178 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
179 * the window if the movie is paused.
181 int vo_w32_check_events(void) {
182 MSG msg;
183 event_flags = 0;
184 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
185 TranslateMessage(&msg);
186 DispatchMessage(&msg);
188 if (WinID >= 0) {
189 RECT r;
190 GetClientRect(vo_window, &r);
191 if (r.right != vo_dwidth || r.bottom != vo_dheight) {
192 vo_dwidth = r.right; vo_dheight = r.bottom;
193 event_flags |= VO_EVENT_RESIZE;
195 GetClientRect(WinID, &r);
196 if (r.right != vo_dwidth || r.bottom != vo_dheight)
197 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
200 return event_flags;
203 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
204 // this defaults to the last screen if specified number does not exist
205 xinerama_x = r->left;
206 xinerama_y = r->top;
207 vo_screenwidth = r->right - r->left;
208 vo_screenheight = r->bottom - r->top;
209 if (mon_cnt == xinerama_screen)
210 return FALSE;
211 mon_cnt++;
212 return TRUE;
216 * \brief Update screen information.
218 * This function should be called in libvo's "control" callback
219 * with parameter VOCTRL_UPDATE_SCREENINFO.
220 * Note that this also enables the new API where geometry and aspect
221 * calculations are done in video_out.c:config_video_out
223 * Global libvo variables changed:
224 * xinerama_x
225 * xinerama_y
226 * vo_screenwidth
227 * vo_screenheight
229 void w32_update_xinerama_info(void) {
230 xinerama_x = xinerama_y = 0;
231 if (xinerama_screen < -1) {
232 int tmp;
233 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
234 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
235 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
236 if (tmp) vo_screenwidth = tmp;
237 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
238 if (tmp) vo_screenheight = tmp;
239 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
240 MONITORINFO mi;
241 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
242 mi.cbSize = sizeof(mi);
243 myGetMonitorInfo(m, &mi);
244 xinerama_x = mi.rcMonitor.left;
245 xinerama_y = mi.rcMonitor.top;
246 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
247 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
248 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
249 mon_cnt = 0;
250 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
252 aspect_save_screenres(vo_screenwidth, vo_screenheight);
255 static void updateScreenProperties(void) {
256 DEVMODE dm;
257 dm.dmSize = sizeof dm;
258 dm.dmDriverExtra = 0;
259 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
260 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
261 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
262 return;
265 vo_screenwidth = dm.dmPelsWidth;
266 vo_screenheight = dm.dmPelsHeight;
267 vo_depthonscreen = dm.dmBitsPerPel;
268 w32_update_xinerama_info();
271 static void changeMode(void) {
272 DEVMODE dm;
273 dm.dmSize = sizeof dm;
274 dm.dmDriverExtra = 0;
276 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
277 dm.dmBitsPerPel = vo_depthonscreen;
278 dm.dmPelsWidth = vo_screenwidth;
279 dm.dmPelsHeight = vo_screenheight;
281 if (vo_vm) {
282 int bestMode = -1;
283 int bestScore = INT_MAX;
284 int i;
285 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
286 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
287 if (dm.dmBitsPerPel != vo_depthonscreen) continue;
288 if (dm.dmPelsWidth < o_dwidth) continue;
289 if (dm.dmPelsHeight < o_dheight) continue;
291 if (score < bestScore) {
292 bestScore = score;
293 bestMode = i;
297 if (bestMode != -1)
298 EnumDisplaySettings(0, bestMode, &dm);
300 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
304 static void resetMode(void) {
305 if (vo_vm)
306 ChangeDisplaySettings(0, 0);
309 static int createRenderingContext(void) {
310 HWND layer = HWND_NOTOPMOST;
311 PIXELFORMATDESCRIPTOR pfd;
312 HDC vo_hdc = GetDC(vo_window);
313 RECT r;
314 int pf;
315 if (WinID < 0) {
316 int style = (vo_border && !vo_fs) ?
317 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
319 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
320 if (vo_fs) {
321 changeMode();
322 while (ShowCursor(0) >= 0) /**/ ;
323 } else {
324 resetMode();
325 while (ShowCursor(1) < 0) /**/ ;
327 updateScreenProperties();
328 ShowWindow(vo_window, SW_HIDE);
329 SetWindowLong(vo_window, GWL_STYLE, style);
330 if (vo_fs) {
331 prev_width = vo_dwidth;
332 prev_height = vo_dheight;
333 prev_x = vo_dx;
334 prev_y = vo_dy;
335 vo_dwidth = vo_screenwidth;
336 vo_dheight = vo_screenheight;
337 vo_dx = xinerama_x;
338 vo_dy = xinerama_y;
339 } else {
340 // make sure there are no "stale" resize events
341 // that would set vo_d* to wrong values
342 vo_w32_check_events();
343 vo_dwidth = prev_width;
344 vo_dheight = prev_height;
345 vo_dx = prev_x;
346 vo_dy = prev_y;
347 // HACK around what probably is a windows focus bug:
348 // when pressing 'f' on the console, then 'f' again to
349 // return to windowed mode, any input into the video
350 // window is lost forever.
351 SetFocus(vo_window);
353 r.left = vo_dx;
354 r.right = r.left + vo_dwidth;
355 r.top = vo_dy;
356 r.bottom = r.top + vo_dheight;
357 AdjustWindowRect(&r, style, 0);
358 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
361 memset(&pfd, 0, sizeof pfd);
362 pfd.nSize = sizeof pfd;
363 pfd.nVersion = 1;
364 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
365 pfd.iPixelType = PFD_TYPE_RGBA;
366 pfd.cColorBits = 24;
367 pfd.iLayerType = PFD_MAIN_PLANE;
368 pf = ChoosePixelFormat(vo_hdc, &pfd);
369 if (!pf) {
370 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
371 ReleaseDC(vo_window, vo_hdc);
372 return 0;
375 SetPixelFormat(vo_hdc, pf, &pfd);
377 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, vo_depthonscreen);
379 ReleaseDC(vo_window, vo_hdc);
380 return 1;
384 * \brief Configure and show window on the screen.
386 * This function should be called in libvo's "config" callback.
387 * It configures a window and shows it on the screen.
389 * Global libvo variables changed:
390 * vo_fs
391 * vo_vm
393 * \return 1 - Success, 0 - Failure
395 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
396 // store original size for videomode switching
397 o_dwidth = width;
398 o_dheight = height;
400 if (WinID < 0) {
401 // the desired size is ignored in wid mode, it always matches the window size.
402 prev_width = vo_dwidth = width;
403 prev_height = vo_dheight = height;
404 prev_x = vo_dx;
405 prev_y = vo_dy;
408 vo_fs = flags & VOFLAG_FULLSCREEN;
409 vo_vm = flags & VOFLAG_MODESWITCHING;
410 return createRenderingContext();
414 * \brief Initialize w32_common framework.
416 * The first function that should be called from the w32_common framework.
417 * It handles window creation on the screen with proper title and attributes.
418 * It also initializes the framework's internal variables. The function should
419 * be called after your own preinit initialization and you shouldn't do any
420 * window management on your own.
422 * Global libvo variables changed:
423 * vo_w32_window
424 * vo_depthonscreen
425 * vo_screenwidth
426 * vo_screenheight
428 * \return 1 = Success, 0 = Failure
430 int vo_w32_init(void) {
431 HICON mplayerIcon = 0;
432 char exedir[MAX_PATH];
433 HINSTANCE user32;
435 if (vo_window)
436 return 1;
438 hInstance = GetModuleHandle(0);
440 if (GetModuleFileName(0, exedir, MAX_PATH))
441 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
442 if (!mplayerIcon)
443 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
446 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
448 if (!RegisterClassEx(&wcex)) {
449 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
450 return 0;
454 if (WinID >= 0)
456 RECT r;
457 GetClientRect(WinID, &r);
458 vo_dwidth = r.right; vo_dheight = r.bottom;
459 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
460 WS_CHILD | WS_VISIBLE,
461 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
462 EnableWindow(vo_window, 0);
463 } else
464 vo_window = CreateWindowEx(0, classname, classname,
465 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
466 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
467 if (!vo_window) {
468 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
469 return 0;
472 myMonitorFromWindow = NULL;
473 myGetMonitorInfo = NULL;
474 myEnumDisplayMonitors = NULL;
475 user32 = GetModuleHandle("user32.dll");
476 if (user32) {
477 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
478 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
479 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
481 updateScreenProperties();
483 return 1;
487 * \brief Toogle fullscreen / windowed mode.
489 * Should be called on VOCTRL_FULLSCREEN event. The window is
490 * always resized after this call, so the rendering context
491 * should be reinitialized with the new dimensions.
492 * It is unspecified if vo_check_events will create a resize
493 * event in addition or not.
495 * Global libvo variables changed:
496 * vo_dwidth
497 * vo_dheight
498 * vo_fs
501 void vo_w32_fullscreen(void) {
502 vo_fs = !vo_fs;
504 createRenderingContext();
508 * \brief Toogle window border attribute.
510 * Should be called on VOCTRL_BORDER event.
512 * Global libvo variables changed:
513 * vo_border
515 void vo_w32_border(void) {
516 vo_border = !vo_border;
517 createRenderingContext();
521 * \brief Toogle window ontop attribute.
523 * Should be called on VOCTRL_ONTOP event.
525 * Global libvo variables changed:
526 * vo_ontop
528 void vo_w32_ontop( void )
530 vo_ontop = !vo_ontop;
531 if (!vo_fs) {
532 createRenderingContext();
537 * \brief Uninitialize w32_common framework.
539 * Should be called last in video driver's uninit function. First release
540 * anything built on top of the created window e.g. rendering context inside
541 * and call vo_w32_uninit at the end.
543 void vo_w32_uninit(void) {
544 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
545 resetMode();
546 ShowCursor(1);
547 vo_depthonscreen = 0;
548 DestroyWindow(vo_window);
549 vo_window = 0;
550 UnregisterClass(classname, 0);