Search
lxdream.org :: lxdream/src/drivers/cdrom/drive.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/drive.c
changeset 1097:d4807997e450
next1099:566cdeb157ec
author nkeynes
date Sun Jan 31 18:35:06 2010 +1000 (12 years ago)
permissions -rw-r--r--
last change Refactor CDROM host support
- Completely separate GDROM hardware (in gdrom/gdrom.c) from generic CDROM
support (now in drivers/cdrom)
- Add concept of 'sector sources' that can be mixed and matched to create
cdrom discs (makes support of arbitrary disc types much simpler)
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/cdrom/drive.c Sun Jan 31 18:35:06 2010 +1000
1.3 @@ -0,0 +1,148 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Host CD/DVD drive support.
1.8 + *
1.9 + * Copyright (c) 2009 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <stdlib.h>
1.23 +#include <glib/gstrfuncs.h>
1.24 +#include <glib/gmem.h>
1.25 +#include "drivers/cdrom/drive.h"
1.26 +#include "drivers/cdrom/cdimpl.h"
1.27 +
1.28 +static GList *cdrom_drive_list;
1.29 +
1.30 +static cdrom_drive_t cdrom_drive_new( const char *name, const char *display_name, cdrom_drive_open_fn_t open_fn )
1.31 +{
1.32 + cdrom_drive_t drive = g_malloc0( sizeof(struct cdrom_drive) );
1.33 + drive->name = g_strdup(name);
1.34 + drive->display_name = g_strdup(display_name);
1.35 + drive->open = open_fn;
1.36 + return drive;
1.37 +}
1.38 +
1.39 +static void cdrom_drive_destroy( cdrom_drive_t drive )
1.40 +{
1.41 + g_free( (char *)drive->name );
1.42 + drive->name = NULL;
1.43 + g_free( (char *)drive->display_name );
1.44 + drive->display_name = NULL;
1.45 + g_free( drive );
1.46 +}
1.47 +
1.48 +GList *cdrom_drive_get_list()
1.49 +{
1.50 + return cdrom_drive_list;
1.51 +}
1.52 +
1.53 +cdrom_drive_t cdrom_drive_add( const char *name, const char *display_name, cdrom_drive_open_fn_t open_fn )
1.54 +{
1.55 + for( GList *ptr = cdrom_drive_list; ptr != NULL; ptr = ptr->next ) {
1.56 + cdrom_drive_t it = (cdrom_drive_t)ptr->data;
1.57 + if( strcmp(it->name, name) == 0 ) {
1.58 + return it;
1.59 + }
1.60 + }
1.61 +
1.62 + cdrom_drive_t new_drive = cdrom_drive_new(name,display_name,open_fn);
1.63 + cdrom_drive_list = g_list_append( cdrom_drive_list, new_drive );
1.64 + return new_drive;
1.65 +}
1.66 +
1.67 +gboolean cdrom_drive_remove( const char *name )
1.68 +{
1.69 + for( GList *ptr = cdrom_drive_list; ptr != NULL; ptr = ptr->next ) {
1.70 + cdrom_drive_t it = (cdrom_drive_t)ptr->data;
1.71 + if( strcmp(it->name, name) == 0 ) {
1.72 + cdrom_drive_list = g_list_delete_link( cdrom_drive_list, ptr );
1.73 + cdrom_drive_destroy(it);
1.74 + return TRUE;
1.75 + }
1.76 + }
1.77 + return FALSE;
1.78 +}
1.79 +
1.80 +void cdrom_drive_remove_all()
1.81 +{
1.82 + for( GList *ptr = cdrom_drive_list; ptr != NULL; ptr = ptr->next ) {
1.83 + cdrom_drive_destroy( (cdrom_drive_t)ptr->data );
1.84 + }
1.85 + g_list_free(cdrom_drive_list);
1.86 + cdrom_drive_list = NULL;
1.87 +}
1.88 +
1.89 +cdrom_drive_t cdrom_drive_get_index( unsigned int index )
1.90 +{
1.91 + return (cdrom_drive_t)g_list_nth_data(cdrom_drive_list, index);
1.92 +}
1.93 +
1.94 +cdrom_disc_t cdrom_drive_open( cdrom_drive_t drive, ERROR *err )
1.95 +{
1.96 + return drive->open(drive, err);
1.97 +}
1.98 +
1.99 +cdrom_drive_t cdrom_drive_find( const char *name )
1.100 +{
1.101 + const char *id = name;
1.102 +
1.103 + /* If we have no drives, just return NULL without looking, to save time */
1.104 + if( cdrom_drive_list == NULL )
1.105 + return NULL;
1.106 +
1.107 + /* Check for a url-style name */
1.108 + const char *lizard_lips = strstr( name, "://" );
1.109 + if( lizard_lips != NULL ) {
1.110 + id = lizard_lips + 3;
1.111 + int method_len = (lizard_lips-name);
1.112 + if( method_len > 8 )
1.113 + return NULL;
1.114 +
1.115 + char method[method_len + 1];
1.116 + memcpy( method, name, method_len );
1.117 + method[method_len] = '\0';
1.118 +
1.119 + if( strcasecmp( method, "file" ) != 0 &&
1.120 + strcasecmp( method, "dvd" ) != 0 &&
1.121 + strcasecmp( method, "cd" ) != 0 &&
1.122 + strcasecmp( method, "cdrom" ) ) {
1.123 + /* Anything else we don't try to recognize */
1.124 + return NULL;
1.125 + }
1.126 +
1.127 + if( *id == '\0' ) {
1.128 + /* Accept eg 'dvd://' as meaning 'the first cd/dvd device */
1.129 + return cdrom_drive_list->data;
1.130 + }
1.131 +
1.132 + char *endp = NULL;
1.133 + unsigned long index = strtoul( id, &endp, 10 );
1.134 + if( endp != NULL && *endp == '\0' ) {
1.135 + /* Accept eg 'dvd://2' as meaning 'the second cd/dvd device */
1.136 + return cdrom_drive_get_index(index);
1.137 + }
1.138 +
1.139 + /* Otherwise it must be a drive identifier, so treat it as if it didn't
1.140 + * have the url prefix. (fallthrough)
1.141 + */
1.142 + }
1.143 +
1.144 + for( GList *ptr = cdrom_drive_list; ptr != NULL; ptr = ptr->next ) {
1.145 + cdrom_drive_t drive = (cdrom_drive_t)ptr->data;
1.146 + if( strcmp(drive->name, id) == 0 )
1.147 + return drive;
1.148 + }
1.149 +
1.150 + return NULL;
1.151 +}
.