vf_pp: Remove deprecated "hex mode" support
[mplayer.git] / libvo / w32_common.c
blobb78f8f1fc433b515797ca64934007847839c9102
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 static int event_flags;
58 static int mon_cnt;
60 static HMONITOR (WINAPI* myMonitorFromWindow)(HWND, DWORD);
61 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
62 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
64 static const struct keymap vk_map[] = {
65 // special keys
66 {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
68 // cursor keys
69 {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
71 // navigation block
72 {VK_INSERT, KEY_INSERT}, {VK_DELETE, KEY_DELETE}, {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
73 {VK_PRIOR, KEY_PAGE_UP}, {VK_NEXT, KEY_PAGE_DOWN},
75 // F-keys
76 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
77 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
78 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F1, KEY_F+12},
79 // numpad
80 {VK_NUMPAD0, KEY_KP0}, {VK_NUMPAD1, KEY_KP1}, {VK_NUMPAD2, KEY_KP2},
81 {VK_NUMPAD3, KEY_KP3}, {VK_NUMPAD4, KEY_KP4}, {VK_NUMPAD5, KEY_KP5},
82 {VK_NUMPAD6, KEY_KP6}, {VK_NUMPAD7, KEY_KP7}, {VK_NUMPAD8, KEY_KP8},
83 {VK_NUMPAD9, KEY_KP9}, {VK_DECIMAL, KEY_KPDEC},
85 {0, 0}
88 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
89 RECT r;
90 POINT p;
91 int mpkey;
92 switch (message) {
93 case WM_ERASEBKGND: // no need to erase background seperately
94 return 1;
95 case WM_PAINT:
96 event_flags |= VO_EVENT_EXPOSE;
97 break;
98 case WM_MOVE:
99 p.x = 0;
100 p.y = 0;
101 ClientToScreen(vo_window, &p);
102 vo_dx = p.x;
103 vo_dy = p.y;
104 break;
105 case WM_SIZE:
106 event_flags |= VO_EVENT_RESIZE;
107 GetClientRect(vo_window, &r);
108 vo_dwidth = r.right;
109 vo_dheight = r.bottom;
110 break;
111 case WM_WINDOWPOSCHANGING:
112 if (vo_keepaspect && !vo_fs) {
113 WINDOWPOS *wpos = lParam;
114 int xborder, yborder;
115 r.left = r.top = 0;
116 r.right = wpos->cx;
117 r.bottom = wpos->cy;
118 AdjustWindowRect(&r, GetWindowLong(vo_window, GWL_STYLE), 0);
119 xborder = (r.right - r.left) - wpos->cx;
120 yborder = (r.bottom - r.top) - wpos->cy;
121 wpos->cx -= xborder; wpos->cy -= yborder;
122 aspect_fit(global_vo, &wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
123 wpos->cx += xborder; wpos->cy += yborder;
125 return 0;
126 case WM_CLOSE:
127 mplayer_put_key(KEY_CLOSE_WIN);
128 break;
129 case WM_SYSCOMMAND:
130 switch (wParam) {
131 case SC_SCREENSAVE:
132 case SC_MONITORPOWER:
133 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
134 return 0;
136 break;
137 case WM_KEYDOWN:
138 mpkey = lookup_keymap_table(vk_map, wParam);
139 if (mpkey)
140 mplayer_put_key(mpkey);
141 break;
142 case WM_CHAR:
143 mplayer_put_key(wParam);
144 break;
145 case WM_LBUTTONDOWN:
146 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
147 mplayer_put_key(MOUSE_BTN0);
148 break;
150 if (!vo_fs) {
151 ReleaseCapture();
152 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
153 return 0;
155 break;
156 case WM_MBUTTONDOWN:
157 if (!vo_nomouse_input)
158 mplayer_put_key(MOUSE_BTN1);
159 break;
160 case WM_RBUTTONDOWN:
161 if (!vo_nomouse_input)
162 mplayer_put_key(MOUSE_BTN2);
163 break;
164 case WM_MOUSEMOVE:
165 if (enable_mouse_movements) {
166 char cmd_str[40];
167 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i",
168 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
169 mp_input_queue_cmd(global_vo->input_ctx, mp_input_parse_cmd(cmd_str));
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);
179 break;
183 return DefWindowProc(hWnd, message, wParam, lParam);
187 * \brief Dispatch incoming window events and handle them.
189 * This function should be placed inside libvo's function "check_events".
191 * Global libvo variables changed:
192 * vo_dwidth: new window client area width
193 * vo_dheight: new window client area height
195 * \return int with these flags possibly set, take care to handle in the right order
196 * if it matters in your driver:
198 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
199 * driver render context accordingly.
200 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
201 * the window if the movie is paused.
203 int vo_w32_check_events(void) {
204 MSG msg;
205 event_flags = 0;
206 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
207 TranslateMessage(&msg);
208 DispatchMessage(&msg);
210 if (WinID >= 0) {
211 RECT r;
212 GetClientRect(vo_window, &r);
213 if (r.right != vo_dwidth || r.bottom != vo_dheight) {
214 vo_dwidth = r.right; vo_dheight = r.bottom;
215 event_flags |= VO_EVENT_RESIZE;
217 GetClientRect(WinID, &r);
218 if (r.right != vo_dwidth || r.bottom != vo_dheight)
219 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
222 return event_flags;
225 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
226 // this defaults to the last screen if specified number does not exist
227 xinerama_x = r->left;
228 xinerama_y = r->top;
229 vo_screenwidth = r->right - r->left;
230 vo_screenheight = r->bottom - r->top;
231 if (mon_cnt == xinerama_screen)
232 return FALSE;
233 mon_cnt++;
234 return TRUE;
238 * \brief Update screen information.
240 * This function should be called in libvo's "control" callback
241 * with parameter VOCTRL_UPDATE_SCREENINFO.
242 * Note that this also enables the new API where geometry and aspect
243 * calculations are done in video_out.c:config_video_out
245 * Global libvo variables changed:
246 * xinerama_x
247 * xinerama_y
248 * vo_screenwidth
249 * vo_screenheight
251 void w32_update_xinerama_info(void) {
252 xinerama_x = xinerama_y = 0;
253 if (xinerama_screen < -1) {
254 int tmp;
255 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
256 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
257 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
258 if (tmp) vo_screenwidth = tmp;
259 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
260 if (tmp) vo_screenheight = tmp;
261 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
262 MONITORINFO mi;
263 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
264 mi.cbSize = sizeof(mi);
265 myGetMonitorInfo(m, &mi);
266 xinerama_x = mi.rcMonitor.left;
267 xinerama_y = mi.rcMonitor.top;
268 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
269 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
270 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
271 mon_cnt = 0;
272 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
274 aspect_save_screenres(vo_screenwidth, vo_screenheight);
277 static void updateScreenProperties(void) {
278 DEVMODE dm;
279 dm.dmSize = sizeof dm;
280 dm.dmDriverExtra = 0;
281 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
282 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
283 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
284 return;
287 vo_screenwidth = dm.dmPelsWidth;
288 vo_screenheight = dm.dmPelsHeight;
289 depthonscreen = dm.dmBitsPerPel;
290 w32_update_xinerama_info();
293 static void changeMode(void) {
294 DEVMODE dm;
295 dm.dmSize = sizeof dm;
296 dm.dmDriverExtra = 0;
298 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
299 dm.dmBitsPerPel = depthonscreen;
300 dm.dmPelsWidth = vo_screenwidth;
301 dm.dmPelsHeight = vo_screenheight;
303 if (vo_vm) {
304 int bestMode = -1;
305 int bestScore = INT_MAX;
306 int i;
307 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
308 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
309 if (dm.dmBitsPerPel != depthonscreen) continue;
310 if (dm.dmPelsWidth < o_dwidth) continue;
311 if (dm.dmPelsHeight < o_dheight) continue;
313 if (score < bestScore) {
314 bestScore = score;
315 bestMode = i;
319 if (bestMode != -1)
320 EnumDisplaySettings(0, bestMode, &dm);
322 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
326 static void resetMode(void) {
327 if (vo_vm)
328 ChangeDisplaySettings(0, 0);
331 static int createRenderingContext(void) {
332 HWND layer = HWND_NOTOPMOST;
333 PIXELFORMATDESCRIPTOR pfd;
334 HDC vo_hdc = GetDC(vo_window);
335 RECT r;
336 int pf;
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);
383 memset(&pfd, 0, sizeof pfd);
384 pfd.nSize = sizeof pfd;
385 pfd.nVersion = 1;
386 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
387 pfd.iPixelType = PFD_TYPE_RGBA;
388 pfd.cColorBits = 24;
389 pfd.iLayerType = PFD_MAIN_PLANE;
390 pf = ChoosePixelFormat(vo_hdc, &pfd);
391 if (!pf) {
392 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
393 ReleaseDC(vo_window, vo_hdc);
394 return 0;
397 SetPixelFormat(vo_hdc, pf, &pfd);
399 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, depthonscreen);
401 ReleaseDC(vo_window, vo_hdc);
402 return 1;
406 * \brief Configure and show window on the screen.
408 * This function should be called in libvo's "config" callback.
409 * It configures a window and shows it on the screen.
411 * Global libvo variables changed:
412 * vo_fs
413 * vo_vm
415 * \return 1 - Success, 0 - Failure
417 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
418 // store original size for videomode switching
419 o_dwidth = width;
420 o_dheight = height;
422 if (WinID < 0) {
423 // the desired size is ignored in wid mode, it always matches the window size.
424 prev_width = vo_dwidth = width;
425 prev_height = vo_dheight = height;
426 prev_x = vo_dx;
427 prev_y = vo_dy;
430 vo_fs = flags & VOFLAG_FULLSCREEN;
431 vo_vm = flags & VOFLAG_MODESWITCHING;
432 return createRenderingContext();
436 * \brief Initialize w32_common framework.
438 * The first function that should be called from the w32_common framework.
439 * It handles window creation on the screen with proper title and attributes.
440 * It also initializes the framework's internal variables. The function should
441 * be called after your own preinit initialization and you shouldn't do any
442 * window management on your own.
444 * Global libvo variables changed:
445 * vo_w32_window
446 * vo_depthonscreen
447 * vo_screenwidth
448 * vo_screenheight
450 * \return 1 = Success, 0 = Failure
452 int vo_w32_init(void) {
453 HICON mplayerIcon = 0;
454 char exedir[MAX_PATH];
455 HINSTANCE user32;
457 if (vo_window)
458 return 1;
460 hInstance = GetModuleHandle(0);
462 if (GetModuleFileName(0, exedir, MAX_PATH))
463 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
464 if (!mplayerIcon)
465 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
468 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
470 if (!RegisterClassEx(&wcex)) {
471 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
472 return 0;
476 if (WinID >= 0)
478 RECT r;
479 GetClientRect(WinID, &r);
480 vo_dwidth = r.right; vo_dheight = r.bottom;
481 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
482 WS_CHILD | WS_VISIBLE,
483 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
484 EnableWindow(vo_window, 0);
485 } else
486 vo_window = CreateWindowEx(0, classname, classname,
487 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
488 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
489 if (!vo_window) {
490 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
491 return 0;
494 myMonitorFromWindow = NULL;
495 myGetMonitorInfo = NULL;
496 myEnumDisplayMonitors = NULL;
497 user32 = GetModuleHandle("user32.dll");
498 if (user32) {
499 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
500 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
501 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
503 updateScreenProperties();
505 return 1;
509 * \brief Toogle fullscreen / windowed mode.
511 * Should be called on VOCTRL_FULLSCREEN event. The window is
512 * always resized after this call, so the rendering context
513 * should be reinitialized with the new dimensions.
514 * It is unspecified if vo_check_events will create a resize
515 * event in addition or not.
517 * Global libvo variables changed:
518 * vo_dwidth
519 * vo_dheight
520 * vo_fs
523 void vo_w32_fullscreen(void) {
524 vo_fs = !vo_fs;
526 createRenderingContext();
530 * \brief Toogle window border attribute.
532 * Should be called on VOCTRL_BORDER event.
534 * Global libvo variables changed:
535 * vo_border
537 void vo_w32_border(void) {
538 vo_border = !vo_border;
539 createRenderingContext();
543 * \brief Toogle window ontop attribute.
545 * Should be called on VOCTRL_ONTOP event.
547 * Global libvo variables changed:
548 * vo_ontop
550 void vo_w32_ontop( void )
552 vo_ontop = !vo_ontop;
553 if (!vo_fs) {
554 createRenderingContext();
559 * \brief Uninitialize w32_common framework.
561 * Should be called last in video driver's uninit function. First release
562 * anything built on top of the created window e.g. rendering context inside
563 * and call vo_w32_uninit at the end.
565 void vo_w32_uninit(void) {
566 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
567 resetMode();
568 ShowCursor(1);
569 depthonscreen = 0;
570 DestroyWindow(vo_window);
571 vo_window = 0;
572 UnregisterClass(classname, 0);