]> git.sesse.net Git - vlc/blob - src/interface/intf_eject.c
Remove useless <sys/stat.h> includes
[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 /*****************************************************************************
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     VLC_UNUSED(p_this);
103     int i_ret = VLC_SUCCESS;
104
105 #ifdef __APPLE__
106     FILE *p_eject;
107     char *psz_disk;
108     char sz_cmd[32];
109
110     /*
111      * The only way to cleanly unmount the disc under MacOS X
112      * is to use the 'disktool' command line utility. It uses
113      * the non-public Disk Arbitration API, which can not be
114      * used by Cocoa or Carbon applications.
115      */
116
117     if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
118         strlen( psz_disk ) > 4 )
119     {
120 #define EJECT_CMD "/usr/sbin/disktool -e %s 0"
121         snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
122 #undef EJECT_CMD
123
124         if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
125         {
126             char psz_result[0x200];
127             i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
128
129             if( i_ret == 0 && ferror( p_eject ) != 0 )
130             {
131                 pclose( p_eject );
132                 return VLC_EGENERIC;
133             }
134
135             pclose( p_eject );
136
137             psz_result[ i_ret ] = 0;
138
139             if( strstr( psz_result, "Disk Ejected" ) != NULL )
140             {
141                 return VLC_SUCCESS;
142             }
143         }
144     }
145
146     return VLC_EGENERIC;
147
148 #elif defined(UNDER_CE)
149     msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
150     return i_ret;
151
152 #elif defined(WIN32)
153     MCI_OPEN_PARMS op;
154     MCI_STATUS_PARMS st;
155     DWORD i_flags;
156     char psz_drive[4];
157
158     memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
159     op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
160
161     strcpy( psz_drive, "X:" );
162     psz_drive[0] = psz_device[0];
163     op.lpstrElementName = psz_drive;
164
165     /* Set the flags for the device type */
166     i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
167               MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
168
169     if( !mciSendCommand( 0, MCI_OPEN, i_flags, (uintptr_t)&op ) )
170     {
171         st.dwItem = MCI_STATUS_READY;
172         /* Eject disc */
173         i_ret = mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 );
174         /* Release access to the device */
175         mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
176     }
177     else i_ret = VLC_EGENERIC;
178
179     return i_ret;
180 #else   /* WIN32 */
181
182     int i_fd;
183
184     /* This code could be extended to support CD/DVD-ROM chargers */
185
186     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
187
188     if( i_fd == -1 )
189     {
190         msg_Err( p_this, "could not open device %s", psz_device );
191         return VLC_EGENERIC;
192     }
193
194 #if defined(__linux__) && defined(HAVE_LINUX_VERSION_H)
195     /* Try a simple ATAPI eject */
196     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
197
198     if( i_ret != 0 )
199     {
200         i_ret = EjectSCSI( i_fd );
201     }
202
203     if( i_ret != 0 )
204     {
205         msg_Err( p_this, "could not eject %s", psz_device );
206     }
207
208 #elif defined (HAVE_DVD_H)
209     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
210
211 #else
212     msg_Warn( p_this, "CD-ROM ejection unsupported on this platform" );
213     i_ret = -1;
214
215 #endif
216     close( i_fd );
217
218     return i_ret;
219 #endif
220 }
221
222 /* The following functions are local */
223
224 #if defined(__linux__) && defined(HAVE_LINUX_VERSION_H)
225 /*****************************************************************************
226  * Eject using SCSI commands. Return 0 if successful
227  *****************************************************************************/
228 /**
229  * \brief Ejects the CD /DVD using SCSI commands
230  * \ingroup vlc_interface
231  * This function is local
232  * \param i_fd a device nummber
233  * \return 0 on success, VLC_EGENERIC on failure
234  */
235 static int EjectSCSI( int i_fd )
236 {
237     int i_status;
238
239     struct sdata
240     {
241         int  inlen;
242         int  outlen;
243         char cmd[256];
244     } scsi_cmd;
245
246     scsi_cmd.inlen  = 0;
247     scsi_cmd.outlen = 0;
248     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
249     scsi_cmd.cmd[1] = 0;
250     scsi_cmd.cmd[2] = 0;
251     scsi_cmd.cmd[3] = 0;
252     scsi_cmd.cmd[4] = 0;
253     scsi_cmd.cmd[5] = 0;
254     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
255     if( i_status != 0 )
256     {
257         return VLC_EGENERIC;
258     }
259
260     scsi_cmd.inlen  = 0;
261     scsi_cmd.outlen = 0;
262     scsi_cmd.cmd[0] = START_STOP;
263     scsi_cmd.cmd[1] = 0;
264     scsi_cmd.cmd[2] = 0;
265     scsi_cmd.cmd[3] = 0;
266     scsi_cmd.cmd[4] = 1;
267     scsi_cmd.cmd[5] = 0;
268     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
269     if( i_status != 0 )
270     {
271         return VLC_EGENERIC;
272     }
273
274     scsi_cmd.inlen  = 0;
275     scsi_cmd.outlen = 0;
276     scsi_cmd.cmd[0] = START_STOP;
277     scsi_cmd.cmd[1] = 0;
278     scsi_cmd.cmd[2] = 0;
279     scsi_cmd.cmd[3] = 0;
280     scsi_cmd.cmd[4] = 2;
281     scsi_cmd.cmd[5] = 0;
282     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
283     if( i_status != 0 )
284     {
285         return VLC_EGENERIC;
286     }
287
288     /* Force kernel to reread partition table when new disc inserted */
289     i_status = ioctl( i_fd, BLKRRPART );
290
291     return i_status;
292 }
293 #endif
294