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