Add and use a special lookup function to do table-based translation to MPlayer keycodes.
[mplayer/glamo.git] / libvo / w32_common.c
blob6fbeaaa4b07420e2d0a2db3ba2ac76367f724c70
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 {0, 0}
57 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
58 RECT r;
59 POINT p;
60 int mpkey;
61 switch (message) {
62 case WM_ERASEBKGND: // no need to erase background seperately
63 return 1;
64 case WM_PAINT:
65 event_flags |= VO_EVENT_EXPOSE;
66 break;
67 case WM_MOVE:
68 p.x = 0;
69 p.y = 0;
70 ClientToScreen(vo_window, &p);
71 vo_dx = p.x;
72 vo_dy = p.y;
73 break;
74 case WM_SIZE:
75 event_flags |= VO_EVENT_RESIZE;
76 GetClientRect(vo_window, &r);
77 vo_dwidth = r.right;
78 vo_dheight = r.bottom;
79 break;
80 case WM_WINDOWPOSCHANGING:
81 if (vo_keepaspect && !vo_fs) {
82 WINDOWPOS *wpos = lParam;
83 int xborder, yborder;
84 RECT r2;
85 GetClientRect(vo_window, &r);
86 GetWindowRect(vo_window, &r2);
87 xborder = (r2.right - r2.left) - (r.right - r.left);
88 yborder = (r2.bottom - r2.top) - (r.bottom - r.top);
89 wpos->cx -= xborder; wpos->cy -= yborder;
90 aspect_fit(&wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
91 wpos->cx += xborder; wpos->cy += yborder;
93 return 0;
94 case WM_CLOSE:
95 mplayer_put_key(KEY_CLOSE_WIN);
96 break;
97 case WM_SYSCOMMAND:
98 switch (wParam) {
99 case SC_SCREENSAVE:
100 case SC_MONITORPOWER:
101 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
102 return 0;
104 break;
105 case WM_KEYDOWN:
106 mpkey = lookup_keymap_table(vk_map, wParam);
107 if (mpkey)
108 mplayer_put_key(mpkey);
109 break;
110 case WM_CHAR:
111 mplayer_put_key(wParam);
112 break;
113 case WM_LBUTTONDOWN:
114 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
115 mplayer_put_key(MOUSE_BTN0);
116 break;
118 if (!vo_fs) {
119 ReleaseCapture();
120 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
121 return 0;
123 break;
124 case WM_MBUTTONDOWN:
125 if (!vo_nomouse_input)
126 mplayer_put_key(MOUSE_BTN1);
127 break;
128 case WM_RBUTTONDOWN:
129 if (!vo_nomouse_input)
130 mplayer_put_key(MOUSE_BTN2);
131 break;
132 case WM_MOUSEMOVE:
133 if (enable_mouse_movements) {
134 char cmd_str[40];
135 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i",
136 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
137 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
139 break;
140 case WM_MOUSEWHEEL:
141 if (!vo_nomouse_input) {
142 int x = GET_WHEEL_DELTA_WPARAM(wParam);
143 if (x > 0)
144 mplayer_put_key(MOUSE_BTN3);
145 else
146 mplayer_put_key(MOUSE_BTN4);
147 break;
151 return DefWindowProc(hWnd, message, wParam, lParam);
155 * \brief Dispatch incoming window events and handle them.
157 * This function should be placed inside libvo's function "check_events".
159 * Global libvo variables changed:
160 * vo_dwidth: new window client area width
161 * vo_dheight: new window client area height
163 * \return int with these flags possibly set, take care to handle in the right order
164 * if it matters in your driver:
166 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
167 * driver render context accordingly.
168 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
169 * the window if the movie is paused.
171 int vo_w32_check_events(void) {
172 MSG msg;
173 event_flags = 0;
174 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
175 TranslateMessage(&msg);
176 DispatchMessage(&msg);
178 if (WinID >= 0) {
179 RECT r;
180 GetClientRect(vo_window, &r);
181 if (r.right != vo_dwidth || r.bottom != vo_dheight) {
182 vo_dwidth = r.right; vo_dheight = r.bottom;
183 event_flags |= VO_EVENT_RESIZE;
185 GetClientRect(WinID, &r);
186 if (r.right != vo_dwidth || r.bottom != vo_dheight)
187 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
190 return event_flags;
193 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
194 // this defaults to the last screen if specified number does not exist
195 xinerama_x = r->left;
196 xinerama_y = r->top;
197 vo_screenwidth = r->right - r->left;
198 vo_screenheight = r->bottom - r->top;
199 if (mon_cnt == xinerama_screen)
200 return FALSE;
201 mon_cnt++;
202 return TRUE;
206 * \brief Update screen information.
208 * This function should be called in libvo's "control" callback
209 * with parameter VOCTRL_UPDATE_SCREENINFO.
210 * Note that this also enables the new API where geometry and aspect
211 * calculations are done in video_out.c:config_video_out
213 * Global libvo variables changed:
214 * xinerama_x
215 * xinerama_y
216 * vo_screenwidth
217 * vo_screenheight
219 void w32_update_xinerama_info(void) {
220 xinerama_x = xinerama_y = 0;
221 if (xinerama_screen < -1) {
222 int tmp;
223 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
224 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
225 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
226 if (tmp) vo_screenwidth = tmp;
227 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
228 if (tmp) vo_screenheight = tmp;
229 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
230 MONITORINFO mi;
231 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
232 mi.cbSize = sizeof(mi);
233 myGetMonitorInfo(m, &mi);
234 xinerama_x = mi.rcMonitor.left;
235 xinerama_y = mi.rcMonitor.top;
236 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
237 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
238 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
239 mon_cnt = 0;
240 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
242 aspect_save_screenres(vo_screenwidth, vo_screenheight);
245 static void updateScreenProperties(void) {
246 DEVMODE dm;
247 dm.dmSize = sizeof dm;
248 dm.dmDriverExtra = 0;
249 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
250 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
251 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
252 return;
255 vo_screenwidth = dm.dmPelsWidth;
256 vo_screenheight = dm.dmPelsHeight;
257 vo_depthonscreen = dm.dmBitsPerPel;
258 w32_update_xinerama_info();
261 static void changeMode(void) {
262 DEVMODE dm;
263 dm.dmSize = sizeof dm;
264 dm.dmDriverExtra = 0;
266 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
267 dm.dmBitsPerPel = vo_depthonscreen;
268 dm.dmPelsWidth = vo_screenwidth;
269 dm.dmPelsHeight = vo_screenheight;
271 if (vo_vm) {
272 int bestMode = -1;
273 int bestScore = INT_MAX;
274 int i;
275 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
276 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
277 if (dm.dmBitsPerPel != vo_depthonscreen) continue;
278 if (dm.dmPelsWidth < o_dwidth) continue;
279 if (dm.dmPelsHeight < o_dheight) continue;
281 if (score < bestScore) {
282 bestScore = score;
283 bestMode = i;
287 if (bestMode != -1)
288 EnumDisplaySettings(0, bestMode, &dm);
290 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
294 static void resetMode(void) {
295 if (vo_vm)
296 ChangeDisplaySettings(0, 0);
299 static int createRenderingContext(void) {
300 HWND layer = HWND_NOTOPMOST;
301 PIXELFORMATDESCRIPTOR pfd;
302 HDC vo_hdc = GetDC(vo_window);
303 RECT r;
304 int pf;
305 if (WinID < 0) {
306 int style = (vo_border && !vo_fs) ?
307 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
309 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
310 if (vo_fs) {
311 changeMode();
312 while (ShowCursor(0) >= 0) /**/ ;
313 } else {
314 resetMode();
315 while (ShowCursor(1) < 0) /**/ ;
317 updateScreenProperties();
318 ShowWindow(vo_window, SW_HIDE);
319 SetWindowLong(vo_window, GWL_STYLE, style);
320 if (vo_fs) {
321 prev_width = vo_dwidth;
322 prev_height = vo_dheight;
323 prev_x = vo_dx;
324 prev_y = vo_dy;
325 vo_dwidth = vo_screenwidth;
326 vo_dheight = vo_screenheight;
327 vo_dx = xinerama_x;
328 vo_dy = xinerama_y;
329 } else {
330 // make sure there are no "stale" resize events
331 // that would set vo_d* to wrong values
332 vo_w32_check_events();
333 vo_dwidth = prev_width;
334 vo_dheight = prev_height;
335 vo_dx = prev_x;
336 vo_dy = prev_y;
337 // HACK around what probably is a windows focus bug:
338 // when pressing 'f' on the console, then 'f' again to
339 // return to windowed mode, any input into the video
340 // window is lost forever.
341 SetFocus(vo_window);
343 r.left = vo_dx;
344 r.right = r.left + vo_dwidth;
345 r.top = vo_dy;
346 r.bottom = r.top + vo_dheight;
347 AdjustWindowRect(&r, style, 0);
348 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
351 memset(&pfd, 0, sizeof pfd);
352 pfd.nSize = sizeof pfd;
353 pfd.nVersion = 1;
354 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
355 pfd.iPixelType = PFD_TYPE_RGBA;
356 pfd.cColorBits = 24;
357 pfd.iLayerType = PFD_MAIN_PLANE;
358 pf = ChoosePixelFormat(vo_hdc, &pfd);
359 if (!pf) {
360 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
361 ReleaseDC(vo_window, vo_hdc);
362 return 0;
365 SetPixelFormat(vo_hdc, pf, &pfd);
367 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, vo_depthonscreen);
369 ReleaseDC(vo_window, vo_hdc);
370 return 1;
374 * \brief Configure and show window on the screen.
376 * This function should be called in libvo's "config" callback.
377 * It configures a window and shows it on the screen.
379 * Global libvo variables changed:
380 * vo_fs
381 * vo_vm
383 * \return 1 - Success, 0 - Failure
385 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
386 // store original size for videomode switching
387 o_dwidth = width;
388 o_dheight = height;
390 if (WinID < 0) {
391 // the desired size is ignored in wid mode, it always matches the window size.
392 prev_width = vo_dwidth = width;
393 prev_height = vo_dheight = height;
394 prev_x = vo_dx;
395 prev_y = vo_dy;
398 vo_fs = flags & VOFLAG_FULLSCREEN;
399 vo_vm = flags & VOFLAG_MODESWITCHING;
400 return createRenderingContext();
404 * \brief Initialize w32_common framework.
406 * The first function that should be called from the w32_common framework.
407 * It handles window creation on the screen with proper title and attributes.
408 * It also initializes the framework's internal variables. The function should
409 * be called after your own preinit initialization and you shouldn't do any
410 * window management on your own.
412 * Global libvo variables changed:
413 * vo_w32_window
414 * vo_depthonscreen
415 * vo_screenwidth
416 * vo_screenheight
418 * \return 1 = Success, 0 = Failure
420 int vo_w32_init(void) {
421 HICON mplayerIcon = 0;
422 char exedir[MAX_PATH];
423 HINSTANCE user32;
425 if (vo_window)
426 return 1;
428 hInstance = GetModuleHandle(0);
430 if (GetModuleFileName(0, exedir, MAX_PATH))
431 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
432 if (!mplayerIcon)
433 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
436 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
438 if (!RegisterClassEx(&wcex)) {
439 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
440 return 0;
444 if (WinID >= 0)
446 RECT r;
447 GetClientRect(WinID, &r);
448 vo_dwidth = r.right; vo_dheight = r.bottom;
449 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
450 WS_CHILD | WS_VISIBLE,
451 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
452 EnableWindow(vo_window, 0);
453 } else
454 vo_window = CreateWindowEx(0, classname, classname,
455 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
456 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
457 if (!vo_window) {
458 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
459 return 0;
462 myMonitorFromWindow = NULL;
463 myGetMonitorInfo = NULL;
464 myEnumDisplayMonitors = NULL;
465 user32 = GetModuleHandle("user32.dll");
466 if (user32) {
467 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
468 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
469 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
471 updateScreenProperties();
473 return 1;
477 * \brief Toogle fullscreen / windowed mode.
479 * Should be called on VOCTRL_FULLSCREEN event. The window is
480 * always resized after this call, so the rendering context
481 * should be reinitialized with the new dimensions.
482 * It is unspecified if vo_check_events will create a resize
483 * event in addition or not.
485 * Global libvo variables changed:
486 * vo_dwidth
487 * vo_dheight
488 * vo_fs
491 void vo_w32_fullscreen(void) {
492 vo_fs = !vo_fs;
494 createRenderingContext();
498 * \brief Toogle window border attribute.
500 * Should be called on VOCTRL_BORDER event.
502 * Global libvo variables changed:
503 * vo_border
505 void vo_w32_border(void) {
506 vo_border = !vo_border;
507 createRenderingContext();
511 * \brief Toogle window ontop attribute.
513 * Should be called on VOCTRL_ONTOP event.
515 * Global libvo variables changed:
516 * vo_ontop
518 void vo_w32_ontop( void )
520 vo_ontop = !vo_ontop;
521 if (!vo_fs) {
522 createRenderingContext();
527 * \brief Uninitialize w32_common framework.
529 * Should be called last in video driver's uninit function. First release
530 * anything built on top of the created window e.g. rendering context inside
531 * and call vo_w32_uninit at the end.
533 void vo_w32_uninit(void) {
534 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
535 resetMode();
536 ShowCursor(1);
537 vo_depthonscreen = 0;
538 DestroyWindow(vo_window);
539 vo_window = 0;
540 UnregisterClass(classname, 0);