Previous 15994 Revisions Next

r2968 Sunday 20th July, 2008 at 01:20:40 UTC by Nathan Woods
File manager cleanups
[src/mess]filemngr.c

src/mess/filemngr.c
r2967r2968
2626***************************************************************************/
2727
2828#define ITEMREF_NEW_IMAGE_NAME   ((void *) 0x0001)
29#define ITEMREF_EMPTY_SLOT      ((void *) 0x0002)
30#define ITEMREF_CREATE         ((void *) 0x0003)
3129
3230
3331
r2967r2968
3533    TYPE DEFINITIONS
3634***************************************************************************/
3735
36/* menu item type in the file selector */
37enum _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};
45typedef enum _file_selector_entry_type file_selector_entry_type;
46
47
48
49/* an entry within the file manager */
50typedef struct _file_selector_entry file_selector_entry;
51struct _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
3862/* state of the file manager */
3963typedef struct _file_manager_menu_state file_manager_menu_state;
4064struct _file_manager_menu_state
r2967r2968
5175struct _file_selector_menu_state
5276{
5377   file_manager_menu_state *manager_menustate;
78   file_selector_entry *entrylist;
5479};
5580
5681
r2967r2968
382407***************************************************************************/
383408
384409/*-------------------------------------------------
385    alloc_directory_entry - allocates a struct
386   of type osd_directory_entry
410    file_selector_render_extra - perform our
411    special rendering
387412-------------------------------------------------*/
388413
389static osd_directory_entry *alloc_directory_entry(ui_menu *menu, const char *name, osd_dir_entry_type type, UINT64 size)
414static 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)
390415{
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;
398417
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
431static 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)
401438   {
402      name_dupe = ((char *) new_entry) + sizeof(*new_entry);
403      strcpy(name_dupe, name);
439      result = -1;
404440   }
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   }
405460
406   new_entry->name = name_dupe;
407   new_entry->type = type;
408   new_entry->size = size;
409   return new_entry;
461   return result;
410462}
411463
412464
413465
414466/*-------------------------------------------------
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
417469-------------------------------------------------*/
418470
419static 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)
471static 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)
420473{
421   file_selector_menu_state *menustate = (file_selector_menu_state *) state;
474   file_selector_entry *entry;
475   file_selector_entry **entryptr;
422476
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;
427493}
428494
429495
430496
431497/*-------------------------------------------------
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
433500-------------------------------------------------*/
434501
435static 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)
502static file_selector_entry *append_dirent_file_selector_entry(ui_menu *menu, file_selector_menu_state *menustate,
503   const osd_directory_entry *dirent)
438504{
439   osd_directory_entry *new_dirent = NULL;
505   astring *buffer;
506   file_selector_entry_type entry_type;
507   file_selector_entry *entry;
440508
441   if (dirent_size != ~0)
509   switch(dirent->type)
442510   {
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;
444514
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;
448522   }
449523
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);
451529
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;
454540}
455541
456542
457543
458544/*-------------------------------------------------
545    append_file_selector_entry_menu_item - appends
546   a menu item for a file selector entry
547-------------------------------------------------*/
548
549static 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/*-------------------------------------------------
459585    menu_file_selector_populate - creates and
460586   allocates all menu items for a directory
461587-------------------------------------------------*/
r2967r2968
465591   zippath_directory *directory = NULL;
466592   file_error err = FILERR_NONE;
467593   const osd_directory_entry *dirent;
468   const char *subtext;
594   const file_selector_entry *entry;
595   const file_selector_entry *selected_entry = NULL;
469596   int count, i;
470597   image_device_info info;
471598   const device_config *device = menustate->manager_menustate->selected_device;
472599   const char *path = astring_c(menustate->manager_menustate->current_directory);
473   int is_selected;
474600
475601   /* open the directory */
476602   err = zippath_opendir(path, &directory);
477603   if (err != FILERR_NONE)
478604      goto done;
479605
606   /* clear out the menu entries */
607   menustate->entrylist = NULL;
608
480609   /* 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);
482611
483612   info = image_device_getinfo(device->machine->config, device);
484613   if (info.creatable && !zippath_is_zip(directory))
485614   {
486615      /* 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);
488617   }
489618
490619   /* add the drives */
491620   count = osd_num_devices();
492621   for (i = 0; i < count; i++)
493622   {
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));
502625   }
503626
504627   /* build the menu for each item */
505628   while((dirent = zippath_readdir(directory)) != NULL)
506629   {
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);
509632
510      /* choose text for the entry type */
511      switch(dirent->type)
633      if (entry != NULL)
512634      {
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;
522642      }
643   }
523644
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);
527648
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);
531652
532653   /* set up custom render proc */
533654   ui_menu_set_custom_render(menu, file_selector_render_extra, ui_get_line_height() + 3.0f * UI_BOX_TB_BORDER, 0);
r2967r2968
559680static void menu_file_selector(running_machine *machine, ui_menu *menu, void *parameter, void *state)
560681{
561682   file_error err;
562   const osd_directory_entry *dirent;
563   int selected;
564   astring *new_path;
565683   const ui_menu_event *event;
566684   ui_menu *child_menu;
567685   file_selector_menu_state *menustate;
568686   file_create_menu_state *child_menustate;
687   const file_selector_entry *entry;
569688
570689   /* get menu state */
571690   menustate = (file_selector_menu_state *) state;
r2967r2968
590709      /* handle selections */
591710      if (event->iptkey == IPT_UI_SELECT)
592711      {
593         if (event->itemref == ITEMREF_EMPTY_SLOT)
712         entry = (const file_selector_entry *) event->itemref;
713         switch(entry->type)
594714         {
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;
611720
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;
619728
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);
632737                  break;
738               }
739               astring_cpyc(menustate->manager_menustate->current_directory, entry->fullpath);
740               ui_menu_reset(menu, 0);
741               break;
633742
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;
646748         }
647749      }
648750   }
r2967r2968
772874         menustate->current_file = astring_cpyc(astring_alloc(),
773875            image_exists(menustate->selected_device) ? image_basename(menustate->selected_device) : "");
774876
877         /* reset the existing menu */
878         ui_menu_reset(menu, 0);
879
775880         /* push the menu */
776881         child_menu = ui_menu_alloc(machine, menu_file_selector, NULL);
777882         child_menustate = ui_menu_alloc_state(child_menu, sizeof(*child_menustate));

Previous 15994 Revisions Next


© 1998-2018 The MESS Team