Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

win32_specific.c

00001 /*****************************************************************************
00002  * win32_specific.c: Win32 specific features
00003  *****************************************************************************
00004  * Copyright (C) 2001-2004 the VideoLAN team
00005  * $Id: win32_specific.c 12816 2005-10-11 09:01:00Z damienf $
00006  *
00007  * Authors: Samuel Hocevar <[email protected]>
00008  *          Gildas Bazin <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 #include <string.h>                                              /* strdup() */
00025 #include <stdlib.h>                                                /* free() */
00026 
00027 #include <vlc/vlc.h>
00028 #include <vlc/input.h>
00029 #include "vlc_playlist.h"
00030 
00031 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
00032 #   include "../extras/getopt.h"
00033 #endif
00034 
00035 #if !defined( UNDER_CE )
00036 #   include <io.h>
00037 #   include <fcntl.h>
00038 #endif
00039 
00040 #include <winsock.h>
00041 
00042 /*****************************************************************************
00043  * system_Init: initialize winsock and misc other things.
00044  *****************************************************************************/
00045 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
00046 {
00047     WSADATA Data;
00048 
00049     /* Get our full path */
00050     char psz_path[MAX_PATH];
00051     char *psz_vlc;
00052 
00053 #if defined( UNDER_CE )
00054     wchar_t psz_wpath[MAX_PATH];
00055     if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
00056     {
00057         WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
00058                              psz_path, MAX_PATH, NULL, NULL );
00059     }
00060     else psz_path[0] = '\0';
00061 
00062 #else
00063     if( ppsz_argv[0] )
00064     {
00065         GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
00066     }
00067     else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
00068     {
00069         psz_path[0] = '\0';
00070     }
00071 #endif
00072 
00073     if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
00074 
00075     p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
00076 
00077     /* Set the default file-translation mode */
00078 #if !defined( UNDER_CE )
00079     _fmode = _O_BINARY;
00080     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
00081 #endif
00082 
00083     /* Call mdate() once to make sure it is initialized properly */
00084     mdate();
00085 
00086     /* WinSock Library Init. */
00087     if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
00088     {
00089         /* Confirm that the WinSock DLL supports 2.0.*/
00090         if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
00091         {
00092             /* We could not find a suitable WinSock DLL. */
00093             WSACleanup( );
00094         }
00095         else
00096         {
00097             /* Everything went ok. */
00098             return;
00099         }
00100     }
00101 
00102     /* Let's try with WinSock 1.1 */
00103     if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
00104     {
00105         /* Confirm that the WinSock DLL supports 1.1.*/
00106         if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
00107         {
00108             /* We could not find a suitable WinSock DLL. */
00109             WSACleanup( );
00110         }
00111         else
00112         {
00113             /* Everything went ok. */
00114             return;
00115         }
00116     }
00117 
00118     fprintf( stderr, "error: can't initialize WinSocks\n" );
00119 
00120     return;
00121 }
00122 
00123 /*****************************************************************************
00124  * system_Configure: check for system specific configuration options.
00125  *****************************************************************************/
00126 static void IPCHelperThread( vlc_object_t * );
00127 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
00128 
00129 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
00130 {
00131 #if !defined( UNDER_CE )
00132     p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
00133     p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
00134 
00135     /* Raise default priority of the current process */
00136 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
00137 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
00138 #endif
00139     if( config_GetInt( p_this, "high-priority" ) )
00140     {
00141         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
00142              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
00143         {
00144             msg_Dbg( p_this, "raised process priority" );
00145         }
00146         else
00147         {
00148             msg_Dbg( p_this, "could not raise process priority" );
00149         }
00150     }
00151 
00152     if( config_GetInt( p_this, "one-instance" ) )
00153     {
00154         HANDLE hmutex;
00155 
00156         msg_Info( p_this, "one instance mode ENABLED");
00157 
00158         /* Use a named mutex to check if another instance is already running */
00159         if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
00160         {
00161             /* Failed for some reason. Just ignore the option and go on as
00162              * normal. */
00163             msg_Err( p_this, "one instance mode DISABLED "
00164                      "(mutex couldn't be created)" );
00165             return;
00166         }
00167 
00168         if( GetLastError() != ERROR_ALREADY_EXISTS )
00169         {
00170             /* We are the 1st instance. */
00171             vlc_object_t *p_helper =
00172              (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
00173 
00174             /* Run the helper thread */
00175             if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
00176                                    VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
00177             {
00178                 msg_Err( p_this, "one instance mode DISABLED "
00179                          "(IPC helper thread couldn't be created)" );
00180 
00181             }
00182 
00183             /* Initialization done.
00184              * Release the mutex to unblock other instances */
00185             ReleaseMutex( hmutex );
00186         }
00187         else
00188         {
00189             /* Another instance is running */
00190 
00191             HWND ipcwindow;
00192 
00193             /* Wait until the 1st instance is initialized */
00194             WaitForSingleObject( hmutex, INFINITE );
00195 
00196             /* Locate the window created by the IPC helper thread of the
00197              * 1st instance */
00198             if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
00199             {
00200                 msg_Err( p_this, "one instance mode DISABLED "
00201                          "(couldn't find 1st instance of program)" );
00202                 ReleaseMutex( hmutex );
00203                 return;
00204             }
00205 
00206             /* We assume that the remaining parameters are filenames
00207              * and their input options */
00208             if( *pi_argc - 1 >= optind )
00209             {
00210                 COPYDATASTRUCT wm_data;
00211                 int i_opt, i_data;
00212                 char *p_data;
00213 
00214                 i_data = sizeof(int);
00215                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
00216                 {
00217                     i_data += sizeof(int);
00218                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
00219                 }
00220 
00221                 p_data = (char *)malloc( i_data );
00222                 *((int *)&p_data[0]) = *pi_argc - optind;
00223                 i_data = sizeof(int);
00224                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
00225                 {
00226                     int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
00227                     *((int *)&p_data[i_data]) = i_len;
00228                     i_data += sizeof(int);
00229                     memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
00230                     i_data += i_len;
00231                 }
00232 
00233                 /* Send our playlist items to the 1st instance */
00234                 wm_data.dwData = 0;
00235                 wm_data.cbData = i_data;
00236                 wm_data.lpData = p_data;
00237                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
00238             }
00239 
00240             /* Initialization done.
00241              * Release the mutex to unblock other instances */
00242             ReleaseMutex( hmutex );
00243 
00244             /* Bye bye */
00245             system_End( p_this );
00246             exit( 0 );
00247         }
00248     }
00249 
00250 #endif
00251 }
00252 
00253 static void IPCHelperThread( vlc_object_t *p_this )
00254 {
00255     HWND ipcwindow;
00256     MSG message;
00257 
00258     ipcwindow =
00259         CreateWindow( _T("STATIC"),                  /* name of window class */
00260                   _T("VLC ipc ") _T(VERSION),       /* window title bar text */
00261                   0,                                         /* window style */
00262                   0,                                 /* default X coordinate */
00263                   0,                                 /* default Y coordinate */
00264                   0,                                         /* window width */
00265                   0,                                        /* window height */
00266                   NULL,                                  /* no parent window */
00267                   NULL,                            /* no menu in this window */
00268                   GetModuleHandle(NULL),  /* handle of this program instance */
00269                   NULL );                               /* sent to WM_CREATE */
00270 
00271     SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
00272     SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
00273 
00274     /* Signal the creation of the thread and events queue */
00275     vlc_thread_ready( p_this );
00276 
00277     while( GetMessage( &message, NULL, 0, 0 ) )
00278     {
00279         TranslateMessage( &message );
00280         DispatchMessage( &message );
00281     }
00282 }
00283 
00284 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
00285                                 LPARAM lParam )
00286 {
00287     if( uMsg == WM_COPYDATA )
00288     {
00289         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
00290         vlc_object_t *p_this;
00291         playlist_t *p_playlist;
00292 
00293         p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
00294 
00295         if( !p_this ) return 0;
00296 
00297         /* Add files to the playlist */
00298         p_playlist = (playlist_t *)vlc_object_find( p_this,
00299                                                     VLC_OBJECT_PLAYLIST,
00300                                                     FIND_ANYWHERE );
00301         if( !p_playlist ) return 0;
00302 
00303         if( pwm_data->lpData )
00304         {
00305             int i_argc, i_data, i_opt, i_options;
00306             char **ppsz_argv;
00307             char *p_data = (char *)pwm_data->lpData;
00308 
00309             i_argc = *((int *)&p_data[0]);
00310             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
00311             i_data = sizeof(int);
00312             for( i_opt = 0; i_opt < i_argc; i_opt++ )
00313             {
00314                 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
00315                 i_data += *((int *)&p_data[i_data]);
00316                 i_data += sizeof(int);
00317             }
00318 
00319             for( i_opt = 0; i_opt < i_argc; i_opt++ )
00320             {
00321                 i_options = 0;
00322 
00323                 /* Count the input options */
00324                 while( i_opt + i_options + 1 < i_argc &&
00325                        *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
00326                 {
00327                     i_options++;
00328                 }
00329                 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
00330                 {
00331                   playlist_AddExt( p_playlist, ppsz_argv[i_opt],
00332                     ppsz_argv[i_opt], PLAYLIST_APPEND ,
00333                     PLAYLIST_END, -1,
00334                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
00335                     i_options );
00336                 } else {
00337                   playlist_AddExt( p_playlist, ppsz_argv[i_opt],
00338                     ppsz_argv[i_opt], PLAYLIST_APPEND | PLAYLIST_GO,
00339                     PLAYLIST_END, -1,
00340                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
00341                     i_options );
00342                 }
00343 
00344                 i_opt += i_options;
00345             }
00346 
00347             free( ppsz_argv );
00348         }
00349 
00350         vlc_object_release( p_playlist );
00351     }
00352 
00353     return DefWindowProc( hwnd, uMsg, wParam, lParam );
00354 }
00355 
00356 /*****************************************************************************
00357  * system_End: terminate winsock.
00358  *****************************************************************************/
00359 void system_End( vlc_t *p_this )
00360 {
00361     if( p_this && p_this->p_libvlc && p_this->p_libvlc->psz_vlcpath )
00362     {
00363         free( p_this->p_libvlc->psz_vlcpath );
00364         p_this->p_libvlc->psz_vlcpath = NULL;
00365     }
00366 
00367     WSACleanup();
00368 }

Generated on Tue Dec 20 10:15:01 2005 for vlc-0.8.4a by  doxygen 1.4.2