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