Fix teletext character set auto-detection.
[mplayer.git] / libvo / w32_common.c
blob56c56977f00bf4d1896efc0ed70d308e083aa75d
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 #include "osdep/keycodes.h"
25 #include "input/input.h"
26 #include "input/mouse.h"
27 #include "mp_msg.h"
28 #include "video_out.h"
29 #include "aspect.h"
30 #include "w32_common.h"
31 #include "mp_fifo.h"
33 extern int enable_mouse_movements;
35 #ifndef MONITOR_DEFAULTTOPRIMARY
36 #define MONITOR_DEFAULTTOPRIMARY 1
37 #endif
39 static const char classname[] = "MPlayer - The Movie Player";
40 int vo_vm = 0;
42 // last non-fullscreen extends
43 static int prev_width;
44 static int prev_height;
45 static int prev_x;
46 static int prev_y;
48 static uint32_t o_dwidth;
49 static uint32_t o_dheight;
51 static HINSTANCE hInstance;
52 #define vo_window vo_w32_window
53 HWND vo_window = 0;
54 static int event_flags;
55 static int mon_cnt;
57 static HMONITOR (WINAPI* myMonitorFromWindow)(HWND, DWORD);
58 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
59 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
61 static const struct keymap vk_map[] = {
62 // special keys
63 {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
65 // cursor keys
66 {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
68 // navigation block
69 {VK_INSERT, KEY_INSERT}, {VK_DELETE, KEY_DELETE}, {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
70 {VK_PRIOR, KEY_PAGE_UP}, {VK_NEXT, KEY_PAGE_DOWN},
72 // F-keys
73 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
74 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
75 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F1, KEY_F+12},
76 // numpad
77 {VK_NUMPAD0, KEY_KP0}, {VK_NUMPAD1, KEY_KP1}, {VK_NUMPAD2, KEY_KP2},
78 {VK_NUMPAD3, KEY_KP3}, {VK_NUMPAD4, KEY_KP4}, {VK_NUMPAD5, KEY_KP5},
79 {VK_NUMPAD6, KEY_KP6}, {VK_NUMPAD7, KEY_KP7}, {VK_NUMPAD8, KEY_KP8},
80 {VK_NUMPAD9, KEY_KP9}, {VK_DECIMAL, KEY_KPDEC},
82 {0, 0}
85 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
86 RECT r;
87 POINT p;
88 int mpkey;
89 switch (message) {
90 case WM_ERASEBKGND: // no need to erase background seperately
91 return 1;
92 case WM_PAINT:
93 event_flags |= VO_EVENT_EXPOSE;
94 break;
95 case WM_MOVE:
96 p.x = 0;
97 p.y = 0;
98 ClientToScreen(vo_window, &p);
99 vo_dx = p.x;
100 vo_dy = p.y;
101 break;
102 case WM_SIZE:
103 event_flags |= VO_EVENT_RESIZE;
104 GetClientRect(vo_window, &r);
105 vo_dwidth = r.right;
106 vo_dheight = r.bottom;
107 break;
108 case WM_WINDOWPOSCHANGING:
109 if (vo_keepaspect && !vo_fs && WinID < 0) {
110 WINDOWPOS *wpos = lParam;
111 int xborder, yborder;
112 r.left = r.top = 0;
113 r.right = wpos->cx;
114 r.bottom = wpos->cy;
115 AdjustWindowRect(&r, GetWindowLong(vo_window, GWL_STYLE), 0);
116 xborder = (r.right - r.left) - wpos->cx;
117 yborder = (r.bottom - r.top) - wpos->cy;
118 wpos->cx -= xborder; wpos->cy -= yborder;
119 aspect_fit(&wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
120 wpos->cx += xborder; wpos->cy += yborder;
122 return 0;
123 case WM_CLOSE:
124 mplayer_put_key(KEY_CLOSE_WIN);
125 break;
126 case WM_SYSCOMMAND:
127 switch (wParam) {
128 case SC_SCREENSAVE:
129 case SC_MONITORPOWER:
130 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
131 return 0;
133 break;
134 case WM_KEYDOWN:
135 mpkey = lookup_keymap_table(vk_map, wParam);
136 if (mpkey)
137 mplayer_put_key(mpkey);
138 break;
139 case WM_CHAR:
140 mplayer_put_key(wParam);
141 break;
142 case WM_LBUTTONDOWN:
143 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
144 mplayer_put_key(MOUSE_BTN0);
145 break;
147 if (!vo_fs) {
148 ReleaseCapture();
149 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
150 return 0;
152 break;
153 case WM_MBUTTONDOWN:
154 if (!vo_nomouse_input)
155 mplayer_put_key(MOUSE_BTN1);
156 break;
157 case WM_RBUTTONDOWN:
158 if (!vo_nomouse_input)
159 mplayer_put_key(MOUSE_BTN2);
160 break;
161 case WM_MOUSEMOVE:
162 if (enable_mouse_movements) {
163 char cmd_str[40];
164 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i",
165 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
166 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
168 break;
169 case WM_MOUSEWHEEL:
170 if (!vo_nomouse_input) {
171 int x = GET_WHEEL_DELTA_WPARAM(wParam);
172 if (x > 0)
173 mplayer_put_key(MOUSE_BTN3);
174 else
175 mplayer_put_key(MOUSE_BTN4);
176 break;
180 return DefWindowProc(hWnd, message, wParam, lParam);
184 * \brief Dispatch incoming window events and handle them.
186 * This function should be placed inside libvo's function "check_events".
188 * Global libvo variables changed:
189 * vo_dwidth: new window client area width
190 * vo_dheight: new window client area height
192 * \return int with these flags possibly set, take care to handle in the right order
193 * if it matters in your driver:
195 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
196 * driver render context accordingly.
197 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
198 * the window if the movie is paused.
200 int vo_w32_check_events(void) {
201 MSG msg;
202 event_flags = 0;
203 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
204 TranslateMessage(&msg);
205 DispatchMessage(&msg);
207 if (WinID >= 0) {
208 RECT r;
209 GetClientRect(vo_window, &r);
210 if (r.right != vo_dwidth || r.bottom != vo_dheight) {
211 vo_dwidth = r.right; vo_dheight = r.bottom;
212 event_flags |= VO_EVENT_RESIZE;
214 GetClientRect(WinID, &r);
215 if (r.right != vo_dwidth || r.bottom != vo_dheight)
216 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
219 return event_flags;
222 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
223 // this defaults to the last screen if specified number does not exist
224 xinerama_x = r->left;
225 xinerama_y = r->top;
226 vo_screenwidth = r->right - r->left;
227 vo_screenheight = r->bottom - r->top;
228 if (mon_cnt == xinerama_screen)
229 return FALSE;
230 mon_cnt++;
231 return TRUE;
235 * \brief Update screen information.
237 * This function should be called in libvo's "control" callback
238 * with parameter VOCTRL_UPDATE_SCREENINFO.
239 * Note that this also enables the new API where geometry and aspect
240 * calculations are done in video_out.c:config_video_out
242 * Global libvo variables changed:
243 * xinerama_x
244 * xinerama_y
245 * vo_screenwidth
246 * vo_screenheight
248 void w32_update_xinerama_info(void) {
249 xinerama_x = xinerama_y = 0;
250 if (xinerama_screen < -1) {
251 int tmp;
252 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
253 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
254 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
255 if (tmp) vo_screenwidth = tmp;
256 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
257 if (tmp) vo_screenheight = tmp;
258 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
259 MONITORINFO mi;
260 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
261 mi.cbSize = sizeof(mi);
262 myGetMonitorInfo(m, &mi);
263 xinerama_x = mi.rcMonitor.left;
264 xinerama_y = mi.rcMonitor.top;
265 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
266 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
267 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
268 mon_cnt = 0;
269 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
271 aspect_save_screenres(vo_screenwidth, vo_screenheight);
274 static void updateScreenProperties(void) {
275 DEVMODE dm;
276 dm.dmSize = sizeof dm;
277 dm.dmDriverExtra = 0;
278 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
279 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
280 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
281 return;
284 vo_screenwidth = dm.dmPelsWidth;
285 vo_screenheight = dm.dmPelsHeight;
286 vo_depthonscreen = dm.dmBitsPerPel;
287 w32_update_xinerama_info();
290 static void changeMode(void) {
291 DEVMODE dm;
292 dm.dmSize = sizeof dm;
293 dm.dmDriverExtra = 0;
295 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
296 dm.dmBitsPerPel = vo_depthonscreen;
297 dm.dmPelsWidth = vo_screenwidth;
298 dm.dmPelsHeight = vo_screenheight;
300 if (vo_vm) {
301 int bestMode = -1;
302 int bestScore = INT_MAX;
303 int i;
304 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
305 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
306 if (dm.dmBitsPerPel != vo_depthonscreen) continue;
307 if (dm.dmPelsWidth < o_dwidth) continue;
308 if (dm.dmPelsHeight < o_dheight) continue;
310 if (score < bestScore) {
311 bestScore = score;
312 bestMode = i;
316 if (bestMode != -1)
317 EnumDisplaySettings(0, bestMode, &dm);
319 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
323 static void resetMode(void) {
324 if (vo_vm)
325 ChangeDisplaySettings(0, 0);
328 static int createRenderingContext(void) {
329 HWND layer = HWND_NOTOPMOST;
330 PIXELFORMATDESCRIPTOR pfd;
331 HDC vo_hdc = GetDC(vo_window);
332 RECT r;
333 int pf;
334 if (WinID < 0) {
335 int style = (vo_border && !vo_fs) ?
336 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
338 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
339 if (vo_fs) {
340 changeMode();
341 while (ShowCursor(0) >= 0) /**/ ;
342 } else {
343 resetMode();
344 while (ShowCursor(1) < 0) /**/ ;
346 updateScreenProperties();
347 ShowWindow(vo_window, SW_HIDE);
348 SetWindowLong(vo_window, GWL_STYLE, style);
349 if (vo_fs) {
350 prev_width = vo_dwidth;
351 prev_height = vo_dheight;
352 prev_x = vo_dx;
353 prev_y = vo_dy;
354 vo_dwidth = vo_screenwidth;
355 vo_dheight = vo_screenheight;
356 vo_dx = xinerama_x;
357 vo_dy = xinerama_y;
358 } else {
359 // make sure there are no "stale" resize events
360 // that would set vo_d* to wrong values
361 vo_w32_check_events();
362 vo_dwidth = prev_width;
363 vo_dheight = prev_height;
364 vo_dx = prev_x;
365 vo_dy = prev_y;
366 // HACK around what probably is a windows focus bug:
367 // when pressing 'f' on the console, then 'f' again to
368 // return to windowed mode, any input into the video
369 // window is lost forever.
370 SetFocus(vo_window);
372 r.left = vo_dx;
373 r.right = r.left + vo_dwidth;
374 r.top = vo_dy;
375 r.bottom = r.top + vo_dheight;
376 AdjustWindowRect(&r, style, 0);
377 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
380 memset(&pfd, 0, sizeof pfd);
381 pfd.nSize = sizeof pfd;
382 pfd.nVersion = 1;
383 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
384 pfd.iPixelType = PFD_TYPE_RGBA;
385 pfd.cColorBits = 24;
386 pfd.iLayerType = PFD_MAIN_PLANE;
387 pf = ChoosePixelFormat(vo_hdc, &pfd);
388 if (!pf) {
389 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
390 ReleaseDC(vo_window, vo_hdc);
391 return 0;
394 SetPixelFormat(vo_hdc, pf, &pfd);
396 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, vo_depthonscreen);
398 ReleaseDC(vo_window, vo_hdc);
399 return 1;
403 * \brief Configure and show window on the screen.
405 * This function should be called in libvo's "config" callback.
406 * It configures a window and shows it on the screen.
408 * Global libvo variables changed:
409 * vo_fs
410 * vo_vm
412 * \return 1 - Success, 0 - Failure
414 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
415 // store original size for videomode switching
416 o_dwidth = width;
417 o_dheight = height;
419 if (WinID < 0) {
420 // the desired size is ignored in wid mode, it always matches the window size.
421 prev_width = vo_dwidth = width;
422 prev_height = vo_dheight = height;
423 prev_x = vo_dx;
424 prev_y = vo_dy;
427 vo_fs = flags & VOFLAG_FULLSCREEN;
428 vo_vm = flags & VOFLAG_MODESWITCHING;
429 return createRenderingContext();
433 * \brief Initialize w32_common framework.
435 * The first function that should be called from the w32_common framework.
436 * It handles window creation on the screen with proper title and attributes.
437 * It also initializes the framework's internal variables. The function should
438 * be called after your own preinit initialization and you shouldn't do any
439 * window management on your own.
441 * Global libvo variables changed:
442 * vo_w32_window
443 * vo_depthonscreen
444 * vo_screenwidth
445 * vo_screenheight
447 * \return 1 = Success, 0 = Failure
449 int vo_w32_init(void) {
450 HICON mplayerIcon = 0;
451 char exedir[MAX_PATH];
452 HINSTANCE user32;
454 if (vo_window)
455 return 1;
457 hInstance = GetModuleHandle(0);
459 if (GetModuleFileName(0, exedir, MAX_PATH))
460 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
461 if (!mplayerIcon)
462 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
465 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
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 updateScreenProperties();
502 return 1;
506 * \brief Toogle fullscreen / windowed mode.
508 * Should be called on VOCTRL_FULLSCREEN event. The window is
509 * always resized after this call, so the rendering context
510 * should be reinitialized with the new dimensions.
511 * It is unspecified if vo_check_events will create a resize
512 * event in addition or not.
514 * Global libvo variables changed:
515 * vo_dwidth
516 * vo_dheight
517 * vo_fs
520 void vo_w32_fullscreen(void) {
521 vo_fs = !vo_fs;
523 createRenderingContext();
527 * \brief Toogle window border attribute.
529 * Should be called on VOCTRL_BORDER event.
531 * Global libvo variables changed:
532 * vo_border
534 void vo_w32_border(void) {
535 vo_border = !vo_border;
536 createRenderingContext();
540 * \brief Toogle window ontop attribute.
542 * Should be called on VOCTRL_ONTOP event.
544 * Global libvo variables changed:
545 * vo_ontop
547 void vo_w32_ontop( void )
549 vo_ontop = !vo_ontop;
550 if (!vo_fs) {
551 createRenderingContext();
556 * \brief Uninitialize w32_common framework.
558 * Should be called last in video driver's uninit function. First release
559 * anything built on top of the created window e.g. rendering context inside
560 * and call vo_w32_uninit at the end.
562 void vo_w32_uninit(void) {
563 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
564 resetMode();
565 ShowCursor(1);
566 vo_depthonscreen = 0;
567 DestroyWindow(vo_window);
568 vo_window = 0;
569 UnregisterClass(classname, 0);