]> git.sesse.net Git - vlc/blob - modules/gui/wince/wince.cpp
Plugins: push cancellation down
[vlc] / modules / gui / wince / wince.cpp
1 /*****************************************************************************
2  * wince.cpp: WinCE gui plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Author: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111,
22  * USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35
36 #if defined( UNDER_CE ) && defined(__MINGW32__)
37 /* This is a gross hack for the wince gcc cross-compiler */
38 #define _off_t long
39 #endif
40
41 #include "wince.h"
42
43 #include <objbase.h>
44
45 /*****************************************************************************
46  * Local prototypes.
47  *****************************************************************************/
48 static int  Open   ( vlc_object_t * );
49 static void Close  ( vlc_object_t * );
50 static void Run    ( intf_thread_t * );
51
52 static int  OpenDialogs( vlc_object_t * );
53
54 static void* MainLoop  ( intf_thread_t * );
55 static void ShowDialog( intf_thread_t *, int, int, intf_dialog_args_t * );
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 #define EMBED_TEXT N_("Embed video in interface")
61 #define EMBED_LONGTEXT N_("Embed the video inside the interface instead " \
62     "of having it in a separate window.")
63
64 vlc_module_begin();
65     set_description( (char *) _("WinCE interface module") );
66     set_capability( "interface", 100 );
67     set_callbacks( Open, Close );
68     add_shortcut( "wince" );
69
70     add_bool( "wince-embed", 1, NULL,
71               EMBED_TEXT, EMBED_LONGTEXT, false );
72
73     add_submodule();
74     set_description( N_("WinCE dialogs provider") );
75     set_capability( "dialogs provider", 10 );
76     set_callbacks( OpenDialogs, Close );
77 vlc_module_end();
78
79 HINSTANCE hInstance = 0;
80
81 #if !defined(__BUILTIN__)
82 extern "C" BOOL WINAPI
83 DllMain( HANDLE hModule, DWORD fdwReason, LPVOID lpReserved )
84 {
85     hInstance = (HINSTANCE)hModule;
86     return TRUE;
87 }
88 #endif
89
90 /* Global variables used by _TOMB() / _FROMB() */
91 wchar_t pwsz_mbtow_wince[2048];
92 char psz_wtomb_wince[2048];
93
94 /*****************************************************************************
95  * Open: initialize interface
96  *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
98 {
99     intf_thread_t *p_intf = (intf_thread_t *)p_this;
100
101     // Check if the application is running.
102     // If it's running then focus its window and bail out.
103     HWND hwndMain = FindWindow( _T("VLC WinCE"), _T("VLC media player") );
104     if( hwndMain )
105     {
106         SetForegroundWindow( hwndMain );
107         return VLC_EGENERIC;
108     }
109
110     // Allocate instance and initialize some members
111     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
112     if( p_intf->p_sys == NULL )
113     {
114         msg_Err( p_intf, "out of memory" );
115         return VLC_EGENERIC;
116     }
117
118     // Suscribe to messages bank
119     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
120
121     // Misc init
122     p_intf->p_sys->p_audio_menu = NULL;
123     p_intf->p_sys->p_video_menu = NULL;
124     p_intf->p_sys->p_navig_menu = NULL;
125     p_intf->p_sys->p_settings_menu = NULL;
126
127     p_intf->pf_run = Run;
128     p_intf->pf_show_dialog = NULL;
129
130     p_intf->p_sys->p_input = NULL;
131     p_intf->p_sys->b_playing = 0;
132     p_intf->p_sys->i_playing = -1;
133     p_intf->p_sys->b_slider_free = 1;
134     p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
135
136     return VLC_SUCCESS;
137 }
138
139 static int OpenDialogs( vlc_object_t *p_this )
140 {
141     intf_thread_t *p_intf = (intf_thread_t *)p_this;
142     int i_ret = Open( p_this );
143
144     p_intf->pf_show_dialog = ShowDialog;
145
146     return i_ret;
147 }
148
149 /*****************************************************************************
150  * Close: destroy interface
151  *****************************************************************************/
152 static void Close( vlc_object_t *p_this )
153 {
154     intf_thread_t *p_intf = (intf_thread_t *)p_this;
155
156     if( p_intf->p_sys->p_input )
157     {
158         vlc_object_release( p_intf->p_sys->p_input );
159     }
160
161     MenuItemExt::ClearList( p_intf->p_sys->p_video_menu );
162     delete p_intf->p_sys->p_video_menu;
163     MenuItemExt::ClearList( p_intf->p_sys->p_audio_menu );
164     delete p_intf->p_sys->p_audio_menu;
165     MenuItemExt::ClearList( p_intf->p_sys->p_settings_menu );
166     delete p_intf->p_sys->p_settings_menu;
167     MenuItemExt::ClearList( p_intf->p_sys->p_navig_menu );
168     delete p_intf->p_sys->p_navig_menu;
169
170     if( p_intf->pf_show_dialog )
171     {
172         /* We must destroy the dialogs thread */
173 #if 0
174         wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
175         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
176 #endif
177         vlc_thread_join( p_intf );
178     }
179
180     // Unsuscribe to messages bank
181     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
182
183     // Destroy structure
184     free( p_intf->p_sys );
185 }
186
187 /*****************************************************************************
188  * Run: main loop
189  *****************************************************************************/
190 static void Run( intf_thread_t *p_intf )
191 {
192     if( p_intf->pf_show_dialog )
193     {
194         /* The module is used in dialog provider mode */
195
196         /* Create a new thread for the dialogs provider */
197         if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
198                                MainLoop, 0, true ) )
199         {
200             msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
201             p_intf->pf_show_dialog = NULL;
202         }
203     }
204     else
205     {
206         /* The module is used in interface mode */
207         MainLoop( p_intf );
208     }
209 }
210
211 static void* MainLoop( vlc_object_t * p_this )
212 {
213     intf_thread_t *p_intf = (intf_thread_t*)p_this;
214     MSG msg;
215     Interface *intf = 0;
216     int canc = vlc_savecancel ();
217
218     if( !hInstance ) hInstance = GetModuleHandle(NULL);
219
220     // Register window class
221     WNDCLASS wc;
222     wc.style = CS_HREDRAW | CS_VREDRAW ;
223     wc.lpfnWndProc = (WNDPROC)CBaseWindow::BaseWndProc;
224     wc.cbClsExtra = 0;
225     wc.cbWndExtra = 0;
226     wc.hIcon = NULL;
227     wc.hInstance = hInstance;
228     wc.hCursor = NULL;
229     wc.hbrBackground = (HBRUSH)(COLOR_MENU+1);
230     wc.lpszMenuName = NULL;
231     wc.lpszClassName = _T("VLC WinCE");
232     RegisterClass( &wc );
233
234 #ifndef UNDER_CE
235     /* Initialize OLE/COM */
236     CoInitialize( 0 );
237 #endif
238
239     if( !p_intf->pf_show_dialog )
240     {
241         /* The module is used in interface mode */
242         p_intf->p_sys->p_window = intf = new Interface( p_intf, 0, hInstance );
243
244         /* Create/Show the interface */
245         if( !intf->InitInstance() ) goto end;
246     }
247
248     /* Creates the dialogs provider */
249     p_intf->p_sys->p_window =
250         CreateDialogsProvider( p_intf, p_intf->pf_show_dialog ?
251                                NULL : p_intf->p_sys->p_window, hInstance );
252
253     p_intf->p_sys->pf_show_dialog = ShowDialog;
254
255     /* OK, initialization is over */
256     vlc_thread_ready( p_intf );
257
258     // Main message loop
259     while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
260     {
261         TranslateMessage( &msg );
262         DispatchMessage( &msg );
263     }
264
265  end:
266     delete intf;
267
268 #ifndef UNDER_CE
269     /* Uninitialize OLE/COM */
270     CoUninitialize();
271 #endif
272     vlc_restorecancel (canc);
273     return NULL;
274 }
275
276 /*****************************************************************************
277  * CBaseWindow Implementation
278  *****************************************************************************/
279 LRESULT CALLBACK CBaseWindow::BaseWndProc( HWND hwnd, UINT msg, WPARAM wParam,
280                                            LPARAM lParam )
281 {
282     CBaseWindow *p_obj;
283
284     // check to see if a copy of the 'this' pointer needs to be saved
285     if( msg == WM_CREATE )
286     {
287         p_obj = (CBaseWindow *)(((LPCREATESTRUCT)lParam)->lpCreateParams);
288         SetWindowLong( hwnd, GWL_USERDATA,
289                        (LONG)((LPCREATESTRUCT)lParam)->lpCreateParams );
290
291         p_obj->hWnd = hwnd;
292     }
293
294     if( msg == WM_INITDIALOG )
295     {
296         p_obj = (CBaseWindow *)lParam;
297         SetWindowLong( hwnd, GWL_USERDATA, lParam );
298         p_obj->hWnd = hwnd;
299     }
300
301     // Retrieve the pointer
302     p_obj = (CBaseWindow *)GetWindowLong( hwnd, GWL_USERDATA );
303
304     if( !p_obj ) return DefWindowProc( hwnd, msg, wParam, lParam );
305
306     // Filter message through child classes
307     return p_obj->WndProc( hwnd, msg, wParam, lParam );
308 }
309
310 int CBaseWindow::CreateDialogBox( HWND hwnd, CBaseWindow *p_obj )
311 {
312     uint8_t p_buffer[sizeof(DLGTEMPLATE) + sizeof(WORD) * 4];
313     DLGTEMPLATE *p_dlg_template = (DLGTEMPLATE *)p_buffer;
314     memset( p_dlg_template, 0, sizeof(DLGTEMPLATE) + sizeof(WORD) * 4 );
315
316     // these values are arbitrary, they won't be used normally anyhow
317     p_dlg_template->x  = 0; p_dlg_template->y  = 0;
318     p_dlg_template->cx = 300; p_dlg_template->cy = 300;
319     p_dlg_template->style =
320         DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX;
321
322     return DialogBoxIndirectParam( GetModuleHandle(0), p_dlg_template, hwnd,
323                                    (DLGPROC)p_obj->BaseWndProc, (LPARAM)p_obj);
324 }
325
326 /*****************************************************************************
327  * ShowDialog
328  *****************************************************************************/
329 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
330                         intf_dialog_args_t *p_arg )
331 {
332     SendMessage( p_intf->p_sys->p_window->GetHandle(), WM_CANCELMODE, 0, 0 );
333     if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
334
335     /* Hack to prevent popup events to be enqueued when
336      * one is already active */
337 #if 0
338     if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
339         !p_intf->p_sys->p_popup_menu )
340 #endif
341     {
342         SendMessage( p_intf->p_sys->p_window->GetHandle(),
343                      WM_APP + i_dialog_event, (WPARAM)i_arg, (LPARAM)p_arg );
344     }
345 }