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