Qt: do not show open options in both normal and advanced UI
[vlc.git] / modules / access / mtp.c
blobeb7e80a09bd4071e66211b2aaf58d6756a11e546
1 /*****************************************************************************
2 * mtp.c: mtp input (mtp: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006-2008 Rémi Denis-Courmont
7 * Authors: Fabio Ritrovato <exsephiroth87@gmail.com>
8 * Original file.c: Christophe Massiot <massiot@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan # org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 #endif
45 #ifdef HAVE_FCNTL_H
46 # include <fcntl.h>
47 #endif
49 #include <unistd.h>
50 #include <poll.h>
52 #include <vlc_fs.h>
54 #include "libmtp.h"
56 /*****************************************************************************
57 * Module descriptor
58 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 vlc_module_begin()
64 set_description( N_("MTP input") )
65 set_shortname( N_("MTP") )
66 set_category( CAT_INPUT )
67 set_subcategory( SUBCAT_INPUT_ACCESS )
68 set_capability( "access", 0 )
69 add_shortcut( "mtp" )
70 set_callbacks( Open, Close )
71 vlc_module_end()
73 /*****************************************************************************
74 * Exported prototypes
75 *****************************************************************************/
77 static int Seek( access_t *, uint64_t );
78 static ssize_t Read( access_t *, uint8_t *, size_t );
79 static int Control( access_t *, int, va_list );
81 static int open_file( access_t *, const char * );
83 struct access_sys_t
85 unsigned int i_nb_reads;
86 int fd;
89 /*****************************************************************************
90 * Open: open the file
91 *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
94 access_t *p_access = ( access_t* )p_this;
95 access_sys_t *p_sys;
96 uint32_t i_bus;
97 uint8_t i_dev;
98 uint16_t i_product_id;
99 int i_track_id;
100 LIBMTP_raw_device_t *p_rawdevices;
101 LIBMTP_mtpdevice_t *p_device;
102 int i_numrawdevices;
103 int i_ret;
105 if( sscanf( p_access->psz_location, "%"SCNu32":%"SCNu8":%"SCNu16":%d",
106 &i_bus, &i_dev, &i_product_id, &i_track_id ) != 4 )
107 return VLC_EGENERIC;
108 i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
109 if( i_ret != 0 || i_numrawdevices <= 0 || !p_rawdevices )
110 return VLC_EGENERIC;
112 for( int i = 0; i < i_numrawdevices; i++ )
114 if( i_bus == p_rawdevices[i].bus_location &&
115 i_dev == p_rawdevices[i].devnum &&
116 i_product_id == p_rawdevices[i].device_entry.product_id )
118 if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
119 ) != NULL )
121 free( p_access->psz_filepath );
122 #warning Oooh no! Not tempnam()!
123 p_access->psz_filepath = tempnam( NULL, "vlc" );
124 if( p_access->psz_filepath == NULL )
126 LIBMTP_Release_Device( p_device );
127 free( p_rawdevices );
128 return VLC_ENOMEM;
130 else
132 msg_Dbg( p_access, "About to write %s",
133 p_access->psz_filepath );
134 LIBMTP_Get_File_To_File( p_device, i_track_id,
135 p_access->psz_filepath, NULL,
136 NULL );
137 LIBMTP_Release_Device( p_device );
138 i = i_numrawdevices;
141 else
143 free( p_rawdevices );
144 return VLC_EGENERIC;
148 free( p_rawdevices );
150 STANDARD_READ_ACCESS_INIT;
151 p_sys->i_nb_reads = 0;
152 int fd = p_sys->fd = -1;
154 /* Open file */
155 msg_Dbg( p_access, "opening file `%s'", p_access->psz_filepath );
156 fd = open_file( p_access, p_access->psz_filepath );
158 if( fd == -1 )
160 free( p_sys );
161 return VLC_EGENERIC;
163 p_sys->fd = fd;
165 #ifdef HAVE_SYS_STAT_H
166 struct stat st;
167 if( fstat( fd, &st ) )
168 msg_Err( p_access, "fstat(%d): %m", fd );
169 p_access->info.i_size = st.st_size;
170 #else
171 # warning File size not known!
172 #endif
174 return VLC_SUCCESS;
177 /*****************************************************************************
178 * Close: close the target
179 *****************************************************************************/
180 static void Close( vlc_object_t * p_this )
182 access_t *p_access = ( access_t* )p_this;
183 access_sys_t *p_sys = p_access->p_sys;
185 close ( p_sys->fd );
186 if( vlc_unlink( p_access->psz_filepath ) != 0 )
187 msg_Err( p_access, "Error deleting file %s, %m",
188 p_access->psz_filepath );
189 free( p_sys );
192 /*****************************************************************************
193 * Read: standard read on a file descriptor.
194 *****************************************************************************/
195 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
197 access_sys_t *p_sys = p_access->p_sys;
198 ssize_t i_ret;
199 int fd = p_sys->fd;
201 i_ret = read( fd, p_buffer, i_len );
203 if( i_ret < 0 )
205 switch( errno )
207 case EINTR:
208 case EAGAIN:
209 break;
211 default:
212 msg_Err( p_access, "read failed (%m)" );
213 dialog_Fatal( p_access, _( "File reading failed" ), "%s (%m)",
214 _( "VLC could not read the file." ) );
215 p_access->info.b_eof = true;
216 return 0;
219 else if( i_ret > 0 )
220 p_access->info.i_pos += i_ret;
221 else
222 p_access->info.b_eof = true;
224 p_sys->i_nb_reads++;
226 return i_ret;
230 /*****************************************************************************
231 * Seek: seek to a specific location in a file
232 *****************************************************************************/
233 static int Seek( access_t *p_access, uint64_t i_pos )
235 p_access->info.i_pos = i_pos;
236 p_access->info.b_eof = false;
238 lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
239 return VLC_SUCCESS;
242 /*****************************************************************************
243 * Control:
244 *****************************************************************************/
245 static int Control( access_t *p_access, int i_query, va_list args )
247 bool *pb_bool;
248 int64_t *pi_64;
250 switch( i_query )
252 /* */
253 case ACCESS_CAN_SEEK:
254 case ACCESS_CAN_FASTSEEK:
255 pb_bool = ( bool* )va_arg( args, bool* );
256 *pb_bool = true;
257 break;
259 case ACCESS_CAN_PAUSE:
260 case ACCESS_CAN_CONTROL_PACE:
261 pb_bool = ( bool* )va_arg( args, bool* );
262 *pb_bool = true;
263 break;
265 case ACCESS_GET_PTS_DELAY:
266 pi_64 = ( int64_t* )va_arg( args, int64_t * );
267 *pi_64 = INT64_C(1000)
268 * var_InheritInteger( p_access, "file-caching" );
269 break;
271 /* */
272 case ACCESS_SET_PAUSE_STATE:
273 /* Nothing to do */
274 break;
276 case ACCESS_GET_TITLE_INFO:
277 case ACCESS_SET_TITLE:
278 case ACCESS_SET_SEEKPOINT:
279 case ACCESS_SET_PRIVATE_ID_STATE:
280 case ACCESS_GET_META:
281 case ACCESS_GET_PRIVATE_ID_STATE:
282 case ACCESS_GET_CONTENT_TYPE:
283 return VLC_EGENERIC;
285 default:
286 msg_Warn( p_access, "unimplemented query %d in control", i_query );
287 return VLC_EGENERIC;
290 return VLC_SUCCESS;
293 /*****************************************************************************
294 * open_file: Opens a specific file
295 *****************************************************************************/
296 static int open_file( access_t *p_access, const char *path )
298 int fd = vlc_open( path, O_RDONLY | O_NONBLOCK );
299 if( fd == -1 )
301 msg_Err( p_access, "cannot open file %s (%m)", path );
302 dialog_Fatal( p_access, _( "File reading failed" ),
303 _( "VLC could not open the file \"%s\". (%m)" ), path );
304 return -1;
307 #if defined( HAVE_FCNTL )
308 fcntl( fd, F_SETFD, fcntl( fd, F_GETFD ) | FD_CLOEXEC );
310 /* We'd rather use any available memory for reading ahead
311 * than for caching what we've already seen/heard */
312 # if defined( F_RDAHEAD )
313 fcntl( fd, F_RDAHEAD, 1 );
314 # endif
315 # if defined( F_NOCACHE )
316 fcntl( fd, F_NOCACHE, 1 );
317 # endif
318 #endif
320 return fd;