mplayer.c: #include mplayer.h and access_mpcontext.h
[mplayer.git] / libvo / w32_common.c
blob1346f50a7ff15e203f31755b6ca7ee4455e76b62
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"
14 // To get "#define vo_ontop global_vo->opts->vo_ontop" etc
15 #include "old_vo_defines.h"
17 extern int enable_mouse_movements;
19 #ifndef MONITOR_DEFAULTTOPRIMARY
20 #define MONITOR_DEFAULTTOPRIMARY 1
21 #endif
23 static const char classname[] = "MPlayer - Media player for Win32";
24 int vo_vm = 0;
26 static int depthonscreen;
27 // last non-fullscreen extends
28 static int prev_width;
29 static int prev_height;
30 static int prev_x;
31 static int prev_y;
33 static uint32_t o_dwidth;
34 static uint32_t o_dheight;
36 static HINSTANCE hInstance;
37 #define vo_window vo_w32_window
38 HWND vo_window = 0;
39 static int event_flags;
40 static int mon_cnt;
42 static HMONITOR (WINAPI* myMonitorFromWindow)(HWND, DWORD);
43 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
44 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
46 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
47 RECT r;
48 POINT p;
49 if (WinID < 0 || message == WM_PAINT || message == WM_ERASEBKGND ||
50 message == WM_SIZE) {
51 switch (message) {
52 case WM_ERASEBKGND: // no need to erase background seperately
53 return 1;
54 case WM_PAINT:
55 event_flags |= VO_EVENT_EXPOSE;
56 break;
57 case WM_MOVE:
58 p.x = 0;
59 p.y = 0;
60 ClientToScreen(vo_window, &p);
61 vo_dx = p.x;
62 vo_dy = p.y;
63 break;
64 case WM_SIZE:
65 event_flags |= VO_EVENT_RESIZE;
66 GetClientRect(vo_window, &r);
67 vo_dwidth = r.right;
68 vo_dheight = r.bottom;
69 break;
70 case WM_WINDOWPOSCHANGING:
71 if (vo_keepaspect && !vo_fs) {
72 WINDOWPOS *wpos = lParam;
73 int xborder, yborder;
74 RECT r2;
75 GetClientRect(vo_window, &r);
76 GetWindowRect(vo_window, &r2);
77 xborder = (r2.right - r2.left) - (r.right - r.left);
78 yborder = (r2.bottom - r2.top) - (r.bottom - r.top);
79 wpos->cx -= xborder; wpos->cy -= yborder;
80 aspect_fit(global_vo, &wpos->cx, &wpos->cy, wpos->cx, wpos->cy);
81 wpos->cx += xborder; wpos->cy += yborder;
83 return 0;
84 case WM_CLOSE:
85 mplayer_put_key(KEY_CLOSE_WIN);
86 break;
87 case WM_SYSCOMMAND:
88 switch (wParam) {
89 case SC_SCREENSAVE:
90 case SC_MONITORPOWER:
91 mp_msg(MSGT_VO, MSGL_V, "vo: win32: killing screensaver\n");
92 return 0;
94 break;
95 case WM_KEYDOWN:
96 switch (wParam) {
97 case VK_LEFT: mplayer_put_key(KEY_LEFT); break;
98 case VK_UP: mplayer_put_key(KEY_UP); break;
99 case VK_RIGHT: mplayer_put_key(KEY_RIGHT); break;
100 case VK_DOWN: mplayer_put_key(KEY_DOWN); break;
101 case VK_TAB: mplayer_put_key(KEY_TAB); break;
102 case VK_CONTROL: mplayer_put_key(KEY_CTRL); break;
103 case VK_BACK: mplayer_put_key(KEY_BS); break;
104 case VK_DELETE: mplayer_put_key(KEY_DELETE); break;
105 case VK_INSERT: mplayer_put_key(KEY_INSERT); break;
106 case VK_HOME: mplayer_put_key(KEY_HOME); break;
107 case VK_END: mplayer_put_key(KEY_END); break;
108 case VK_PRIOR: mplayer_put_key(KEY_PAGE_UP); break;
109 case VK_NEXT: mplayer_put_key(KEY_PAGE_DOWN); break;
110 case VK_ESCAPE: mplayer_put_key(KEY_ESC); break;
112 break;
113 case WM_CHAR:
114 mplayer_put_key(wParam);
115 break;
116 case WM_LBUTTONDOWN:
117 if (!vo_nomouse_input && (vo_fs || (wParam & MK_CONTROL))) {
118 mplayer_put_key(MOUSE_BTN0);
119 break;
121 if (!vo_fs) {
122 ReleaseCapture();
123 SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
124 return 0;
126 break;
127 case WM_MBUTTONDOWN:
128 if (!vo_nomouse_input)
129 mplayer_put_key(MOUSE_BTN1);
130 break;
131 case WM_RBUTTONDOWN:
132 if (!vo_nomouse_input)
133 mplayer_put_key(MOUSE_BTN2);
134 break;
135 case WM_MOUSEMOVE:
136 if (enable_mouse_movements) {
137 char cmd_str[40];
138 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i",
139 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
140 mp_input_queue_cmd(global_vo->input_ctx, mp_input_parse_cmd(cmd_str));
142 break;
143 case WM_MOUSEWHEEL:
144 if (!vo_nomouse_input) {
145 int x = GET_WHEEL_DELTA_WPARAM(wParam);
146 if (x > 0)
147 mplayer_put_key(MOUSE_BTN3);
148 else
149 mplayer_put_key(MOUSE_BTN4);
150 break;
153 } else switch (message) {
154 case WM_MOUSEMOVE:
155 case WM_LBUTTONDOWN:
156 case WM_LBUTTONUP:
157 case WM_LBUTTONDBLCLK:
158 case WM_MBUTTONDOWN:
159 case WM_MBUTTONUP:
160 case WM_MBUTTONDBLCLK:
161 case WM_RBUTTONDOWN:
162 case WM_RBUTTONUP:
163 case WM_RBUTTONDBLCLK:
164 SendNotifyMessage(WinID, message, wParam, lParam);
167 return DefWindowProc(hWnd, message, wParam, lParam);
170 int vo_w32_check_events(void) {
171 MSG msg;
172 event_flags = 0;
173 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
174 TranslateMessage(&msg);
175 DispatchMessage(&msg);
177 if (WinID >= 0) {
178 RECT r;
179 GetClientRect(WinID, &r);
180 if (r.right != vo_dwidth || r.bottom != vo_dheight)
181 MoveWindow(vo_window, 0, 0, r.right, r.bottom, FALSE);
184 return event_flags;
187 static BOOL CALLBACK mon_enum(HMONITOR hmon, HDC hdc, LPRECT r, LPARAM p) {
188 // this defaults to the last screen if specified number does not exist
189 xinerama_x = r->left;
190 xinerama_y = r->top;
191 vo_screenwidth = r->right - r->left;
192 vo_screenheight = r->bottom - r->top;
193 if (mon_cnt == xinerama_screen)
194 return FALSE;
195 mon_cnt++;
196 return TRUE;
199 void w32_update_xinerama_info(void) {
200 xinerama_x = xinerama_y = 0;
201 if (xinerama_screen < -1) {
202 int tmp;
203 xinerama_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
204 xinerama_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
205 tmp = GetSystemMetrics(SM_CXVIRTUALSCREEN);
206 if (tmp) vo_screenwidth = tmp;
207 tmp = GetSystemMetrics(SM_CYVIRTUALSCREEN);
208 if (tmp) vo_screenheight = tmp;
209 } else if (xinerama_screen == -1 && myMonitorFromWindow && myGetMonitorInfo) {
210 MONITORINFO mi;
211 HMONITOR m = myMonitorFromWindow(vo_window, MONITOR_DEFAULTTOPRIMARY);
212 mi.cbSize = sizeof(mi);
213 myGetMonitorInfo(m, &mi);
214 xinerama_x = mi.rcMonitor.left;
215 xinerama_y = mi.rcMonitor.top;
216 vo_screenwidth = mi.rcMonitor.right - mi.rcMonitor.left;
217 vo_screenheight = mi.rcMonitor.bottom - mi.rcMonitor.top;
218 } else if (xinerama_screen > 0 && myEnumDisplayMonitors) {
219 mon_cnt = 0;
220 myEnumDisplayMonitors(NULL, NULL, mon_enum, 0);
222 aspect_save_screenres(vo_screenwidth, vo_screenheight);
225 static void updateScreenProperties(void) {
226 DEVMODE dm;
227 dm.dmSize = sizeof dm;
228 dm.dmDriverExtra = 0;
229 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
230 if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm)) {
231 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to enumerate display settings!\n");
232 return;
235 vo_screenwidth = dm.dmPelsWidth;
236 vo_screenheight = dm.dmPelsHeight;
237 depthonscreen = dm.dmBitsPerPel;
238 w32_update_xinerama_info();
241 static void changeMode(void) {
242 DEVMODE dm;
243 dm.dmSize = sizeof dm;
244 dm.dmDriverExtra = 0;
246 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
247 dm.dmBitsPerPel = depthonscreen;
248 dm.dmPelsWidth = vo_screenwidth;
249 dm.dmPelsHeight = vo_screenheight;
251 if (vo_vm) {
252 int bestMode = -1;
253 int bestScore = INT_MAX;
254 int i;
255 for (i = 0; EnumDisplaySettings(0, i, &dm); ++i) {
256 int score = (dm.dmPelsWidth - o_dwidth) * (dm.dmPelsHeight - o_dheight);
257 if (dm.dmBitsPerPel != depthonscreen) continue;
258 if (dm.dmPelsWidth < o_dwidth) continue;
259 if (dm.dmPelsHeight < o_dheight) continue;
261 if (score < bestScore) {
262 bestScore = score;
263 bestMode = i;
267 if (bestMode != -1)
268 EnumDisplaySettings(0, bestMode, &dm);
270 ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
274 static void resetMode(void) {
275 if (vo_vm)
276 ChangeDisplaySettings(0, 0);
279 static int createRenderingContext(void) {
280 HWND layer = HWND_NOTOPMOST;
281 PIXELFORMATDESCRIPTOR pfd;
282 HDC vo_hdc = GetDC(vo_window);
283 RECT r;
284 int pf;
285 if (WinID < 0) {
286 int style = (vo_border && !vo_fs) ?
287 (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP;
289 if (vo_fs || vo_ontop) layer = HWND_TOPMOST;
290 if (vo_fs) {
291 changeMode();
292 while (ShowCursor(0) >= 0) /**/ ;
293 } else {
294 resetMode();
295 while (ShowCursor(1) < 0) /**/ ;
297 updateScreenProperties();
298 ShowWindow(vo_window, SW_HIDE);
299 SetWindowLong(vo_window, GWL_STYLE, style);
300 if (vo_fs) {
301 prev_width = vo_dwidth;
302 prev_height = vo_dheight;
303 prev_x = vo_dx;
304 prev_y = vo_dy;
305 vo_dwidth = vo_screenwidth;
306 vo_dheight = vo_screenheight;
307 vo_dx = xinerama_x;
308 vo_dy = xinerama_y;
309 } else {
310 // make sure there are no "stale" resize events
311 // that would set vo_d* to wrong values
312 vo_w32_check_events();
313 vo_dwidth = prev_width;
314 vo_dheight = prev_height;
315 vo_dx = prev_x;
316 vo_dy = prev_y;
317 // HACK around what probably is a windows focus bug:
318 // when pressing 'f' on the console, then 'f' again to
319 // return to windowed mode, any input into the video
320 // window is lost forever.
321 SetFocus(vo_window);
323 r.left = vo_dx;
324 r.right = r.left + vo_dwidth;
325 r.top = vo_dy;
326 r.bottom = r.top + vo_dheight;
327 AdjustWindowRect(&r, style, 0);
328 SetWindowPos(vo_window, layer, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_SHOWWINDOW);
331 memset(&pfd, 0, sizeof pfd);
332 pfd.nSize = sizeof pfd;
333 pfd.nVersion = 1;
334 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
335 pfd.iPixelType = PFD_TYPE_RGBA;
336 pfd.cColorBits = 24;
337 pfd.iLayerType = PFD_MAIN_PLANE;
338 pf = ChoosePixelFormat(vo_hdc, &pfd);
339 if (!pf) {
340 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to select a valid pixel format!\n");
341 ReleaseDC(vo_window, vo_hdc);
342 return 0;
345 SetPixelFormat(vo_hdc, pf, &pfd);
347 mp_msg(MSGT_VO, MSGL_V, "vo: win32: running at %dx%d with depth %d\n", vo_screenwidth, vo_screenheight, depthonscreen);
349 ReleaseDC(vo_window, vo_hdc);
350 return 1;
353 int vo_w32_config(uint32_t width, uint32_t height, uint32_t flags) {
354 // store original size for videomode switching
355 o_dwidth = width;
356 o_dheight = height;
358 prev_width = vo_dwidth = width;
359 prev_height = vo_dheight = height;
360 prev_x = vo_dx;
361 prev_y = vo_dy;
363 vo_fs = flags & VOFLAG_FULLSCREEN;
364 vo_vm = flags & VOFLAG_MODESWITCHING;
365 return createRenderingContext();
368 int vo_w32_init(void) {
369 HICON mplayerIcon = 0;
370 char exedir[MAX_PATH];
371 HINSTANCE user32;
373 if (vo_window)
374 return 1;
376 hInstance = GetModuleHandle(0);
378 if (GetModuleFileName(0, exedir, MAX_PATH))
379 mplayerIcon = ExtractIcon(hInstance, exedir, 0);
380 if (!mplayerIcon)
381 mplayerIcon = LoadIcon(0, IDI_APPLICATION);
384 WNDCLASSEX wcex = { sizeof wcex, CS_OWNDC | CS_DBLCLKS, WndProc, 0, 0, hInstance, mplayerIcon, LoadCursor(0, IDC_ARROW), NULL, 0, classname, mplayerIcon };
386 if (!RegisterClassEx(&wcex)) {
387 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to register window class!\n");
388 return 0;
392 if (WinID >= 0)
394 RECT r;
395 GetClientRect(WinID, &r);
396 vo_dwidth = r.right; vo_dheight = r.bottom;
397 vo_window = CreateWindowEx(WS_EX_NOPARENTNOTIFY, classname, classname,
398 WS_CHILD | WS_VISIBLE,
399 0, 0, vo_dwidth, vo_dheight, WinID, 0, hInstance, 0);
400 } else
401 vo_window = CreateWindowEx(0, classname, classname,
402 vo_border ? (WS_OVERLAPPEDWINDOW | WS_SIZEBOX) : WS_POPUP,
403 CW_USEDEFAULT, 0, 100, 100, 0, 0, hInstance, 0);
404 if (!vo_window) {
405 mp_msg(MSGT_VO, MSGL_ERR, "vo: win32: unable to create window!\n");
406 return 0;
409 myMonitorFromWindow = NULL;
410 myGetMonitorInfo = NULL;
411 myEnumDisplayMonitors = NULL;
412 user32 = GetModuleHandle("user32.dll");
413 if (user32) {
414 myMonitorFromWindow = (void *)GetProcAddress(user32, "MonitorFromWindow");
415 myGetMonitorInfo = GetProcAddress(user32, "GetMonitorInfoA");
416 myEnumDisplayMonitors = GetProcAddress(user32, "EnumDisplayMonitors");
418 updateScreenProperties();
420 return 1;
423 void vo_w32_fullscreen(void) {
424 vo_fs = !vo_fs;
426 createRenderingContext();
429 void vo_w32_border(void) {
430 vo_border = !vo_border;
431 createRenderingContext();
434 void vo_w32_ontop( void )
436 vo_ontop = !vo_ontop;
437 if (!vo_fs) {
438 createRenderingContext();
442 void vo_w32_uninit(void) {
443 mp_msg(MSGT_VO, MSGL_V, "vo: win32: uninit\n");
444 resetMode();
445 ShowCursor(1);
446 depthonscreen = 0;
447 DestroyWindow(vo_window);
448 vo_window = 0;
449 UnregisterClass(classname, 0);