src/mess/osd/winui/softwarelist.c
| r8212 | r8213 | |
| 38 | 38 | typedef struct _file_info file_info; |
| 39 | 39 | struct _file_info |
| 40 | 40 | { |
| 41 | | const device_config *device; |
| 42 | | |
| 43 | | // hash information |
| 44 | | char hash_string[HASH_BUF_SIZE]; |
| 45 | | BOOL hash_realized; |
| 46 | | const hash_info *hashinfo; |
| 47 | | |
| 48 | | const char *zip_entry_name; |
| 49 | 41 | char list_name[256]; |
| 50 | 42 | char description[256]; |
| 51 | 43 | char file_name[256]; |
| r8212 | r8213 | |
| 53 | 45 | char year[10]; |
| 54 | 46 | }; |
| 55 | 47 | |
| 56 | | typedef struct _directory_search_info directory_search_info; |
| 57 | | struct _directory_search_info |
| 58 | | { |
| 59 | | directory_search_info *next; |
| 60 | | HANDLE find_handle; |
| 61 | | WIN32_FIND_DATA fd; |
| 62 | | char directory_name[1]; |
| 63 | | }; |
| 64 | | |
| 65 | 48 | typedef struct _software_list_info software_list_info; |
| 66 | 49 | struct _software_list_info |
| 67 | 50 | { |
| 68 | 51 | WNDPROC old_window_proc; |
| 69 | 52 | file_info **file_index; |
| 70 | 53 | int file_index_length; |
| 71 | | int hashes_realized; |
| 72 | | int current_position; |
| 73 | | directory_search_info *first_search_info; |
| 74 | | directory_search_info *last_search_info; |
| 75 | 54 | const software_config *config; |
| 76 | 55 | }; |
| 77 | 56 | |
| r8212 | r8213 | |
| 107 | 86 | |
| 108 | 87 | |
| 109 | 88 | |
| 110 | | const device_config *SoftwareList_LookupDevice(HWND hwndPicker, int nIndex) |
| 111 | | { |
| 112 | | software_list_info *pPickerInfo; |
| 113 | | pPickerInfo = GetSoftwareListInfo(hwndPicker); |
| 114 | | if ((nIndex < 0) || (nIndex >= pPickerInfo->file_index_length)) |
| 115 | | { |
| 116 | | return NULL; |
| 117 | | } |
| 118 | | return pPickerInfo->file_index[nIndex]->device; |
| 119 | | } |
| 120 | | |
| 121 | | |
| 122 | | |
| 123 | 89 | int SoftwareList_LookupIndex(HWND hwndPicker, LPCSTR pszFilename) |
| 124 | 90 | { |
| 125 | 91 | software_list_info *pPickerInfo; |
| r8212 | r8213 | |
| 138 | 104 | |
| 139 | 105 | iodevice_t SoftwareList_GetImageType(HWND hwndPicker, int nIndex) |
| 140 | 106 | { |
| 141 | | iodevice_t type; |
| 142 | | const device_config *device = SoftwareList_LookupDevice(hwndPicker, nIndex); |
| 143 | | |
| 144 | | if (device != NULL) |
| 145 | | { |
| 146 | | type = image_device_getinfo(GetSoftwareListInfo(hwndPicker)->config->mconfig, device).type; |
| 147 | | } |
| 148 | | else |
| 149 | | { |
| 150 | | type = IO_UNKNOWN; |
| 151 | | } |
| 152 | | return type; |
| 107 | return IO_UNKNOWN; |
| 153 | 108 | } |
| 154 | 109 | |
| 155 | 110 | |
| 156 | 111 | |
| 157 | 112 | void SoftwareList_SetDriver(HWND hwndPicker, const software_config *config) |
| 158 | 113 | { |
| 159 | | int i; |
| 160 | 114 | software_list_info *pPickerInfo; |
| 161 | 115 | |
| 162 | 116 | pPickerInfo = GetSoftwareListInfo(hwndPicker); |
| 163 | 117 | pPickerInfo->config = config; |
| 164 | | |
| 165 | | // invalidate the hash "realization" |
| 166 | | for (i = 0; i < pPickerInfo->file_index_length; i++) |
| 167 | | { |
| 168 | | pPickerInfo->file_index[i]->hashinfo = NULL; |
| 169 | | pPickerInfo->file_index[i]->hash_realized = FALSE; |
| 170 | | } |
| 171 | | pPickerInfo->hashes_realized = 0; |
| 172 | 118 | } |
| 173 | 119 | |
| 174 | 120 | |
| 175 | | |
| 176 | | static void ComputeFileHash(software_list_info *pPickerInfo, |
| 177 | | file_info *pFileInfo, const unsigned char *pBuffer, unsigned int nLength) |
| 178 | | { |
| 179 | | image_device_info info; |
| 180 | | unsigned int functions; |
| 181 | | |
| 182 | | // get the device info |
| 183 | | info = image_device_getinfo(pPickerInfo->config->mconfig, pFileInfo->device); |
| 184 | | |
| 185 | | // determine which functions to use |
| 186 | | functions = hashfile_functions_used(pPickerInfo->config->hashfile, info.type); |
| 187 | | |
| 188 | | // compute the hash |
| 189 | | image_device_compute_hash(pFileInfo->hash_string, pFileInfo->device, pBuffer, nLength, functions); |
| 190 | | } |
| 191 | | |
| 192 | | |
| 193 | | |
| 194 | | static BOOL SoftwareList_CalculateHash(HWND hwndPicker, int nIndex) |
| 195 | | { |
| 196 | | software_list_info *pPickerInfo; |
| 197 | | file_info *pFileInfo; |
| 198 | | LPSTR pszZipName; |
| 199 | | BOOL rc = FALSE; |
| 200 | | unsigned char *pBuffer; |
| 201 | | unsigned int nLength; |
| 202 | | HANDLE hFile, hFileMapping; |
| 203 | | LVFINDINFO lvfi; |
| 204 | | BOOL res; |
| 205 | | |
| 206 | | pPickerInfo = GetSoftwareListInfo(hwndPicker); |
| 207 | | assert((nIndex >= 0) && (nIndex < pPickerInfo->file_index_length)); |
| 208 | | pFileInfo = pPickerInfo->file_index[nIndex]; |
| 209 | | |
| 210 | | if (pFileInfo->zip_entry_name) |
| 211 | | { |
| 212 | | // this is in a ZIP file |
| 213 | | zip_file *zip; |
| 214 | | zip_error ziperr; |
| 215 | | const zip_file_header *zipent; |
| 216 | | |
| 217 | | // open the ZIP file |
| 218 | | nLength = pFileInfo->zip_entry_name - pFileInfo->file_name; |
| 219 | | pszZipName = (LPSTR) alloca(nLength); |
| 220 | | memcpy(pszZipName, pFileInfo->file_name, nLength); |
| 221 | | pszZipName[nLength - 1] = '\0'; |
| 222 | | |
| 223 | | // get the entry name |
| 224 | | ziperr = zip_file_open(pszZipName, &zip); |
| 225 | | if (ziperr == ZIPERR_NONE) |
| 226 | | { |
| 227 | | zipent = zip_file_first_file(zip); |
| 228 | | while(!rc && zipent) |
| 229 | | { |
| 230 | | if (!mame_stricmp(zipent->filename, pFileInfo->zip_entry_name)) |
| 231 | | { |
| 232 | | pBuffer = (unsigned char *)malloc(zipent->uncompressed_length); |
| 233 | | if (pBuffer) |
| 234 | | { |
| 235 | | ziperr = zip_file_decompress(zip, pBuffer, zipent->uncompressed_length); |
| 236 | | if (ziperr == ZIPERR_NONE) |
| 237 | | { |
| 238 | | ComputeFileHash(pPickerInfo, pFileInfo, pBuffer, zipent->uncompressed_length); |
| 239 | | rc = TRUE; |
| 240 | | } |
| 241 | | free(pBuffer); |
| 242 | | } |
| 243 | | } |
| 244 | | zipent = zip_file_next_file(zip); |
| 245 | | } |
| 246 | | zip_file_close(zip); |
| 247 | | } |
| 248 | | } |
| 249 | | else |
| 250 | | { |
| 251 | | // plain open file; map it into memory and calculate the hash |
| 252 | | hFile = win_create_file_utf8(pFileInfo->file_name, GENERIC_READ, FILE_SHARE_READ, NULL, |
| 253 | | OPEN_EXISTING, 0, NULL); |
| 254 | | if (hFile != INVALID_HANDLE_VALUE) |
| 255 | | { |
| 256 | | nLength = GetFileSize(hFile, NULL); |
| 257 | | hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); |
| 258 | | if (hFileMapping) |
| 259 | | { |
| 260 | | pBuffer = (unsigned char *)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0); |
| 261 | | if (pBuffer) |
| 262 | | { |
| 263 | | ComputeFileHash(pPickerInfo, pFileInfo, pBuffer, nLength); |
| 264 | | UnmapViewOfFile(pBuffer); |
| 265 | | rc = TRUE; |
| 266 | | } |
| 267 | | CloseHandle(hFileMapping); |
| 268 | | } |
| 269 | | CloseHandle(hFile); |
| 270 | | } |
| 271 | | } |
| 272 | | |
| 273 | | if (rc) |
| 274 | | { |
| 275 | | memset(&lvfi, 0, sizeof(lvfi)); |
| 276 | | lvfi.flags = LVFI_PARAM; |
| 277 | | lvfi.lParam = nIndex; |
| 278 | | nIndex = ListView_FindItem(hwndPicker, -1, &lvfi); |
| 279 | | if (nIndex > 0) |
| 280 | | res = ListView_RedrawItems(hwndPicker, nIndex, nIndex); |
| 281 | | } |
| 282 | | |
| 283 | | return rc; |
| 284 | | } |
| 285 | | |
| 286 | | |
| 287 | | |
| 288 | | static void SoftwareList_RealizeHash(HWND hwndPicker, int nIndex) |
| 289 | | { |
| 290 | | software_list_info *pPickerInfo; |
| 291 | | file_info *pFileInfo; |
| 292 | | unsigned int nHashFunctionsUsed = 0; |
| 293 | | unsigned int nCalculatedHashes = 0; |
| 294 | | iodevice_t type; |
| 295 | | |
| 296 | | pPickerInfo = GetSoftwareListInfo(hwndPicker); |
| 297 | | assert((nIndex >= 0) && (nIndex < pPickerInfo->file_index_length)); |
| 298 | | pFileInfo = pPickerInfo->file_index[nIndex]; |
| 299 | | |
| 300 | | // Determine which hash functions we need to use for this file, and which hashes |
| 301 | | // have already been calculated |
| 302 | | if ((pPickerInfo->config->hashfile != NULL) && (pFileInfo->device != NULL)) |
| 303 | | { |
| 304 | | image_device_info info = image_device_getinfo(pPickerInfo->config->mconfig, pFileInfo->device); |
| 305 | | type = info.type; |
| 306 | | if (type < IO_COUNT) |
| 307 | | nHashFunctionsUsed = hashfile_functions_used(pPickerInfo->config->hashfile, type); |
| 308 | | nCalculatedHashes = hash_data_used_functions(pFileInfo->hash_string); |
| 309 | | } |
| 310 | | |
| 311 | | // Did we fully compute all hashes? |
| 312 | | if ((nHashFunctionsUsed & ~nCalculatedHashes) == 0) |
| 313 | | { |
| 314 | | // We have calculated all hashs for this file; mark it as realized |
| 315 | | pPickerInfo->file_index[nIndex]->hash_realized = TRUE; |
| 316 | | pPickerInfo->hashes_realized++; |
| 317 | | |
| 318 | | if (pPickerInfo->config->hashfile) |
| 319 | | { |
| 320 | | pPickerInfo->file_index[nIndex]->hashinfo = hashfile_lookup(pPickerInfo->config->hashfile, |
| 321 | | pPickerInfo->file_index[nIndex]->hash_string); |
| 322 | | } |
| 323 | | } |
| 324 | | } |
| 325 | | |
| 326 | | |
| 327 | 121 | BOOL SoftwareList_AddFile(HWND hwndPicker,LPCSTR pszName, LPCSTR pszListname, LPCSTR pszDescription, LPCSTR pszPublisher, LPCSTR pszYear) |
| 328 | 122 | { |
| 329 | 123 | Picker_ResetIdle(hwndPicker); |
| r8212 | r8213 | |
| 332 | 126 | file_info **ppNewIndex; |
| 333 | 127 | file_info *pInfo; |
| 334 | 128 | int nIndex, nSize; |
| 335 | | // LPCSTR pszExtension = NULL; |
| 336 | | const device_config *device = NULL; |
| 337 | 129 | |
| 338 | 130 | // first check to see if it is already here |
| 339 | 131 | if (SoftwareList_LookupIndex(hwndPicker, pszName) >= 0) |
| r8212 | r8213 | |
| 354 | 146 | strcpy(pInfo->description, pszDescription); |
| 355 | 147 | strcpy(pInfo->publisher, pszPublisher); |
| 356 | 148 | strcpy(pInfo->year, pszYear); |
| 357 | | |
| 358 | | // set up device and CRC, if specified |
| 359 | | pInfo->device = device; |
| 360 | 149 | |
| 361 | 150 | ppNewIndex = (file_info**)malloc((pPickerInfo->file_index_length + 1) * sizeof(*pPickerInfo->file_index)); |
| 362 | 151 | memcpy(ppNewIndex,pPickerInfo->file_index,pPickerInfo->file_index_length * sizeof(*pPickerInfo->file_index)); |
| r8212 | r8213 | |
| 368 | 157 | pPickerInfo->file_index = ppNewIndex; |
| 369 | 158 | pPickerInfo->file_index[nIndex] = pInfo; |
| 370 | 159 | |
| 371 | | // Realize the hash |
| 372 | | SoftwareList_RealizeHash(hwndPicker, nIndex); |
| 373 | | |
| 374 | 160 | // Actually insert the item into the picker |
| 375 | 161 | Picker_InsertItemSorted(hwndPicker, nIndex); |
| 376 | 162 | return TRUE; |
| r8212 | r8213 | |
| 382 | 168 | } |
| 383 | 169 | |
| 384 | 170 | |
| 385 | | static void SoftwareList_FreeSearchInfo(directory_search_info *pSearchInfo) |
| 386 | | { |
| 387 | | if (pSearchInfo->find_handle != INVALID_HANDLE_VALUE) |
| 388 | | FindClose(pSearchInfo->find_handle); |
| 389 | | free(pSearchInfo); |
| 390 | | } |
| 391 | | |
| 392 | | |
| 393 | | |
| 394 | 171 | static void SoftwareList_InternalClear(software_list_info *pPickerInfo) |
| 395 | 172 | { |
| 396 | | directory_search_info *p; |
| 397 | 173 | int i; |
| 398 | 174 | |
| 399 | 175 | for (i = 0; i < pPickerInfo->file_index_length; i++) |
| 400 | 176 | free(pPickerInfo->file_index[i]); |
| 401 | 177 | |
| 402 | | while(pPickerInfo->first_search_info) |
| 403 | | { |
| 404 | | p = pPickerInfo->first_search_info->next; |
| 405 | | SoftwareList_FreeSearchInfo(pPickerInfo->first_search_info); |
| 406 | | pPickerInfo->first_search_info = p; |
| 407 | | } |
| 408 | | |
| 409 | 178 | pPickerInfo->file_index = NULL; |
| 410 | 179 | pPickerInfo->file_index_length = 0; |
| 411 | | pPickerInfo->current_position = 0; |
| 412 | | pPickerInfo->last_search_info = NULL; |
| 413 | 180 | } |
| 414 | 181 | |
| 415 | 182 | |
| r8212 | r8213 | |
| 427 | 194 | |
| 428 | 195 | BOOL SoftwareList_Idle(HWND hwndPicker) |
| 429 | 196 | { |
| 430 | | software_list_info *pPickerInfo; |
| 431 | | file_info *pFileInfo; |
| 432 | | static const char szWildcards[] = "\\*.*"; |
| 433 | | directory_search_info *pSearchInfo; |
| 434 | | LPSTR pszFilter; |
| 435 | | BOOL bSuccess; |
| 436 | | int nCount; |
| 437 | | BOOL bDone = FALSE; |
| 438 | | image_device_info info; |
| 439 | | |
| 440 | | pPickerInfo = GetSoftwareListInfo(hwndPicker); |
| 441 | | |
| 442 | | pSearchInfo = pPickerInfo->first_search_info; |
| 443 | | |
| 444 | | if (pSearchInfo) |
| 445 | | { |
| 446 | | // searching through directories |
| 447 | | if (pSearchInfo->find_handle != INVALID_HANDLE_VALUE) |
| 448 | | { |
| 449 | | bSuccess = FindNextFile(pSearchInfo->find_handle, &pSearchInfo->fd); |
| 450 | | } |
| 451 | | else |
| 452 | | { |
| 453 | | pszFilter = (LPSTR)alloca(strlen(pSearchInfo->directory_name) + strlen(szWildcards) + 1); |
| 454 | | strcpy(pszFilter, pSearchInfo->directory_name); |
| 455 | | strcat(pszFilter, szWildcards); |
| 456 | | pSearchInfo->find_handle = win_find_first_file_utf8(pszFilter, &pSearchInfo->fd); |
| 457 | | bSuccess = pSearchInfo->find_handle != INVALID_HANDLE_VALUE; |
| 458 | | } |
| 459 | | |
| 460 | | if (bSuccess) |
| 461 | | { |
| 462 | | //SoftwareList_AddEntry(hwndPicker, pSearchInfo); |
| 463 | | } |
| 464 | | else |
| 465 | | { |
| 466 | | pPickerInfo->first_search_info = pSearchInfo->next; |
| 467 | | if (!pPickerInfo->first_search_info) |
| 468 | | pPickerInfo->last_search_info = NULL; |
| 469 | | SoftwareList_FreeSearchInfo(pSearchInfo); |
| 470 | | } |
| 471 | | } |
| 472 | | else if (pPickerInfo->config!=NULL && pPickerInfo->config->hashfile && (pPickerInfo->hashes_realized |
| 473 | | < pPickerInfo->file_index_length)) |
| 474 | | { |
| 475 | | // time to realize some hashes |
| 476 | | nCount = 100; |
| 477 | | |
| 478 | | while((nCount > 0) && pPickerInfo->file_index[pPickerInfo->current_position]->hash_realized) |
| 479 | | { |
| 480 | | pPickerInfo->current_position++; |
| 481 | | pPickerInfo->current_position %= pPickerInfo->file_index_length; |
| 482 | | nCount--; |
| 483 | | } |
| 484 | | |
| 485 | | pFileInfo = pPickerInfo->file_index[pPickerInfo->current_position]; |
| 486 | | if (!pFileInfo->hash_realized) |
| 487 | | { |
| 488 | | info = image_device_getinfo(pPickerInfo->config->mconfig, pFileInfo->device); |
| 489 | | if (hashfile_functions_used(pPickerInfo->config->hashfile, info.type)) |
| 490 | | { |
| 491 | | // only calculate the hash if it is appropriate for this device |
| 492 | | if (!SoftwareList_CalculateHash(hwndPicker, pPickerInfo->current_position)) |
| 493 | | return FALSE; |
| 494 | | } |
| 495 | | SoftwareList_RealizeHash(hwndPicker, pPickerInfo->current_position); |
| 496 | | |
| 497 | | // under normal circumstances this will be redundant, but in the unlikely |
| 498 | | // event of a failure, we do not want to keep running into a brick wall |
| 499 | | // by calculating this hash over and over |
| 500 | | if (!pPickerInfo->file_index[pPickerInfo->current_position]->hash_realized) |
| 501 | | { |
| 502 | | pPickerInfo->file_index[pPickerInfo->current_position]->hash_realized = TRUE; |
| 503 | | pPickerInfo->hashes_realized++; |
| 504 | | } |
| 505 | | } |
| 506 | | } |
| 507 | | else |
| 508 | | { |
| 509 | | // we are done! |
| 510 | | bDone = TRUE; |
| 511 | | } |
| 512 | | |
| 513 | | return !bDone; |
| 197 | return FALSE; |
| 514 | 198 | } |
| 515 | 199 | |
| 516 | 200 | |
| r8212 | r8213 | |
| 531 | 215 | |
| 532 | 216 | switch(nColumn) |
| 533 | 217 | { |
| 534 | | case MESS_COLUMN_IMAGES: |
| 218 | case 0: |
| 535 | 219 | t_buf = tstring_from_utf8(pFileInfo->file_name); |
| 536 | 220 | if( !t_buf ) |
| 537 | 221 | return s; |