]> git.sesse.net Git - vlc/blob - modules/gui/eject.c
Qt: prefsItem: namespace TYPE_*
[vlc] / modules / gui / eject.c
1 /*****************************************************************************
2  * eject.c: CD/DVD-ROM ejection handling functions
3  *****************************************************************************
4  * Copyright (C) 2001-2011 the VideoLAN team
5  *
6  * Authors: Julien Blache <jb@technologeek.org> for the Linux part
7  *                with code taken from the Linux "eject" command
8  *          Jon Lech Johansen <jon-vl@nanocrew.net> for Darwin
9  *          Gildas Bazin <gbazin@netcourrier.com> for Win32
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /**
27  *  \file
28  *  This file contain functions to eject CD and DVD drives
29  */
30
31 /*#ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif*/
34
35 #include <vlc_common.h>
36 #include <vlc_fs.h>
37
38 #if defined( WIN32 )
39 #   include <mmsystem.h>
40 #elif defined(__linux__)
41 #   include <sys/types.h>
42 #   include <unistd.h>
43 #   include <fcntl.h>
44 #   include <sys/ioctl.h>
45 #   include <sys/mount.h>
46
47 #   include <linux/cdrom.h>
48
49 #   include <scsi/scsi.h>
50 #   include <scsi/sg.h>
51 #   include <scsi/scsi_ioctl.h>
52 #elif defined (HAVE_DVD_H)
53 #   include <unistd.h>
54 #   include <fcntl.h>
55 #   include <dvd.h>
56 #endif
57
58 #if defined(__linux__)
59 /**
60  * \brief Ejects the CD /DVD using SCSI commands
61  * \ingroup vlc_interface
62  * This function is local
63  * \param i_fd a device nummber
64  * \return 0 on success, VLC_EGENERIC on failure
65  */
66 static int EjectSCSI( int i_fd )
67 {
68     struct sdata
69     {
70         int  inlen;
71         int  outlen;
72         char cmd[256];
73     } scsi_cmd;
74
75     scsi_cmd.inlen  = 0;
76     scsi_cmd.outlen = 0;
77     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
78     scsi_cmd.cmd[1] = 0;
79     scsi_cmd.cmd[2] = 0;
80     scsi_cmd.cmd[3] = 0;
81     scsi_cmd.cmd[4] = 0;
82     scsi_cmd.cmd[5] = 0;
83     if( ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd ) < 0 )
84         return VLC_EGENERIC;
85
86     scsi_cmd.inlen  = 0;
87     scsi_cmd.outlen = 0;
88     scsi_cmd.cmd[0] = START_STOP;
89     scsi_cmd.cmd[1] = 0;
90     scsi_cmd.cmd[2] = 0;
91     scsi_cmd.cmd[3] = 0;
92     scsi_cmd.cmd[4] = 1;
93     scsi_cmd.cmd[5] = 0;
94     if( ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd ) < 0 )
95         return VLC_EGENERIC;
96
97     scsi_cmd.inlen  = 0;
98     scsi_cmd.outlen = 0;
99     scsi_cmd.cmd[0] = START_STOP;
100     scsi_cmd.cmd[1] = 0;
101     scsi_cmd.cmd[2] = 0;
102     scsi_cmd.cmd[3] = 0;
103     scsi_cmd.cmd[4] = 2;
104     scsi_cmd.cmd[5] = 0;
105     if( ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd ) < 0 )
106         return VLC_EGENERIC;
107
108     /* Force kernel to reread partition table when new disc inserted */
109     ioctl( i_fd, BLKRRPART );
110     return VLC_SUCCESS;
111 }
112 #endif
113
114 #undef intf_Eject
115 /**
116  * Ejects the optical disc in a device
117  * \param p_this the calling vlc_object_t
118  * \param psz_device the CD/DVD to eject
119  * \return VLC_SUCCESS or VLC_EGENERIC
120  */
121 static int intf_Eject( vlc_object_t *p_this, const char *psz_device )
122 {
123     VLC_UNUSED(p_this);
124
125 #if defined(WIN32)
126     MCI_OPEN_PARMS op;
127     DWORD i_flags;
128     char psz_drive[4];
129
130     memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
131     op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
132
133     strcpy( psz_drive, "X:" );
134     psz_drive[0] = psz_device[0];
135     op.lpstrElementName = psz_drive;
136
137     /* Set the flags for the device type */
138     i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
139               MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
140
141     if( mciSendCommand( 0, MCI_OPEN, i_flags, (uintptr_t)&op ) )
142         return VLC_EGENERIC;
143
144     /* Eject disc */
145     mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 );
146     /* Release access to the device */
147     mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
148
149     return VLC_SUCCESS;
150
151 #elif defined (__linux__) || defined (HAVE_DVD_H)
152     /* This code could be extended to support CD/DVD-ROM chargers */
153     int fd = vlc_open( psz_device, O_RDONLY | O_NONBLOCK );
154     if( fd == -1 )
155     {
156         msg_Err( p_this, "could not open device %s", psz_device );
157         return VLC_EGENERIC;
158     }
159
160 # if defined(__linux__)
161     /* Try a simple ATAPI eject */
162     if( ioctl( fd, CDROMEJECT, 0 ) < 0
163      && EjectSCSI( fd ) )
164 # else
165     if( ioctl( fd, CDROMEJECT, 0 ) < 0 )
166 # endif
167     {
168         msg_Err( p_this, "could not eject %s", psz_device );
169         close( fd );
170         return VLC_EGENERIC;
171     }
172     return VLC_SUCCESS;
173
174 #else
175     msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
176     return VLC_EGENERIC;
177 #endif
178 }
179
180 #define intf_Eject(o, p) intf_Eject(VLC_OBJECT(o), p)