decoder: don't control the vout when not started
[vlc.git] / doc / libvlc / win_player.c
blob900178f0ba78051345c065035b4dd03c90aa284b
1 /* compile: cc win_player.c -o win_player.exe -L<path/libvlc> -lvlc */
3 #include <windows.h>
4 #include <assert.h>
6 #include <vlc/vlc.h>
8 #define SCREEN_WIDTH 1500
9 #define SCREEN_HEIGHT 900
11 struct vlc_context
13 libvlc_instance_t *p_libvlc;
14 libvlc_media_player_t *p_mediaplayer;
18 static const char *AspectRatio = NULL;
20 static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
22 if( message == WM_CREATE )
24 /* Store p_mediaplayer for future use */
25 CREATESTRUCT *c = (CREATESTRUCT *)lParam;
26 SetWindowLongPtr( hWnd, GWLP_USERDATA, (LONG_PTR)c->lpCreateParams );
27 return 0;
30 LONG_PTR p_user_data = GetWindowLongPtr( hWnd, GWLP_USERDATA );
31 if( p_user_data == 0 )
32 return DefWindowProc(hWnd, message, wParam, lParam);
33 struct vlc_context *ctx = (struct vlc_context *)p_user_data;
35 switch(message)
37 case WM_DESTROY:
38 PostQuitMessage(0);
39 return 0;
40 case WM_DROPFILES:
42 HDROP hDrop = (HDROP)wParam;
43 char file_path[MAX_PATH];
44 libvlc_media_player_stop_async( ctx->p_mediaplayer );
46 if (DragQueryFile(hDrop, 0, file_path, sizeof(file_path)))
48 libvlc_media_t *p_media = libvlc_media_new_path( ctx->p_libvlc, file_path );
49 libvlc_media_t *p_old_media = libvlc_media_player_get_media( ctx->p_mediaplayer );
50 libvlc_media_player_set_media( ctx->p_mediaplayer, p_media );
51 libvlc_media_release( p_old_media );
53 libvlc_media_player_play( ctx->p_mediaplayer );
55 DragFinish(hDrop);
57 return 0;
58 case WM_KEYDOWN:
59 case WM_SYSKEYDOWN:
61 int key = tolower( (unsigned char)MapVirtualKey( wParam, 2 ) );
62 if (key == 'a')
64 if (AspectRatio == NULL)
65 AspectRatio = "16:10";
66 else if (strcmp(AspectRatio,"16:10")==0)
67 AspectRatio = "16:9";
68 else if (strcmp(AspectRatio,"16:9")==0)
69 AspectRatio = "4:3";
70 else if (strcmp(AspectRatio,"4:3")==0)
71 AspectRatio = "185:100";
72 else if (strcmp(AspectRatio,"185:100")==0)
73 AspectRatio = "221:100";
74 else if (strcmp(AspectRatio,"221:100")==0)
75 AspectRatio = "235:100";
76 else if (strcmp(AspectRatio,"235:100")==0)
77 AspectRatio = "239:100";
78 else if (strcmp(AspectRatio,"239:100")==0)
79 AspectRatio = "5:3";
80 else if (strcmp(AspectRatio,"5:3")==0)
81 AspectRatio = "5:4";
82 else if (strcmp(AspectRatio,"5:4")==0)
83 AspectRatio = "1:1";
84 else if (strcmp(AspectRatio,"1:1")==0)
85 AspectRatio = NULL;
86 libvlc_video_set_aspect_ratio( ctx->p_mediaplayer, AspectRatio );
88 break;
90 default: break;
93 return DefWindowProc (hWnd, message, wParam, lParam);
96 int WINAPI WinMain(HINSTANCE hInstance,
97 HINSTANCE hPrevInstance,
98 LPSTR lpCmdLine,
99 int nCmdShow)
101 WNDCLASSEX wc;
102 char *file_path;
103 struct vlc_context Context;
104 libvlc_media_t *p_media;
105 (void)hPrevInstance;
106 HWND hWnd;
108 /* remove "" around the given path */
109 if (lpCmdLine[0] == '"')
111 file_path = strdup( lpCmdLine+1 );
112 if (file_path[strlen(file_path)-1] == '"')
113 file_path[strlen(file_path)-1] = '\0';
115 else
116 file_path = strdup( lpCmdLine );
118 Context.p_libvlc = libvlc_new( 0, NULL );
119 p_media = libvlc_media_new_path( Context.p_libvlc, file_path );
120 free( file_path );
121 Context.p_mediaplayer = libvlc_media_player_new_from_media( p_media );
123 ZeroMemory(&wc, sizeof(WNDCLASSEX));
125 wc.cbSize = sizeof(WNDCLASSEX);
126 wc.style = CS_HREDRAW | CS_VREDRAW;
127 wc.lpfnWndProc = WindowProc;
128 wc.hInstance = hInstance;
129 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
130 wc.lpszClassName = "WindowClass";
132 RegisterClassEx(&wc);
134 RECT wr = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
135 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
137 hWnd = CreateWindowEx(0,
138 "WindowClass",
139 "libvlc Demo app",
140 WS_OVERLAPPEDWINDOW,
141 CW_USEDEFAULT, CW_USEDEFAULT,
142 wr.right - wr.left,
143 wr.bottom - wr.top,
144 NULL,
145 NULL,
146 hInstance,
147 &Context);
148 DragAcceptFiles(hWnd, TRUE);
150 libvlc_media_player_set_hwnd(Context.p_mediaplayer, hWnd);
152 ShowWindow(hWnd, nCmdShow);
154 libvlc_media_player_play( Context.p_mediaplayer );
156 MSG msg;
157 while (GetMessage(&msg, NULL, 0, 0))
159 TranslateMessage(&msg);
160 DispatchMessage(&msg);
162 if(msg.message == WM_QUIT)
163 break;
166 libvlc_media_player_stop_async( Context.p_mediaplayer );
168 libvlc_media_release( libvlc_media_player_get_media( Context.p_mediaplayer ) );
169 libvlc_media_player_release( Context.p_mediaplayer );
170 libvlc_release( Context.p_libvlc );
172 return msg.wParam;