Search
lxdream.org :: lxdream/src/drivers/cdrom/drive.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/drive.c
changeset 1099:566cdeb157ec
prev1097:d4807997e450
next1296:30ecee61f811
author nkeynes
date Wed Feb 10 18:16:19 2010 +1000 (14 years ago)
permissions -rw-r--r--
last change First draft of basic ISO9660 filesystem reader
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Host CD/DVD drive support.
     5  *
     6  * Copyright (c) 2009 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <stdlib.h>
    20 #include <string.h>
    21 #include <glib/gstrfuncs.h>
    22 #include <glib/gmem.h>
    23 #include "drivers/cdrom/drive.h"
    24 #include "drivers/cdrom/cdimpl.h"
    26 static GList *cdrom_drive_list;
    28 static cdrom_drive_t cdrom_drive_new( const char *name, const char *display_name, cdrom_drive_open_fn_t open_fn )
    29 {
    30     cdrom_drive_t drive = g_malloc0( sizeof(struct cdrom_drive) );
    31     drive->name = g_strdup(name);
    32     drive->display_name = g_strdup(display_name);
    33     drive->open = open_fn;
    34     return drive;
    35 }
    37 static void cdrom_drive_destroy( cdrom_drive_t drive )
    38 {
    39     g_free( (char *)drive->name );
    40     drive->name = NULL;
    41     g_free( (char *)drive->display_name );
    42     drive->display_name = NULL;
    43     g_free( drive );
    44 }
    46 GList *cdrom_drive_get_list()
    47 {
    48     return cdrom_drive_list;
    49 }
    51 cdrom_drive_t cdrom_drive_add( const char *name, const char *display_name, cdrom_drive_open_fn_t open_fn )
    52 {
    53     for( GList *ptr = cdrom_drive_list; ptr != NULL; ptr = ptr->next ) {
    54         cdrom_drive_t it = (cdrom_drive_t)ptr->data;
    55         if( strcmp(it->name, name) == 0 ) {
    56             return it;
    57         }
    58     }
    60     cdrom_drive_t new_drive = cdrom_drive_new(name,display_name,open_fn);
    61     cdrom_drive_list = g_list_append( cdrom_drive_list, new_drive );
    62     return new_drive;
    63 }
    65 gboolean cdrom_drive_remove( const char *name )
    66 {
    67     for( GList *ptr = cdrom_drive_list; ptr != NULL; ptr = ptr->next ) {
    68         cdrom_drive_t it = (cdrom_drive_t)ptr->data;
    69         if( strcmp(it->name, name) == 0 ) {
    70             cdrom_drive_list = g_list_delete_link( cdrom_drive_list, ptr );
    71             cdrom_drive_destroy(it);
    72             return TRUE;
    73         }
    74     }
    75     return FALSE;
    76 }
    78 void cdrom_drive_remove_all()
    79 {
    80     for( GList *ptr = cdrom_drive_list; ptr != NULL; ptr = ptr->next ) {
    81         cdrom_drive_destroy( (cdrom_drive_t)ptr->data );
    82     }
    83     g_list_free(cdrom_drive_list);
    84     cdrom_drive_list = NULL;
    85 }
    87 cdrom_drive_t cdrom_drive_get_index( unsigned int index )
    88 {
    89     return (cdrom_drive_t)g_list_nth_data(cdrom_drive_list, index);
    90 }
    92 cdrom_disc_t cdrom_drive_open( cdrom_drive_t drive, ERROR *err )
    93 {
    94     return drive->open(drive, err);
    95 }
    97 cdrom_drive_t cdrom_drive_find( const char *name )
    98 {
    99     const char *id = name;
   101     /* If we have no drives, just return NULL without looking, to save time */
   102     if( cdrom_drive_list == NULL )
   103         return NULL;
   105     /* Check for a url-style name */
   106     const char *lizard_lips = strstr( name, "://" );
   107     if( lizard_lips != NULL ) {
   108         id = lizard_lips + 3;
   109         int method_len = (lizard_lips-name);
   110         if( method_len > 8 )
   111             return NULL;
   113         char method[method_len + 1];
   114         memcpy( method, name, method_len );
   115         method[method_len] = '\0';
   117         if( strcasecmp( method, "file" ) != 0 &&
   118             strcasecmp( method, "dvd" ) != 0 &&
   119             strcasecmp( method, "cd" ) != 0 &&
   120             strcasecmp( method, "cdrom" ) ) {
   121             /* Anything else we don't try to recognize */
   122             return NULL;
   123         }
   125         if( *id == '\0' ) {
   126             /* Accept eg  'dvd://' as meaning 'the first cd/dvd device */
   127             return cdrom_drive_list->data;
   128         }
   130         char *endp = NULL;
   131         unsigned long index = strtoul( id, &endp, 10 );
   132         if( endp != NULL && *endp == '\0' ) {
   133             /* Accept eg 'dvd://2' as meaning 'the second cd/dvd device */
   134             return cdrom_drive_get_index(index);
   135         }
   137         /* Otherwise it must be a drive identifier, so treat it as if it didn't
   138          * have the url prefix. (fallthrough)
   139          */
   140     }
   142     for( GList *ptr = cdrom_drive_list; ptr != NULL; ptr = ptr->next ) {
   143         cdrom_drive_t drive = (cdrom_drive_t)ptr->data;
   144         if( strcmp(drive->name, id) == 0 )
   145             return drive;
   146     }
   148     return NULL;
   149 }
.