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