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