]> git.sesse.net Git - vlc/blob - src/interface/intf_eject.c
Updated copyrights in libvlc
[vlc] / src / interface / intf_eject.c
1 /*****************************************************************************
2  * intf_eject.c: CD/DVD-ROM ejection handling functions
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id: intf_eject.c,v 1.23 2004/01/06 12:02:06 zorglub Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 #include <stdio.h>
35 #include <stdlib.h>
36
37 #ifdef HAVE_UNISTD_H
38 #    include <unistd.h>
39 #endif
40
41 #include <string.h>
42
43 #ifdef HAVE_FCNTL_H
44 #   include <fcntl.h>
45 #endif
46
47 #ifdef HAVE_DVD_H
48 #   include <dvd.h>
49 #endif
50
51 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
52 #   include <linux/version.h>
53     /* handy macro found in 2.1 kernels, but not in older ones */
54 #   ifndef KERNEL_VERSION
55 #       define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
56 #   endif
57
58 #   include <sys/types.h>
59 #   include <sys/stat.h>
60 #   include <sys/ioctl.h>
61
62 #   include <sys/ioctl.h>
63 #   include <sys/mount.h>
64
65 #   include <linux/cdrom.h>
66 #   if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
67 #       include <linux/ucdrom.h>
68 #   endif
69
70 #   include <scsi/scsi.h>
71 #   include <scsi/sg.h>
72 #   include <scsi/scsi_ioctl.h>
73 #endif
74
75 #if defined( WIN32 ) && !defined( UNDER_CE )
76 #   include <mmsystem.h>
77 #endif
78
79 /*****************************************************************************
80  * Local prototypes
81  *****************************************************************************/
82 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
83 static int EjectSCSI ( int i_fd );
84 #endif
85
86 /*****************************************************************************
87  * intf_Eject: eject the CDRom
88  *****************************************************************************
89  * returns 0 on success
90  * returns 1 on failure
91  * returns -1 if not implemented
92  *****************************************************************************/
93 /**
94  * \brief Ejects the CD /DVD
95  * \ingroup vlc_interface
96  * \param p_this the calling vlc_object_t
97  * \param psz_device the CD/DVD to eject
98  * \return 0 on success, 1 on failure, -1 if not implemented
99  */
100 int __intf_Eject( vlc_object_t *p_this, const char *psz_device )
101 {
102     int i_ret = VLC_SUCCESS;
103
104 #ifdef SYS_DARWIN
105     FILE *p_eject;
106     char *psz_disk;
107     char sz_cmd[32];
108
109     /*
110      * The only way to cleanly unmount the disc under MacOS X
111      * is to use the 'disktool' command line utility. It uses
112      * the non-public Disk Arbitration API, which can not be
113      * used by Cocoa or Carbon applications.
114      */
115
116     if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
117         strlen( psz_disk ) > 4 )
118     {
119 #define EJECT_CMD "/usr/sbin/disktool -e %s 0"
120         snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
121 #undef EJECT_CMD
122
123         if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
124         {
125             char psz_result[0x200];
126             i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
127
128             if( i_ret == 0 && ferror( p_eject ) != 0 )
129             {
130                 pclose( p_eject );
131                 return VLC_EGENERIC;
132             }
133
134             pclose( p_eject );
135
136             psz_result[ i_ret ] = 0;
137
138             if( strstr( psz_result, "Disk Ejected" ) != NULL )
139             {
140                 return VLC_SUCCESS;
141             }
142         }
143     }
144
145     return VLC_EGENERIC;
146
147 #elif defined(UNDER_CE)
148     msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
149     return i_ret;
150
151 #elif defined(WIN32)
152     MCI_OPEN_PARMS op;
153     MCI_STATUS_PARMS st;
154     DWORD i_flags;
155     char psz_drive[4];
156
157     memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
158     op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
159
160     strcpy( psz_drive, "X:" );
161     psz_drive[0] = psz_device[0];
162     op.lpstrElementName = psz_drive;
163
164     /* Set the flags for the device type */
165     i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
166               MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
167
168     if( !mciSendCommand( 0, MCI_OPEN, i_flags, (unsigned long)&op ) )
169     {
170         st.dwItem = MCI_STATUS_READY;
171         /* Eject disc */
172         i_ret = mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 );
173         /* Release access to the device */
174         mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
175     }
176     else i_ret = VLC_EGENERIC;
177
178     return i_ret;
179 #else   /* WIN32 */
180
181     int i_fd;
182
183     /* This code could be extended to support CD/DVD-ROM chargers */
184
185     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
186
187     if( i_fd == -1 )
188     {
189         msg_Err( p_this, "could not open device %s", psz_device );
190         return VLC_EGENERIC;
191     }
192
193 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
194     /* Try a simple ATAPI eject */
195     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
196
197     if( i_ret != 0 )
198     {
199         i_ret = EjectSCSI( i_fd );
200     }
201
202     if( i_ret != 0 )
203     {
204         msg_Err( p_this, "could not eject %s", psz_device );
205     }
206
207 #elif defined (HAVE_DVD_H)
208     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
209
210 #else
211     msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
212     i_ret = -1;
213
214 #endif
215     close( i_fd );
216
217     return i_ret;
218 #endif
219 }
220
221 /* The following functions are local */
222
223 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
224 /*****************************************************************************
225  * Eject using SCSI commands. Return 0 if successful
226  *****************************************************************************/
227 /**
228  * \brief Ejects the CD /DVD using SCSI commands
229  * \ingroup vlc_interface
230  * This function is local
231  * \param i_fd a device nummber
232  * \return 0 on success, VLC_EGENERIC on failure
233  */
234 static int EjectSCSI( int i_fd )
235 {
236     int i_status;
237
238     struct sdata
239     {
240         int  inlen;
241         int  outlen;
242         char cmd[256];
243     } scsi_cmd;
244
245     scsi_cmd.inlen  = 0;
246     scsi_cmd.outlen = 0;
247     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
248     scsi_cmd.cmd[1] = 0;
249     scsi_cmd.cmd[2] = 0;
250     scsi_cmd.cmd[3] = 0;
251     scsi_cmd.cmd[4] = 0;
252     scsi_cmd.cmd[5] = 0;
253     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
254     if( i_status != 0 )
255     {
256         return VLC_EGENERIC;
257     }
258
259     scsi_cmd.inlen  = 0;
260     scsi_cmd.outlen = 0;
261     scsi_cmd.cmd[0] = START_STOP;
262     scsi_cmd.cmd[1] = 0;
263     scsi_cmd.cmd[2] = 0;
264     scsi_cmd.cmd[3] = 0;
265     scsi_cmd.cmd[4] = 1;
266     scsi_cmd.cmd[5] = 0;
267     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
268     if( i_status != 0 )
269     {
270         return VLC_EGENERIC;
271     }
272
273     scsi_cmd.inlen  = 0;
274     scsi_cmd.outlen = 0;
275     scsi_cmd.cmd[0] = START_STOP;
276     scsi_cmd.cmd[1] = 0;
277     scsi_cmd.cmd[2] = 0;
278     scsi_cmd.cmd[3] = 0;
279     scsi_cmd.cmd[4] = 2;
280     scsi_cmd.cmd[5] = 0;
281     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
282     if( i_status != 0 )
283     {
284         return VLC_EGENERIC;
285     }
286
287     /* Force kernel to reread partition table when new disc inserted */
288     i_status = ioctl( i_fd, BLKRRPART );
289
290     return i_status;
291 }
292 #endif
293