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