]> git.sesse.net Git - vlc/blob - src/interface/intf_eject.c
* ./src/interface/intf_eject.c: Fixed a MacOS X bug.
[vlc] / src / interface / intf_eject.c
1 /*****************************************************************************
2  * intf_eject.c: CD/DVD-ROM ejection handling functions
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: intf_eject.c,v 1.7 2002/04/04 00:23:36 jlj Exp $
6  *
7  * Author: Julien Blache <jb@technologeek.org> for the Linux part
8  *               with code taken from the Linux "eject" command
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <videolan/vlc.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #ifdef HAVE_UNISTD_H
31 #    include <unistd.h>
32 #endif
33
34 #include <string.h>
35
36 #ifdef HAVE_FCNTL_H
37 #   include <fcntl.h>
38 #endif
39
40 #ifdef HAVE_DVD_H
41 #   include <dvd.h>
42 #endif
43
44 #ifdef SYS_LINUX
45 #   include <linux/version.h>
46     /* handy macro found in 2.1 kernels, but not in older ones */
47 #   ifndef KERNEL_VERSION
48 #       define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
49 #   endif
50
51 #   include <sys/types.h>
52 #   include <sys/stat.h>
53 #   include <sys/ioctl.h>
54
55 #   include <sys/ioctl.h>
56 #   include <linux/cdrom.h>
57 #   if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
58 #       include <linux/ucdrom.h>
59 #   endif
60
61 #   include <sys/mount.h>
62 #   include <scsi/scsi.h>
63 #   include <scsi/sg.h>
64 #   ifdef HAVE_SCSI_SCSI_IOCTL_H
65 #      include <scsi/scsi_ioctl.h>
66 #   endif
67 #endif
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 #if defined(SYS_LINUX) && defined(HAVE_SCSI_SCSI_IOCTL_H)
73 static int EjectSCSI ( int i_fd );
74 #endif
75
76 /*****************************************************************************
77  * intf_Eject: eject the CDRom
78  *****************************************************************************
79  * returns 0 on success
80  * returns 1 on failure
81  * returns -1 if not implemented
82  *****************************************************************************/
83 int intf_Eject( const char *psz_device )
84 {
85     int i_fd;
86     int i_ret;
87
88 #ifdef SYS_DARWIN
89     FILE *p_eject;
90     char *psz_disk;
91     char sz_cmd[32];
92
93     /*
94      * The only way to cleanly unmount the disc under MacOS X
95      * is to use the 'disktool' command line utility. It uses
96      * the non-public Disk Arbitration API, which can not be
97      * used by Cocoa or Carbon applications. 
98      */
99
100     if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
101         strlen( psz_disk ) > 4 )
102     {
103 #define EJECT_CMD "disktool -e %s 0"
104         snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
105 #undef EJECT_CMD
106
107         if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
108         {
109             char psz_result[0x200];
110             i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
111
112             if( i_ret == 0 && ferror( p_eject ) != 0 )
113             {
114                 pclose( p_eject );
115                 return 1;
116             }
117
118             pclose( p_eject );
119
120             psz_result[ i_ret ] = 0;
121
122             if( strstr( psz_result, "Disk Ejected" ) != NULL )
123             {
124                 return 0;
125             }
126         }
127     }
128
129     return 1;
130
131 #endif
132
133     /* This code could be extended to support CD/DVD-ROM chargers */
134
135     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
136    
137     if( i_fd == -1 )
138     {
139         intf_ErrMsg( "intf error: couldn't open device %s", psz_device );
140         return 1;
141     }
142
143 #ifdef SYS_LINUX
144     /* Try a simple ATAPI eject */
145     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
146
147 #ifdef HAVE_SCSI_SCSI_IOCTL_H
148     if( i_ret != 0 )
149     {
150         i_ret = EjectSCSI( i_fd );
151     }
152 #endif
153
154     if( i_ret != 0 )
155     {
156         intf_ErrMsg( "intf error: couldn't eject %s", psz_device );
157     }
158
159 #elif defined (HAVE_DVD_H)
160     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
161
162 #else
163     intf_ErrMsg( "intf error: CD-Rom ejection unsupported on this platform" );
164     i_ret = -1;
165
166 #endif
167     close( i_fd );
168
169     return i_ret;
170 }
171
172 /* The following functions are local */
173
174 #if defined(SYS_LINUX) && defined(HAVE_SCSI_SCSI_IOCTL_H)
175 /*****************************************************************************
176  * Eject using SCSI commands. Return 0 if successful
177  *****************************************************************************/
178 static int EjectSCSI( int i_fd )
179 {
180     int i_status;
181
182     struct sdata
183     {
184         int  inlen;
185         int  outlen;
186         char cmd[256];
187     } scsi_cmd;
188
189     scsi_cmd.inlen  = 0;
190     scsi_cmd.outlen = 0;
191     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
192     scsi_cmd.cmd[1] = 0;
193     scsi_cmd.cmd[2] = 0;
194     scsi_cmd.cmd[3] = 0;
195     scsi_cmd.cmd[4] = 0;
196     scsi_cmd.cmd[5] = 0;
197     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
198     if( i_status != 0 )
199     {
200         return 1;
201     }
202
203     scsi_cmd.inlen  = 0;
204     scsi_cmd.outlen = 0;
205     scsi_cmd.cmd[0] = START_STOP;
206     scsi_cmd.cmd[1] = 0;
207     scsi_cmd.cmd[2] = 0;
208     scsi_cmd.cmd[3] = 0;
209     scsi_cmd.cmd[4] = 1;
210     scsi_cmd.cmd[5] = 0;
211     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
212     if( i_status != 0 )
213     {
214         return 1;
215     }
216   
217     scsi_cmd.inlen  = 0;
218     scsi_cmd.outlen = 0;
219     scsi_cmd.cmd[0] = START_STOP;
220     scsi_cmd.cmd[1] = 0;
221     scsi_cmd.cmd[2] = 0;
222     scsi_cmd.cmd[3] = 0;
223     scsi_cmd.cmd[4] = 2;
224     scsi_cmd.cmd[5] = 0;
225     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
226     if( i_status != 0 )
227     {
228         return 1;
229     }
230   
231     /* Force kernel to reread partition table when new disc inserted */
232     i_status = ioctl( i_fd, BLKRRPART );
233   
234     return i_status;
235 }
236 #endif
237