Previous 15994 Revisions Next

r2874 Friday 20th June, 2008 at 11:30:45 UTC by Nathan Woods
File manager rewrite
[docs]messnew.txt
[src/emu]uimenu.c
[src/mess]filemngr.c messcore.mak osdmess.h zippath.c* zippath.h*
[src/mess/osd/windows]winmess.c

src/emu/uimenu.c
r2873r2874
815815   ADD_MENU("Image Information", ui_menu_image_info, 0);
816816
817817     /* add image info menu */
818   ADD_MENU("File Manager", menu_file_manager, 1);
818   ADD_MENU("File Manager", menu_file_manager, 0);
819819
820820#if HAS_WAVE
821821     /* add tape control menu */
src/mess/messcore.mak
r2873r2874
9494   $(MESSOBJ)/opresolv.o   \
9595   $(MESSOBJ)/infomess.o   \
9696   $(MESSOBJ)/climess.o   \
97   $(MESSOBJ)/zippath.o   \
9798
9899$(LIBEMU): $(EMUOBJS)
src/mess/zippath.c
r0r2874
1/***************************************************************************
2
3    zippath.c
4
5    File/directory/path operations that work with ZIP files
6
7***************************************************************************/
8
9#include <ctype.h>
10
11#include "zippath.h"
12#include "unzip.h"
13#include "osdmess.h"
14
15
16/***************************************************************************
17    TYPE DEFINITIONS
18***************************************************************************/
19
20typedef struct _zippath_returned_directory zippath_returned_directory;
21struct _zippath_returned_directory
22{
23   zippath_returned_directory *next;
24   char name[1];
25};
26
27
28
29struct _zippath_directory
30{
31   /* common */
32   unsigned int returned_parent : 1;
33   osd_directory_entry returned_entry;
34
35   /* specific to normal directories */
36   osd_directory *directory;
37
38   /* specific to ZIP directories */
39   unsigned int called_zip_first : 1;
40   zip_file *zipfile;
41   astring *zipprefix;
42   zippath_returned_directory *returned_dirlist;
43};
44
45
46
47/***************************************************************************
48    PATH OPERATIONS
49***************************************************************************/
50
51/*-------------------------------------------------
52    parse_parent_path - parses out the parent path
53-------------------------------------------------*/
54
55static void parse_parent_path(const char *path, int *beginpos, int *endpos)
56{
57   int length = strlen(path);
58   int pos;
59
60   /* skip over trailing path separators */
61   pos = length - 1;
62   while((pos > 0) && osd_is_path_separator(path[pos]))
63      pos--;
64
65   /* return endpos */
66   if (endpos != NULL)
67      *endpos = pos;
68
69   /* now skip until we find a path separator */
70   while((pos > 0) && !osd_is_path_separator(path[pos]))
71      pos--;
72
73   /* return beginpos */
74   if (beginpos != NULL)
75      *beginpos = pos;
76}
77
78
79
80/*-------------------------------------------------
81    zippath_parent - retrieves the parent directory
82-------------------------------------------------*/
83
84astring *zippath_parent(astring *dst, const char *path)
85{
86   int pos;
87   parse_parent_path(path, &pos, NULL);
88
89   /* return the result */
90   return (pos > 0) ? astring_cpych(dst, path, pos + 1) : NULL;
91}
92
93
94
95/*-------------------------------------------------
96    zippath_parent_basename - retrieves the parent
97   directory basename
98-------------------------------------------------*/
99
100astring *zippath_parent_basename(astring *dst, const char *path)
101{
102   int beginpos, endpos;
103   parse_parent_path(path, &beginpos, &endpos);
104
105   return astring_cpych(dst, path + beginpos + 1, endpos - beginpos);
106}
107
108
109
110/*-------------------------------------------------
111    zippath_combine - combines two paths
112-------------------------------------------------*/
113
114astring *zippath_combine(astring *dst, const char *path1, const char *path2)
115{
116   astring *result;
117
118   if (!strcmp(path2, "."))
119   {
120      result = astring_cpyc(dst, path1);
121   }
122   else if (!strcmp(path2, ".."))
123   {
124      result = zippath_parent(dst, path1);
125   }
126   else if (osd_is_absolute_path(path2))
127   {
128      result = astring_cpyc(dst, path2);
129   }
130   else if ((path1[0] != '\0') && !osd_is_path_separator(path1[strlen(path1) - 1]))
131   {
132      result = astring_assemble_3(dst, path1, PATH_SEPARATOR, path2);     
133   }
134   else
135   {
136      result = astring_assemble_2(dst, path1, path2);     
137   }
138   return result;
139}
140
141
142
143/***************************************************************************
144    DIRECTORY OPERATIONS
145***************************************************************************/
146
147/*-------------------------------------------------
148    is_root - tests to see if this path is the root
149-------------------------------------------------*/
150
151static int is_root(const char *path)
152{
153   int i = 0;
154
155   /* skip drive letter */
156   if (isalpha(path[i]) && (path[i + 1] == ':'))
157      i += 2;
158
159   /* skip path separators */
160   while (osd_is_path_separator(path[i]))
161      i++;
162
163   return path[i] == '\0';
164}
165
166
167
168/*-------------------------------------------------
169    is_zip_file - tests to see if this file is a
170   ZIP file
171-------------------------------------------------*/
172
173static int is_zip_file(const char *path)
174{
175   const char *s = strrchr(path, '.');
176   return (s != NULL) && !mame_stricmp(s, ".zip");
177}
178
179
180
181/*-------------------------------------------------
182    zippath_resolve - separates a ZIP path out into
183   true path and ZIP entry components
184-------------------------------------------------*/
185
186static file_error zippath_resolve(const char *path, osd_dir_entry_type *entry_type,
187   zip_file **zipfile, astring *newpath)
188{
189   file_error err;
190   osd_directory_entry *current_entry = NULL;
191   osd_dir_entry_type current_entry_type;
192   astring *apath = astring_cpyc(astring_alloc(), path);
193   astring *apath_trimmed = astring_alloc();
194   astring *parent = NULL;
195   int went_up = FALSE;
196   int i;
197
198   /* be conservative */
199   *entry_type = ENTTYPE_NONE;
200   *zipfile = NULL;
201
202   do
203   {
204      /* trim the path of trailing path separators */
205      i = astring_len(apath);
206      while((i > 0) && osd_is_path_separator(astring_c(apath)[i - 1]))
207         i--;
208      apath_trimmed = astring_cpysubstr(apath_trimmed, apath, 0, i);
209
210      /* stat the path */
211      current_entry = osd_stat(astring_c(apath_trimmed));
212      if (current_entry == NULL)
213      {
214         err = FILERR_FAILURE;
215         goto done;
216      }
217
218      /* get the entry type and free the stat entry */
219      current_entry_type = current_entry->type;
220      free(current_entry);
221      current_entry = NULL;
222
223      /* if we have not found the file or directory, go up */
224      if (current_entry_type == ENTTYPE_NONE)
225      {
226         went_up = TRUE;
227         parent = zippath_parent(astring_alloc(), astring_c(apath));
228         astring_free(apath);
229         apath = parent;
230      }
231   }
232   while((current_entry_type == ENTTYPE_NONE) && (apath != NULL));
233
234   /* if we did not find anything, then error out */
235   if (current_entry_type == ENTTYPE_NONE)
236   {
237      err = FILERR_NOT_FOUND;
238      goto done;
239   }
240
241   /* is this file a ZIP file? */
242   if ((current_entry_type == ENTTYPE_FILE) && is_zip_file(astring_c(apath_trimmed))
243      && (zip_file_open(astring_c(apath_trimmed), zipfile) == ZIPERR_NONE))
244   {
245      /* this was a true ZIP path - copy out the sub path */
246      astring_cpyc(newpath, path + astring_len(apath));
247      *entry_type = (astring_len(newpath) == 0) || (astring_c(newpath)[astring_len(newpath)-1] == '/')
248         ? ENTTYPE_DIR : ENTTYPE_FILE;
249   }
250   else
251   {
252      /* this was a normal path */
253      if (went_up)
254      {
255         err = FILERR_NOT_FOUND;
256         goto done;
257      }
258      astring_cpyc(newpath, path);
259      *entry_type = current_entry_type;
260   }
261
262   /* success! */
263   err = FILERR_NONE;
264
265done:
266   if (apath != NULL)
267      astring_free(apath);
268   if (apath_trimmed != NULL)
269      astring_free(apath_trimmed);
270   return err;
271}
272
273
274/*-------------------------------------------------
275    zippath_opendir - opens a directory
276-------------------------------------------------*/
277
278file_error zippath_opendir(const char *path, zippath_directory **directory)
279{
280   file_error err;
281   osd_dir_entry_type entry_type;
282   astring *newpath = astring_alloc();
283   zippath_directory *result;
284
285   /* allocate a directory */
286   result = (zippath_directory *) malloc(sizeof(*result));
287   if (result == NULL)
288   {
289      err = FILERR_OUT_OF_MEMORY;
290      goto done;
291   }
292   memset(result, 0, sizeof(*result));
293
294   /* resolve the path */
295   err = zippath_resolve(path, &entry_type, &result->zipfile, newpath);
296   if (err != FILERR_NONE)
297      goto done;
298
299   /* we have to be a directory */
300   if (entry_type != ENTTYPE_DIR)
301   {
302      err = FILERR_NOT_FOUND;
303      goto done;
304   }
305
306   /* was the result a ZIP? */
307   if (result->zipfile != NULL)
308   {
309      result->zipprefix = newpath;
310      newpath = NULL;
311   }
312   else
313   {
314      /* a conventional directory */
315      result->directory = osd_opendir(path);
316      if (result->directory == NULL)
317      {
318         err = FILERR_FAILURE;
319         goto done;
320      }
321
322      /* is this path the root? if so, pretend we've already returned the parent */
323      if (is_root(path))
324         result->returned_parent = TRUE;
325   }
326
327done:
328   if (((directory == NULL) || (err != FILERR_NONE)) && (result != NULL))
329   {
330      zippath_closedir(result);
331      result = NULL;
332   }
333   if (newpath != NULL)
334   {
335      astring_free(newpath);
336      newpath = NULL;
337   }
338   if (directory != NULL)
339      *directory = result;
340   return err;
341}
342
343
344/*-------------------------------------------------
345    zippath_closedir - closes a directory
346-------------------------------------------------*/
347
348void zippath_closedir(zippath_directory *directory)
349{
350   zippath_returned_directory *dirlist;
351
352   if (directory->directory != NULL)
353      osd_closedir(directory->directory);
354
355   if (directory->zipfile != NULL)
356      zip_file_close(directory->zipfile);
357
358   if (directory->zipprefix != NULL)
359      astring_free(directory->zipprefix);
360
361   while(directory->returned_dirlist != NULL)
362   {
363      dirlist = directory->returned_dirlist;
364      directory->returned_dirlist = directory->returned_dirlist->next;
365      free(dirlist);
366   }
367
368   free(directory);
369}
370
371
372/*-------------------------------------------------
373    get_relative_path - checks to see if a specified
374   header is in the zippath_directory, and if so
375   returns the relative path
376-------------------------------------------------*/
377
378static const char *get_relative_path(zippath_directory *directory, const zip_file_header *header)
379{
380   const char *result = NULL;
381   int len = astring_len(directory->zipprefix);
382
383   if ((len <= strlen(header->filename))
384      && !strncmp(astring_c(directory->zipprefix), header->filename, len))
385   {
386      result = &header->filename[len];
387   }
388
389   return result;
390}
391
392
393/*-------------------------------------------------
394    zippath_readdir - reads a directory
395-------------------------------------------------*/
396
397const osd_directory_entry *zippath_readdir(zippath_directory *directory)
398{
399   const osd_directory_entry *result = NULL;
400   const zip_file_header *header;
401   const char *relpath;
402   const char *separator;
403   zippath_returned_directory *rdent;
404
405   if (!directory->returned_parent)
406   {
407      /* first thing's first - return parent directory */
408      directory->returned_parent = TRUE;
409      memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
410      directory->returned_entry.name = "..";
411      directory->returned_entry.type = ENTTYPE_DIR;
412      result = &directory->returned_entry;                 
413   }
414   else if (directory->directory != NULL)
415   {
416      /* a normal directory read */
417      do
418      {
419         result = osd_readdir(directory->directory);
420      }
421      while((result != NULL) && (!strcmp(result->name, ".") || !strcmp(result->name, "..")));
422
423      /* special case - is this entry a ZIP file?  if so we need to return it as a "directory" */
424      if ((result != NULL) && is_zip_file(result->name))
425      {
426         /* copy; but change the entry type */
427         directory->returned_entry = *result;
428         directory->returned_entry.type = ENTTYPE_DIR;
429         result = &directory->returned_entry;
430      }
431   }
432   else if (directory->zipfile != NULL)
433   {
434      do
435      {
436         /* a zip file read */
437         do
438         {
439            if (!directory->called_zip_first)
440               header = zip_file_first_file(directory->zipfile);
441            else
442               header = zip_file_next_file(directory->zipfile);
443            directory->called_zip_first = TRUE;
444            relpath = NULL;
445         }
446         while((header != NULL) && ((relpath = get_relative_path(directory, header)) == NULL));
447
448         if (relpath != NULL)
449         {
450            /* we've found a ZIP entry; but this may be an entry deep within the target directory */
451            separator = strchr(relpath, '/');
452            if (separator != NULL)
453            {
454               /* a nested entry; loop through returned_dirlist to see if we've returned the parent directory */
455               for (rdent = directory->returned_dirlist; rdent != NULL; rdent = rdent->next)
456               {
457                  if (!core_strnicmp(rdent->name, relpath, separator - relpath))
458                     break;
459               }
460
461               if (rdent == NULL)
462               {
463                  /* we've found a new directory; add this to returned_dirlist */
464                  rdent = malloc(sizeof(*rdent) + (separator - relpath));
465                  rdent->next = directory->returned_dirlist;
466                  memcpy(rdent->name, relpath, (separator - relpath) * sizeof(rdent->name[0]));
467                  rdent->name[separator - relpath] = '\0';
468                  directory->returned_dirlist = rdent;                 
469
470                  /* ...and return it */
471                  memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
472                  directory->returned_entry.name = rdent->name;
473                  directory->returned_entry.type = ENTTYPE_DIR;
474                  result = &directory->returned_entry;                 
475               }
476            }
477            else
478            {
479               /* a real file */
480               memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
481               directory->returned_entry.name = relpath;
482               directory->returned_entry.type = ENTTYPE_FILE;
483               directory->returned_entry.size = header->uncompressed_length;
484               result = &directory->returned_entry;                 
485            }
486         }
487      }
488      while((relpath != NULL) && (result == NULL));
489   }
490   return result;
491}
492
src/mess/zippath.h
r0r2874
1/***************************************************************************
2
3    zippath.h
4
5    File/directory/path operations that work with ZIP files
6
7***************************************************************************/
8
9#pragma once
10
11#ifndef __ZIPPATH_H__
12#define __ZIPPATH_H__
13
14#include "corefile.h"
15#include "astring.h"
16
17
18/***************************************************************************
19    TYPE DEFINITIONS
20***************************************************************************/
21
22typedef struct _zippath_directory zippath_directory;
23
24
25
26/***************************************************************************
27    FUNCTION PROTOTYPES
28***************************************************************************/
29
30
31/* ----- path operations ----- */
32
33/* retrieves the parent directory */
34astring *zippath_parent(astring *dst, const char *path);
35
36/* retrieves the parent directory basename */
37astring *zippath_parent_basename(astring *dst, const char *path);
38
39/* combines two paths */
40astring *zippath_combine(astring *dst, const char *path1, const char *path2);
41
42
43/* ----- directory operations ----- */
44
45/* opens a directory */
46file_error zippath_opendir(const char *path, zippath_directory **directory);
47
48/* closes a directory */
49void zippath_closedir(zippath_directory *directory);
50
51/* reads a directory entry */
52const osd_directory_entry *zippath_readdir(zippath_directory *directory);
53
54
55#endif /* __ZIPPATH_H__ */
src/mess/filemngr.c
r2873r2874
11/*********************************************************************
22
3  filemngr.c
3   filemngr.c
44
5  MESS's clunky built-in file manager
5   MESS's clunky built-in file manager
66
7   TODO
8      - Support image creation; the old file manager did not do this
9        well.
10         - Should only allow image creation if the device allows
11         - Also support creation arguments
12      - Restrict directory listing by file extension
13      - Support file manager invocation from the main menu for
14        required images
15      - Restrict empty slot if image required
16
717*********************************************************************/
818
919#include "driver.h"
10#include "utils.h"
1120#include "image.h"
1221#include "ui.h"
1322#include "uimenu.h"
14#include "mslegacy.h"
23#include "zippath.h"
1524
1625
1726
1827/***************************************************************************
19    PARAMETERS
28    TYPE DEFINITIONS
2029***************************************************************************/
2130
22#define SEL_BITS   12
23#define SEL_MASK   ((1<<SEL_BITS)-1)
31typedef union _fileselector_state fileselector_state;
32union _fileselector_state
33{
34   UINT32 i;
35   struct
36   {
37      int selected : 16;
38      unsigned int menu_is_built : 1;
39   } s;
40};
2441
2542
2643
r2873r2874
2845    LOCAL VARIABLES
2946***************************************************************************/
3047
31static int count_chars_entered;
32static char *enter_string;
33static int enter_string_size;
34static int enter_filename_mode;
35static char curdir[260];
48static const device_config *selected_device;
49static astring *current_directory;
50static astring *current_file;
3651
37static char entered_filename[512];
3852
3953
40
4154/***************************************************************************
4255    IMPLEMENTATION
4356***************************************************************************/
4457
45static void start_enter_string(char *string_buffer, int max_string_size, int filename_mode)
46{
47   enter_string = string_buffer;
48   count_chars_entered = strlen(string_buffer);
49   enter_string_size = max_string_size;
50   enter_filename_mode = filename_mode;
51}
58/*-------------------------------------------------
59    alloc_directory_entry - allocates a struct
60   of type osd_directory_entry
61-------------------------------------------------*/
5262
53
54/* code, lower case (w/o shift), upper case (with shift), control */
55static const int code_to_char_table[] =
63static osd_directory_entry *alloc_directory_entry(const char *name, osd_dir_entry_type type, UINT64 size)
5664{
57   KEYCODE_0, '0', ')', 0,
58   KEYCODE_1, '1', '!', 0,
59   KEYCODE_2, '2', '"', 0,
60   KEYCODE_3, '3', '#', 0,
61   KEYCODE_4, '4', '$', 0,
62   KEYCODE_5, '5', '%', 0,
63   KEYCODE_6, '6', '^', 0,
64   KEYCODE_7, '7', '&', 0,
65   KEYCODE_8, '8', '*', 0,
66   KEYCODE_9, '9', '(', 0,
67   KEYCODE_A, 'a', 'A', 1,
68   KEYCODE_B, 'b', 'B', 2,
69   KEYCODE_C, 'c', 'C', 3,
70   KEYCODE_D, 'd', 'D', 4,
71   KEYCODE_E, 'e', 'E', 5,
72   KEYCODE_F, 'f', 'F', 6,
73   KEYCODE_G, 'g', 'G', 7,
74   KEYCODE_H, 'h', 'H', 8,
75   KEYCODE_I, 'i', 'I', 9,
76   KEYCODE_J, 'j', 'J', 10,
77   KEYCODE_K, 'k', 'K', 11,
78   KEYCODE_L, 'l', 'L', 12,
79   KEYCODE_M, 'm', 'M', 13,
80   KEYCODE_N, 'n', 'N', 14,
81   KEYCODE_O, 'o', 'O', 15,
82   KEYCODE_P, 'p', 'P', 16,
83   KEYCODE_Q, 'q', 'Q', 17,
84   KEYCODE_R, 'r', 'R', 18,
85   KEYCODE_S, 's', 'S', 19,
86   KEYCODE_T, 't', 'T', 20,
87   KEYCODE_U, 'u', 'U', 21,
88   KEYCODE_V, 'v', 'V', 22,
89   KEYCODE_W, 'w', 'W', 23,
90   KEYCODE_X, 'x', 'X', 24,
91   KEYCODE_Y, 'y', 'Y', 25,
92   KEYCODE_Z, 'z', 'Z', 26,
93   KEYCODE_OPENBRACE, '[', '{', 27,
94   KEYCODE_BACKSLASH, '\\', '|', 28,
95   KEYCODE_CLOSEBRACE, ']', '}', 29,
96   KEYCODE_TILDE, '^', '~', 30,
97   KEYCODE_BACKSPACE, 127, 127, 31,
98   KEYCODE_COLON, ':', ';', 0,
99   KEYCODE_EQUALS, '=', '+', 0,
100   KEYCODE_MINUS, '-', '_', 0,
101   KEYCODE_STOP, '.', '<', 0,
102   KEYCODE_COMMA, ',', '>', 0,
103   KEYCODE_SLASH, '/', '?', 0,
104   KEYCODE_ENTER, 13, 13, 13,
105   KEYCODE_ESC, 27, 27, 27
106};
65   char *name_dupe = NULL;
66   osd_directory_entry *new_entry;
67   size_t name_length;
68   
69   /* allocate the new entry */
70   name_length = (name != NULL) ? strlen(name) + 1 : 0;
71   new_entry = malloc_or_die(sizeof(*new_entry) + name_length);
10772
108/*
109 * For now I use a lookup table for valid filename characters.
110 * Maybe change this for different platforms?
111 * Put it to osd_cpu? Make it an osd_... function?
112 */
113static const char valid_filename_char[256] =
114{
115   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* 00-0f */
116   0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* 10-1f */
117   1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,    /*   !"#$%&'()*+,-./ */
118   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,    /* 0123456789:;<=>? */
119   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    /* @ABCDEFGHIJKLMNO */
120   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,    /* PQRSTUVWXYZ[\]^_ */
121   0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    /* `abcdefghijklmno */
122   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0,    /* pqrstuvwxyz{|}~   */
123   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* 80-8f */
124   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* 90-9f */
125   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* a0-af */
126   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* b0-bf */
127   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* c0-cf */
128   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* d0-df */
129   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* e0-ef */
130   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0      /* f0-ff */
131};
132
133static char code_to_ascii(input_code code)
134{
135   int i;
136
137   for (i = 0; i < (sizeof (code_to_char_table) / (sizeof (int) * 4)); i++)
138
73   /* copy the name, if specified */
74   if (name != NULL)
13975   {
140      if (code_to_char_table[i * 4] == code)
141      {
142         if (input_code_pressed(KEYCODE_LCONTROL) || input_code_pressed(KEYCODE_RCONTROL))
143            return code_to_char_table[i * 4 + 3];
144         if (input_code_pressed(KEYCODE_LSHIFT) || input_code_pressed(KEYCODE_RSHIFT))
145            return code_to_char_table[i * 4 + 2];
146         return code_to_char_table[i * 4 + 1];
147      }
76      name_dupe = ((char *) new_entry) + sizeof(*new_entry);
77      strcpy(name_dupe, name);
14878   }
14979
150   return -1;
80   new_entry->name = name_dupe;
81   new_entry->type = type;
82   new_entry->size = size;
83   return new_entry;
15184}
15285
153static char *update_entered_string(void)
154{
155   input_code code;
156   int ascii_char;
15786
158   /* get key */
159   code = input_code_poll_switches(FALSE);
16087
161   /* key was pressed? */
162   if (code == INPUT_CODE_INVALID)
163      return NULL;
88/*-------------------------------------------------
89    dupe_directory_entry - duplicates a struct
90   of type osd_directory_entry
91-------------------------------------------------*/
16492
165   ascii_char = code_to_ascii(code);
93static osd_directory_entry *dupe_directory_entry(const osd_directory_entry *entry)
94{
95   return alloc_directory_entry(entry->name, entry->type, entry->size);
96}
16697
167   switch (ascii_char)
168   {
169      /* char could not be converted to ascii */
170   case -1:
171      return NULL;
17298
173   case 13:   /* Return */
174      return enter_string;
17599
176   case 25:   /* Ctrl-Y (clear line) */
177      count_chars_entered = 0;
178      enter_string[count_chars_entered] = '\0';
179      break;
100/*-------------------------------------------------
101    extra_text_draw_box - generically adds header
102   or footer text
103-------------------------------------------------*/
180104
181   case 27:   /* Escape */
182      return NULL;
105static void extra_text_draw_box(float origx1, float origx2, float origy, float yspan, const char *text, int direction)
106{
107   float width, maxwidth;
108   float x1, y1, x2, y2, temp;
183109
184      /* delete */
185   case 127:
186      count_chars_entered--;
187      if (count_chars_entered < 0)
188         count_chars_entered = 0;
189      enter_string[count_chars_entered] = '\0';
190      break;
110   /* get the size of the text */
111   ui_draw_text_full(text, 0.0f, 0.0f, 1.0f, JUSTIFY_CENTER, WRAP_TRUNCATE,
112      DRAW_NONE, ARGB_WHITE, ARGB_BLACK, &width, NULL);
113   width += 2 * UI_BOX_LR_BORDER;
114   maxwidth = MAX(width, origx2 - origx1);
191115
192      /* got a char - add to string */
193   default:
194      if (count_chars_entered < enter_string_size)
195      {
196         if ((enter_filename_mode && valid_filename_char[(unsigned)ascii_char]) ||
197            !enter_filename_mode)
198         {
199            /* store char */
200            enter_string[count_chars_entered] = ascii_char;
201            /* update count of chars entered */
202            count_chars_entered++;
203            /* add null to end of string */
204            enter_string[count_chars_entered] = '\0';
205         }
206      }
207      break;
116   /* compute our bounds */
117   x1 = 0.5f - 0.5f * maxwidth;
118   x2 = x1 + maxwidth;
119   y1 = origy + (yspan * direction);
120   y2 = origy + (UI_BOX_TB_BORDER * direction);
121
122   if (y1 > y2)
123   {
124      temp = y1;
125      y1 = y2;
126      y2 = temp;
208127   }
209128
210   return NULL;
211}
129   /* draw a box */
130   ui_draw_outlined_box(x1, y1, x2, y2, UI_FILLCOLOR);
212131
132   /* take off the borders */
133   x1 += UI_BOX_LR_BORDER;
134   x2 -= UI_BOX_LR_BORDER;
135   y1 += UI_BOX_TB_BORDER;
136   y2 -= UI_BOX_TB_BORDER;
213137
214static char current_filespecification[32] = "*.*";
215static const char fs_directory[] = "[DIR]";
216static const char fs_device[] = "[DRIVE]";
217static const char fs_file[] = "[FILE]";
218/*const char fs_archive[] = "[ARCHIVE]"; */
138   /* draw the text within it */
139   ui_draw_text_full(text, x1, y1, x2 - x1, JUSTIFY_CENTER, WRAP_TRUNCATE,
140                 DRAW_NORMAL, ARGB_WHITE, ARGB_BLACK, NULL, NULL);
141}
219142
220static ui_menu_item *fs_item;
221static int *fs_types;
222static int *fs_order;
223static int fs_chunk;
224static int fs_total;
225static int fs_insession;
226143
227enum {
228   FILESELECT_NONE,
229   FILESELECT_QUIT,
230   FILESELECT_FILESPEC,
231   FILESELECT_DEVICE,
232   FILESELECT_DIRECTORY,
233   FILESELECT_FILE
234};
235144
145/*-------------------------------------------------
146    extra_text_render - generically adds header
147   and footer text
148-------------------------------------------------*/
236149
237static void fs_free(void)
150static void extra_text_render(const menu_extra *extra, float origx1, float origy1, float origx2, float origy2,
151   const char *header, const char *footer)
238152{
239   if (fs_chunk > 0)
240   {
241      int i;
242      /* free duplicated strings of file and directory names */
243      for (i = 0; i < fs_total; i++)
244      {
245         switch(fs_types[i]) {
246         case FILESELECT_FILE:
247         case FILESELECT_DIRECTORY:
248            if (fs_item[i].text != ui_getstring(UI_emptyslot))
249               free((char *) fs_item[i].text);
250            break;
251         }
252      }
253      free(fs_item);
254      free(fs_types);
255      free(fs_order);
256      fs_chunk = 0;
257      fs_total = 0;
258   }
153   header = ((header != NULL) && (header[0] != '\0')) ? header : NULL;
154   footer = ((footer != NULL) && (footer[0] != '\0')) ? footer : NULL;
155
156   if (header != NULL)
157      extra_text_draw_box(origx1, origx2, origy1, extra->top, header, -1);
158   if (footer != NULL)
159      extra_text_draw_box(origx1, origx2, origy2, extra->bottom, footer, +1);
259160}
260161
261static int fs_alloc(void)
262{
263   if (fs_total >= fs_chunk)
264   {
265      if (fs_chunk)
266      {
267         fs_chunk += 256;
268         logerror("fs_alloc() next chunk (total %d)\n", fs_chunk);
269         fs_item = realloc(fs_item, fs_chunk * sizeof(*fs_item));
270         fs_types = realloc(fs_types, fs_chunk * sizeof(int));
271         fs_order = realloc(fs_order, fs_chunk * sizeof(int));
272      }
273      else
274      {
275         fs_chunk = 512;
276         logerror("fs_alloc() first chunk %d\n", fs_chunk);
277         fs_item = malloc(fs_chunk * sizeof(*fs_item));
278         fs_types = malloc(fs_chunk * sizeof(int));
279         fs_order = malloc(fs_chunk * sizeof(int));
280      }
281162
282      /* what do we do if reallocation fails? raise(SIGABRT) seems a way outa here */
283      if (!fs_item || !fs_types || !fs_order)
284      {
285         logerror("failed to allocate fileselect buffers!\n");
286         exit(-1);
287      }
288   }
289163
290   memset(&fs_item[fs_total], 0, sizeof(fs_item[fs_total]));
291   fs_order[fs_total] = fs_total;
292   return fs_total++;
293}
164/*-------------------------------------------------
165    file_selector_render_extra - perform our
166    special rendering
167-------------------------------------------------*/
294168
295static int DECL_SPEC fs_compare(const void *p1, const void *p2)
169static void file_selector_render_extra(const menu_extra *extra, float origx1, float origy1, float origx2, float origy2)
296170{
297   int i1 = *(int *)p1;
298   int i2 = *(int *)p2;
299
300   if (fs_types[i1] != fs_types[i2])
301      return fs_types[i1] - fs_types[i2];
302   return strcmp(fs_item[i1].text, fs_item[i2].text);
171   extra_text_render(extra, origx1, origy1, origx2, origy2, astring_c(current_directory), NULL);
303172}
304173
305#define MAX_ENTRIES_IN_MENU ((1<<12)-2)
306174
307static void fs_generate_filelist(void)
308{
309   osd_directory *dir;
310   int qsort_start, count, i, n;
311   ui_menu_item *tmp_menu_item;
312   int *tmp_types;
313175
314   /* just to be safe */
315   fs_free();
176/*-------------------------------------------------
177    build_file_selector_menu_items - creates and
178   allocates all menu items for a directory
179-------------------------------------------------*/
316180
317   /* quit back to main menu option at top */
318   n = fs_alloc();
319   fs_item[n].text = ui_getstring(UI_quitfileselector);
320   fs_types[n] = FILESELECT_QUIT;
181static file_error build_file_selector_menu_items(const char *path, ui_menu_item *item_list,
182   size_t item_list_length, int *menu_items, int *selected)
183{
184   zippath_directory *directory = NULL;
185   file_error err = FILERR_NONE;
186   const osd_directory_entry *dirent;
187   osd_directory_entry *dirent_dupe;
188   const char *subtext;
189   int count, i;
321190
322   /* insert blank line */
323   n = fs_alloc();
324   fs_item[n].text = MENU_SEPARATOR_ITEM;
325   fs_types[n] = FILESELECT_NONE;
191   /* reset count */
192   *menu_items = 0;
193   *selected = 0;
326194
327   /* current directory */
328   n = fs_alloc();
329   fs_item[n].text = curdir;
330   fs_types[n] = FILESELECT_NONE;
195   /* add the "[empty slot]" entry */
196   memset(&item_list[*menu_items], 0, sizeof(item_list[*menu_items]));
197   item_list[*menu_items].text = "[empty slot]";
198   item_list[*menu_items].ref = alloc_directory_entry(NULL, ENTTYPE_FILE, 0);
199   (*menu_items)++;
331200
332   /* blank line */
333   n = fs_alloc();
334   fs_item[n].text = MENU_SEPARATOR_ITEM;
335   fs_types[n] = FILESELECT_NONE;
336
337   /* file specification */
338   n = fs_alloc();
339   fs_item[n].text = ui_getstring(UI_filespecification);
340   fs_item[n].subtext = current_filespecification;
341   fs_types[n] = FILESELECT_FILESPEC;
342
343   /* insert blank line */
344   n = fs_alloc();
345   fs_item[n].text = MENU_SEPARATOR_ITEM;
346   fs_types[n] = FILESELECT_NONE;
347
348   /* insert empty specifier */
349   n = fs_alloc();
350   fs_item[n].text = ui_getstring(UI_emptyslot);
351   fs_item[n].subtext = "";
352   fs_types[n] = FILESELECT_FILE;
353
354   qsort_start = fs_total;
355
356   /* devices first */
201   /* add the drives */
357202   count = osd_num_devices();
358   if (count > 0)
203   for (i = 0; i < count; i++)
359204   {
360      logerror("fs_generate_filelist: %d devices\n", count);
361      for (i = 0; i < count; i++)
362      {
363         if (fs_total >= MAX_ENTRIES_IN_MENU)
364            break;
365         n = fs_alloc();
366         fs_item[n].text = osd_get_device_name(i);
367         fs_item[n].subtext = fs_device;
368         fs_types[n] = FILESELECT_DEVICE;
369      }
205      memset(&item_list[*menu_items], 0, sizeof(item_list[*menu_items]));
206      item_list[*menu_items].text = osd_get_device_name(i);
207      item_list[*menu_items].subtext = "[DRIVE]";
208      item_list[*menu_items].ref = alloc_directory_entry(item_list[*menu_items].text, ENTTYPE_DIR, 0);
209      (*menu_items)++;
370210   }
371211
372   /* directory entries */
373   dir = osd_opendir(curdir);
374   if (dir)
212   /* open the directory */
213   err = zippath_opendir(path, &directory);
214   if (err != FILERR_NONE)
215      goto done;
216
217   /* build the menu for each item */
218   while((dirent = zippath_readdir(directory)) != NULL)
375219   {
376      const osd_directory_entry *dirent;
220      /* if there are too many entries... */
221      if (*menu_items >= item_list_length - 10)
222         break;
377223
378      while((dirent = osd_readdir(dir)) != NULL)
224      /* set the selected item to be the first non-parent directory or file */
225      if ((*selected == 0) && strcmp(dirent->name, ".."))
226         *selected = *menu_items;
227
228      /* choose text for the entry type */
229      switch(dirent->type)
379230      {
380         if (fs_total >= MAX_ENTRIES_IN_MENU)
231         case ENTTYPE_FILE:
232            subtext = "[FILE]";
381233            break;
382
383         switch(dirent->type)
384         {
385            case ENTTYPE_DIR:
386               n = fs_alloc();
387               fs_item[n].text = mame_strdup(dirent->name);
388               fs_types[n] = FILESELECT_DIRECTORY;
389               fs_item[n].subtext = fs_directory;
390               break;
391
392            case ENTTYPE_FILE:
393               n = fs_alloc();
394               fs_item[n].text = mame_strdup(dirent->name);
395               fs_types[n] = FILESELECT_FILE;
396               fs_item[n].subtext = fs_file;
397               break;
398
399            default:
400               /* ignore other file types */
401               break;
402         }
234         case ENTTYPE_DIR:
235            subtext = "[DIR]";
236            break;
237         default:
238            subtext = "[UNK]";
239            break;
403240      }
404      osd_closedir(dir);
405   }
406241
407   logerror("fs_generate_filelist: sorting %d entries\n", n - qsort_start);
408   qsort(&fs_order[qsort_start], n - qsort_start, sizeof(int), fs_compare);
242      /* dupe the menu item */
243      dirent_dupe = dupe_directory_entry(dirent);
409244
410   tmp_menu_item = malloc(n * sizeof(*tmp_menu_item));
411   tmp_types = malloc(n * sizeof(int));
245      /* do we have to select this file? */
246      if (!mame_stricmp(astring_c(current_file), dirent_dupe->name))
247         *selected = *menu_items;
412248
413   /* no space to sort? have to leave now... */
414   if (!tmp_menu_item || !tmp_types )
415      return;
416
417   /* copy items in original order */
418   memcpy(tmp_menu_item, fs_item, n * sizeof(*tmp_menu_item));
419   memcpy(tmp_types, fs_types, n * sizeof(int));
420
421   for (i = qsort_start; i < n; i++)
422   {
423      int j = fs_order[i];
424      fs_item[i] = tmp_menu_item[j];
425      fs_types[i] = tmp_types[j];
249      /* record the menu item */
250      memset(&item_list[*menu_items], 0, sizeof(item_list[*menu_items]));
251      item_list[*menu_items].text = dirent_dupe->name;
252      item_list[*menu_items].subtext = subtext;
253      item_list[*menu_items].ref = dirent_dupe;
254      (*menu_items)++;
426255   }
427256
428   free(tmp_menu_item);
429   free(tmp_types);
257   /* add an item for the return */
258   memset(&item_list[*menu_items], 0, sizeof(item_list[*menu_items]));
259   item_list[*menu_items].text = "Return to Prior Menu";
260   (*menu_items)++;
261
262done:
263   if (directory != NULL)
264      zippath_closedir(directory);
265   return err;
430266}
431267
432268
433269
434static int string_ends_with(const char *str, const char *target)
435{
436   size_t str_length = strlen(str);
437   size_t target_length = strlen(target);
270/*-------------------------------------------------
271    check_path - performs a quick check to see if
272   a path exists
273-------------------------------------------------*/
438274
439   return (str_length >= target_length)
440      && !strcmp(&str[str_length - target_length], target);
441
275static file_error check_path(const char *path)
276{
277   return zippath_opendir(path, NULL);
442278}
443279
444280
445281
446#define UI_SHIFT_PRESSED               (input_code_pressed(KEYCODE_LSHIFT) || input_code_pressed(KEYCODE_RSHIFT))
282/*-------------------------------------------------
283    menu_file_selector - MESS-specific menu
284-------------------------------------------------*/
447285
448/* and mask to get bits */
449#define SEL_BITS_MASK         (~SEL_MASK)
450
451static int fileselect(running_machine *machine, int selected, const char *default_selection, const char *working_directory)
286static UINT32 menu_file_selector(running_machine *machine, UINT32 state)
452287{
453   int sel, total, arrowize;
454   int visible;
288   static ui_menu_item item_list[200];
289   static int menu_items;
455290
456   sel = selected - 1;
291   file_error err;
292   const osd_directory_entry *dirent;
293   int visible_items, selected, i;
294   astring *new_path;
295   fileselector_state fs_state;
296   int menu_is_built;
297   menu_extra extra;
457298
458   /* beginning a file manager session */
459   if (fs_insession == 0)
460   {
461      fs_insession = 1;
462      strncpyz(curdir, working_directory, MIN(strlen(working_directory)+1, 260));
463   }
299   /* interpret the state */
300   fs_state.i = state;
301   selected = fs_state.s.selected;
302   menu_is_built = fs_state.s.menu_is_built;
464303
465   /* generate menu? */
466   if (fs_total == 0)
304   /* do we have to build the menu? */
305   if (!menu_is_built)
467306   {
468      fs_generate_filelist();
307      err = build_file_selector_menu_items(astring_c(current_directory), item_list, ARRAY_LENGTH(item_list),
308         &menu_items, &selected);
309      if (err != FILERR_NONE)
310      {
311         /* if we error here, we must pop out */
312         fs_state.i = ui_menu_stack_pop();
313         goto done;
314      }
315
316      menu_is_built = TRUE;
469317   }
470318
471   total = fs_total;
319   /* compute extras */
320   memset(&extra, 0, sizeof(extra));
321   extra.top = ui_get_line_height() + 3.0f * UI_BOX_TB_BORDER;
322   extra.render = file_selector_render_extra;
472323
473   if (total > 0)
324   /* draw the menu */
325   visible_items = ui_menu_draw(item_list, menu_items, selected, &extra);
326
327   /* handle the keys */
328   if (ui_menu_generic_keys(machine, (UINT32 *) &selected, menu_items, visible_items))
329      goto done;
330
331   if (input_ui_pressed(machine, IPT_UI_SELECT))
474332   {
475      /* make sure it is in range - might go out of range if
476       * we were stepping up and down directories */
477      if ((sel & SEL_MASK) >= total)
478         sel = (sel & SEL_BITS_MASK) | (total - 1);
333      dirent = (const osd_directory_entry *) item_list[selected].ref;
334      menu_is_built = FALSE;
479335
480      arrowize = 0;
481      if (sel < total)
336      switch(dirent->type)
482337      {
483         switch (fs_types[sel])
484         {
485            /* arrow pointing inwards (arrowize = 1) */
486            case FILESELECT_QUIT:
487            case FILESELECT_FILE:
488               break;
338         case ENTTYPE_DIR:
339            /* prepare the selection in the new directory */
340            selected = 1;
341            if (!strcmp(dirent->name, ".."))
342               zippath_parent_basename(current_file, astring_c(current_directory));
489343
490            case FILESELECT_FILESPEC:
491            case FILESELECT_DIRECTORY:
492            case FILESELECT_DEVICE:
493               /* arrow pointing to right -
494                * indicating more available if
495                * selected, or editable */
496               arrowize = 2;
344            /* change the directory */
345            new_path = zippath_combine(astring_alloc(), astring_c(current_directory), dirent->name);
346            err = check_path(astring_c(new_path));
347            if (err != FILERR_NONE)
348            {
349               /* this path is problematic; present the user with an error and bail */
350               ui_popup_time(1, "Error accessing %s", astring_c(new_path));
351               astring_free(new_path);
497352               break;
498         }
499      }
353            }
354            astring_free(current_directory);
355            current_directory = new_path;
356            break;
500357
501      if (sel & (1 << SEL_BITS))   /* are we waiting for a new key? */
502      {
503         char *name;
504
505         /* change menu item to show this filename */
506         fs_item[sel & SEL_MASK].subtext = current_filespecification;
507
508         /* display the menu */
509         visible = ui_menu_draw(fs_item, fs_total, sel & SEL_MASK, NULL);
510
511         /* update string with any keys that are pressed */
512         name = update_entered_string();
513
514         /* finished entering filename? */
515         if (name)
516         {
517            /* yes */
518            sel &= SEL_MASK;
519
520            /* if no name entered - go back to default all selection */
521            if (strlen(name) == 0)
522               strcpy(current_filespecification, "*");
358         case ENTTYPE_FILE:
359            if (dirent->name != NULL)
360            {
361               new_path = zippath_combine(astring_alloc(), astring_c(current_directory), dirent->name);
362               image_load(selected_device, astring_c(new_path));
363               astring_free(new_path);
364            }
523365            else
524               strcpy(current_filespecification, name);
525            fs_free();
526         }
366            {
367               image_unload(selected_device);
368            }
369            fs_state.i = ui_menu_stack_pop();
370            goto done;
527371
528         return sel + 1;
372         default:
373            /* do nothing */
374            break;
529375      }
376   }
530377
378   /* reassemble state */
379   fs_state.s.selected = selected;
380   fs_state.s.menu_is_built = menu_is_built;
531381
532      visible = ui_menu_draw(fs_item, fs_total, sel, NULL);
533
534      /* Maybe the following code up to IPT_UI_PAUSE can be replaced with a call to ui_menu_generic_keys() */
535
536      /* up backs up by one item */
537      if (input_ui_pressed_repeat(machine, IPT_UI_UP, 6))
538         sel = (sel + total - 1) % total;
539
540      /* down advances by one item */
541      if (input_ui_pressed_repeat(machine, IPT_UI_DOWN, 6))
542         sel = (sel +  1) % total;
543
544      /* page up backs up by visible_items */
545      if (input_ui_pressed_repeat(machine, IPT_UI_PAGE_UP, 6))
382done:
383   if (!menu_is_built)
384   {
385      /* time to tear down the menus */
386      for (i = 0; i < menu_items; i++)
546387      {
547         if (sel >= visible - 1)
548            sel -= visible - 1;
549         else
550            sel = 0;
388         if (item_list[i].ref != NULL)
389         {
390            free(item_list[i].ref);
391            item_list[i].ref = NULL;
392         }
551393      }
394   }
552395
553      /* page down advances by visible_items */
554      if (input_ui_pressed_repeat(machine, IPT_UI_PAGE_DOWN, 6))
555      {
556         sel += visible - 1;
557         if (sel >= total)
558            sel = total - 1;
559      }
396   return fs_state.i;
397}
560398
561      /* home goes to the start */
562      if (input_ui_pressed(machine, IPT_UI_HOME))
563         sel = 0;
564399
565      /* end goes to the last */
566      if (input_ui_pressed(machine, IPT_UI_END))
567         sel = total - 1;
568400
569      /* pause enables/disables pause */
570      if (input_ui_pressed(machine, IPT_UI_PAUSE))
571         mame_pause(machine, !mame_is_paused(machine));
401/*-------------------------------------------------
402    file_manager_render_extra - perform our
403    special rendering
404-------------------------------------------------*/
572405
573      if (input_ui_pressed(machine, IPT_UI_SELECT))
574      {
575         if (sel < SEL_MASK)
576         {
577            switch (fs_types[sel])
578            {
579            case FILESELECT_QUIT:
580               sel = -1;
581               break;
406static void file_manager_render_extra(const menu_extra *extra, float origx1, float origy1, float origx2, float origy2)
407{
408   const char *path = (selected_device != NULL) ? image_filename(selected_device) : NULL;
409   extra_text_render(extra, origx1, origy1, origx2, origy2, NULL, path);
410}
582411
583            case FILESELECT_FILESPEC:
584               start_enter_string(current_filespecification, 32, 0);
585412
586               /* flush keyboard buffer */
587               while (input_code_poll_switches(FALSE) != INPUT_CODE_INVALID)
588                  ;
589413
590               sel |= 1 << SEL_BITS; /* we'll ask for a key */
591               break;
414/*-------------------------------------------------
415    menu_file_manager - MESS-specific menu
416-------------------------------------------------*/
592417
593            case FILESELECT_FILE:
594               /* copy filename */
595               if (fs_item[sel].text == ui_getstring(UI_emptyslot))
596               {
597                  entered_filename[0] = '\0';
598               }
599               else
600               {
601                  snprintf(entered_filename, ARRAY_LENGTH(entered_filename), "%s%s%s",
602                     curdir,
603                     string_ends_with(curdir, PATH_SEPARATOR) ? "" : PATH_SEPARATOR,
604                     fs_item[sel].text);
605               }
418UINT32 menu_file_manager(running_machine *machine, UINT32 state)
419{
420   const device_config *device;
421   ui_menu_item item_list[40];
422   int menu_items = 0;
423   char buffer[2048];
424   int buffer_pos = 0;
425   const char *entry_typename;
426   const char *entry_basename;
427   int visible_items;
428   int selected_absolute_index = -1;
429   fileselector_state fs_state;
430   menu_extra extra;
606431
607               fs_free();
608               sel = -3;
609               break;
610
611            case FILESELECT_DEVICE:
612               strncpyz(curdir, fs_item[sel].text, MIN(strlen(fs_item[sel].text)+1, 260));
613               fs_free();
614               break;
615
616            case FILESELECT_DIRECTORY:
617               if( strcmp(fs_item[sel].text, "..") == 0 )
618               {
619                  char* sep = strrchr(curdir, PATH_SEPARATOR[0]);
620                  if( sep ) {
621                     *sep = '\0';
622                     sep = strrchr(curdir, PATH_SEPARATOR[0]);
623                     if( sep )
624                        *(++sep) = '\0';
625                  }
626               }
627               else if( strcmp(fs_item[sel].text, ".") == 0 )
628               {
629                  break;
630               }
631               else {
632                  strncatz(curdir, fs_item[sel].text, 260);
633                  strncatz(curdir, PATH_SEPARATOR, 260);
634               }
635               fs_free();
636               break;
637
638            default:
639               break;
640            }
641         }
642      }
643
644      if (input_ui_pressed(machine, IPT_UI_CANCEL))
645         sel = -1;
646
647      if (input_ui_pressed(machine, IPT_UI_CONFIGURE))
648         sel = -2;
649   }
650   else
432   /* possible cleanups from the file selector - ugly global variable usage */
433   selected_device = NULL;
434   if (current_directory != NULL)
651435   {
652      sel = -1;
436      astring_free(current_directory);
437      current_directory = NULL;
653438   }
654
655   if (sel == -1 || sel == -3)
656      fs_free();
657
658   if (sel == -1 || sel == -2 || sel == -3)
439   if (current_file != NULL)
659440   {
660      fs_insession = 0;
441      astring_free(current_file);
442      current_file = NULL;
661443   }
662444
663   return sel + 1;
664}
665
666static int filemanager(running_machine *machine, int selected)
667{
668   static int previous_sel;
669   const char *name;
670   ui_menu_item menu_items[40];
671   const device_config *devices[40];
672   char names[40][64];
673   UINT32 sel, total, visible, arrowize;
674   const device_config *image;
675
676   sel = selected - 1;
677   total = 0;
678
679   /* Cycle through all devices for this system */
680   for (image = image_device_first(machine->config); image != NULL; image = image_device_next(image))
445   /* cycle through all devices for this system */
446   for (device = image_device_first(machine->config); device != NULL; device = image_device_next(device))
681447   {
682      strcpy( names[total], image_typename_id(image) );
683      name = image_filename(image);
448      /* sanity check */
449      if ((buffer_pos >= ARRAY_LENGTH(buffer) || (menu_items >= ARRAY_LENGTH(item_list))))
450         break;
684451
685      memset(&menu_items[total], 0, sizeof(menu_items[total]));
686      menu_items[total].text = (names[total]) ? names[total] : "---";
687      menu_items[total].subtext = (name) ? name : "---";
688
689      devices[total] = image;
690     
691      total++;
692   }
693
694
695   /* if the fileselect() mode is active */
696   if (sel & (2 << SEL_BITS))
697   {
698      image = devices[previous_sel & SEL_MASK];
699      sel = fileselect(machine, selected & ~(2 << SEL_BITS), image_filename(image), image_working_directory(image));
700      if (sel != 0 && sel != -1 && sel!=-2)
701         return sel | (2 << SEL_BITS);
702
703      if (sel==-2)
452      /* is this selected? */
453      if (state == menu_items)
704454      {
705         /* selected a file */
706
707         /* finish entering name */
708         previous_sel = previous_sel & SEL_MASK;
709
710         /* attempt a filename change */
711         image = devices[previous_sel];
712         if (entered_filename[0])
713            image_load(image, entered_filename);
714         else
715            image_unload(image);
455         selected_device = device;
456         selected_absolute_index = image_absolute_index(device);
716457      }
717458
718      sel = previous_sel;
459      /* get the image type/id */
460      entry_typename = &buffer[buffer_pos];
461      buffer_pos += snprintf(&buffer[buffer_pos],
462         ARRAY_LENGTH(buffer) - buffer_pos,
463         "%s",
464         image_typename_id(device)) + 1;
719465
720      /* change menu item to show this filename */
721      menu_items[sel & SEL_MASK].subtext = entered_filename;
466      /* get the base name */
467      entry_basename = image_basename(device);
468
469      /* record the menu item */
470      memset(&item_list[menu_items], 0, sizeof(item_list[menu_items]));
471      item_list[menu_items].text = entry_typename;
472      item_list[menu_items].subtext = (entry_basename != NULL) ? entry_basename : "---";
473      menu_items++;
722474   }
723475
724   memset(&menu_items[total], 0, sizeof(menu_items[total]));
725   menu_items[total].text = "Return to Prior Menu";
726   total++;
476   /* add an item for the return */
477   memset(&item_list[menu_items], 0, sizeof(item_list[menu_items]));
478   item_list[menu_items++].text = "Return to Prior Menu";
727479
728   arrowize = 0;
729   if (sel < total - 1)
730      arrowize = 2;
480   /* compute extras */
481   memset(&extra, 0, sizeof(extra));
482   extra.bottom = ui_get_line_height() + 3.0f * UI_BOX_TB_BORDER;
483   extra.render = file_manager_render_extra;
731484
732   if (sel & (1 << SEL_BITS))   /* are we waiting for a new key? */
733   {
734      /* change menu item to show this filename */
735      menu_items[sel & SEL_MASK].subtext = entered_filename;
485   /* draw the menu */
486   visible_items = ui_menu_draw(item_list, menu_items, state, &extra);
736487
737      /* display the menu */
738      visible = ui_menu_draw(menu_items, total, sel & SEL_MASK, NULL);
488   /* handle the keys */
489   if (ui_menu_generic_keys(machine, &state, menu_items, visible_items))
490      return state;
739491
740      /* update string with any keys that are pressed */
741      name = update_entered_string();
742
743      /* finished entering filename? */
744      if (name)
745      {
746         /* yes */
747         sel &= SEL_MASK;
748         image = devices[sel];
749         image_load(image, NULL);
750      }
751
752      return sel + 1;
753   }
754
755   visible = ui_menu_draw(menu_items, total, sel, NULL);
756
492   /* was selected pressed? */
757493   if (input_ui_pressed(machine, IPT_UI_SELECT))
758494   {
759      int os_sel;
495      if (selected_absolute_index >= 0)
496      {
497         file_error err;
498         char *full_path;
760499
761      image = NULL;
500         err = osd_get_full_path(&full_path, image_filename(selected_device));
501         if (err != FILERR_NONE)
502            return state;
762503
763      /* Return to main menu? */
764      if (sel == total-1)
765      {
766         sel = -1;
767         os_sel = -1;
768      }
769      /* no, let the osd code have a crack at changing files */
770      else
771      {
772         image = devices[sel];
773         os_sel = 0;
774      }
504         current_directory = zippath_parent(astring_alloc(), full_path);
505         current_file = astring_cpyc(astring_alloc(), image_basename(selected_device));
506         free(full_path);
775507
776      if (os_sel != 0)
777      {
778         if (os_sel == 1)
779         {
780            /* attempt a filename change */
781            image_load(image, entered_filename);
782         }
508         memset(&fs_state, 0, sizeof(fs_state));
509         return ui_menu_stack_push(menu_file_selector, fs_state.i);
783510      }
784      /* osd code won't handle it, lets use our clunky interface */
785      else if (!UI_SHIFT_PRESSED)
786      {
787         /* save selection and switch to fileselect() */
788         previous_sel = sel;
789         sel = (2 << SEL_BITS);
790         fs_total = 0;
791      }
792511      else
793512      {
794         if (strcmp(menu_items[sel].text, "---") == 0)
795            entered_filename[0] = '\0';
796         else
797            strcpy(entered_filename, menu_items[sel].text);
798         start_enter_string(entered_filename, (sizeof(entered_filename) / sizeof(entered_filename[0])) - 1, 1);
799
800         /* flush keyboard buffer */
801         while (input_code_poll_switches(FALSE) != INPUT_CODE_INVALID)
802            ;
803
804         sel |= 1 << SEL_BITS;   /* we'll ask for a key */
513         return ui_menu_stack_pop();
805514      }
806515   }
807   else if (input_ui_pressed(machine, IPT_UI_CANCEL))
808   {
809      sel = -1;
810   }
811   else
812   {
813      ui_menu_generic_keys(machine, &sel, total, visible);
814   }
815516
816   if (input_ui_pressed(machine, IPT_UI_CONFIGURE))
817      sel = -2;
818
819   return sel + 1;
517   return state;
820518}
821
822
823
824/*-------------------------------------------------
825    menu_file_manager - MESS-specific menu
826-------------------------------------------------*/
827
828UINT32 menu_file_manager(running_machine *machine, UINT32 state)
829{
830   int result = filemanager(machine, state);
831   if (result == 0)
832      return ui_menu_stack_pop();
833   return result;
834}
src/mess/osd/windows/winmess.c
r2873r2874
22#include <windows.h>
33#include <ctype.h>
44#include <tchar.h>
5#include <shlwapi.h>
56
67#include "osdmess.h"
78#include "utils.h"
r2873r2874
201202
202203
203204//============================================================
205//   osd_get_full_path
206//============================================================
207
208file_error osd_get_full_path(char **dst, const char *path)
209{
210   file_error err;
211   TCHAR *t_path;
212   TCHAR buffer[MAX_PATH];
213
214   // convert the path to TCHARs
215   t_path = tstring_from_utf8(path);
216   if (t_path == NULL)
217   {
218      err = FILERR_OUT_OF_MEMORY;
219      goto done;
220   }
221
222   // cannonicalize the path
223   if (!GetFullPathName(path, ARRAY_LENGTH(buffer), buffer, NULL))
224   {
225      err = win_error_to_mame_file_error(GetLastError());
226      goto done;
227   }
228
229   // convert the result back to UTF-8
230   *dst = utf8_from_tstring(buffer);
231   if (!*dst)
232   {
233      err = FILERR_OUT_OF_MEMORY;
234      goto done;
235   }
236
237   err = FILERR_NONE;
238
239done:
240   if (t_path != NULL)
241      free(t_path);
242   return err;
243}
244
245
246//============================================================
204247//   osd_mkdir
205248//============================================================
206249
src/mess/osdmess.h
r2873r2874
202202char *osd_basename(char *filename);
203203
204204
205/*-----------------------------------------------------------------------------
206    osd_get_full_path: retrieves the full path
205207
208    Parameters:
209
210      path - the path in question
211      dst - pointer to receive new path; the returned string needs to be free()-ed!
212
213    Return value:
214
215        file error
216
217-----------------------------------------------------------------------------*/
218file_error osd_get_full_path(char **dst, const char *path);
219
220
221
206222/***************************************************************************
207223    UNCATEGORIZED INTERFACES
208224***************************************************************************/
docs/messnew.txt
r2873r2874
120120
121121User Interface Changes:
122122-----------------------
123- File Manager and Tape Control cleanups.  [Nate Woods]
123- File manager rewrite.  [Nate Woods]
124124
125- Tape Control cleanups.  [Nate Woods]
126
125127- Fixed default key naming of keyboard inputs for non-ASCII characters.  [Nate
126128  Woods]
127129

Previous 15994 Revisions Next


© 1998-2018 The MESS Team