DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/dos/cdrom_ioctl_linux.cpp
00001 /*
00002  *  Copyright (C) 2002-2020  The DOSBox Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License along
00015  *  with this program; if not, write to the Free Software Foundation, Inc.,
00016  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018  
00019 
00020 #include <string.h>
00021 #include "cdrom.h"
00022 #include "support.h"
00023 
00024 #if defined (LINUX)
00025 #include <fcntl.h>
00026 #include <unistd.h>
00027 #include <linux/cdrom.h>
00028 #include <sys/ioctl.h>
00029 #include <sys/stat.h>
00030 #include <sys/types.h>
00031 
00032 CDROM_Interface_Ioctl::CDROM_Interface_Ioctl(void) : CDROM_Interface_SDL()
00033 {
00034         strcpy(device_name, "");
00035 }
00036 
00037 bool CDROM_Interface_Ioctl::GetUPC(unsigned char& attr, char* upc)
00038 {
00039         int cdrom_fd = open(device_name, O_RDONLY | O_NONBLOCK);
00040         if (cdrom_fd == -1) return false;
00041         
00042         struct cdrom_mcn cdrom_mcn;
00043         int ret = ioctl(cdrom_fd, CDROM_GET_MCN, &cdrom_mcn);
00044         
00045         close(cdrom_fd);
00046         
00047         if (ret > 0) {
00048                 attr = 0;
00049                 safe_strncpy(upc, (char*)cdrom_mcn.medium_catalog_number, 14);
00050         }
00051 
00052         return (ret > 0);
00053 }
00054 
00055 bool CDROM_Interface_Ioctl::ReadSectors(PhysPt buffer, bool raw, unsigned long sector, unsigned long num)
00056 {
00057         int cdrom_fd = open(device_name, O_RDONLY | O_NONBLOCK);
00058         if (cdrom_fd == -1) return false;
00059         
00060         Bitu buflen = raw ? num * (unsigned int)CD_FRAMESIZE_RAW : num * (unsigned int)CD_FRAMESIZE;
00061     assert(buflen != 0u);
00062 
00063         Bit8u* buf = new Bit8u[buflen]; 
00064         int ret;
00065         
00066         if (raw) {
00067                 struct cdrom_read cdrom_read;
00068                 cdrom_read.cdread_lba = (int)sector;
00069                 cdrom_read.cdread_bufaddr = (char*)buf;
00070                 cdrom_read.cdread_buflen = (int)buflen;
00071                 
00072                 ret = ioctl(cdrom_fd, CDROMREADRAW, &cdrom_read);               
00073         } else {
00074                 ret = lseek(cdrom_fd, (off_t)(sector * (unsigned long)CD_FRAMESIZE), SEEK_SET);
00075                 if (ret >= 0) ret = read(cdrom_fd, buf, buflen);
00076                 if ((Bitu)ret != buflen) ret = -1;
00077         }
00078         close(cdrom_fd);
00079 
00080         MEM_BlockWrite(buffer, buf, buflen);
00081         delete[] buf;
00082         
00083         return (ret > 0);
00084 }
00085 
00086 bool CDROM_Interface_Ioctl::SetDevice(char* path, int forceCD)
00087 {
00088         bool success = CDROM_Interface_SDL::SetDevice(path, forceCD);
00089         
00090         if (success) {
00091 #if defined(C_SDL2)
00092         strcpy(device_name, "unknown");
00093 #else
00094                 const char* tmp = SDL_CDName(forceCD);
00095                 if (tmp) safe_strncpy(device_name, tmp, 512);
00096                 else success = false;
00097 #endif
00098         }
00099         
00100         return success;
00101 }
00102 
00103 bool CDROM_Interface_Ioctl::ReadSectorsHost(void *buffer, bool raw, unsigned long sector, unsigned long num)
00104 {
00105     (void)buffer;//UNUSED
00106     (void)sector;//UNUSED
00107     (void)raw;//UNUSED
00108     (void)num;//UNUSED
00109         return false;/*TODO*/
00110 }
00111 
00112 #endif