Port to *BSD. It runs but isn't terribly useful without Cg.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5934 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Soren Jorvang
2010-07-22 03:29:35 +00:00
parent cdce0ec322
commit 83883cee45
29 changed files with 5353 additions and 148 deletions

View File

@ -1,51 +1,55 @@
// Most of the code in this file was shamelessly ripped from libcdio With minor adjustments
#include "CDUtils.h"
#include "Common.h"
#ifdef _WIN32
#include <windows.h>
#elif __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOBSD.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/IOBSD.h>
#include <paths.h>
#elif __linux__
#else
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>
#endif // WIN32
#ifdef __linux__
#include <linux/cdrom.h>
#endif
#ifdef _WIN32
// Returns a string that can be used in a CreateFile call if
// Returns a string that can be used in a CreateFile call if
// c_drive letter is a character. If not NULL is returned.
const char *is_cdrom(const char c_drive_letter)
{
UINT uDriveType;
char sz_win32_drive[4];
sz_win32_drive[0]= c_drive_letter;
sz_win32_drive[1]=':';
sz_win32_drive[2]='\\';
sz_win32_drive[3]='\0';
uDriveType = GetDriveType(sz_win32_drive);
switch(uDriveType)
{
case DRIVE_CDROM:
{
{
case DRIVE_CDROM:
{
char sz_win32_drive_full[] = "\\\\.\\X:";
sz_win32_drive_full[4] = c_drive_letter;
return __strdup(&sz_win32_drive_full[4]);
}
default:
//cdio_debug("Drive %c is not a CD-ROM", c_drive_letter);
return NULL;
}
default:
{
//cdio_debug("Drive %c is not a CD-ROM", c_drive_letter);
return NULL;
}
}
}
@ -53,7 +57,7 @@ const char *is_cdrom(const char c_drive_letter)
std::vector<std::string> cdio_get_devices() {
std::vector<std::string> drives;
char drive_letter;
// Scan the system for CD-ROM drives.
for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
const char *drive_str=is_cdrom(drive_letter);
@ -74,55 +78,53 @@ std::vector<std::string> cdio_get_devices()
io_iterator_t media_iterator;
CFMutableDictionaryRef classes_to_match;
std::vector<std::string> drives;
kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
if( kern_result != KERN_SUCCESS )
return( drives );
classes_to_match = IOServiceMatching( kIOCDMediaClass );
if( classes_to_match == NULL )
return( drives );
CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectableKey),
kCFBooleanTrue );
kern_result = IOServiceGetMatchingServices( master_port,
classes_to_match,
&media_iterator );
CFDictionarySetValue( classes_to_match,
CFSTR(kIOMediaEjectableKey), kCFBooleanTrue );
kern_result = IOServiceGetMatchingServices( master_port,
classes_to_match, &media_iterator );
if( kern_result != KERN_SUCCESS)
return( drives );
next_media = IOIteratorNext( media_iterator );
if( next_media != 0 )
{
{
char psz_buf[0x32];
size_t dev_path_length;
CFTypeRef str_bsd_path;
do
{
str_bsd_path =
{
str_bsd_path =
IORegistryEntryCreateCFProperty( next_media,
CFSTR( kIOBSDNameKey ),
kCFAllocatorDefault,
0 );
CFSTR( kIOBSDNameKey ), kCFAllocatorDefault,
0 );
if( str_bsd_path == NULL )
{
{
IOObjectRelease( next_media );
continue;
}
// Below, by appending 'r' to the BSD node name, we indicate
// a raw disk. Raw disks receive I/O requests directly and
// don't go through a buffer cache.
snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
dev_path_length = strlen( psz_buf );
if( CFStringGetCString( (CFStringRef)str_bsd_path,
(char*)&psz_buf + dev_path_length,
sizeof(psz_buf) - dev_path_length,
kCFStringEncodingASCII))
{
(char*)&psz_buf + dev_path_length,
sizeof(psz_buf) - dev_path_length,
kCFStringEncodingASCII))
{
if(psz_buf != NULL)
{
std::string str = psz_buf;
@ -131,26 +133,31 @@ std::vector<std::string> cdio_get_devices()
}
CFRelease( str_bsd_path );
IOObjectRelease( next_media );
} while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
}
IOObjectRelease( media_iterator );
return drives;
}
#elif defined __linux__
#else
// checklist: /dev/cdrom, /dev/dvd /dev/hd?, /dev/scd? /dev/sr?
static struct
{
const char * format;
unsigned int num_min;
unsigned int num_max;
const char * format;
unsigned int num_min;
unsigned int num_max;
} checklist[] =
{
#ifdef __linux__
{ "/dev/cdrom", 0, 0},
{ "/dev/dvd", 0, 0},
{ "/dev/hd%c", 'a', 'z' },
{ "/dev/scd%d", 0, 27 },
{ "/dev/sr%d", 0, 27 },
{ "/dev/scd%d", 0, 27 },
{ "/dev/sr%d", 0, 27 },
#else
{ "/dev/acd%d", 0, 27 },
{ "/dev/cd%d", 0, 27 },
#endif
{ NULL }
};
@ -161,7 +168,8 @@ bool is_device(const char *source_name)
if (0 != lstat(source_name, &buf))
return false;
return ((S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode)) && !S_ISLNK(buf.st_mode));
return ((S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode)) &&
!S_ISLNK(buf.st_mode));
}
// Check a device to see if it is a DVD/CD-ROM drive
@ -169,39 +177,40 @@ static bool is_cdrom(const char *drive, char *mnttype)
{
bool is_cd=false;
int cdfd;
// Check if the device exists
if (!is_device(drive))
return(false);
// If it does exist, verify that it is a cdrom/dvd drive
cdfd = open(drive, (O_RDONLY|O_NONBLOCK), 0);
if ( cdfd >= 0 )
{
{
#ifdef __linux__
if (ioctl(cdfd, CDROM_GET_CAPABILITY, 0) != -1)
#endif
is_cd = true;
close(cdfd);
}
}
return(is_cd);
}
// Returns a pointer to an array of strings with the device names
std::vector<std::string> cdio_get_devices ()
{
unsigned int i;
char drive[40];
std::vector<std::string> drives;
// Scan the system for DVD/CD-ROM drives.
for ( i=0; checklist[i].format; ++i )
{
{
unsigned int j;
for ( j=checklist[i].num_min; j<=checklist[i].num_max; ++j )
{
{
sprintf(drive, checklist[i].format, j);
if ( (is_cdrom(drive, NULL)) > 0 )
{
{
std::string str = drive;
drives.push_back(str);
}
@ -215,8 +224,8 @@ std::vector<std::string> cdio_get_devices ()
bool cdio_is_cdrom(std::string device)
{
#ifdef __linux__
// Resolve symbolic links. This allows symbolic links to valid cdrom/dvd drives to
// be passed from the command line with the -e flag.
// Resolve symbolic links. This allows symbolic links to valid
// drives to be passed from the command line with the -e flag.
char *devname = realpath(device.c_str(), NULL);
if (!devname)
return false;
@ -225,23 +234,23 @@ bool cdio_is_cdrom(std::string device)
std::vector<std::string> devices = cdio_get_devices();
bool res = false;
for (unsigned int i = 0; i < devices.size(); i++)
{
{
#ifdef __linux__
if (strncmp(devices[i].c_str(), devname, MAX_PATH) == 0)
#else
if (strncmp(devices[i].c_str(), device.c_str(), MAX_PATH) == 0)
#endif
{
{
res = true;
break;
}
}
}
#ifdef __linux__
if (devname)
free(devname);
#endif
devices.clear();
return res;
}

View File

@ -29,15 +29,14 @@ template<> struct CompileTimeAssert<true> {};
#ifndef _WIN32
#if defined __APPLE__
char* strndup (char const *s, size_t n);
size_t strnlen(const char *s, size_t n);
#else
#include <errno.h>
#ifdef __linux__
#include <byteswap.h>
#else
char * strndup(char const *s, size_t n);
size_t strnlen(const char *s, size_t n);
#endif
#endif // APPLE
#include <errno.h>
// go to debugger mode
#ifdef GEKKO
#define Crash()

View File

@ -48,6 +48,10 @@
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
#endif
#if defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__
#define stat64 stat // XXX
#endif
// This namespace has various generic functions related to files and paths.
// The code still needs a ton of cleanup.
// REMEMBER: strdup considered harmful!

View File

@ -120,7 +120,7 @@ u8* MemArena::Find4GBBase()
return base;
#else
void* base = mmap(0, 0x31000000, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_SHARED, 0, 0);
MAP_ANON | MAP_SHARED, -1, 0);
if (base == MAP_FAILED) {
PanicAlert("Failed to map 1 GB of memory space: %s", strerror(errno));
return 0;

View File

@ -37,7 +37,7 @@ const char *GetLastErrorMsg()
return errStr;
}
#ifdef __APPLE__
#if !defined(__linux__) && !defined(_WIN32)
// strlen with cropping after size n
size_t strnlen(const char *s, size_t n)
{

View File

@ -16,7 +16,7 @@
// http://code.google.com/p/dolphin-emu/
#include <time.h>
#include <sys/timeb.h>
#include <sys/time.h>
#ifdef _WIN32
#include <Windows.h>
@ -37,9 +37,9 @@ u32 Timer::GetTimeMs() {
#else
u32 Timer::GetTimeMs()
{
struct timeb t;
ftime(&t);
return ((u32)(t.time * 1000 + t.millitm));
struct timeval t;
(void)gettimeofday(&t, NULL);
return((u32)(t.tv_sec * 1000 + t.tv_usec / 1000));
}
#endif
@ -202,9 +202,9 @@ std::string Timer::GetTimeFormatted()
strftime(tmp, 6, "%M:%S", gmTime);
// Now tack on the milliseconds
struct timeb tp;
(void)::ftime(&tp);
sprintf(formattedTime, "%s:%03i", tmp, tp.millitm);
struct timeval t;
(void)gettimeofday(&t, NULL);
sprintf(formattedTime, "%s:%03ld", tmp, t.tv_usec / 1000);
return std::string(formattedTime);
}
@ -214,8 +214,8 @@ std::string Timer::GetTimeFormatted()
// ----------------
double Timer::GetDoubleTime()
{
struct timeb tp;
(void)::ftime(&tp);
struct timeval t;
(void)gettimeofday(&t, NULL);
u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970(); // Get continous timestamp
/* Remove a few years. We only really want enough seconds to make sure that we are
@ -226,7 +226,7 @@ double Timer::GetDoubleTime()
//if (TmpSeconds < 0) return 0; // Check the the user's clock is working somewhat
u32 Seconds = (u32)TmpSeconds; // Make a smaller integer that fits in the double
double ms = tp.millitm / 1000.0;
double ms = t.tv_usec / 1000.0 / 1000.0;
double TmpTime = Seconds + ms;
return TmpTime;
}