| 1 |
/** |
| 2 |
* @file option.c |
| 3 |
* @brief 複写設定 |
| 4 |
* @author BananaJinn |
| 5 |
* @version $Id: option.c,v 1.25 2010/11/01 14:34:11 bananajinn Exp $ |
| 6 |
* 円盤複写屋 |
| 7 |
* Copyright (C) 2004-2006 BananaJinn<banana@mxh.mesh.ne.jp>. |
| 8 |
*/ |
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
#if !defined(WIN32) |
| 12 |
# include <stdlib.h> |
| 13 |
# include <pwd.h> |
| 14 |
# include <unistd.h> |
| 15 |
# include <sys/types.h> |
| 16 |
#endif /* !WIN32 */ |
| 17 |
#include "mem.h" |
| 18 |
#include "option.h" |
| 19 |
#include "cmd.h" |
| 20 |
#include "ui.h" |
| 21 |
#include "text.h" |
| 22 |
#include "ebstring.h" |
| 23 |
|
| 24 |
static OPTIONS g_Option; |
| 25 |
|
| 26 |
/** |
| 27 |
* 読み取り速度配列を取得 |
| 28 |
* @param[in] drive 装置 |
| 29 |
* @param[out] option オプション構造体 |
| 30 |
* @retval RET_OK 正常終了 |
| 31 |
* @retval RET_NG エラー |
| 32 |
*/ |
| 33 |
static int GetReadableSpeed(CMDDRIVE *drive, OPTIONS *option) |
| 34 |
{ |
| 35 |
BYTE speeds[] = { 52, 48, 32, 24, 20, 16, 12, 10, 8, 6, 4, 2, 1 }; |
| 36 |
|
| 37 |
if(!REALDRIVE(drive)){ |
| 38 |
option->readable_speed = NULL; |
| 39 |
option->num_readable_speed = 0; |
| 40 |
return RET_OK; |
| 41 |
} |
| 42 |
option->readable_speed = (BYTE *)MemNew(sizeof(speeds)); |
| 43 |
if(option->readable_speed == NULL){ |
| 44 |
UIDispMessage(MSG_MEM_ALLOC_ERROR |
| 45 |
/*"メモリ確保に失敗しました。"*/, UIDMT_ERROR); |
| 46 |
return RET_NG; |
| 47 |
} |
| 48 |
|
| 49 |
option->num_readable_speed = sizeof(speeds)/sizeof(BYTE); |
| 50 |
memcpy(option->readable_speed, speeds, sizeof(speeds)); |
| 51 |
return RET_OK; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* 現在の記録速度を取得 |
| 56 |
* @param[in] drive 装置 |
| 57 |
* @param[in] option オプション構造体 |
| 58 |
* @return 速度(n倍速) |
| 59 |
*/ |
| 60 |
static int GetCurrentWriteSpeed(CMDDRIVE *drive, OPTIONS *option) |
| 61 |
{ |
| 62 |
struct _MODEPAGE2A *mp2a; |
| 63 |
WORD speed_kbs; |
| 64 |
WORD len; |
| 65 |
int ret; |
| 66 |
|
| 67 |
ret = SendModeSense(drive, MSPC_CURRENT, 0x2a); |
| 68 |
if(ret != RET_OK){ |
| 69 |
return 0; |
| 70 |
} |
| 71 |
mp2a = (struct _MODEPAGE2A *)(drive->data_buf+ |
| 72 |
(drive->cmd_type==CMDDRVCTYPE_ATAPI ? 8:16)); |
| 73 |
len = Get2bytes(drive->data_buf)-(drive->cmd_type==CMDDRVCTYPE_ATAPI ? 6:14); |
| 74 |
if(len > 28){ |
| 75 |
speed_kbs = Get2bytes(mp2a->v3_cur_write_speed); |
| 76 |
} |
| 77 |
else{ |
| 78 |
speed_kbs = Get2bytes(mp2a->cur_write_speed); |
| 79 |
} |
| 80 |
|
| 81 |
if(option->flags & OPFLG_DVD){ |
| 82 |
return speed_kbs/1385; |
| 83 |
} |
| 84 |
else if(option->flags & OPFLG_BD){ |
| 85 |
return speed_kbs/4495; |
| 86 |
} |
| 87 |
return (int)((speed_kbs+0.5)/176.4); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* 書き込み可能速度配列を取得 |
| 92 |
* @param[in] drive 装置 |
| 93 |
* @param[out] option オプション構造体 |
| 94 |
* @retval RET_OK 正常終了 |
| 95 |
* @retval RET_NG エラー |
| 96 |
*/ |
| 97 |
static int GetWritableSpeed(CMDDRIVE *drive, OPTIONS *option) |
| 98 |
{ |
| 99 |
BYTE speeds[] = { 52, 48, 32, 24, 20, 16, 12, 10, 8, 6, 4, 2, 1 }; |
| 100 |
struct _MODEPAGE2A *mp2a; |
| 101 |
struct _WRITESPEED_PERFOMANCE *wr_speed; |
| 102 |
WORD num_wr_speed_des, index_des; |
| 103 |
WORD speed_kbs; |
| 104 |
WORD len; |
| 105 |
int ret; |
| 106 |
|
| 107 |
if(!REALDRIVE(drive)){ |
| 108 |
option->writable_speed = NULL; |
| 109 |
option->num_writable_speed = 0; |
| 110 |
return RET_OK; |
| 111 |
} |
| 112 |
|
| 113 |
ret = SendModeSense(drive, MSPC_CURRENT, 0x2a); |
| 114 |
if(ret != RET_OK){ |
| 115 |
DispCommandError(drive); |
| 116 |
return ret; |
| 117 |
} |
| 118 |
option->writable_speed = NULL; |
| 119 |
option->num_writable_speed = 0; |
| 120 |
len = Get2bytes(drive->data_buf)-(drive->cmd_type==CMDDRVCTYPE_ATAPI ? 6:14); |
| 121 |
if(len > 30){ |
| 122 |
mp2a = (struct _MODEPAGE2A *)(drive->data_buf+ |
| 123 |
(drive->cmd_type==CMDDRVCTYPE_ATAPI ? 8:16)); |
| 124 |
num_wr_speed_des = Get2bytes(mp2a->num_wr_speed_des); |
| 125 |
if(num_wr_speed_des > 0){ |
| 126 |
option->writable_speed = (BYTE *)MemNew(num_wr_speed_des*sizeof(BYTE)); |
| 127 |
if(option->writable_speed == NULL){ |
| 128 |
UIDispMessage(MSG_MEM_ALLOC_ERROR |
| 129 |
/*"メモリ確保に失敗しました。"*/, UIDMT_ERROR); |
| 130 |
return RET_NG; |
| 131 |
} |
| 132 |
wr_speed = mp2a->wr_speed_des; |
| 133 |
for(index_des=0; index_des<num_wr_speed_des; index_des++){ |
| 134 |
speed_kbs = Get2bytes(wr_speed[index_des].wr_speed_supp); |
| 135 |
if(option->flags & OPFLG_DVD){ |
| 136 |
option->writable_speed[index_des] = (BYTE)(speed_kbs/1385); |
| 137 |
} |
| 138 |
else if(option->flags & OPFLG_BD){ |
| 139 |
option->writable_speed[index_des] = (BYTE)(speed_kbs/4495); |
| 140 |
} |
| 141 |
else{ |
| 142 |
option->writable_speed[index_des] = (BYTE)((speed_kbs+0.5)/176.4); |
| 143 |
} |
| 144 |
} |
| 145 |
option->num_writable_speed = num_wr_speed_des; |
| 146 |
} |
| 147 |
} |
| 148 |
if(option->num_writable_speed==0){ |
| 149 |
/* サポート速度取得できなかった場合 */ |
| 150 |
option->writable_speed = (BYTE *)MemNew(sizeof(speeds)); |
| 151 |
if(option->writable_speed == NULL){ |
| 152 |
UIDispMessage(MSG_MEM_ALLOC_ERROR |
| 153 |
/*"メモリ確保に失敗しました。"*/, UIDMT_ERROR); |
| 154 |
return RET_NG; |
| 155 |
} |
| 156 |
for(index_des=0; index_des<sizeof(speeds)/sizeof(BYTE); index_des++){ |
| 157 |
/* 速度設定してみる */ |
| 158 |
ret = SetSpeed(drive, 0, speeds[index_des]); |
| 159 |
if(ret == RET_OK){ |
| 160 |
if(GetCurrentWriteSpeed(drive, option) == speeds[index_des]){ |
| 161 |
/* 設定可能 */ |
| 162 |
option->writable_speed[option->num_writable_speed] = speeds[index_des]; |
| 163 |
option->num_writable_speed++; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
if(option->num_writable_speed == 0){ |
| 168 |
option->num_writable_speed = sizeof(speeds)/sizeof(BYTE); |
| 169 |
memcpy(option->writable_speed, speeds, sizeof(speeds)); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return RET_OK; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* 1トラックのISOイメージかどうかを確認する |
| 178 |
* @param[in] drive 装置 |
| 179 |
* @retval TRUE 1トラックISOの場合 |
| 180 |
* @retval FALSE そうではない場合 |
| 181 |
*/ |
| 182 |
static BOOL Check1TrackISO(CMDDRIVE *drive) |
| 183 |
{ |
| 184 |
struct _DISCINFO *di; |
| 185 |
int ret; |
| 186 |
WORD track_num; |
| 187 |
|
| 188 |
if(!REALDRIVE(drive)){ |
| 189 |
return drive->type == CMDDRVTYPE_ISO || |
| 190 |
drive->type == CMDDRVTYPE_MKISOFS; |
| 191 |
} |
| 192 |
|
| 193 |
if(DT_DVD_FAMILY(drive->disc_type)){ |
| 194 |
/* CDのみ対応 */ |
| 195 |
return FALSE; |
| 196 |
} |
| 197 |
|
| 198 |
ret = SendReadDiscInfo(drive); |
| 199 |
if(ret != RET_OK){ |
| 200 |
return FALSE; |
| 201 |
} |
| 202 |
di = (struct _DISCINFO *)drive->data_buf; |
| 203 |
track_num = (WORD)di->last_track_ls_msb<<8 | di->last_track_ls_lsb; |
| 204 |
if(track_num != 1){ |
| 205 |
return FALSE; |
| 206 |
} |
| 207 |
|
| 208 |
ret = SendRead10(drive, 0x10, 1, 2048); |
| 209 |
if(ret != RET_OK){ |
| 210 |
return FALSE; |
| 211 |
} |
| 212 |
if(strncmp((char *)(drive->data_buf+1), "CD001", 5) != 0){ |
| 213 |
return FALSE; |
| 214 |
} |
| 215 |
|
| 216 |
return TRUE; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
#if defined(WIN32) |
| 221 |
|
| 222 |
void GetRegString(const char *key, char *string_ret, DWORD size) |
| 223 |
{ |
| 224 |
LONG lRet; |
| 225 |
HKEY hKey; |
| 226 |
DWORD dwCount; |
| 227 |
DWORD dwType; |
| 228 |
|
| 229 |
if(size==0) |
| 230 |
return; |
| 231 |
if(string_ret==NULL) |
| 232 |
return; |
| 233 |
if(key==NULL) |
| 234 |
return; |
| 235 |
|
| 236 |
lRet = RegCreateKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\EnbanFukusyaYa", 0, NULL, |
| 237 |
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); |
| 238 |
if(lRet!=ERROR_SUCCESS) |
| 239 |
return; |
| 240 |
dwCount = size; |
| 241 |
dwType = REG_SZ; |
| 242 |
lRet = RegQueryValueEx(hKey, key, NULL, &dwType, string_ret, &dwCount); |
| 243 |
if(lRet!=ERROR_SUCCESS){ |
| 244 |
RegCloseKey(hKey); |
| 245 |
return; |
| 246 |
} |
| 247 |
RegCloseKey(hKey); |
| 248 |
string_ret[size-1]='\0'; |
| 249 |
} |
| 250 |
|
| 251 |
void WriteRegString(const char *key, const char *string) |
| 252 |
{ |
| 253 |
LONG lRet; |
| 254 |
HKEY hKey; |
| 255 |
|
| 256 |
if(key==NULL) |
| 257 |
return; |
| 258 |
if(string==NULL) |
| 259 |
string=""; |
| 260 |
lRet = RegCreateKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\EnbanFukusyaYa", 0, NULL, |
| 261 |
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); |
| 262 |
if(lRet!=ERROR_SUCCESS) |
| 263 |
return; |
| 264 |
lRet = RegSetValueEx(hKey, key, 0, REG_SZ, string, strlen(string)); |
| 265 |
if(lRet!=ERROR_SUCCESS){ |
| 266 |
RegCloseKey(hKey); |
| 267 |
return; |
| 268 |
} |
| 269 |
RegCloseKey(hKey); |
| 270 |
} |
| 271 |
|
| 272 |
static void LoadOption() |
| 273 |
{ |
| 274 |
char buf[32]; |
| 275 |
GetRegString("imagedir", g_Option.temppath, _MAX_PATH); |
| 276 |
GetRegString("readspeed", buf, sizeof(buf)); |
| 277 |
g_Option.read_speed = atoi(buf); |
| 278 |
GetRegString("writespeed", buf, sizeof(buf)); |
| 279 |
g_Option.write_speed = atoi(buf); |
| 280 |
} |
| 281 |
|
| 282 |
static void SaveOption() |
| 283 |
{ |
| 284 |
char buf[32]; |
| 285 |
WriteRegString("imagedir", g_Option.temppath); |
| 286 |
sprintf(buf, "%d", g_Option.read_speed); |
| 287 |
WriteRegString("readspeed", buf); |
| 288 |
sprintf(buf, "%d", g_Option.write_speed); |
| 289 |
WriteRegString("writespeed", buf); |
| 290 |
} |
| 291 |
#else /* WIN32 */ |
| 292 |
|
| 293 |
#define OPTFNAME ".enbanrc" |
| 294 |
static char *GetOptionFilename() |
| 295 |
{ |
| 296 |
char *fname; |
| 297 |
struct passwd *pwent; |
| 298 |
|
| 299 |
pwent = getpwuid(getuid()); |
| 300 |
fname = EbStringNewWithFormat("%s/%s", pwent->pw_dir, OPTFNAME); |
| 301 |
return fname; |
| 302 |
} |
| 303 |
|
| 304 |
static void DeleteSpaces(char *buf) |
| 305 |
{ |
| 306 |
int qflag=0; |
| 307 |
|
| 308 |
while(*buf){ |
| 309 |
if(*buf>0 && *buf<=0x20 && !qflag){ |
| 310 |
memmove(buf, buf+1, strlen(buf)); |
| 311 |
} |
| 312 |
else if(*buf==0x22){ /* " */ |
| 313 |
qflag ^= 1; |
| 314 |
buf++; |
| 315 |
} |
| 316 |
else{ |
| 317 |
buf++; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
static void DeleteQuotation(char *buf) |
| 323 |
{ |
| 324 |
if(*buf=='\"'){ |
| 325 |
if(buf[strlen(buf)-1]=='\"'){ |
| 326 |
memmove(buf, buf+1, strlen(buf)); |
| 327 |
buf[strlen(buf)-1]='\0'; |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
static void LoadOption() |
| 334 |
{ |
| 335 |
char *fname; |
| 336 |
char buf[80]; |
| 337 |
FILE *fp; |
| 338 |
|
| 339 |
fname = GetOptionFilename(); |
| 340 |
if(fname==NULL) |
| 341 |
return; |
| 342 |
fp = fopen(fname, "r"); |
| 343 |
if(fp==NULL){ |
| 344 |
EbStringFree(fname); |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
while(fgets(buf, sizeof(buf), fp)){ |
| 349 |
if(buf[0]=='\0') |
| 350 |
continue; |
| 351 |
if(buf[0]=='#') |
| 352 |
continue; |
| 353 |
DeleteSpaces(buf); |
| 354 |
if(!strncmp(buf, "imagedir=", 9)){ |
| 355 |
DeleteQuotation(buf+9); |
| 356 |
strncpy(g_Option.temppath, buf+9, _MAX_PATH); |
| 357 |
g_Option.temppath[_MAX_PATH] = '\0'; |
| 358 |
} |
| 359 |
else if(!strncmp(buf, "readspeed=", 10)){ |
| 360 |
DeleteQuotation(buf+10); |
| 361 |
g_Option.read_speed = atoi(buf+10); |
| 362 |
} |
| 363 |
else if(!strncmp(buf, "writespeed=", 11)){ |
| 364 |
DeleteQuotation(buf+11); |
| 365 |
g_Option.write_speed = atoi(buf+11); |
| 366 |
} |
| 367 |
} |
| 368 |
fclose(fp); |
| 369 |
|
| 370 |
EbStringFree(fname); |
| 371 |
} |
| 372 |
|
| 373 |
static void SaveOption() |
| 374 |
{ |
| 375 |
char *fname; |
| 376 |
FILE *fp; |
| 377 |
|
| 378 |
fname = GetOptionFilename(); |
| 379 |
if(fname==NULL) |
| 380 |
return; |
| 381 |
fp = fopen(fname, "w"); |
| 382 |
if(fp==NULL){ |
| 383 |
MemFree(fname); |
| 384 |
return; |
| 385 |
} |
| 386 |
|
| 387 |
fprintf(fp, "imagedir=\"%s\"\n", g_Option.temppath); |
| 388 |
fprintf(fp, "readspeed=%d\n", g_Option.read_speed); |
| 389 |
fprintf(fp, "writespeed=%d\n", g_Option.write_speed); |
| 390 |
fclose(fp); |
| 391 |
|
| 392 |
EbStringFree(fname); |
| 393 |
} |
| 394 |
#endif /* WIN32 */ |
| 395 |
|
| 396 |
|
| 397 |
int SetOption(CMDDRIVE *reader, CMDDRIVE *writer, int disc_type) |
| 398 |
{ |
| 399 |
int ret; |
| 400 |
struct _MODEPAGE05 *mp05; |
| 401 |
|
| 402 |
memset(&g_Option, 0, sizeof(OPTIONS)); |
| 403 |
|
| 404 |
/* 一時ファイルを作るディレクトリを決める */ |
| 405 |
#ifdef WIN32 |
| 406 |
if(GetTempPath(sizeof(g_Option.temppath), g_Option.temppath)==0){ |
| 407 |
g_Option.temppath[0] = '\0'; |
| 408 |
} |
| 409 |
#else |
| 410 |
strcpy(g_Option.temppath, "/tmp"); |
| 411 |
#endif |
| 412 |
g_Option.flags |= OPFLG_TEMPPATH; |
| 413 |
|
| 414 |
if(writer->disc_type==DT_DVDPR || writer->disc_type==DT_DVDPRW || |
| 415 |
writer->disc_type==DT_DVDPRDL || writer->disc_type==DT_DVDPRWDL){ |
| 416 |
/* WriteParametersPage 設定が無いので */ |
| 417 |
g_Option.bufe = TRUE; |
| 418 |
g_Option.test_write = FALSE; |
| 419 |
} |
| 420 |
else{ |
| 421 |
g_Option.bufe = FALSE; |
| 422 |
g_Option.test_write = FALSE; |
| 423 |
if(writer!=NULL){ |
| 424 |
if((writer->type == CMDDRVTYPE_DRIVE) || (writer->type == CMDDRVTYPE_NET)){ |
| 425 |
ret = SendModeSense(writer, MSPC_CHANGEABLE, 5); |
| 426 |
if(ret!=RET_OK){ |
| 427 |
DispCommandError(writer); |
| 428 |
return ret; |
| 429 |
} |
| 430 |
mp05 = (struct _MODEPAGE05 *) |
| 431 |
(writer->data_buf + (writer->cmd_type==CMDDRVCTYPE_ATAPI ? 8:16)); |
| 432 |
if(mp05->BUFE){ |
| 433 |
g_Option.bufe = TRUE; |
| 434 |
g_Option.flags |= OPFLG_BUFE; |
| 435 |
} |
| 436 |
if(mp05->test_write){ |
| 437 |
g_Option.test_write = FALSE; |
| 438 |
g_Option.flags |= OPFLG_TESTWRITE; |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
if(DT_DVD_FAMILY(disc_type)){ |
| 445 |
g_Option.flags |= OPFLG_DVD; |
| 446 |
if(writer->disc_type!=DT_DVDPR && writer->disc_type!=DT_DVDPRW && |
| 447 |
writer->disc_type!=DT_DVDPRDL && writer->disc_type!=DT_DVDPRWDL){ |
| 448 |
g_Option.dao = TRUE; |
| 449 |
g_Option.flags |= OPFLG_DAO; |
| 450 |
} |
| 451 |
} |
| 452 |
else if(DT_BD_FAMILY(disc_type)){ |
| 453 |
g_Option.flags |= OPFLG_BD; |
| 454 |
if(writer->disc_type!=DT_BDRE){ |
| 455 |
g_Option.dao = TRUE; |
| 456 |
g_Option.flags |= OPFLG_DAO; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
if(reader!=NULL && writer!=NULL){ |
| 461 |
if(reader->type==CMDDRVTYPE_DRIVE && writer->type==CMDDRVTYPE_DRIVE){ |
| 462 |
if(reader->u.drive.hid != writer->u.drive.hid || |
| 463 |
reader->u.drive.tid != writer->u.drive.tid){ |
| 464 |
g_Option.on_the_fly = TRUE; |
| 465 |
g_Option.flags |= OPFLG_ONTHEFLY; |
| 466 |
} |
| 467 |
} |
| 468 |
else{ |
| 469 |
g_Option.on_the_fly = TRUE; |
| 470 |
g_Option.flags |= OPFLG_ONTHEFLY; |
| 471 |
g_Option.flags &= ~OPFLG_DAO; |
| 472 |
} |
| 473 |
} |
| 474 |
else{ |
| 475 |
g_Option.on_the_fly = TRUE; |
| 476 |
} |
| 477 |
|
| 478 |
ret = GetReadableSpeed(reader, &g_Option); |
| 479 |
if(ret != RET_OK){ |
| 480 |
return ret; |
| 481 |
} |
| 482 |
|
| 483 |
ret = GetWritableSpeed(writer, &g_Option); |
| 484 |
if(ret != RET_OK){ |
| 485 |
MemFree(g_Option.readable_speed); |
| 486 |
g_Option.readable_speed=NULL; |
| 487 |
g_Option.num_readable_speed=0; |
| 488 |
return ret; |
| 489 |
} |
| 490 |
|
| 491 |
if(Check1TrackISO(reader)){ |
| 492 |
g_Option.flags |= OPFLG_OUTSIDE; |
| 493 |
} |
| 494 |
|
| 495 |
LoadOption(); |
| 496 |
ret = UISetting(&g_Option); |
| 497 |
SaveOption(); |
| 498 |
|
| 499 |
MemFree(g_Option.readable_speed); |
| 500 |
g_Option.readable_speed=NULL; |
| 501 |
g_Option.num_readable_speed=0; |
| 502 |
MemFree(g_Option.writable_speed); |
| 503 |
g_Option.writable_speed=NULL; |
| 504 |
g_Option.num_writable_speed=0; |
| 505 |
return ret; |
| 506 |
} |
| 507 |
|
| 508 |
OPTIONS *GetOption() |
| 509 |
{ |
| 510 |
return &g_Option; |
| 511 |
} |
| 512 |
|
| 513 |
|