Search
lxdream.org :: lxdream/src/drivers/cdrom/sector.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/sector.h
changeset 1107:7b279d10f46f
prev1097:d4807997e450
next1296:30ecee61f811
author nkeynes
date Tue Feb 28 18:22:52 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Add a GL-only video driver for android usage (since the Java code is
responsible for creating the context)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * low-level 'block device' for input to cdrom discs.
     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 #ifndef cdrom_sector_H
    20 #define cdrom_sector_H 1
    22 #include <stdio.h>
    23 #include <glib/gtypes.h>
    24 #include "drivers/cdrom/defs.h"
    26 #ifdef __cplusplus
    27 extern "C" {
    28 #endif
    30 /******************************** Sector Source ******************************/
    31 #define SECTOR_SOURCE_TAG 0x42444556
    32 typedef struct sector_source *sector_source_t;
    33 typedef enum {
    34     NULL_SECTOR_SOURCE,
    35     FILE_SECTOR_SOURCE,
    36     MEM_SECTOR_SOURCE,
    37     DISC_SECTOR_SOURCE,
    38     TRACK_SECTOR_SOURCE
    39 } sector_source_type_t;
    41 typedef cdrom_error_t (*sector_source_read_fn_t)(sector_source_t, cdrom_lba_t, cdrom_count_t, unsigned char *outbuf);
    42 typedef cdrom_error_t (*sector_source_read_sectors_fn_t)(sector_source_t, cdrom_lba_t, cdrom_count_t, cdrom_read_mode_t mode,
    43         unsigned char *outbuf, size_t *length);
    44 typedef void (*sector_source_destroy_fn_t)(sector_source_t);
    46 /**
    47  * A 'sector source' is a read-only random-access data source that supports
    48  * reads of arbitrary blocks within their capacity. A block device has a
    49  * defined mode, block size, and block count.
    50  *
    51  * Source are ref-counted, and automatically destroyed when the reference
    52  * count reaches 0.
    53  */
    54 struct sector_source {
    55     uint32_t tag;       /* sector source tag */
    56     uint32_t ref_count; /* Reference count. Initialized to 0 */
    57     sector_source_type_t type;
    59     sector_mode_t mode; /* Implies sector size. */
    60     cdrom_count_t size; /* Block count */
    62     /**
    63      * Read blocks from the device using the native block size.
    64      * @param buf Buffer to receive the blocks
    65      * @param block First block to transfer (numbered from 0)
    66      * @param block_count number of blocks to transfer.
    67      * @return 0 on success, otherwise an error code.
    68      */
    69     sector_source_read_fn_t read_blocks;
    71     /**
    72      * Read sectors from the device using the specified read mode, performing any
    73      * necessary conversions.
    74      */
    75     sector_source_read_sectors_fn_t read_sectors;
    77     /**
    78      * Release all resources and memory used by the device (note should never
    79      * be called directly
    80      */
    81     sector_source_destroy_fn_t destroy;
    83 };
    85 /**
    86  * Block device that always returns zeros.
    87  */
    88 sector_source_t null_sector_source_new( sector_mode_t mode, cdrom_count_t size );
    90 #define FILE_SECTOR_FULL_FILE ((cdrom_count_t)-1)
    92 /**
    93  * File reader. Last block is 0-padded.
    94  */
    95 sector_source_t file_sector_source_new_filename( const gchar *filename, sector_mode_t mode,
    96                                                  uint32_t offset, cdrom_count_t sector_count );
    97 sector_source_t file_sector_source_new( FILE *f, sector_mode_t mode, uint32_t offset, cdrom_count_t sector_count,
    98                                                 gboolean closeOnDestroy );
    99 sector_source_t file_sector_source_new_full( FILE *f, sector_mode_t mode, gboolean closeOnDestroy );
   101 /**
   102  * Temp-file creator - initially empty. Creates a file in the system temp dir,
   103  * unlinked on destruction or program exit.
   104  */
   105 sector_source_t tmpfile_sector_source_new(  sector_mode_t mode );
   107 /**
   108  * Construct a file source that shares its file descriptor with another
   109  * file source.
   110  */
   111 sector_source_t file_sector_source_new_source( sector_source_t ref, sector_mode_t mode, uint32_t offset,
   112                                                cdrom_count_t sector_count );
   114 /**
   115  * Change the value of the source's closeOnDestroy flag
   116  */
   117 void file_sector_source_set_close_on_destroy( sector_source_t ref, gboolean closeOnDestroy );
   119 /**
   120  * Retrieve the source's underlying FILE
   121  */
   122 FILE *file_sector_source_get_file( sector_source_t ref );
   124 /**
   125  * Retrieve the source's underlying file descriptor
   126  */
   127 int file_sector_source_get_fd( sector_source_t ref );
   129 /** Construct a memory source with the given mode and size */
   130 sector_source_t mem_sector_source_new( sector_mode_t mode, cdrom_count_t size );
   132 /**
   133  * Construct a memory source using the supplied buffer for data.
   134  * @param buffer The buffer to read from, which must be at least size * sector_size in length
   135  * @param mode The sector mode of the data in the buffer, which cannot be SECTOR_UNKNOWN
   136  * @param size Number of sectors in the buffer
   137  * @param freeOnDestroy If true, the source owns the buffer and will release it when the
   138  *   source is destroyed.
   139  */
   140 sector_source_t mem_sector_source_new_buffer( unsigned char *buffer, sector_mode_t mode, cdrom_count_t size,
   141                                        gboolean freeOnDestroy );
   143 /**
   144  * Retrieve the underlying buffer for a memory source
   145  */
   146 unsigned char *mem_sector_source_get_buffer( sector_source_t source );
   148 /**
   149  * Increment the reference count for a block device.
   150  */
   151 void sector_source_ref( sector_source_t device );
   153 /**
   154  * Unreference a block device. If decremented to 0, the device will be
   155  * destroyed.
   156  */
   157 void sector_source_unref( sector_source_t device );
   159 /**
   160  * Release an unbound block device. If the ref count is 0, the device is
   161  * destroyed. Otherwise the function has no effect.
   162  */
   163 void sector_source_release( sector_source_t device );
   165 cdrom_error_t sector_source_read( sector_source_t device, cdrom_lba_t lba, cdrom_count_t block_count, unsigned char *buf );
   167 cdrom_error_t sector_source_read_sectors( sector_source_t device, cdrom_lba_t lba, cdrom_count_t block_count,
   168                                           cdrom_read_mode_t mode, unsigned char *buf, size_t *length );
   170 /***** Internals for sector source implementations *****/
   172 /**
   173  * Initialize a new (pre-allocated) sector source
   174  */
   175 sector_source_t sector_source_init( sector_source_t device, sector_source_type_t type, sector_mode_t mode, cdrom_count_t size,
   176                         sector_source_read_fn_t readfn, sector_source_destroy_fn_t destroyfn );
   178 /**
   179  * Default sector source destructor method
   180  */
   181 void default_sector_source_destroy( sector_source_t device );
   184 /**
   185  * Extract the necessary fields from a single raw sector for the given read mode.
   186  * @param raw_sector input raw 2352 byte sector
   187  * @param mode sector mode and field specification flags
   188  * @param buf output buffer for sector data
   189  * @param length output length of sector written to buf
   190  * @return CDROM_ERROR_OK on success, otherwise an appropriate error code.
   191  */
   192 cdrom_error_t sector_extract_from_raw( unsigned char *raw_sector, cdrom_read_mode_t mode, unsigned char *buf, size_t *length );
   194 /**
   195  * Test if the given pointer is a valid sector source
   196  */
   197 #define IS_SECTOR_SOURCE(dev) ((dev) != NULL && ((sector_source_t)(dev))->tag == SECTOR_SOURCE_TAG)
   199  /**
   200   * Test if the given pointer is a valid sector source of the given type
   201   */
   202 #define IS_SECTOR_SOURCE_TYPE(dev,id) (IS_SECTOR_SOURCE(dev) && ((sector_source_t)(dev))->type == id)
   206 #ifdef __cplusplus
   207 }
   208 #endif
   210 #endif /* !cdrom_sector_H */
.