]> git.sesse.net Git - vlc/blob - modules/control/win_msg.c
vlc_plugin: fix non-LGPL plugins meta infos
[vlc] / modules / control / win_msg.c
1 /*****************************************************************************
2  * ntservice.c: Windows NT/2K/XP service interface
3  *****************************************************************************
4  * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *          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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_interface.h>
32 #include <vlc_playlist.h>
33 #include <vlc_input.h>
34 #include <vlc_url.h> // FIXME: move URL generation to calling process
35
36 #include <windows.h>
37
38 struct intf_sys_t
39 {
40     HWND window;
41     HANDLE ready;
42     vlc_thread_t thread;
43 };
44
45 /* Must be same as in src/win32/specific.c */
46 typedef struct
47 {
48     int argc;
49     int enqueue;
50     char data[];
51 } vlc_ipc_data_t;
52
53 static LRESULT CALLBACK WMCOPYWNDPROC(HWND hwnd, UINT uMsg,
54                                       WPARAM wParam, LPARAM lParam)
55 {
56     if( uMsg == WM_QUIT  )
57     {
58         PostQuitMessage( 0 );
59     }
60     else if( uMsg == WM_COPYDATA )
61     {
62         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
63
64         intf_thread_t *intf = (intf_thread_t *)(uintptr_t)
65             GetWindowLongPtr( hwnd, GWLP_USERDATA );
66         if( intf == NULL )
67             return 0; /* XXX: is this even possible? */
68
69         /* Add files to the playlist */
70         if( pwm_data->lpData )
71         {
72             char **ppsz_argv;
73             vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
74             size_t i_data = 0;
75             int i_argc = p_data->argc, i_opt, i_options;
76
77             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
78             for( i_opt = 0; i_opt < i_argc; i_opt++ )
79             {
80                 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(size_t);
81                 i_data += sizeof(size_t) + *((size_t *)(p_data->data + i_data));
82             }
83
84             for( i_opt = 0; i_opt < i_argc; i_opt++ )
85             {
86                 i_options = 0;
87
88                 /* Count the input options */
89                 while( i_opt + i_options + 1 < i_argc &&
90                         *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
91                 {
92                     i_options++;
93                 }
94
95 #warning URI conversion must be done in calling process instead!
96                 /* FIXME: This breaks relative paths if calling vlc.exe is
97                  * started from a different working directory. */
98                 char *psz_URI = NULL;
99                 if( strstr( ppsz_argv[i_opt], "://" ) == NULL )
100                     psz_URI = vlc_path2uri( ppsz_argv[i_opt], NULL );
101                 playlist_AddExt( pl_Get(intf),
102                         (psz_URI != NULL) ? psz_URI : ppsz_argv[i_opt],
103                         NULL, PLAYLIST_APPEND |
104                         ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
105                         PLAYLIST_END, -1,
106                         i_options,
107                         (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
108                         VLC_INPUT_OPTION_TRUSTED,
109                         true, pl_Unlocked );
110
111                 i_opt += i_options;
112                 free( psz_URI );
113             }
114
115             free( ppsz_argv );
116         }
117     }
118
119     return DefWindowProc( hwnd, uMsg, wParam, lParam );
120 }
121
122 static void *HelperThread(void *data)
123 {
124     intf_thread_t *intf = data;
125     intf_sys_t *sys = intf->p_sys;
126
127     HWND ipcwindow =
128         CreateWindow(L"STATIC",                      /* name of window class */
129                   L"VLC ipc " TEXT(VERSION),        /* window title bar text */
130                   0,                                         /* window style */
131                   0,                                 /* default X coordinate */
132                   0,                                 /* default Y coordinate */
133                   0,                                         /* window width */
134                   0,                                        /* window height */
135                   NULL,                                  /* no parent window */
136                   NULL,                            /* no menu in this window */
137                   GetModuleHandle(NULL),  /* handle of this program instance */
138                   NULL) ;                               /* sent to WM_CREATE */
139
140     SetWindowLongPtr(ipcwindow, GWLP_WNDPROC, (LRESULT)WMCOPYWNDPROC);
141     SetWindowLongPtr(ipcwindow, GWLP_USERDATA, (uintptr_t)data);
142
143     sys->window = ipcwindow;
144     /* Signal the creation of the thread and events queue */
145     SetEvent(sys->ready);
146
147     MSG message;
148     while (GetMessage(&message, NULL, 0, 0))
149     {
150         TranslateMessage(&message);
151         DispatchMessage(&message);
152     }
153
154     return NULL;
155 }
156
157 static int Open(vlc_object_t *obj)
158 {
159     intf_thread_t *intf = (intf_thread_t *)obj;
160     intf_sys_t *sys = malloc(sizeof (*sys));
161     if (unlikely(sys == NULL))
162         return VLC_ENOMEM;
163
164     intf->p_sys = sys;
165
166     /* Run the helper thread */
167     sys->ready = CreateEvent(NULL, FALSE, FALSE, NULL);
168
169     if (vlc_clone(&sys->thread, HelperThread, intf, VLC_THREAD_PRIORITY_LOW))
170     {
171         free(sys);
172         msg_Err(intf, "one instance mode DISABLED "
173                  "(IPC helper thread couldn't be created)");
174         return VLC_ENOMEM;
175     }
176
177     WaitForSingleObject(sys->ready, INFINITE);
178     CloseHandle(sys->ready);
179
180     return VLC_SUCCESS;
181 }
182
183 static void Close(vlc_object_t *obj)
184 {
185     intf_thread_t *intf = (intf_thread_t *)obj;
186     intf_sys_t *sys = intf->p_sys;
187
188     SendMessage(sys->window, WM_QUIT, 0, 0);
189     vlc_join(sys->thread, NULL);
190     free(sys);
191 }
192
193 vlc_module_begin()
194     set_shortname(N_("WinMsg"))
195     set_description(N_("Windows messages interface"))
196     set_category(CAT_INTERFACE)
197     set_subcategory(SUBCAT_INTERFACE_CONTROL)
198     set_capability("interface", 0)
199     set_callbacks(Open, Close)
200 vlc_module_end()