]> git.sesse.net Git - vlc/blob - src/interface/intf_eject.c
Here too
[vlc] / src / interface / intf_eject.c
1 /*****************************************************************************
2  * intf_eject.c: CD/DVD-ROM ejection handling functions
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Julien Blache <jb@technologeek.org> for the Linux part
8  *                with code taken from the Linux "eject" command
9  *          Jon Lech Johansen <jon-vl@nanocrew.net> for Darwin
10  *          Gildas Bazin <gbazin@netcourrier.com> for Win32
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /**
28  *  \file
29  *  This file contain functions to eject CD and DVD drives
30  */
31
32 #include <vlc/vlc.h>
33
34 #ifdef HAVE_UNISTD_H
35 #    include <unistd.h>
36 #endif
37
38 #ifdef HAVE_FCNTL_H
39 #   include <fcntl.h>
40 #endif
41
42 #ifdef HAVE_DVD_H
43 #   include <dvd.h>
44 #endif
45
46 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
47 #   include <linux/version.h>
48     /* handy macro found in 2.1 kernels, but not in older ones */
49 #   ifndef KERNEL_VERSION
50 #       define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
51 #   endif
52
53 #   include <sys/types.h>
54 #   include <sys/stat.h>
55 #   include <sys/ioctl.h>
56
57 #   include <sys/ioctl.h>
58 #   include <sys/mount.h>
59
60 #   include <linux/cdrom.h>
61 #   if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
62 #       include <linux/ucdrom.h>
63 #   endif
64
65 #   include <scsi/scsi.h>
66 #   include <scsi/sg.h>
67 #   include <scsi/scsi_ioctl.h>
68 #endif
69
70 #if defined( WIN32 ) && !defined( UNDER_CE )
71 #   include <mmsystem.h>
72 #endif
73
74 #include <vlc_interface.h>
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
80 static int EjectSCSI ( int i_fd );
81 #endif
82
83 /*****************************************************************************
84  * intf_Eject: eject the CDRom
85  *****************************************************************************
86  * returns 0 on success
87  * returns 1 on failure
88  * returns -1 if not implemented
89  *****************************************************************************/
90 /**
91  * \brief Ejects the CD /DVD
92  * \ingroup vlc_interface
93  * \param p_this the calling vlc_object_t
94  * \param psz_device the CD/DVD to eject
95  * \return 0 on success, 1 on failure, -1 if not implemented
96  */
97 int __intf_Eject( vlc_object_t *p_this, const char *psz_device )
98 {
99     int i_ret = VLC_SUCCESS;
100
101 #ifdef __APPLE__
102     FILE *p_eject;
103     char *psz_disk;
104     char sz_cmd[32];
105
106     /*
107      * The only way to cleanly unmount the disc under MacOS X
108      * is to use the 'disktool' command line utility. It uses
109      * the non-public Disk Arbitration API, which can not be
110      * used by Cocoa or Carbon applications.
111      */
112
113     if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
114         strlen( psz_disk ) > 4 )
115     {
116 #define EJECT_CMD "/usr/sbin/disktool -e %s 0"
117         snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
118 #undef EJECT_CMD
119
120         if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
121         {
122             char psz_result[0x200];
123             i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
124
125             if( i_ret == 0 && ferror( p_eject ) != 0 )
126             {
127                 pclose( p_eject );
128                 return VLC_EGENERIC;
129             }
130
131             pclose( p_eject );
132
133             psz_result[ i_ret ] = 0;
134
135             if( strstr( psz_result, "Disk Ejected" ) != NULL )
136             {
137                 return VLC_SUCCESS;
138             }
139         }
140     }
141
142     return VLC_EGENERIC;
143
144 #elif defined(UNDER_CE)
145     msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
146     return i_ret;
147
148 #elif defined(WIN32)
149     MCI_OPEN_PARMS op;
150     MCI_STATUS_PARMS st;
151     DWORD i_flags;
152     char psz_drive[4];
153
154     memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
155     op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
156
157     strcpy( psz_drive, "X:" );
158     psz_drive[0] = psz_device[0];
159     op.lpstrElementName = psz_drive;
160
161     /* Set the flags for the device type */
162     i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
163               MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
164
165     if( !mciSendCommand( 0, MCI_OPEN, i_flags, (unsigned long)&op ) )
166     {
167         st.dwItem = MCI_STATUS_READY;
168         /* Eject disc */
169         i_ret = mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 );
170         /* Release access to the device */
171         mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
172     }
173     else i_ret = VLC_EGENERIC;
174
175     return i_ret;
176 #else   /* WIN32 */
177
178     int i_fd;
179
180     /* This code could be extended to support CD/DVD-ROM chargers */
181
182     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
183
184     if( i_fd == -1 )
185     {
186         msg_Err( p_this, "could not open device %s", psz_device );
187         return VLC_EGENERIC;
188     }
189
190 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
191     /* Try a simple ATAPI eject */
192     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
193
194     if( i_ret != 0 )
195     {
196         i_ret = EjectSCSI( i_fd );
197     }
198
199     if( i_ret != 0 )
200     {
201         msg_Err( p_this, "could not eject %s", psz_device );
202     }
203
204 #elif defined (HAVE_DVD_H)
205     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
206
207 #else
208     msg_Warn( p_this, "CD-ROM ejection unsupported on this platform" );
209     i_ret = -1;
210
211 #endif
212     close( i_fd );
213
214     return i_ret;
215 #endif
216 }
217
218 /* The following functions are local */
219
220 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
221 /*****************************************************************************
222  * Eject using SCSI commands. Return 0 if successful
223  *****************************************************************************/
224 /**
225  * \brief Ejects the CD /DVD using SCSI commands
226  * \ingroup vlc_interface
227  * This function is local
228  * \param i_fd a device nummber
229  * \return 0 on success, VLC_EGENERIC on failure
230  */
231 static int EjectSCSI( int i_fd )
232 {
233     int i_status;
234
235     struct sdata
236     {
237         int  inlen;
238         int  outlen;
239         char cmd[256];
240     } scsi_cmd;
241
242     scsi_cmd.inlen  = 0;
243     scsi_cmd.outlen = 0;
244     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
245     scsi_cmd.cmd[1] = 0;
246     scsi_cmd.cmd[2] = 0;
247     scsi_cmd.cmd[3] = 0;
248     scsi_cmd.cmd[4] = 0;
249     scsi_cmd.cmd[5] = 0;
250     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
251     if( i_status != 0 )
252     {
253         return VLC_EGENERIC;
254     }
255
256     scsi_cmd.inlen  = 0;
257     scsi_cmd.outlen = 0;
258     scsi_cmd.cmd[0] = START_STOP;
259     scsi_cmd.cmd[1] = 0;
260     scsi_cmd.cmd[2] = 0;
261     scsi_cmd.cmd[3] = 0;
262     scsi_cmd.cmd[4] = 1;
263     scsi_cmd.cmd[5] = 0;
264     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
265     if( i_status != 0 )
266     {
267         return VLC_EGENERIC;
268     }
269
270     scsi_cmd.inlen  = 0;
271     scsi_cmd.outlen = 0;
272     scsi_cmd.cmd[0] = START_STOP;
273     scsi_cmd.cmd[1] = 0;
274     scsi_cmd.cmd[2] = 0;
275     scsi_cmd.cmd[3] = 0;
276     scsi_cmd.cmd[4] = 2;
277     scsi_cmd.cmd[5] = 0;
278     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
279     if( i_status != 0 )
280     {
281         return VLC_EGENERIC;
282     }
283
284     /* Force kernel to reread partition table when new disc inserted */
285     i_status = ioctl( i_fd, BLKRRPART );
286
287     return i_status;
288 }
289 #endif
290