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