| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include "cdrom.h" |
| 22 | #include "support.h" |
| 23 | |
| 24 | #if defined (LINUX) |
| 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
| 27 | #include <linux/cdrom.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | CDROM_Interface_Ioctl::CDROM_Interface_Ioctl(void) : CDROM_Interface_SDL() |
| 33 | { |
| 34 | strcpy(device_name, ""); |
| 35 | } |
| 36 | |
| 37 | bool CDROM_Interface_Ioctl::GetUPC(unsigned char& attr, char* upc) |
| 38 | { |
| 39 | int cdrom_fd = open(device_name, O_RDONLY | O_NONBLOCK); |
| 40 | if (cdrom_fd == -1) return false; |
| 41 | |
| 42 | struct cdrom_mcn cdrom_mcn; |
| 43 | int ret = ioctl(cdrom_fd, CDROM_GET_MCN, &cdrom_mcn); |
| 44 | |
| 45 | close(cdrom_fd); |
| 46 | |
| 47 | if (ret > 0) { |
| 48 | attr = 0; |
| 49 | safe_strncpy(upc, (char*)cdrom_mcn.medium_catalog_number, 14); |
| 50 | } |
| 51 | |
| 52 | return (ret > 0); |
| 53 | } |
| 54 | |
| 55 | bool CDROM_Interface_Ioctl::ReadSectors(PhysPt buffer, bool raw, unsigned long sector, unsigned long num) |
| 56 | { |
| 57 | int cdrom_fd = open(device_name, O_RDONLY | O_NONBLOCK); |
| 58 | if (cdrom_fd == -1) return false; |
| 59 | |
| 60 | Bits buflen = raw ? num * CD_FRAMESIZE_RAW : num * CD_FRAMESIZE; |
| 61 | Bit8u* buf = new Bit8u[buflen]; |
| 62 | int ret; |
| 63 | |
| 64 | if (raw) { |
| 65 | struct cdrom_read cdrom_read; |
| 66 | cdrom_read.cdread_lba = sector; |
| 67 | cdrom_read.cdread_bufaddr = (char*)buf; |
| 68 | cdrom_read.cdread_buflen = buflen; |
| 69 | |
| 70 | ret = ioctl(cdrom_fd, CDROMREADRAW, &cdrom_read); |
| 71 | } else { |
| 72 | ret = lseek(cdrom_fd, sector * CD_FRAMESIZE, SEEK_SET); |
| 73 | if (ret >= 0) ret = read(cdrom_fd, buf, buflen); |
| 74 | if (ret != buflen) ret = -1; |
| 75 | } |
| 76 | close(cdrom_fd); |
| 77 | |
| 78 | MEM_BlockWrite(buffer, buf, buflen); |
| 79 | delete[] buf; |
| 80 | |
| 81 | return (ret > 0); |
| 82 | } |
| 83 | |
| 84 | bool CDROM_Interface_Ioctl::SetDevice(char* path, int forceCD) |
| 85 | { |
| 86 | bool success = CDROM_Interface_SDL::SetDevice(path, forceCD); |
| 87 | |
| 88 | if (success) { |
| 89 | const char* tmp = SDL_CDName(forceCD); |
| 90 | if (tmp) safe_strncpy(device_name, tmp, 512); |
| 91 | else success = false; |
| 92 | } |
| 93 | |
| 94 | return success; |
| 95 | } |
| 96 | |
| 97 | #endif |