Fix crash with partial fribidi conversion.
[mplayer/greg.git] / libvo / w32_common.c
blobda8ed746950288b761ff24e31fd1b4fbc733b6ac
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 /** HDC used when rendering to a device instead of window */
55 static HDC dev_hdc;
56 static int event_flags;
57 static int mon_cnt;
59 static HMONITOR (WINAPI* myMonitorFromWindow)(HWND, DWORD);
60 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
61 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
63 static const struct keymap vk_map[] = {
64 // special keys
65 {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
67 // cursor keys
68 {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
70 // navigation block
71 {VK_INSERT, KEY_INSERT}, {VK_DELETE, KEY_DELETE}, {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
72 {VK_PRIOR, KEY_PAGE_UP}, {VK_NEXT, KEY_PAGE_DOWN},
74 // F-keys
75 {VK_F1, KEY_F+1}, {VK_F2, KEY_F+2}, {VK_F3, KEY_F+3}, {VK_F4, KEY_F+4},
76 {VK_F5, KEY_F+5}, {VK_F6, KEY_F+6}, {VK_F7, KEY_F+7}, {VK_F8, KEY_F+8},
77 {VK_F9, KEY_F+9}, {VK_F10, KEY_F+10}, {VK_F11, KEY_F+11}, {VK_F1, KEY_F+12},
78 // numpad
79 {VK_NUMPAD0, KEY_KP0}, {VK_NUMPAD1, KEY_KP1}, {VK_NUMPAD2, KEY_KP2},
80 {VK_NUMPAD3, KEY_KP3}, {VK_NUMPAD4, KEY_KP4}, {VK_NUMPAD5, KEY_KP5},
81 {VK_NUMPAD6, KEY_KP6}, {VK_NUMPAD7, KEY_KP7}, {VK_NUMPAD8, KEY_KP8},
82 {VK_NUMPAD9, KEY_KP9}, {VK_DECIMAL, KEY_KPDEC},
84 {0, 0}
87 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
88 RECT r;
89 POINT p;
90 int mpkey;
91 switch (message) {
92 case WM_ERASEBKGND: // no need to erase background seperately
93 return 1;
94 case WM_PAINT:
95 event_flags |= VO_EVENT_EXPOSE;
96 break;
97 case WM_MOVE:
98 p.x = 0;
99 p.y = 0;
100 ClientToScreen(vo_window, &p);
101 vo_dx = p.x;
102 vo_dy = p.y;
103 break;
104 case WM_SIZE:
105 event_flags |= VO_EVENT_RESIZE;
106 GetClientRect(vo_window, &r);
107 vo_dwidth = r.right;
108 vo_dheight = r.bottom;
109 break;
110 case WM_WINDOWPOSCHANGING:
111 if (vo_keepaspect && !vo_fs && WinID < 0) {
112 WINDOWPOS *wpos = lParam;
113 int xborder, yborder;
114 r.left = r.top = 0;
115 r.right = wpos->cx;
116 r.bottom = wpos->cy;
117 AdjustWindowRect(&r, GetWindowLong(vo_window, GWL_STYLE), 0);
118 xborder = (r.right - r.left) - wpos->cx;
119 yborder = (r.bottom - r.top) - wpos->cy;
120 wpos->cx -= xborder; wpos->cy -= yborder;
121 aspect_fit(&wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
122 wpos->cx += xborder; wpos->cy += yborder;
124 return 0;
125 case WM_CLOSE:
126 mplayer_put_key(KEY_CLOSE_WIN);
127 break;
128 case WM_SYSCOMMAND:
129 switch (wParam) {
130 case SC_SCREENSAVE:
131 case SC_MONITORPOWER:
132 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
133 return 0;
135 break;
136 case WM_KEYDOWN:
137 mpkey = lookup_keymap_table(vk_map, wParam);
138 if (mpkey)
139 mplayer_put_key(mpkey);
140 break;
141 case WM_CHAR:
142 mplayer_put_key(wParam);
143 break;
144 case WM_LBUTTONDOWN:
145 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
146 mplayer_put_key(MOUSE_BTN0);
147 break;
149 if (!vo_fs) {
150 ReleaseCapture();
151 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
152 return 0;
154 break;
155 case WM_MBUTTONDOWN:
156 if (!vo_nomouse_input)
157 mplayer_put_key(MOUSE_BTN1);
158 break;
159 case WM_RBUTTONDOWN:
160 if (!vo_nomouse_input)
161 mplayer_put_key(MOUSE_BTN2);
162 break;
163 case WM_MOUSEMOVE:
164 if (enable_mouse_movements) {
165 char cmd_str[40];
166 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i",
167 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
168 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
170 break;
171 case WM_MOUSEWHEEL:
172 if (!vo_nomouse_input) {
173 int x = GET_WHEEL_DELTA_WPARAM(wParam);
174 if (x > 0)
175 mplayer_put_key(MOUSE_BTN3);
176 else
177 mplayer_put_key(MOUSE_BTN4);
178 break;
182 return DefWindowProc(hWnd, message, wParam, lParam);
186 * \brief Dispatch incoming window events and handle them.
188 * This function should be placed inside libvo's function "check_events".
190 * Global libvo variables changed:
191 * vo_dwidth: new window client area width
192 * vo_dheight: new window client area height
194 * \return int with these flags possibly set, take care to handle in the right order
195 * if it matters in your driver:
197 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
198 * driver render context accordingly.
199 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
200 * the window if the movie is paused.
202 int vo_w32_check_events(void) {
203 MSG msg;
204 event_flags = 0;
205 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
206 TranslateMessage(&msg);
207 DispatchMessage(&msg);
209 if (WinID >= 0) {
210 RECT r;
211 GetClientRect(vo_window, &r);
212 if (r.right != vo_dwidth || r.bottom != vo_dheight) {
213 vo_dwidth = r.right; vo_dheight = r.bottom;
214 event_flags |= VO_EVENT_RESIZE;
216 GetClientRect(WinID, &r);
217 if (r.right != vo_dwidth || r.bottom != vo_dheight)
218 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
221 return event_flags;
224 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
225 // this defaults to the last screen if specified number does not exist
226 xinerama_x = r->left;
227 xinerama_y = r->top;
228 vo_screenwidth = r->right - r->left;
229 vo_screenheight = r->bottom - r->top;
230 if (mon_cnt == xinerama_screen)
231 return FALSE;
232 mon_cnt++;
233 return TRUE;
237 * \brief Update screen information.
239 * This function should be called in libvo's "control" callback
240 * with parameter VOCTRL_UPDATE_SCREENINFO.
241 * Note that this also enables the new API where geometry and aspect
242 * calculations are done in video_out.c:config_video_out
244 * Global libvo variables changed:
245 * xinerama_x
246 * xinerama_y
247 * vo_screenwidth
248 * vo_screenheight
250 void w32_update_xinerama_info(void) {
251 xinerama_x = xinerama_y = 0;
252 if (xinerama_screen < -1) {
253 int tmp;
254 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
255 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
256 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
257 if (tmp) vo_screenwidth = tmp;
258 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
259 if (tmp) vo_screenheight = tmp;
260 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
261 MONITORINFO mi;
262 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
263 mi.cbSize = sizeof(mi);
264 myGetMonitorInfo(m, &mi);
265 xinerama_x = mi.rcMonitor.left;
266 xinerama_y = mi.rcMonitor.top;
267 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
268 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
269 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
270 mon_cnt = 0;
271 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
273 aspect_save_screenres(vo_screenwidth, vo_screenheight);
276 static void updateScreenProperties(void) {
277 DEVMODE dm;
278 dm.dmSize = sizeof dm;
279 dm.dmDriverExtra = 0;
280 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
281 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
282 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
283 return;
286 vo_screenwidth = dm.dmPelsWidth;
287 vo_screenheight = dm.dmPelsHeight;
288 vo_depthonscreen = dm.dmBitsPerPel;
289 w32_update_xinerama_info();
292 static void changeMode(void) {
293 DEVMODE dm;
294 dm.dmSize = sizeof dm;
295 dm.dmDriverExtra = 0;
297 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
298 dm.dmBitsPerPel = vo_depthonscreen;
299 dm.dmPelsWidth = vo_screenwidth;
300 dm.dmPelsHeight = vo_screenheight;
302 if (vo_vm) {
303 int bestMode = -1;
304 int bestScore = INT_MAX;
305 int i;
306 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
307 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
308 if (dm.dmBitsPerPel != vo_depthonscreen) continue;
309 if (dm.dmPelsWidth < o_dwidth) continue;
310 if (dm.dmPelsHeight < o_dheight) continue;
312 if (score < bestScore) {
313 bestScore = score;
314 bestMode = i;
318 if (bestMode != -1)
319 EnumDisplaySettings(0, bestMode, &dm);
321 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
325 static void resetMode(void) {
326 if (vo_vm)
327 ChangeDisplaySettings(0, 0);
330 static int createRenderingContext(void) {
331 HWND layer = HWND_NOTOPMOST;
332 PIXELFORMATDESCRIPTOR pfd;
333 HDC vo_hdc = vo_w32_get_dc(vo_window);
334 RECT r;
335 int pf;
336 if (WinID < 0) {
337 int style = (vo_border && !vo_fs) ?
338 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
340 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
341 if (vo_fs) {
342 changeMode();
343 while (ShowCursor(0) >= 0) /**/ ;
344 } else {
345 resetMode();
346 while (ShowCursor(1) < 0) /**/ ;
348 updateScreenProperties();
349 ShowWindow(vo_window, SW_HIDE);
350 SetWindowLong(vo_window, GWL_STYLE, style);
351 if (vo_fs) {
352 prev_width = vo_dwidth;
353 prev_height = vo_dheight;
354 prev_x = vo_dx;
355 prev_y = vo_dy;
356 vo_dwidth = vo_screenwidth;
357 vo_dheight = vo_screenheight;
358 vo_dx = xinerama_x;
359 vo_dy = xinerama_y;
360 } else {
361 // make sure there are no "stale" resize events
362 // that would set vo_d* to wrong values
363 vo_w32_check_events();
364 vo_dwidth = prev_width;
365 vo_dheight = prev_height;
366 vo_dx = prev_x;
367 vo_dy = prev_y;
368 // HACK around what probably is a windows focus bug:
369 // when pressing 'f' on the console, then 'f' again to
370 // return to windowed mode, any input into the video
371 // window is lost forever.
372 SetFocus(vo_window);
374 r.left = vo_dx;
375 r.right = r.left + vo_dwidth;
376 r.top = vo_dy;
377 r.bottom = r.top + vo_dheight;
378 AdjustWindowRect(&r, style, 0);
379 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
382 memset(&pfd, 0, sizeof pfd);
383 pfd.nSize = sizeof pfd;
384 pfd.nVersion = 1;
385 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
386 pfd.iPixelType = PFD_TYPE_RGBA;
387 pfd.cColorBits = 24;
388 pfd.iLayerType = PFD_MAIN_PLANE;
389 pf = ChoosePixelFormat(vo_hdc, &pfd);
390 if (!pf) {
391 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
392 ReleaseDC(vo_window, vo_hdc);
393 return 0;
396 SetPixelFormat(vo_hdc, pf, &pfd);
398 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, vo_depthonscreen);
400 vo_w32_release_dc(vo_window, vo_hdc);
401 return 1;
405 * \brief Configure and show window on the screen.
407 * This function should be called in libvo's "config" callback.
408 * It configures a window and shows it on the screen.
410 * Global libvo variables changed:
411 * vo_fs
412 * vo_vm
414 * \return 1 - Success, 0 - Failure
416 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
417 // store original size for videomode switching
418 o_dwidth = width;
419 o_dheight = height;
421 if (WinID < 0) {
422 // the desired size is ignored in wid mode, it always matches the window size.
423 prev_width = vo_dwidth = width;
424 prev_height = vo_dheight = height;
425 prev_x = vo_dx;
426 prev_y = vo_dy;
429 vo_fs = flags & VOFLAG_FULLSCREEN;
430 vo_vm = flags & VOFLAG_MODESWITCHING;
431 return createRenderingContext();
435 * \brief return the name of the selected device if it is indepedant
437 static char *get_display_name(void) {
438 DISPLAY_DEVICE disp;
439 disp.cb = sizeof(disp);
440 EnumDisplayDevices(NULL, vo_adapter_num, &disp, 0);
441 if (disp.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
442 return NULL;
443 return disp.DeviceName;
447 * \brief Initialize w32_common framework.
449 * The first function that should be called from the w32_common framework.
450 * It handles window creation on the screen with proper title and attributes.
451 * It also initializes the framework's internal variables. The function should
452 * be called after your own preinit initialization and you shouldn't do any
453 * window management on your own.
455 * Global libvo variables changed:
456 * vo_w32_window
457 * vo_depthonscreen
458 * vo_screenwidth
459 * vo_screenheight
461 * \return 1 = Success, 0 = Failure
463 int vo_w32_init(void) {
464 HICON mplayerIcon = 0;
465 char exedir[MAX_PATH];
466 HINSTANCE user32;
467 char *dev;
469 if (vo_window)
470 return 1;
472 hInstance = GetModuleHandle(0);
474 if (GetModuleFileName(0, exedir, MAX_PATH))
475 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
476 if (!mplayerIcon)
477 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
480 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
482 if (!RegisterClassEx(&wcex)) {
483 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
484 return 0;
488 if (WinID >= 0)
490 RECT r;
491 GetClientRect(WinID, &r);
492 vo_dwidth = r.right; vo_dheight = r.bottom;
493 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
494 WS_CHILD | WS_VISIBLE,
495 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
496 EnableWindow(vo_window, 0);
497 } else
498 vo_window = CreateWindowEx(0, classname, classname,
499 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
500 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
501 if (!vo_window) {
502 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
503 return 0;
506 myMonitorFromWindow = NULL;
507 myGetMonitorInfo = NULL;
508 myEnumDisplayMonitors = NULL;
509 user32 = GetModuleHandle("user32.dll");
510 if (user32) {
511 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
512 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
513 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
515 dev_hdc = 0;
516 dev = get_display_name();
517 if (dev) dev_hdc = CreateDC(dev, NULL, NULL, NULL);
518 updateScreenProperties();
520 return 1;
524 * \brief Toogle fullscreen / windowed mode.
526 * Should be called on VOCTRL_FULLSCREEN event. The window is
527 * always resized after this call, so the rendering context
528 * should be reinitialized with the new dimensions.
529 * It is unspecified if vo_check_events will create a resize
530 * event in addition or not.
532 * Global libvo variables changed:
533 * vo_dwidth
534 * vo_dheight
535 * vo_fs
538 void vo_w32_fullscreen(void) {
539 vo_fs = !vo_fs;
541 createRenderingContext();
545 * \brief Toogle window border attribute.
547 * Should be called on VOCTRL_BORDER event.
549 * Global libvo variables changed:
550 * vo_border
552 void vo_w32_border(void) {
553 vo_border = !vo_border;
554 createRenderingContext();
558 * \brief Toogle window ontop attribute.
560 * Should be called on VOCTRL_ONTOP event.
562 * Global libvo variables changed:
563 * vo_ontop
565 void vo_w32_ontop( void )
567 vo_ontop = !vo_ontop;
568 if (!vo_fs) {
569 createRenderingContext();
574 * \brief Uninitialize w32_common framework.
576 * Should be called last in video driver's uninit function. First release
577 * anything built on top of the created window e.g. rendering context inside
578 * and call vo_w32_uninit at the end.
580 void vo_w32_uninit(void) {
581 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
582 resetMode();
583 ShowCursor(1);
584 vo_depthonscreen = 0;
585 if (dev_hdc) DeleteDC(dev_hdc);
586 dev_hdc = 0;
587 DestroyWindow(vo_window);
588 vo_window = 0;
589 UnregisterClass(classname, 0);
593 * \brief get a device context to draw in
595 * \param wnd window the DC should belong to if it makes sense
597 HDC vo_w32_get_dc(HWND wnd) {
598 if (dev_hdc) return dev_hdc;
599 return GetDC(wnd);
603 * \brief release a device context
605 * \param wnd window the DC probably belongs to
607 void vo_w32_release_dc(HWND wnd, HDC dc) {
608 if (dev_hdc) return;
609 ReleaseDC(wnd, dc);