]> git.sesse.net Git - vlc/blob - src/interface/intf_eject.c
* fixed a typo in satellite input
[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.9 2002/04/04 22:08:05 massiot 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 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
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 <sys/mount.h>
57
58 #   include <linux/cdrom.h>
59 #   if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
60 #       include <linux/ucdrom.h>
61 #   endif
62
63 #   include <scsi/scsi.h>
64 #   include <scsi/sg.h>
65 #   include <scsi/scsi_ioctl.h>
66 #endif
67
68 /*****************************************************************************
69  * Local prototypes
70  *****************************************************************************/
71 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
72 static int EjectSCSI ( int i_fd );
73 #endif
74
75 /*****************************************************************************
76  * intf_Eject: eject the CDRom
77  *****************************************************************************
78  * returns 0 on success
79  * returns 1 on failure
80  * returns -1 if not implemented
81  *****************************************************************************/
82 int intf_Eject( const char *psz_device )
83 {
84     int i_fd;
85     int i_ret;
86
87 #ifdef SYS_DARWIN
88     FILE *p_eject;
89     char *psz_disk;
90     char sz_cmd[32];
91
92     /*
93      * The only way to cleanly unmount the disc under MacOS X
94      * is to use the 'disktool' command line utility. It uses
95      * the non-public Disk Arbitration API, which can not be
96      * used by Cocoa or Carbon applications. 
97      */
98
99     if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
100         strlen( psz_disk ) > 4 )
101     {
102 #define EJECT_CMD "disktool -e %s 0"
103         snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
104 #undef EJECT_CMD
105
106         if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
107         {
108             char psz_result[0x200];
109             i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
110
111             if( i_ret == 0 && ferror( p_eject ) != 0 )
112             {
113                 pclose( p_eject );
114                 return 1;
115             }
116
117             pclose( p_eject );
118
119             psz_result[ i_ret ] = 0;
120
121             if( strstr( psz_result, "Disk Ejected" ) != NULL )
122             {
123                 return 0;
124             }
125         }
126     }
127
128     return 1;
129
130 #endif
131
132     /* This code could be extended to support CD/DVD-ROM chargers */
133
134     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
135    
136     if( i_fd == -1 )
137     {
138         intf_ErrMsg( "intf error: couldn't open device %s", psz_device );
139         return 1;
140     }
141
142 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
143     /* Try a simple ATAPI eject */
144     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
145
146     if( i_ret != 0 )
147     {
148         i_ret = EjectSCSI( i_fd );
149     }
150
151     if( i_ret != 0 )
152     {
153         intf_ErrMsg( "intf error: couldn't eject %s", psz_device );
154     }
155
156 #elif defined (HAVE_DVD_H)
157     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
158
159 #else
160     intf_ErrMsg( "intf error: CD-Rom ejection unsupported on this platform" );
161     i_ret = -1;
162
163 #endif
164     close( i_fd );
165
166     return i_ret;
167 }
168
169 /* The following functions are local */
170
171 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
172 /*****************************************************************************
173  * Eject using SCSI commands. Return 0 if successful
174  *****************************************************************************/
175 static int EjectSCSI( int i_fd )
176 {
177     int i_status;
178
179     struct sdata
180     {
181         int  inlen;
182         int  outlen;
183         char cmd[256];
184     } scsi_cmd;
185
186     scsi_cmd.inlen  = 0;
187     scsi_cmd.outlen = 0;
188     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
189     scsi_cmd.cmd[1] = 0;
190     scsi_cmd.cmd[2] = 0;
191     scsi_cmd.cmd[3] = 0;
192     scsi_cmd.cmd[4] = 0;
193     scsi_cmd.cmd[5] = 0;
194     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
195     if( i_status != 0 )
196     {
197         return 1;
198     }
199
200     scsi_cmd.inlen  = 0;
201     scsi_cmd.outlen = 0;
202     scsi_cmd.cmd[0] = START_STOP;
203     scsi_cmd.cmd[1] = 0;
204     scsi_cmd.cmd[2] = 0;
205     scsi_cmd.cmd[3] = 0;
206     scsi_cmd.cmd[4] = 1;
207     scsi_cmd.cmd[5] = 0;
208     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
209     if( i_status != 0 )
210     {
211         return 1;
212     }
213   
214     scsi_cmd.inlen  = 0;
215     scsi_cmd.outlen = 0;
216     scsi_cmd.cmd[0] = START_STOP;
217     scsi_cmd.cmd[1] = 0;
218     scsi_cmd.cmd[2] = 0;
219     scsi_cmd.cmd[3] = 0;
220     scsi_cmd.cmd[4] = 2;
221     scsi_cmd.cmd[5] = 0;
222     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
223     if( i_status != 0 )
224     {
225         return 1;
226     }
227   
228     /* Force kernel to reread partition table when new disc inserted */
229     i_status = ioctl( i_fd, BLKRRPART );
230   
231     return i_status;
232 }
233 #endif
234