Commit 1b4e3f9e authored by Diego Biurrun's avatar Diego Biurrun

win32: Use proper handle instead of file descriptor to access devices

This also allows simplifying the Windows init code.
parent fca3d72f
...@@ -92,7 +92,7 @@ static int os2_open ( dvdcss_t, const char * ); ...@@ -92,7 +92,7 @@ static int os2_open ( dvdcss_t, const char * );
int dvdcss_use_ioctls( dvdcss_t dvdcss ) int dvdcss_use_ioctls( dvdcss_t dvdcss )
{ {
#if defined( WIN32 ) #if defined( WIN32 )
if( dvdcss->b_file ) if( dvdcss->p_handle )
{ {
return 0; return 0;
} }
...@@ -344,38 +344,32 @@ int dvdcss_open_device ( dvdcss_t dvdcss ) ...@@ -344,38 +344,32 @@ int dvdcss_open_device ( dvdcss_t dvdcss )
print_debug( dvdcss, "opening target `%s'", psz_device ); print_debug( dvdcss, "opening target `%s'", psz_device );
#if defined( WIN32 ) #if defined( WIN32 )
dvdcss->b_file = 1; dvdcss->p_handle = NULL;
/* If device is "X:" or "X:\", we are not actually opening a file. */
if (psz_device[0] && psz_device[1] == ':' &&
(!psz_device[2] || (psz_device[2] == '\\' && !psz_device[3])))
dvdcss->b_file = 0;
/* Initialize readv temporary buffer */
dvdcss->p_readv_buffer = NULL; dvdcss->p_readv_buffer = NULL;
dvdcss->i_readv_buf_size = 0; dvdcss->i_readv_buf_size = 0;
#endif /* defined( WIN32 ) */
if( !dvdcss->b_file ) #if defined( WIN32 ) || defined( __OS2__ )
/* If device is "X:" or "X:\", we are not actually opening a file. */
if( psz_device[0] && psz_device[1] == ':' &&
( !psz_device[2] || ( psz_device[2] == '\\' && !psz_device[3] ) ) )
{ {
#if defined( WIN32 )
print_debug( dvdcss, "using Win2K API for access" ); print_debug( dvdcss, "using Win2K API for access" );
dvdcss->pf_seek = win2k_seek; dvdcss->pf_seek = win2k_seek;
dvdcss->pf_read = win2k_read; dvdcss->pf_read = win2k_read;
dvdcss->pf_readv = win2k_readv; dvdcss->pf_readv = win2k_readv;
return win2k_open( dvdcss, psz_device ); return win2k_open( dvdcss, psz_device );
}
else
#elif defined( __OS2__ ) #elif defined( __OS2__ )
/* If device is "X:" or "X:\", we are not actually opening a file. */
if( psz_device[0] && psz_device[1] == ':' &&
( !psz_device[2] || ( psz_device[2] == '\\' && !psz_device[3] ) ) )
{
print_debug( dvdcss, "using OS/2 API for access" ); print_debug( dvdcss, "using OS/2 API for access" );
dvdcss->pf_seek = libc_seek; dvdcss->pf_seek = libc_seek;
dvdcss->pf_read = libc_read; dvdcss->pf_read = libc_read;
dvdcss->pf_readv = libc_readv; dvdcss->pf_readv = libc_readv;
return os2_open( dvdcss, psz_device ); return os2_open( dvdcss, psz_device );
#endif /* ! ( defined( WIN32 ) || defined( __OS2__ ) ) */
} }
else else
#endif #endif /* defined( WIN32 ) || defined( __OS2__ ) */
{ {
print_debug( dvdcss, "using libc for access" ); print_debug( dvdcss, "using libc for access" );
dvdcss->pf_seek = libc_seek; dvdcss->pf_seek = libc_seek;
...@@ -393,9 +387,9 @@ int dvdcss_close_device ( dvdcss_t dvdcss ) ...@@ -393,9 +387,9 @@ int dvdcss_close_device ( dvdcss_t dvdcss )
dvdcss->p_readv_buffer = NULL; dvdcss->p_readv_buffer = NULL;
dvdcss->i_readv_buf_size = 0; dvdcss->i_readv_buf_size = 0;
if( !dvdcss->b_file ) if( dvdcss->p_handle )
{ {
CloseHandle( (HANDLE) dvdcss->i_fd ); CloseHandle( dvdcss->p_handle );
} }
else else
#endif #endif
...@@ -445,19 +439,17 @@ static int win2k_open ( dvdcss_t dvdcss, const char *psz_device ) ...@@ -445,19 +439,17 @@ static int win2k_open ( dvdcss_t dvdcss, const char *psz_device )
* won't send back the right result). * won't send back the right result).
* (See Microsoft Q241374: Read and Write Access Required for SCSI * (See Microsoft Q241374: Read and Write Access Required for SCSI
* Pass Through Requests) */ * Pass Through Requests) */
dvdcss->i_fd = (int) dvdcss->p_handle = CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE,
CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, NULL, OPEN_EXISTING,
FILE_FLAG_RANDOM_ACCESS, NULL ); FILE_FLAG_RANDOM_ACCESS, NULL );
if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE ) if( dvdcss->p_handle == INVALID_HANDLE_VALUE )
dvdcss->i_fd = (int) dvdcss->p_handle = CreateFile( psz_dvd, GENERIC_READ, FILE_SHARE_READ,
CreateFile( psz_dvd, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, NULL, OPEN_EXISTING,
FILE_FLAG_RANDOM_ACCESS, NULL ); FILE_FLAG_RANDOM_ACCESS, NULL );
if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE ) if( dvdcss->p_handle == INVALID_HANDLE_VALUE )
{ {
print_error( dvdcss, "failed opening device" ); print_error( dvdcss, "failed opening device" );
return -1; return -1;
...@@ -541,8 +533,7 @@ static int win2k_seek( dvdcss_t dvdcss, int i_blocks ) ...@@ -541,8 +533,7 @@ static int win2k_seek( dvdcss_t dvdcss, int i_blocks )
li_seek.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE; li_seek.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE;
li_seek.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd, li_seek.LowPart = SetFilePointer( dvdcss->p_handle, li_seek.LowPart,
li_seek.LowPart,
&li_seek.HighPart, FILE_BEGIN ); &li_seek.HighPart, FILE_BEGIN );
if( (li_seek.LowPart == INVALID_SET_FILE_POINTER) if( (li_seek.LowPart == INVALID_SET_FILE_POINTER)
&& GetLastError() != NO_ERROR) && GetLastError() != NO_ERROR)
...@@ -601,8 +592,7 @@ static int win2k_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) ...@@ -601,8 +592,7 @@ static int win2k_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
{ {
DWORD i_bytes; DWORD i_bytes;
if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer, if( !ReadFile( dvdcss->p_handle, p_buffer,i_blocks * DVDCSS_BLOCK_SIZE,
i_blocks * DVDCSS_BLOCK_SIZE,
&i_bytes, NULL ) ) &i_bytes, NULL ) )
{ {
dvdcss->i_pos = -1; dvdcss->i_pos = -1;
...@@ -725,7 +715,7 @@ static int win2k_readv ( dvdcss_t dvdcss, const struct iovec *p_iovec, ...@@ -725,7 +715,7 @@ static int win2k_readv ( dvdcss_t dvdcss, const struct iovec *p_iovec,
if( i_blocks_total <= 0 ) return 0; if( i_blocks_total <= 0 ) return 0;
if( !ReadFile( (HANDLE)dvdcss->i_fd, dvdcss->p_readv_buffer, if( !ReadFile( dvdcss->p_handle, dvdcss->p_readv_buffer,
i_blocks_total, &i_bytes, NULL ) ) i_blocks_total, &i_bytes, NULL ) )
{ {
/* The read failed... too bad. /* The read failed... too bad.
......
...@@ -26,6 +26,11 @@ ...@@ -26,6 +26,11 @@
#include <limits.h> #include <limits.h>
#ifdef WIN32
# include "config.h"
# include <windows.h>
#endif
#include "dvdcss/dvdcss.h" #include "dvdcss/dvdcss.h"
#include "css.h" #include "css.h"
#include "device.h" #include "device.h"
...@@ -70,7 +75,7 @@ struct dvdcss_s ...@@ -70,7 +75,7 @@ struct dvdcss_s
int b_debug; int b_debug;
#ifdef WIN32 #ifdef WIN32
int b_file; HANDLE p_handle;
char * p_readv_buffer; char * p_readv_buffer;
int i_readv_buf_size; int i_readv_buf_size;
#endif /* WIN32 */ #endif /* WIN32 */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment