input: move all key code lists to input/keycodes.h
[mplayer/greg.git] / libvo / w32_common.c
blob9b89602f401c30ba44515d7bba49e67b1238046b
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 "input/keycodes.h"
27 #include "input/input.h"
28 #include "mp_msg.h"
29 #include "video_out.h"
30 #include "aspect.h"
31 #include "w32_common.h"
32 #include "mp_fifo.h"
34 #ifndef MONITOR_DEFAULTTOPRIMARY
35 #define MONITOR_DEFAULTTOPRIMARY 1
36 #endif
38 static const char classname[] = "MPlayer - The Movie Player";
39 int vo_vm = 0;
41 static int depthonscreen;
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 mp_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(global_vo, &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 vo_mouse_movement(global_vo, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
165 break;
166 case WM_MOUSEWHEEL:
167 if (!vo_nomouse_input) {
168 int x = GET_WHEEL_DELTA_WPARAM(wParam);
169 if (x > 0)
170 mplayer_put_key(MOUSE_BTN3);
171 else
172 mplayer_put_key(MOUSE_BTN4);
173 break;
177 return DefWindowProc(hWnd, message, wParam, lParam);
181 * \brief Dispatch incoming window events and handle them.
183 * This function should be placed inside libvo's function "check_events".
185 * Global libvo variables changed:
186 * vo_dwidth: new window client area width
187 * vo_dheight: new window client area height
189 * \return int with these flags possibly set, take care to handle in the right order
190 * if it matters in your driver:
192 * VO_EVENT_RESIZE = The window was resized. If necessary reinit your
193 * driver render context accordingly.
194 * VO_EVENT_EXPOSE = The window was exposed. Call e.g. flip_frame() to redraw
195 * the window if the movie is paused.
197 int vo_w32_check_events(void) {
198 MSG msg;
199 event_flags = 0;
200 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
201 TranslateMessage(&msg);
202 DispatchMessage(&msg);
204 if (WinID >= 0) {
205 BOOL res;
206 RECT r;
207 res = GetClientRect(vo_window, &r);
208 if (res && (r.right != vo_dwidth || r.bottom != vo_dheight)) {
209 vo_dwidth = r.right; vo_dheight = r.bottom;
210 event_flags |= VO_EVENT_RESIZE;
212 res = GetClientRect(WinID, &r);
213 if (res && (r.right != vo_dwidth || r.bottom != vo_dheight))
214 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
215 if (!IsWindow(WinID))
216 // Window has probably been closed, e.g. due to program crash
217 mplayer_put_key(KEY_CLOSE_WIN);
220 return event_flags;
223 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
224 // this defaults to the last screen if specified number does not exist
225 xinerama_x = r->left;
226 xinerama_y = r->top;
227 vo_screenwidth = r->right - r->left;
228 vo_screenheight = r->bottom - r->top;
229 if (mon_cnt == xinerama_screen)
230 return FALSE;
231 mon_cnt++;
232 return TRUE;
236 * \brief Update screen information.
238 * This function should be called in libvo's "control" callback
239 * with parameter VOCTRL_UPDATE_SCREENINFO.
240 * Note that this also enables the new API where geometry and aspect
241 * calculations are done in video_out.c:config_video_out
243 * Global libvo variables changed:
244 * xinerama_x
245 * xinerama_y
246 * vo_screenwidth
247 * vo_screenheight
249 void w32_update_xinerama_info(void) {
250 xinerama_x = xinerama_y = 0;
251 if (xinerama_screen < -1) {
252 int tmp;
253 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
254 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
255 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
256 if (tmp) vo_screenwidth = tmp;
257 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
258 if (tmp) vo_screenheight = tmp;
259 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
260 MONITORINFO mi;
261 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
262 mi.cbSize = sizeof(mi);
263 myGetMonitorInfo(m, &mi);
264 xinerama_x = mi.rcMonitor.left;
265 xinerama_y = mi.rcMonitor.top;
266 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
267 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
268 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
269 mon_cnt = 0;
270 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
272 aspect_save_screenres(vo_screenwidth, vo_screenheight);
275 static void updateScreenProperties(void) {
276 DEVMODE dm;
277 dm.dmSize = sizeof dm;
278 dm.dmDriverExtra = 0;
279 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
280 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
281 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
282 return;
285 vo_screenwidth = dm.dmPelsWidth;
286 vo_screenheight = dm.dmPelsHeight;
287 depthonscreen = dm.dmBitsPerPel;
288 w32_update_xinerama_info();
291 static void changeMode(void) {
292 DEVMODE dm;
293 dm.dmSize = sizeof dm;
294 dm.dmDriverExtra = 0;
296 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
297 dm.dmBitsPerPel = depthonscreen;
298 dm.dmPelsWidth = vo_screenwidth;
299 dm.dmPelsHeight = vo_screenheight;
301 if (vo_vm) {
302 int bestMode = -1;
303 int bestScore = INT_MAX;
304 int i;
305 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
306 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
307 if (dm.dmBitsPerPel != depthonscreen) continue;
308 if (dm.dmPelsWidth < o_dwidth) continue;
309 if (dm.dmPelsHeight < o_dheight) continue;
311 if (score < bestScore) {
312 bestScore = score;
313 bestMode = i;
317 if (bestMode != -1)
318 EnumDisplaySettings(0, bestMode, &dm);
320 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
324 static void resetMode(void) {
325 if (vo_vm)
326 ChangeDisplaySettings(0, 0);
329 static int createRenderingContext(void) {
330 HWND layer = HWND_NOTOPMOST;
331 RECT r;
332 int style = (vo_border && !vo_fs) ?
333 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
335 if (WinID >= 0)
336 return 1;
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);
378 return 1;
382 * \brief Configure and show window on the screen.
384 * This function should be called in libvo's "config" callback.
385 * It configures a window and shows it on the screen.
387 * Global libvo variables changed:
388 * vo_fs
389 * vo_vm
391 * \return 1 - Success, 0 - Failure
393 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
394 PIXELFORMATDESCRIPTOR pfd;
395 int pf;
396 HDC vo_hdc = vo_w32_get_dc(vo_window);
398 memset(&pfd, 0, sizeof pfd);
399 pfd.nSize = sizeof pfd;
400 pfd.nVersion = 1;
401 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
402 if (flags & VOFLAG_STEREO)
403 pfd.dwFlags |= PFD_STEREO;
404 pfd.iPixelType = PFD_TYPE_RGBA;
405 pfd.cColorBits = 24;
406 pfd.iLayerType = PFD_MAIN_PLANE;
407 pf = ChoosePixelFormat(vo_hdc, &pfd);
408 if (!pf) {
409 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
410 vo_w32_release_dc(vo_window, vo_hdc);
411 return 0;
414 SetPixelFormat(vo_hdc, pf, &pfd);
415 vo_w32_release_dc(vo_window, vo_hdc);
417 // we already have a fully initialized window, so nothing needs to be done
418 if (flags & VOFLAG_HIDDEN)
419 return 1;
420 // store original size for videomode switching
421 o_dwidth = width;
422 o_dheight = height;
424 if (WinID < 0) {
425 // the desired size is ignored in wid mode, it always matches the window size.
426 prev_width = vo_dwidth = width;
427 prev_height = vo_dheight = height;
428 prev_x = vo_dx;
429 prev_y = vo_dy;
432 vo_fs = flags & VOFLAG_FULLSCREEN;
433 vo_vm = flags & VOFLAG_MODESWITCHING;
434 return createRenderingContext();
438 * \brief return the name of the selected device if it is indepedant
439 * \return pointer to string, must be freed.
441 static char *get_display_name(void) {
442 DISPLAY_DEVICE disp;
443 disp.cb = sizeof(disp);
444 EnumDisplayDevices(NULL, vo_adapter_num, &disp, 0);
445 if (disp.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
446 return NULL;
447 return strdup(disp.DeviceName);
451 * \brief Initialize w32_common framework.
453 * The first function that should be called from the w32_common framework.
454 * It handles window creation on the screen with proper title and attributes.
455 * It also initializes the framework's internal variables. The function should
456 * be called after your own preinit initialization and you shouldn't do any
457 * window management on your own.
459 * Global libvo variables changed:
460 * vo_w32_window
461 * vo_screenwidth
462 * vo_screenheight
464 * \return 1 = Success, 0 = Failure
466 int vo_w32_init(void) {
467 HICON mplayerIcon = 0;
468 char exedir[MAX_PATH];
469 HINSTANCE user32;
470 char *dev;
472 if (vo_window)
473 return 1;
475 hInstance = GetModuleHandle(0);
477 if (GetModuleFileName(0, exedir, MAX_PATH))
478 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
479 if (!mplayerIcon)
480 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
483 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
485 if (!RegisterClassEx(&wcex)) {
486 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
487 return 0;
491 if (WinID >= 0)
493 RECT r;
494 GetClientRect(WinID, &r);
495 vo_dwidth = r.right; vo_dheight = r.bottom;
496 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
497 WS_CHILD | WS_VISIBLE,
498 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
499 EnableWindow(vo_window, 0);
500 } else
501 vo_window = CreateWindowEx(0, classname, classname,
502 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
503 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
504 if (!vo_window) {
505 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
506 return 0;
509 myMonitorFromWindow = NULL;
510 myGetMonitorInfo = NULL;
511 myEnumDisplayMonitors = NULL;
512 user32 = GetModuleHandle("user32.dll");
513 if (user32) {
514 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
515 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
516 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
518 dev_hdc = 0;
519 dev = get_display_name();
520 if (dev) dev_hdc = CreateDC(dev, NULL, NULL, NULL);
521 free(dev);
522 updateScreenProperties();
524 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, depthonscreen);
526 return 1;
530 * \brief Toogle fullscreen / windowed mode.
532 * Should be called on VOCTRL_FULLSCREEN event. The window is
533 * always resized after this call, so the rendering context
534 * should be reinitialized with the new dimensions.
535 * It is unspecified if vo_check_events will create a resize
536 * event in addition or not.
538 * Global libvo variables changed:
539 * vo_dwidth
540 * vo_dheight
541 * vo_fs
544 void vo_w32_fullscreen(void) {
545 vo_fs = !vo_fs;
547 createRenderingContext();
551 * \brief Toogle window border attribute.
553 * Should be called on VOCTRL_BORDER event.
555 * Global libvo variables changed:
556 * vo_border
558 void vo_w32_border(void) {
559 vo_border = !vo_border;
560 createRenderingContext();
564 * \brief Toogle window ontop attribute.
566 * Should be called on VOCTRL_ONTOP event.
568 * Global libvo variables changed:
569 * vo_ontop
571 void vo_w32_ontop( void )
573 vo_ontop = !vo_ontop;
574 if (!vo_fs) {
575 createRenderingContext();
580 * \brief Uninitialize w32_common framework.
582 * Should be called last in video driver's uninit function. First release
583 * anything built on top of the created window e.g. rendering context inside
584 * and call vo_w32_uninit at the end.
586 void vo_w32_uninit(void) {
587 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
588 resetMode();
589 ShowCursor(1);
590 depthonscreen = 0;
591 if (dev_hdc) DeleteDC(dev_hdc);
592 dev_hdc = 0;
593 DestroyWindow(vo_window);
594 vo_window = 0;
595 UnregisterClass(classname, 0);
599 * \brief get a device context to draw in
601 * \param wnd window the DC should belong to if it makes sense
603 HDC vo_w32_get_dc(HWND wnd) {
604 if (dev_hdc) return dev_hdc;
605 return GetDC(wnd);
609 * \brief release a device context
611 * \param wnd window the DC probably belongs to
613 void vo_w32_release_dc(HWND wnd, HDC dc) {
614 if (dev_hdc) return;
615 ReleaseDC(wnd, dc);