mosaic: use vlc_picture_chain_HasNext to check if we can drop a picture
[vlc.git] / 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_url.h> // FIXME: move URL generation to calling process
34 #include <vlc_input_item.h>
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 void add_to_playlist(intf_thread_t *intf, const char *uri,
54                             bool play_now, int options_count,
55                             const char *const *options)
56 {
57     vlc_playlist_t *playlist = vlc_intf_GetMainPlaylist(intf);
58
59     input_item_t *media = input_item_New(uri, NULL);
60     if (!media)
61         return;
62     input_item_AddOptions(media, options_count, options, VLC_INPUT_OPTION_TRUSTED);
63
64     vlc_playlist_Lock(playlist);
65     vlc_playlist_AppendOne(playlist, media);
66     if (play_now)
67         vlc_playlist_Start(playlist);
68     vlc_playlist_Unlock(playlist);
69     input_item_Release(media);
70 }
71
72 static LRESULT CALLBACK WMCOPYWNDPROC(HWND hwnd, UINT uMsg,
73                                       WPARAM wParam, LPARAM lParam)
74 {
75     if( uMsg == WM_QUIT  )
76     {
77         PostQuitMessage( 0 );
78     }
79     else if( uMsg == WM_COPYDATA )
80     {
81         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
82
83         intf_thread_t *intf = (intf_thread_t *)(uintptr_t)
84             GetWindowLongPtr( hwnd, GWLP_USERDATA );
85         if( intf == NULL )
86             return 0; /* XXX: is this even possible? */
87
88         /* Add files to the playlist */
89         if( pwm_data->lpData )
90         {
91             char **ppsz_argv;
92             vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
93             size_t i_data = 0;
94             int i_argc = p_data->argc, i_opt, i_options;
95
96             ppsz_argv = vlc_alloc( i_argc, sizeof(char *) );
97             for( i_opt = 0; i_opt < i_argc; i_opt++ )
98             {
99                 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(size_t);
100                 i_data += sizeof(size_t) + *((size_t *)(p_data->data + i_data));
101             }
102
103             for( i_opt = 0; i_opt < i_argc; i_opt++ )
104             {
105                 i_options = 0;
106
107                 /* Count the input options */
108                 while( i_opt + i_options + 1 < i_argc &&
109                         *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
110                 {
111                     i_options++;
112                 }
113
114 #warning URI conversion must be done in calling process instead!
115                 /* FIXME: This breaks relative paths if calling vlc.exe is
116                  * started from a different working directory. */
117                 char *psz_URI = NULL;
118                 if( strstr( ppsz_argv[i_opt], "://" ) == NULL )
119                     psz_URI = vlc_path2uri( ppsz_argv[i_opt], NULL );
120                 add_to_playlist(intf, (psz_URI != NULL) ? psz_URI : ppsz_argv[i_opt],
121                                 (i_opt == 0 && !p_data->enqueue), i_options,
122                                 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ));
123                 i_opt += i_options;
124                 free( psz_URI );
125             }
126
127             free( ppsz_argv );
128         }
129     }
130
131     return DefWindowProc( hwnd, uMsg, wParam, lParam );
132 }
133
134 static void *HelperThread(void *data)
135 {
136     intf_thread_t *intf = data;
137     intf_sys_t *sys = intf->p_sys;
138
139     HWND ipcwindow =
140         CreateWindow(L"STATIC",                      /* name of window class */
141                   L"VLC ipc " TEXT(VERSION),        /* window title bar text */
142                   0,                                         /* window style */
143                   0,                                 /* default X coordinate */
144                   0,                                 /* default Y coordinate */
145                   0,                                         /* window width */
146                   0,                                        /* window height */
147                   NULL,                                  /* no parent window */
148                   NULL,                            /* no menu in this window */
149                   GetModuleHandle(NULL),  /* handle of this program instance */
150                   NULL) ;                               /* sent to WM_CREATE */
151
152     SetWindowLongPtr(ipcwindow, GWLP_WNDPROC, (LRESULT)WMCOPYWNDPROC);
153     SetWindowLongPtr(ipcwindow, GWLP_USERDATA, (uintptr_t)data);
154
155     sys->window = ipcwindow;
156     /* Signal the creation of the thread and events queue */
157     SetEvent(sys->ready);
158
159     MSG message;
160     while (GetMessage(&message, NULL, 0, 0))
161     {
162         TranslateMessage(&message);
163         DispatchMessage(&message);
164     }
165
166     return NULL;
167 }
168
169 static int Open(vlc_object_t *obj)
170 {
171     intf_thread_t *intf = (intf_thread_t *)obj;
172     intf_sys_t *sys = malloc(sizeof (*sys));
173     if (unlikely(sys == NULL))
174         return VLC_ENOMEM;
175
176     intf->p_sys = sys;
177
178     /* Run the helper thread */
179     sys->ready = CreateEvent(NULL, FALSE, FALSE, NULL);
180
181     if (vlc_clone(&sys->thread, HelperThread, intf, VLC_THREAD_PRIORITY_LOW))
182     {
183         free(sys);
184         msg_Err(intf, "one instance mode DISABLED "
185                  "(IPC helper thread couldn't be created)");
186         return VLC_ENOMEM;
187     }
188
189     WaitForSingleObject(sys->ready, INFINITE);
190     CloseHandle(sys->ready);
191
192     return VLC_SUCCESS;
193 }
194
195 static void Close(vlc_object_t *obj)
196 {
197     intf_thread_t *intf = (intf_thread_t *)obj;
198     intf_sys_t *sys = intf->p_sys;
199
200     SendMessage(sys->window, WM_QUIT, 0, 0);
201     vlc_join(sys->thread, NULL);
202     free(sys);
203 }
204
205 vlc_module_begin()
206     set_shortname(N_("WinMsg"))
207     set_description(N_("Windows messages interface"))
208     set_category(CAT_INTERFACE)
209     set_subcategory(SUBCAT_INTERFACE_CONTROL)
210     set_capability("interface", 0)
211     set_callbacks(Open, Close)
212 vlc_module_end()