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