📄 win32.c
字号:
/* $Id: win32.c,v 1.3 2006/09/26 22:10:24 dgp85 Exp $ Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//* This file contains Win32-specific code and implements low-level control of the CD drive. Inspired by vlc's cdrom.h code */#ifdef HAVE_CONFIG_H# include "config.h"#endifstatic const char _rcsid[] = "$Id: win32.c,v 1.3 2006/09/26 22:10:24 dgp85 Exp $";#include <cdio/cdio.h>#include <cdio/sector.h>#include <cdio/util.h>#include <cdio/scsi_mmc.h>#include "cdio_assert.h"#include "cdio_private.h" /* protoype for cdio_is_device_win32 */#include <string.h>#ifdef HAVE_WIN32_CDROM#include <ctype.h>#include <stdio.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_ERRNO_H#include <errno.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef HAVE_FCNTL_H#include <fcntl.h>#endif#include <windows.h>#include <winioctl.h>#include "win32.h"#ifdef HAVE_SYS_STAT_H#include <sys/stat.h>#endif#ifdef HAVE_SYS_TYPES_H#include <sys/types.h>#endif#if defined (MSVC) || defined (_XBOX)#undef IN#else#include "aspi32.h"#endif#ifdef _XBOX#include "stdint.h"#include <xtl.h>#define WIN_NT 1#else#define WIN_NT ( GetVersion() < 0x80000000 )#endif/* General ioctl() CD-ROM command function */static bool _cdio_mciSendCommand(int id, UINT msg, DWORD flags, void *arg){#ifdef _XBOX return false;#else MCIERROR mci_error; mci_error = mciSendCommand(id, msg, flags, (DWORD)arg); if ( mci_error ) { char error[256]; mciGetErrorString(mci_error, error, 256); cdio_warn("mciSendCommand() error: %s", error); } return(mci_error == 0);#endif}static access_mode_t str_to_access_mode_win32(const char *psz_access_mode) { const access_mode_t default_access_mode = WIN_NT ? _AM_IOCTL : _AM_ASPI; if (NULL==psz_access_mode) return default_access_mode; if (!strcmp(psz_access_mode, "ioctl")) return _AM_IOCTL; else if (!strcmp(psz_access_mode, "ASPI")) {#ifdef _XBOX return _AM_ASPI;#else cdio_warn ("XBOX doesn't support access type: %s. Default used instead.", psz_access_mode); return default_access_mode;#endif } else { cdio_warn ("unknown access type: %s. Default used instead.", psz_access_mode); return default_access_mode; }}static discmode_tget_discmode_win32(void *p_user_data) { _img_private_t *p_env = p_user_data; if (p_env->hASPI) { return get_discmode_aspi (p_env); } else { return get_discmode_win32ioctl (p_env); }}static const char *is_cdrom_win32(const char drive_letter) { if ( WIN_NT ) { return is_cdrom_win32ioctl (drive_letter); } else { return is_cdrom_aspi(drive_letter); }}/*! Run a SCSI MMC command. env private CD structure i_timeout_ms time in milliseconds we will wait for the command to complete. If this value is -1, use the default time-out value. p_buf Buffer for data, both sending and receiving i_buf Size of buffer e_direction direction the transfer is to go. cdb CDB bytes. All values that are needed should be set on input. We'll figure out what the right CDB length should be. Return 0 if command completed successfully. */static intrun_scsi_cmd_win32( const void *p_user_data, unsigned int i_timeout_ms, unsigned int i_cdb, const scsi_mmc_cdb_t *p_cdb, scsi_mmc_direction_t e_direction, unsigned int i_buf, /*in/out*/ void *p_buf ){ const _img_private_t *p_env = p_user_data; if (p_env->hASPI) { return run_scsi_cmd_aspi( p_env, i_timeout_ms, i_cdb, p_cdb, e_direction, i_buf, p_buf ); } else { return run_scsi_cmd_win32ioctl( p_env, i_timeout_ms, i_cdb, p_cdb, e_direction, i_buf, p_buf ); }}/*! Initialize CD device. */static bool_cdio_init_win32 (void *user_data){ _img_private_t *p_env = user_data; if (p_env->gen.init) { cdio_error ("init called more than once"); return false; } p_env->gen.init = true; p_env->gen.toc_init = false; p_env->gen.b_cdtext_init = false; p_env->gen.b_cdtext_error = false; /* Initializations */ p_env->h_device_handle = NULL; p_env->i_sid = 0; p_env->hASPI = 0; p_env->lpSendCommand = 0; p_env->b_aspi_init = false; p_env->b_ioctl_init = false; if ( _AM_IOCTL == p_env->access_mode ) { return init_win32ioctl(p_env); } else { return init_aspi(p_env); }}/*! Release and free resources associated with cd. */static void_free_win32 (void *user_data){ _img_private_t *p_env = user_data; if (NULL == p_env) return; free (p_env->gen.source_name); if( p_env->h_device_handle ) CloseHandle( p_env->h_device_handle ); if( p_env->hASPI ) FreeLibrary( (HMODULE)p_env->hASPI ); free (p_env);}/*! Reads an audio device into data starting from lsn. Returns 0 if no error. */static int_cdio_read_audio_sectors (void *user_data, void *data, lsn_t lsn, unsigned int nblocks) { _img_private_t *p_env = user_data; if ( p_env->hASPI ) { return read_audio_sectors_aspi( p_env, data, lsn, nblocks ); } else { return read_audio_sectors_win32ioctl( p_env, data, lsn, nblocks ); }}/*! Reads a single mode1 sector from cd device into data starting from lsn. Returns 0 if no error. */static int_cdio_read_mode1_sector (void *user_data, void *data, lsn_t lsn, bool b_form2){ _img_private_t *p_env = user_data; if (p_env->gen.ioctls_debugged == 75) cdio_debug ("only displaying every 75th ioctl from now on"); if (p_env->gen.ioctls_debugged == 30 * 75) cdio_debug ("only displaying every 30*75th ioctl from now on"); if (p_env->gen.ioctls_debugged < 75 || (p_env->gen.ioctls_debugged < (30 * 75) && p_env->gen.ioctls_debugged % 75 == 0) || p_env->gen.ioctls_debugged % (30 * 75) == 0) cdio_debug ("reading %lu", (unsigned long int) lsn); p_env->gen.ioctls_debugged++; if ( p_env->hASPI ) { return read_mode1_sector_aspi( p_env, data, lsn, b_form2 ); } else { return read_mode1_sector_win32ioctl( p_env, data, lsn, b_form2 ); }}/*! Reads nblocks of mode1 sectors from cd device into data starting from lsn. Returns 0 if no error. */static int_cdio_read_mode1_sectors (void *user_data, void *data, lsn_t lsn, bool b_form2, unsigned int nblocks){ _img_private_t *p_env = user_data; int i; int retval; for (i = 0; i < nblocks; i++) { if (b_form2) { if ( (retval = _cdio_read_mode1_sector (p_env, ((char *)data) + (M2RAW_SECTOR_SIZE * i), lsn + i, true)) ) return retval; } else { char buf[M2RAW_SECTOR_SIZE] = { 0, }; if ( (retval = _cdio_read_mode1_sector (p_env, buf, lsn + i, false)) ) return retval; memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i), buf, CDIO_CD_FRAMESIZE); } } return 0;}/*! Reads a single mode2 sector from cd device into data starting from lsn. Returns 0 if no error. */static int_cdio_read_mode2_sector (void *user_data, void *data, lsn_t lsn, bool b_form2){ char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, }; _img_private_t *p_env = user_data; if (p_env->gen.ioctls_debugged == 75) cdio_debug ("only displaying every 75th ioctl from now on"); if (p_env->gen.ioctls_debugged == 30 * 75) cdio_debug ("only displaying every 30*75th ioctl from now on"); if (p_env->gen.ioctls_debugged < 75 || (p_env->gen.ioctls_debugged < (30 * 75) && p_env->gen.ioctls_debugged % 75 == 0) || p_env->gen.ioctls_debugged % (30 * 75) == 0) cdio_debug ("reading %lu", (unsigned long int) lsn); p_env->gen.ioctls_debugged++; if ( p_env->hASPI ) { int ret; ret = read_mode2_sector_aspi(user_data, buf, lsn, 1); if( ret != 0 ) return ret; if (b_form2) memcpy (data, buf, M2RAW_SECTOR_SIZE); else memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE); return 0; } else { return read_mode2_sector_win32ioctl( p_env, data, lsn, b_form2 ); }}/*! Reads nblocks of mode2 sectors from cd device into data starting from lsn. Returns 0 if no error. */static int_cdio_read_mode2_sectors (void *user_data, void *data, lsn_t lsn, bool b_form2, unsigned int nblocks){ int i; int retval; unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE; for (i = 0; i < nblocks; i++) { if ( (retval = _cdio_read_mode2_sector (user_data, ((char *)data) + (blocksize * i), lsn + i, b_form2)) ) return retval; } return 0;}/*! Return the size of the CD in logical block address (LBA) units. */static uint32_t stat_size_win32 (void *user_data){ _img_private_t *p_env = user_data; return p_env->tocent[p_env->gen.i_tracks].start_lsn;}/*! Set the key "arg" to "value" in source device.*/static intset_arg_win32 (void *user_data, const char key[], const char value[]){ _img_private_t *p_env = user_data; if (!strcmp (key, "source")) { if (!value) return -2; free (p_env->gen.source_name); p_env->gen.source_name = strdup (value); } else if (!strcmp (key, "access-mode"))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -