]> git.sesse.net Git - vlc/blob - src/interface/intf_eject.c
Improvements of the OS X GUI.
[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.10 2002/05/06 22:59:46 massiot Exp $
6  *
7  * Author: Julien Blache <jb@technologeek.org> for the Linux part
8  *               with code taken from the Linux "eject" command
9  *         Jon Lech Johanson <jon-vl@nanocrew.net> for Darwin
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #include <videolan/vlc.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #ifdef HAVE_UNISTD_H
32 #    include <unistd.h>
33 #endif
34
35 #include <string.h>
36
37 #ifdef HAVE_FCNTL_H
38 #   include <fcntl.h>
39 #endif
40
41 #ifdef HAVE_DVD_H
42 #   include <dvd.h>
43 #endif
44
45 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
46 #   include <linux/version.h>
47     /* handy macro found in 2.1 kernels, but not in older ones */
48 #   ifndef KERNEL_VERSION
49 #       define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
50 #   endif
51
52 #   include <sys/types.h>
53 #   include <sys/stat.h>
54 #   include <sys/ioctl.h>
55
56 #   include <sys/ioctl.h>
57 #   include <sys/mount.h>
58
59 #   include <linux/cdrom.h>
60 #   if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
61 #       include <linux/ucdrom.h>
62 #   endif
63
64 #   include <scsi/scsi.h>
65 #   include <scsi/sg.h>
66 #   include <scsi/scsi_ioctl.h>
67 #endif
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_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_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 "/usr/sbin/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 #else /* SYS_DARWIN */
131
132     int i_fd;
133
134     /* This code could be extended to support CD/DVD-ROM chargers */
135
136     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
137    
138     if( i_fd == -1 )
139     {
140         intf_ErrMsg( "intf error: couldn't open device %s", psz_device );
141         return 1;
142     }
143
144 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
145     /* Try a simple ATAPI eject */
146     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
147
148     if( i_ret != 0 )
149     {
150         i_ret = EjectSCSI( i_fd );
151     }
152
153     if( i_ret != 0 )
154     {
155         intf_ErrMsg( "intf error: couldn't eject %s", psz_device );
156     }
157
158 #elif defined (HAVE_DVD_H)
159     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
160
161 #else
162     intf_ErrMsg( "intf error: CD-Rom ejection unsupported on this platform" );
163     i_ret = -1;
164
165 #endif
166     close( i_fd );
167
168     return i_ret;
169 #endif
170 }
171
172 /* The following functions are local */
173
174 #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_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