]> Shamusworld >> Repos - virtualjaguar/blob - src/cdintf_linux.cpp
1d35eea62dd6f740fcf070bbf53f908b51ac414c
[virtualjaguar] / src / cdintf_linux.cpp
1 //
2 // OS specific CDROM interface (linux)
3 //
4 // by James L. Hammons and Niels Wagenaar
5 //
6 // NOTE : This CD-ROM code *could* work with other UN*X related OS. However,
7 //        we/I are not sure on this matter.
8 //
9 // This is very experimental and I have the feeling that this won't even compile.
10 // Hell, I don't even have a Linux dev system (broken) or Jaguar CD releases to
11 // test this code :(
12 //
13 // Big thanks to the DOSBOX team which provided us with great knowlegde about
14 // CD-ROM access and Linux/UN*X.
15
16
17 // *** OS dependent CDROM stuffola ***
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <linux/cdrom.h>
21 #include <sys/ioctl.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24
25 // *** End OS dependent ***
26 #include "log.h"
27 #include "string.h"
28
29 // *** Virtual Jaguar dependent ***
30 //#include "SDL.h"                // Yes, we use SDL for initializing the CD-ROM and
31 //                                // give us access to certain CD-ROM states. But not just yet.
32
33 // *** SDL CD-ROM dependent ***        // Not yet needed!
34 // SDL_CD      *cdrom;                 // Our variable for SDL CD-ROM access.
35 // CDstatus     status;                // Let us get our status.
36 // char        *status_str;
37
38 // *** Local variables ***
39 char                   device_name[512] ; // Devicename, for example /dev/cdrom
40
41 //
42 // Linux support functions
43 // OS specific implementation of OS agnostic functions
44 //
45
46 bool CDIntfInit(void)
47 {
48     // Setting device_name to /deb/cdrom. /dev/cdrom is the default CD-ROM
49     // drive on most UN*X systems. Well, I think it is.
50     //
51     // In the future we can probably use SDL for getting CDROM states and
52     // CD-ROM specific information.
53     strcpy(device_name, "/dev/cdrom");
54
55     // Let us open the device_name and check if we can open the CD-ROM.
56     int cdrom_fd = open(device_name, O_RDONLY | O_NONBLOCK);
57     
58         if (cdrom_fd <= 0) 
59     {
60         // CD-ROM isn't accessable.
61         // Write the error in the log file and return false.
62         WriteLog("CDINTF: CDIntfInit - Unable to open CDROM!\n");
63         return false;
64     }    
65     else
66     {
67         // CD-ROM is accessable.
68         // Write the success in the log file and return true.
69         WriteLog("CDINTF: CDIntfInit - Succesfully opened CDROM!\n"); 
70         close(device_name);
71         return true;
72     }
73         
74 }
75
76 void CDIntfDone(void)
77 {
78     // Just in case : closing device_name.
79     WriteLog("CDINTF: CDIntfDone - Closing CDROM!\n");
80     close(device_name);
81 }
82
83 bool CDIntfReadBlock(uint32 sector, uint8 * buffer)
84 {
85         unsigned int   buflen = CD_FRAMESIZE_RAW;          // Raw read, 2352 bytes per sector
86         //unsigned char *buf    = new unsigned int[buflen];     // DOSBOX, do we need this?
87         int ret;
88         struct cdrom_read cdrom_read;
89
90         // Let us open the device_name and check if we can open the CD-ROM.
91     int cdrom_fd = open(device_name, O_RDONLY | O_NONBLOCK);
92         if (cdrom_fd <= 0)
93     {
94         // CD-ROM isn't accessable.
95         // Write the error in the log file and return false.
96         WriteLog("CDINTF: CDIntfReadBlock - Unable to open CDROM!\n");
97         return false;
98     }    
99         
100         // Setting up the cdrom_read struct :
101         cdrom_read.cdread_lba = sector;             // Which sector to read.
102         cdrom_read.cdread_bufaddr = (char*)buffer;  // Where to put the data (?)
103         cdrom_read.cdread_buflen = buflen;          // 2352 bytes/sector -> RAW read
104                                           
105         // Let us read the content we want.     -1 (false) when it didn't work.
106         ret = ioctl(cdrom_fd, CDROMREADRAW, &cdrom_read);               
107
108         // Close the CD-ROM.
109         close(cdrom_fd);
110
111         // The following was taken from DOSBOX. After reading the content, they write
112         // back the information from buf (based upon the size of buflen) to buffer.
113         // I think that this is not needed. *fingers crossed*
114         //
115         // MEM_BlockWrite(buffer, buf, buflen);
116         // delete[] buf;
117         
118         // Uncomment the following for debug reasons.
119         //
120     // WriteLog("CDINTF: CDIntfReadBlock - Reading sector %d!\n", sector);
121     
122         return (ret > 0);
123
124 }
125
126 uint32 CDIntfGetNumSessions(void)
127 {
128         // Still need relevant code here...
129         return 2;
130 }
131
132 void CDIntfSelectDrive(uint32 driveNum)
133 {
134         WriteLog("CDINTF: SelectDrive unimplemented!\n");
135 }
136
137 uint32 CDIntfGetCurrentDrive(void)
138 {
139         WriteLog("CDINTF: GetCurrentDrive unimplemented!\n");
140         return 0;
141 }
142
143 const uint8 * CDIntfGetDriveName(uint32)
144 {
145         WriteLog("CDINTF: GetDriveName unimplemented!\n");
146         return NULL;
147 }
148
149 uint8 CDIntfGetSessionInfo(uint32 session, uint32 offset)
150 {
151         WriteLog("CDINTF: GetSessionInfo unimplemented!\n");
152         return 0xFF;
153 }
154
155 uint8 CDIntfGetTrackInfo(uint32 track, uint32 offset)
156 {
157         WriteLog("CDINTF: GetTrackInfo unimplemented!\n");
158         return 0xFF;
159 }