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