src/mess/filemngr.c
| r2967 | r2968 | |
| 26 | 26 | ***************************************************************************/ |
| 27 | 27 | |
| 28 | 28 | #define ITEMREF_NEW_IMAGE_NAME ((void *) 0x0001) |
| 29 | | #define ITEMREF_EMPTY_SLOT ((void *) 0x0002) |
| 30 | | #define ITEMREF_CREATE ((void *) 0x0003) |
| 31 | 29 | |
| 32 | 30 | |
| 33 | 31 | |
| r2967 | r2968 | |
| 35 | 33 | TYPE DEFINITIONS |
| 36 | 34 | ***************************************************************************/ |
| 37 | 35 | |
| 36 | /* menu item type in the file selector */ |
| 37 | enum _file_selector_entry_type |
| 38 | { |
| 39 | SELECTOR_ENTRY_TYPE_EMPTY, |
| 40 | SELECTOR_ENTRY_TYPE_CREATE, |
| 41 | SELECTOR_ENTRY_TYPE_DRIVE, |
| 42 | SELECTOR_ENTRY_TYPE_DIRECTORY, |
| 43 | SELECTOR_ENTRY_TYPE_FILE |
| 44 | }; |
| 45 | typedef enum _file_selector_entry_type file_selector_entry_type; |
| 46 | |
| 47 | |
| 48 | |
| 49 | /* an entry within the file manager */ |
| 50 | typedef struct _file_selector_entry file_selector_entry; |
| 51 | struct _file_selector_entry |
| 52 | { |
| 53 | file_selector_entry *next; |
| 54 | |
| 55 | file_selector_entry_type type; |
| 56 | const char *basename; |
| 57 | const char *fullpath; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | |
| 38 | 62 | /* state of the file manager */ |
| 39 | 63 | typedef struct _file_manager_menu_state file_manager_menu_state; |
| 40 | 64 | struct _file_manager_menu_state |
| r2967 | r2968 | |
| 51 | 75 | struct _file_selector_menu_state |
| 52 | 76 | { |
| 53 | 77 | file_manager_menu_state *manager_menustate; |
| 78 | file_selector_entry *entrylist; |
| 54 | 79 | }; |
| 55 | 80 | |
| 56 | 81 | |
| r2967 | r2968 | |
| 382 | 407 | ***************************************************************************/ |
| 383 | 408 | |
| 384 | 409 | /*------------------------------------------------- |
| 385 | | alloc_directory_entry - allocates a struct |
| 386 | | of type osd_directory_entry |
| 410 | file_selector_render_extra - perform our |
| 411 | special rendering |
| 387 | 412 | -------------------------------------------------*/ |
| 388 | 413 | |
| 389 | | static osd_directory_entry *alloc_directory_entry(ui_menu *menu, const char *name, osd_dir_entry_type type, UINT64 size) |
| 414 | static void file_selector_render_extra(running_machine *machine, ui_menu *menu, void *state, void *selectedref, float top, float bottom, float origx1, float origy1, float origx2, float origy2) |
| 390 | 415 | { |
| 391 | | char *name_dupe = NULL; |
| 392 | | osd_directory_entry *new_entry; |
| 393 | | size_t name_length; |
| 394 | | |
| 395 | | /* allocate the new entry */ |
| 396 | | name_length = (name != NULL) ? strlen(name) + 1 : 0; |
| 397 | | new_entry = ui_menu_pool_alloc(menu, sizeof(*new_entry) + name_length); |
| 416 | file_selector_menu_state *menustate = (file_selector_menu_state *) state; |
| 398 | 417 | |
| 399 | | /* copy the name, if specified */ |
| 400 | | if (name != NULL) |
| 418 | extra_text_render(machine, menu, state, selectedref, top, bottom, |
| 419 | origx1, origy1, origx2, origy2, |
| 420 | astring_c(menustate->manager_menustate->current_directory), |
| 421 | NULL); |
| 422 | } |
| 423 | |
| 424 | |
| 425 | |
| 426 | /*------------------------------------------------- |
| 427 | compare_file_selector_entries - sorting proc |
| 428 | for file selector entries |
| 429 | -------------------------------------------------*/ |
| 430 | |
| 431 | static int compare_file_selector_entries(const file_selector_entry *e1, const file_selector_entry *e2) |
| 432 | { |
| 433 | int result; |
| 434 | const char *e1_basename = (e1->basename != NULL) ? e1->basename : ""; |
| 435 | const char *e2_basename = (e2->basename != NULL) ? e2->basename : ""; |
| 436 | |
| 437 | if (e1->type < e2->type) |
| 401 | 438 | { |
| 402 | | name_dupe = ((char *) new_entry) + sizeof(*new_entry); |
| 403 | | strcpy(name_dupe, name); |
| 439 | result = -1; |
| 404 | 440 | } |
| 441 | else if (e1->type > e2->type) |
| 442 | { |
| 443 | result = 1; |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | result = mame_stricmp(e1_basename, e2_basename); |
| 448 | if (result == 0) |
| 449 | { |
| 450 | result = strcmp(e1_basename, e2_basename); |
| 451 | if (result == 0) |
| 452 | { |
| 453 | if (e1 < e2) |
| 454 | result = -1; |
| 455 | else if (e1 > e2) |
| 456 | result = 1; |
| 457 | } |
| 458 | } |
| 459 | } |
| 405 | 460 | |
| 406 | | new_entry->name = name_dupe; |
| 407 | | new_entry->type = type; |
| 408 | | new_entry->size = size; |
| 409 | | return new_entry; |
| 461 | return result; |
| 410 | 462 | } |
| 411 | 463 | |
| 412 | 464 | |
| 413 | 465 | |
| 414 | 466 | /*------------------------------------------------- |
| 415 | | file_selector_render_extra - perform our |
| 416 | | special rendering |
| 467 | append_file_selector_entry - appends a new |
| 468 | file selector entry to an entry list |
| 417 | 469 | -------------------------------------------------*/ |
| 418 | 470 | |
| 419 | | static void file_selector_render_extra(running_machine *machine, ui_menu *menu, void *state, void *selectedref, float top, float bottom, float origx1, float origy1, float origx2, float origy2) |
| 471 | static file_selector_entry *append_file_selector_entry(ui_menu *menu, file_selector_menu_state *menustate, |
| 472 | file_selector_entry_type entry_type, const char *entry_basename, const char *entry_fullpath) |
| 420 | 473 | { |
| 421 | | file_selector_menu_state *menustate = (file_selector_menu_state *) state; |
| 474 | file_selector_entry *entry; |
| 475 | file_selector_entry **entryptr; |
| 422 | 476 | |
| 423 | | extra_text_render(machine, menu, state, selectedref, top, bottom, |
| 424 | | origx1, origy1, origx2, origy2, |
| 425 | | astring_c(menustate->manager_menustate->current_directory), |
| 426 | | NULL); |
| 477 | /* allocate a new entry */ |
| 478 | entry = (file_selector_entry *) ui_menu_pool_alloc(menu, sizeof(*entry)); |
| 479 | memset(entry, 0, sizeof(*entry)); |
| 480 | entry->type = entry_type; |
| 481 | entry->basename = (entry_basename != NULL) ? ui_menu_pool_strdup(menu, entry_basename) : entry_basename; |
| 482 | entry->fullpath = (entry_fullpath != NULL) ? ui_menu_pool_strdup(menu, entry_fullpath) : entry_fullpath; |
| 483 | |
| 484 | /* find the end of the list */ |
| 485 | entryptr = &menustate->entrylist; |
| 486 | while ((*entryptr != NULL) && (compare_file_selector_entries(entry, *entryptr) >= 0)) |
| 487 | entryptr = &(*entryptr)->next; |
| 488 | |
| 489 | /* insert the entry */ |
| 490 | entry->next = *entryptr; |
| 491 | *entryptr = entry; |
| 492 | return entry; |
| 427 | 493 | } |
| 428 | 494 | |
| 429 | 495 | |
| 430 | 496 | |
| 431 | 497 | /*------------------------------------------------- |
| 432 | | append_menu_item - appends a single menu item |
| 498 | append_file_selector_entry_menu_item - appends |
| 499 | a menu item for a file selector entry |
| 433 | 500 | -------------------------------------------------*/ |
| 434 | 501 | |
| 435 | | static void append_menu_item(ui_menu *menu, file_selector_menu_state *menustate, |
| 436 | | const char *text, const char *subtext, |
| 437 | | const char *dirent_name, osd_dir_entry_type dirent_type, UINT64 dirent_size, int is_selected) |
| 502 | static file_selector_entry *append_dirent_file_selector_entry(ui_menu *menu, file_selector_menu_state *menustate, |
| 503 | const osd_directory_entry *dirent) |
| 438 | 504 | { |
| 439 | | osd_directory_entry *new_dirent = NULL; |
| 505 | astring *buffer; |
| 506 | file_selector_entry_type entry_type; |
| 507 | file_selector_entry *entry; |
| 440 | 508 | |
| 441 | | if (dirent_size != ~0) |
| 509 | switch(dirent->type) |
| 442 | 510 | { |
| 443 | | new_dirent = alloc_directory_entry(menu, dirent_name, dirent_type, dirent_size); |
| 511 | case ENTTYPE_FILE: |
| 512 | entry_type = SELECTOR_ENTRY_TYPE_FILE; |
| 513 | break; |
| 444 | 514 | |
| 445 | | /* silly hack */ |
| 446 | | if (text == dirent_name) |
| 447 | | text = new_dirent->name; |
| 515 | case ENTTYPE_DIR: |
| 516 | entry_type = SELECTOR_ENTRY_TYPE_DIRECTORY; |
| 517 | break; |
| 518 | |
| 519 | default: |
| 520 | /* exceptional case; do not add a menu item */ |
| 521 | return NULL; |
| 448 | 522 | } |
| 449 | 523 | |
| 450 | | ui_menu_item_append(menu, text, subtext, 0, new_dirent); |
| 524 | /* determine the full path */ |
| 525 | buffer = zippath_combine( |
| 526 | astring_alloc(), |
| 527 | astring_c(menustate->manager_menustate->current_directory), |
| 528 | dirent->name); |
| 451 | 529 | |
| 452 | | if (is_selected) |
| 453 | | ui_menu_set_selection(menu, new_dirent); |
| 530 | /* create the file selector entry */ |
| 531 | entry = append_file_selector_entry( |
| 532 | menu, |
| 533 | menustate, |
| 534 | entry_type, |
| 535 | dirent->name, |
| 536 | astring_c(buffer)); |
| 537 | |
| 538 | astring_free(buffer); |
| 539 | return entry; |
| 454 | 540 | } |
| 455 | 541 | |
| 456 | 542 | |
| 457 | 543 | |
| 458 | 544 | /*------------------------------------------------- |
| 545 | append_file_selector_entry_menu_item - appends |
| 546 | a menu item for a file selector entry |
| 547 | -------------------------------------------------*/ |
| 548 | |
| 549 | static void append_file_selector_entry_menu_item(ui_menu *menu, const file_selector_entry *entry) |
| 550 | { |
| 551 | const char *text = NULL; |
| 552 | const char *subtext = NULL; |
| 553 | |
| 554 | switch(entry->type) |
| 555 | { |
| 556 | case SELECTOR_ENTRY_TYPE_EMPTY: |
| 557 | text = "[empty slot]"; |
| 558 | break; |
| 559 | |
| 560 | case SELECTOR_ENTRY_TYPE_CREATE: |
| 561 | text = "[create]"; |
| 562 | break; |
| 563 | |
| 564 | case SELECTOR_ENTRY_TYPE_DRIVE: |
| 565 | text = entry->basename; |
| 566 | subtext = "[DRIVE]"; |
| 567 | break; |
| 568 | |
| 569 | case SELECTOR_ENTRY_TYPE_DIRECTORY: |
| 570 | text = entry->basename; |
| 571 | subtext = "[DIR]"; |
| 572 | break; |
| 573 | |
| 574 | case SELECTOR_ENTRY_TYPE_FILE: |
| 575 | text = entry->basename; |
| 576 | subtext = "[FILE]"; |
| 577 | break; |
| 578 | } |
| 579 | ui_menu_item_append(menu, text, subtext, 0, (void *) entry); |
| 580 | } |
| 581 | |
| 582 | |
| 583 | |
| 584 | /*------------------------------------------------- |
| 459 | 585 | menu_file_selector_populate - creates and |
| 460 | 586 | allocates all menu items for a directory |
| 461 | 587 | -------------------------------------------------*/ |
| r2967 | r2968 | |
| 465 | 591 | zippath_directory *directory = NULL; |
| 466 | 592 | file_error err = FILERR_NONE; |
| 467 | 593 | const osd_directory_entry *dirent; |
| 468 | | const char *subtext; |
| 594 | const file_selector_entry *entry; |
| 595 | const file_selector_entry *selected_entry = NULL; |
| 469 | 596 | int count, i; |
| 470 | 597 | image_device_info info; |
| 471 | 598 | const device_config *device = menustate->manager_menustate->selected_device; |
| 472 | 599 | const char *path = astring_c(menustate->manager_menustate->current_directory); |
| 473 | | int is_selected; |
| 474 | 600 | |
| 475 | 601 | /* open the directory */ |
| 476 | 602 | err = zippath_opendir(path, &directory); |
| 477 | 603 | if (err != FILERR_NONE) |
| 478 | 604 | goto done; |
| 479 | 605 | |
| 606 | /* clear out the menu entries */ |
| 607 | menustate->entrylist = NULL; |
| 608 | |
| 480 | 609 | /* add the "[empty slot]" entry */ |
| 481 | | ui_menu_item_append(menu, "[empty slot]", NULL, 0, ITEMREF_EMPTY_SLOT); |
| 610 | append_file_selector_entry(menu, menustate, SELECTOR_ENTRY_TYPE_EMPTY, NULL, NULL); |
| 482 | 611 | |
| 483 | 612 | info = image_device_getinfo(device->machine->config, device); |
| 484 | 613 | if (info.creatable && !zippath_is_zip(directory)) |
| 485 | 614 | { |
| 486 | 615 | /* add the "[create]" entry */ |
| 487 | | ui_menu_item_append(menu, "[create]", NULL, 0, ITEMREF_CREATE); |
| 616 | append_file_selector_entry(menu, menustate, SELECTOR_ENTRY_TYPE_CREATE, NULL, NULL); |
| 488 | 617 | } |
| 489 | 618 | |
| 490 | 619 | /* add the drives */ |
| 491 | 620 | count = osd_num_devices(); |
| 492 | 621 | for (i = 0; i < count; i++) |
| 493 | 622 | { |
| 494 | | append_menu_item(menu, |
| 495 | | menustate, |
| 496 | | osd_get_device_name(i), |
| 497 | | "[DRIVE]", |
| 498 | | osd_get_device_name(i), |
| 499 | | ENTTYPE_DIR, |
| 500 | | 0, |
| 501 | | FALSE); |
| 623 | append_file_selector_entry(menu, menustate, SELECTOR_ENTRY_TYPE_DRIVE, |
| 624 | osd_get_device_name(i), osd_get_device_name(i)); |
| 502 | 625 | } |
| 503 | 626 | |
| 504 | 627 | /* build the menu for each item */ |
| 505 | 628 | while((dirent = zippath_readdir(directory)) != NULL) |
| 506 | 629 | { |
| 507 | | /* set the selected item to be the first non-parent directory or file */ |
| 508 | | is_selected = (ui_menu_get_selection(menu) == NULL) && strcmp(dirent->name, ".."); |
| 630 | /* append a dirent entry */ |
| 631 | entry = append_dirent_file_selector_entry(menu, menustate, dirent); |
| 509 | 632 | |
| 510 | | /* choose text for the entry type */ |
| 511 | | switch(dirent->type) |
| 633 | if (entry != NULL) |
| 512 | 634 | { |
| 513 | | case ENTTYPE_FILE: |
| 514 | | subtext = "[FILE]"; |
| 515 | | break; |
| 516 | | case ENTTYPE_DIR: |
| 517 | | subtext = "[DIR]"; |
| 518 | | break; |
| 519 | | default: |
| 520 | | subtext = "[UNK]"; |
| 521 | | break; |
| 635 | /* set the selected item to be the first non-parent directory or file */ |
| 636 | if ((selected_entry == NULL) && strcmp(dirent->name, "..")) |
| 637 | selected_entry = entry; |
| 638 | |
| 639 | /* do we have to select this file? */ |
| 640 | if (!mame_stricmp(astring_c(menustate->manager_menustate->current_file), dirent->name)) |
| 641 | selected_entry = entry; |
| 522 | 642 | } |
| 643 | } |
| 523 | 644 | |
| 524 | | /* do we have to select this file? */ |
| 525 | | if (!mame_stricmp(astring_c(menustate->manager_menustate->current_file), dirent->name)) |
| 526 | | is_selected = TRUE; |
| 645 | /* append all of the menu entries */ |
| 646 | for (entry = menustate->entrylist; entry != NULL; entry = entry->next) |
| 647 | append_file_selector_entry_menu_item(menu, entry); |
| 527 | 648 | |
| 528 | | /* record the menu item */ |
| 529 | | append_menu_item(menu, menustate, dirent->name, subtext, dirent->name, dirent->type, dirent->size, is_selected); |
| 530 | | } |
| 649 | /* set the selection (if we have one) */ |
| 650 | if (selected_entry != NULL) |
| 651 | ui_menu_set_selection(menu, (void *) selected_entry); |
| 531 | 652 | |
| 532 | 653 | /* set up custom render proc */ |
| 533 | 654 | ui_menu_set_custom_render(menu, file_selector_render_extra, ui_get_line_height() + 3.0f * UI_BOX_TB_BORDER, 0); |
| r2967 | r2968 | |
| 559 | 680 | static void menu_file_selector(running_machine *machine, ui_menu *menu, void *parameter, void *state) |
| 560 | 681 | { |
| 561 | 682 | file_error err; |
| 562 | | const osd_directory_entry *dirent; |
| 563 | | int selected; |
| 564 | | astring *new_path; |
| 565 | 683 | const ui_menu_event *event; |
| 566 | 684 | ui_menu *child_menu; |
| 567 | 685 | file_selector_menu_state *menustate; |
| 568 | 686 | file_create_menu_state *child_menustate; |
| 687 | const file_selector_entry *entry; |
| 569 | 688 | |
| 570 | 689 | /* get menu state */ |
| 571 | 690 | menustate = (file_selector_menu_state *) state; |
| r2967 | r2968 | |
| 590 | 709 | /* handle selections */ |
| 591 | 710 | if (event->iptkey == IPT_UI_SELECT) |
| 592 | 711 | { |
| 593 | | if (event->itemref == ITEMREF_EMPTY_SLOT) |
| 712 | entry = (const file_selector_entry *) event->itemref; |
| 713 | switch(entry->type) |
| 594 | 714 | { |
| 595 | | /* empty slot - unload */ |
| 596 | | image_unload(menustate->manager_menustate->selected_device); |
| 597 | | ui_menu_stack_pop(machine); |
| 598 | | } |
| 599 | | else if (event->itemref == ITEMREF_CREATE) |
| 600 | | { |
| 601 | | /* create */ |
| 602 | | child_menu = ui_menu_alloc(machine, menu_file_create, NULL); |
| 603 | | child_menustate = ui_menu_alloc_state(child_menu, sizeof(*child_menustate)); |
| 604 | | child_menustate->manager_menustate = menustate->manager_menustate; |
| 605 | | ui_menu_stack_push(child_menu); |
| 606 | | } |
| 607 | | else |
| 608 | | { |
| 609 | | dirent = (const osd_directory_entry *) event->itemref; |
| 610 | | ui_menu_reset(menu, 0); |
| 715 | case SELECTOR_ENTRY_TYPE_EMPTY: |
| 716 | /* empty slot - unload */ |
| 717 | image_unload(menustate->manager_menustate->selected_device); |
| 718 | ui_menu_stack_pop(machine); |
| 719 | break; |
| 611 | 720 | |
| 612 | | switch(dirent->type) |
| 613 | | { |
| 614 | | case ENTTYPE_DIR: |
| 615 | | /* prepare the selection in the new directory */ |
| 616 | | selected = 1; |
| 617 | | if (!strcmp(dirent->name, "..")) |
| 618 | | zippath_parent_basename(menustate->manager_menustate->current_file, astring_c(menustate->manager_menustate->current_directory)); |
| 721 | case SELECTOR_ENTRY_TYPE_CREATE: |
| 722 | /* create */ |
| 723 | child_menu = ui_menu_alloc(machine, menu_file_create, NULL); |
| 724 | child_menustate = ui_menu_alloc_state(child_menu, sizeof(*child_menustate)); |
| 725 | child_menustate->manager_menustate = menustate->manager_menustate; |
| 726 | ui_menu_stack_push(child_menu); |
| 727 | break; |
| 619 | 728 | |
| 620 | | /* change the directory */ |
| 621 | | new_path = zippath_combine(astring_alloc(), astring_c(menustate->manager_menustate->current_directory), dirent->name); |
| 622 | | err = check_path(astring_c(new_path)); |
| 623 | | if (err != FILERR_NONE) |
| 624 | | { |
| 625 | | /* this path is problematic; present the user with an error and bail */ |
| 626 | | ui_popup_time(1, "Error accessing %s", astring_c(new_path)); |
| 627 | | astring_free(new_path); |
| 628 | | break; |
| 629 | | } |
| 630 | | astring_free(menustate->manager_menustate->current_directory); |
| 631 | | menustate->manager_menustate->current_directory = new_path; |
| 729 | case SELECTOR_ENTRY_TYPE_DRIVE: |
| 730 | case SELECTOR_ENTRY_TYPE_DIRECTORY: |
| 731 | /* drive/directory - first check the path */ |
| 732 | err = check_path(entry->fullpath); |
| 733 | if (err != FILERR_NONE) |
| 734 | { |
| 735 | /* this path is problematic; present the user with an error and bail */ |
| 736 | ui_popup_time(1, "Error accessing %s", entry->fullpath); |
| 632 | 737 | break; |
| 738 | } |
| 739 | astring_cpyc(menustate->manager_menustate->current_directory, entry->fullpath); |
| 740 | ui_menu_reset(menu, 0); |
| 741 | break; |
| 633 | 742 | |
| 634 | | case ENTTYPE_FILE: |
| 635 | | /* specified a file */ |
| 636 | | new_path = zippath_combine(astring_alloc(), astring_c(menustate->manager_menustate->current_directory), dirent->name); |
| 637 | | image_load(menustate->manager_menustate->selected_device, astring_c(new_path)); |
| 638 | | astring_free(new_path); |
| 639 | | ui_menu_stack_pop(machine); |
| 640 | | break; |
| 641 | | |
| 642 | | default: |
| 643 | | /* do nothing */ |
| 644 | | break; |
| 645 | | } |
| 743 | case SELECTOR_ENTRY_TYPE_FILE: |
| 744 | /* file */ |
| 745 | image_load(menustate->manager_menustate->selected_device, entry->fullpath); |
| 746 | ui_menu_stack_pop(machine); |
| 747 | break; |
| 646 | 748 | } |
| 647 | 749 | } |
| 648 | 750 | } |
| r2967 | r2968 | |
| 772 | 874 | menustate->current_file = astring_cpyc(astring_alloc(), |
| 773 | 875 | image_exists(menustate->selected_device) ? image_basename(menustate->selected_device) : ""); |
| 774 | 876 | |
| 877 | /* reset the existing menu */ |
| 878 | ui_menu_reset(menu, 0); |
| 879 | |
| 775 | 880 | /* push the menu */ |
| 776 | 881 | child_menu = ui_menu_alloc(machine, menu_file_selector, NULL); |
| 777 | 882 | child_menustate = ui_menu_alloc_state(child_menu, sizeof(*child_menustate)); |