Previous 15994 Revisions Next

r7341 Saturday 13th February, 2010 at 17:22:30 UTC by Miodrag Milanović
- Removed unused OSD calls
- Taken some Couriersud work from MAME in SDL osd
[src/mess]filemngr.c image.c mess.c zippath.c
[src/mess/osd/sdl]climain.c sdl.mak w32util.c winutils.c winutils.h
[src/mess/osd/windows]menu.c winutils.c winutils.h
[src/mess/tools/dat2html]dat2html.c
[src/mess/tools/imgtool]imgtool.c imgtool.h main.c
[src/mess/tools/imgtool/windows]wimgtool.c
[src/mess/tools/messtest]core.c testimgt.c testmess.c
[src/osd]osdcore.h
[src/osd/sdl]sdl.mak sdlclip.c sdlfile.c sdlos_macosx.c sdlos_unix.c sdlos_win32.c
[src/osd/windows]winfile.c

src/osd/sdl/sdlclip.c
r7340r7341
1#include <stdlib.h>
2#include <fcntl.h>
3#include <unistd.h>
4#include <ctype.h>
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <ctype.h>
8#include <errno.h>
9
10#ifdef SDLMAME_WIN32
11#define WIN32_LEAN_AND_MEAN
12#include <windows.h>
13#include <tchar.h>
14#include "strconv.h"
15#endif
16
17#ifdef SDLMAME_MACOSX
18#include <Carbon/Carbon.h>
19#endif
20
21
22#ifdef SDLMAME_MACOSX
23//============================================================
24//  osd_get_clipboard_text
25//============================================================
26
27char *osd_get_clipboard_text(void)
28{
29   char *result = NULL; /* core expects a malloced C string of uft8 data */
30
31   PasteboardRef pasteboard_ref;
32   OSStatus err;
33   PasteboardSyncFlags sync_flags;
34   PasteboardItemID item_id;
35   CFIndex flavor_count;
36   CFArrayRef flavor_type_array;
37   CFIndex flavor_index;
38   ItemCount item_count;
39   UInt32 item_index;
40   Boolean   success = false;
41
42   err = PasteboardCreate(kPasteboardClipboard, &pasteboard_ref);
43
44   if (!err)
45   {
46      sync_flags = PasteboardSynchronize( pasteboard_ref );
47
48      err = PasteboardGetItemCount(pasteboard_ref, &item_count );
49
50      for (item_index=1; item_index<=item_count; item_index++)
51      {
52         err = PasteboardGetItemIdentifier(pasteboard_ref, item_index, &item_id);
53
54         if (!err)
55         {
56            err = PasteboardCopyItemFlavors(pasteboard_ref, item_id, &flavor_type_array);
57
58            if (!err)
59            {
60               flavor_count = CFArrayGetCount(flavor_type_array);
61
62               for (flavor_index = 0; flavor_index < flavor_count; flavor_index++)
63               {
64                  CFStringRef flavor_type;
65                  CFDataRef flavor_data;
66                  CFStringEncoding encoding;
67                  CFStringRef string_ref;
68                  CFDataRef data_ref;
69                  CFIndex length;
70                  CFRange range;
71
72                  flavor_type = (CFStringRef)CFArrayGetValueAtIndex(flavor_type_array, flavor_index);
73
74                  if (UTTypeConformsTo (flavor_type, kUTTypeUTF16PlainText))
75                     encoding = kCFStringEncodingUTF16;
76                  else if (UTTypeConformsTo (flavor_type, kUTTypeUTF8PlainText))
77                     encoding = kCFStringEncodingUTF8;
78                  else if (UTTypeConformsTo (flavor_type, kUTTypePlainText))
79                     encoding = kCFStringEncodingMacRoman;
80                  else
81                     continue;
82
83                  err = PasteboardCopyItemFlavorData(pasteboard_ref, item_id, flavor_type, &flavor_data);
84
85                  if( !err )
86                  {
87                     string_ref = CFStringCreateFromExternalRepresentation (kCFAllocatorDefault, flavor_data, encoding);
88                     data_ref = CFStringCreateExternalRepresentation (kCFAllocatorDefault, string_ref, kCFStringEncodingUTF8, '?');
89
90                     length = CFDataGetLength (data_ref);
91                     range = CFRangeMake (0,length);
92
93                     result = (char *)malloc (length+1);
94                     if (result != NULL)
95                     {
96                        CFDataGetBytes (data_ref, range, (unsigned char *)result);
97                        result[length] = 0;
98                        success = true;
99                        break;
100                     }
101
102                     CFRelease(data_ref);
103                     CFRelease(string_ref);
104                     CFRelease(flavor_data);
105                  }
106               }
107
108               CFRelease(flavor_type_array);
109            }
110         }
111
112         if (success)
113            break;
114      }
115
116      CFRelease(pasteboard_ref);
117   }
118
119   return result;
120}
121#endif
122
123#ifdef SDLMAME_WIN32
124//============================================================
125//  get_clipboard_text_by_format
126//============================================================
127
128static char *get_clipboard_text_by_format(UINT format, char *(*convert)(LPCVOID data))
129{
130   char *result = NULL;
131   HANDLE data_handle;
132   LPVOID data;
133
134   // check to see if this format is available
135   if (IsClipboardFormatAvailable(format))
136   {
137      // open the clipboard
138      if (OpenClipboard(NULL))
139      {
140         // try to access clipboard data
141         data_handle = GetClipboardData(format);
142         if (data_handle != NULL)
143         {
144            // lock the data
145            data = GlobalLock(data_handle);
146            if (data != NULL)
147            {
148               // invoke the convert
149               result = (*convert)(data);
150
151               // unlock the data
152               GlobalUnlock(data_handle);
153            }
154         }
155
156         // close out the clipboard
157         CloseClipboard();
158      }
159   }
160   return result;
161}
162
163
164
165//============================================================
166//  convert_wide
167//============================================================
168
169static char *convert_wide(LPCVOID data)
170{
171   return utf8_from_wstring((LPCWSTR) data);
172}
173
174
175
176//============================================================
177//  convert_ansi
178//============================================================
179
180static char *convert_ansi(LPCVOID data)
181{
182   return utf8_from_astring((LPCSTR) data);
183}
184
185
186
187//============================================================
188//  osd_get_clipboard_text
189//============================================================
190
191char *osd_get_clipboard_text(void)
192{
193   char *result = NULL;
194
195   // try to access unicode text
196   if (result == NULL)
197      result = get_clipboard_text_by_format(CF_UNICODETEXT, convert_wide);
198
199   // try to access ANSI text
200   if (result == NULL)
201      result = get_clipboard_text_by_format(CF_TEXT, convert_ansi);
202
203   return result;
204}
205#endif
206
207#if defined(SDL_VIDEO_DRIVER_X11)
208//============================================================
209//  osd_get_clipboard_text
210//============================================================
211
212char *osd_get_clipboard_text(void)
213{
214   SDL_SysWMinfo info;
215   Display* display;
216   Window our_win;
217   Window selection_win;
218   Atom data_type;
219   int data_format;
220   unsigned long nitems;
221   unsigned long bytes_remaining;
222   unsigned char* prop;
223   char* result;
224   XEvent event;
225   Uint32 t0, t1;
226   Atom types[2];
227   int i;
228
229   /* get & validate SDL sys-wm info */
230   SDL_VERSION(&info.version);
231   if ( ! SDL_GetWMInfo( &info ) )
232      return NULL;
233   if ( info.subsystem != SDL_SYSWM_X11 )
234      return NULL;
235   if ( (display = info.info.x11.display) == NULL )
236      return NULL;
237   if ( (our_win = info.info.x11.window) == None )
238      return NULL;
239
240   /* request data to owner */
241   selection_win = XGetSelectionOwner( display, XA_PRIMARY );
242   if ( selection_win == None )
243      return NULL;
244
245   /* first, try UTF-8, then latin-1 */
246   types[0] = XInternAtom( display, "UTF8_STRING", False );
247   types[1] = XA_STRING; /* latin-1 */
248
249   for ( i = 0; i < ARRAY_LENGTH(types); i++ )
250   {
251
252      XConvertSelection( display, XA_PRIMARY, types[i], types[i], our_win, CurrentTime );
253
254      /* wait for SelectionNotify, but no more than 100 ms */
255      t0 = t1 = SDL_GetTicks();
256      while ( 1 )
257      {
258         if (  XCheckTypedWindowEvent( display, our_win,  SelectionNotify, &event ) ) break;
259         SDL_Delay( 1 );
260         t1 = SDL_GetTicks();
261         if ( t1 - t0 > 100 )
262            return NULL;
263      }
264      if ( event.xselection.property == None )
265         continue;
266
267      /* get property & check its type */
268      if ( XGetWindowProperty( display, our_win, types[i], 0, 65536, False, types[i],
269                &data_type, &data_format, &nitems, &bytes_remaining, &prop )
270           != Success )
271         continue;
272      if ( ! prop )
273         continue;
274      if ( (data_format != 8) || (data_type != types[i]) )
275      {
276         XFree( prop );
277         continue;
278      }
279
280      /* return a copy & free original */
281      result = core_strdup( (char*) prop );
282      XFree( prop );
283      return result;
284   }
285
286   return NULL;
287}
288
289#endif
290
291#if defined(SDLMAME_UNIX) && !defined(SDLMAME_MACOSX) && !defined(SDL_VIDEO_DRIVER_X11)
292//============================================================
293//  osd_get_clipboard_text
294//============================================================
295
296char *osd_get_clipboard_text(void)
297{
298   char *result = NULL;
299
300   return result;
301}
302#endif
src/osd/sdl/sdlos_macosx.c
r7340r7341
1616
1717#include <mach/mach.h>
1818#include <mach/mach_time.h>
19#include <Carbon/Carbon.h>
1920
2021// MAME headers
2122#include "osdcore.h"
r7340r7341
204205{
205206   return setenv(name, value, overwrite);
206207}
208
209
210//============================================================
211//  osd_get_clipboard_text
212//============================================================
213
214char *osd_get_clipboard_text(void)
215{
216   char *result = NULL; /* core expects a malloced C string of uft8 data */
217
218   PasteboardRef pasteboard_ref;
219   OSStatus err;
220   PasteboardSyncFlags sync_flags;
221   PasteboardItemID item_id;
222   CFIndex flavor_count;
223   CFArrayRef flavor_type_array;
224   CFIndex flavor_index;
225   ItemCount item_count;
226   UInt32 item_index;
227   Boolean   success = false;
228
229   err = PasteboardCreate(kPasteboardClipboard, &pasteboard_ref);
230
231   if (!err)
232   {
233      sync_flags = PasteboardSynchronize( pasteboard_ref );
234
235      err = PasteboardGetItemCount(pasteboard_ref, &item_count );
236
237      for (item_index=1; item_index<=item_count; item_index++)
238      {
239         err = PasteboardGetItemIdentifier(pasteboard_ref, item_index, &item_id);
240
241         if (!err)
242         {
243            err = PasteboardCopyItemFlavors(pasteboard_ref, item_id, &flavor_type_array);
244
245            if (!err)
246            {
247               flavor_count = CFArrayGetCount(flavor_type_array);
248
249               for (flavor_index = 0; flavor_index < flavor_count; flavor_index++)
250               {
251                  CFStringRef flavor_type;
252                  CFDataRef flavor_data;
253                  CFStringEncoding encoding;
254                  CFStringRef string_ref;
255                  CFDataRef data_ref;
256                  CFIndex length;
257                  CFRange range;
258
259                  flavor_type = (CFStringRef)CFArrayGetValueAtIndex(flavor_type_array, flavor_index);
260
261                  if (UTTypeConformsTo (flavor_type, kUTTypeUTF16PlainText))
262                     encoding = kCFStringEncodingUTF16;
263                  else if (UTTypeConformsTo (flavor_type, kUTTypeUTF8PlainText))
264                     encoding = kCFStringEncodingUTF8;
265                  else if (UTTypeConformsTo (flavor_type, kUTTypePlainText))
266                     encoding = kCFStringEncodingMacRoman;
267                  else
268                     continue;
269
270                  err = PasteboardCopyItemFlavorData(pasteboard_ref, item_id, flavor_type, &flavor_data);
271
272                  if( !err )
273                  {
274                     string_ref = CFStringCreateFromExternalRepresentation (kCFAllocatorDefault, flavor_data, encoding);
275                     data_ref = CFStringCreateExternalRepresentation (kCFAllocatorDefault, string_ref, kCFStringEncodingUTF8, '?');
276
277                     length = CFDataGetLength (data_ref);
278                     range = CFRangeMake (0,length);
279
280                     result = (char *)malloc (length+1);
281                     if (result != NULL)
282                     {
283                        CFDataGetBytes (data_ref, range, (unsigned char *)result);
284                        result[length] = 0;
285                        success = true;
286                        break;
287                     }
288
289                     CFRelease(data_ref);
290                     CFRelease(string_ref);
291                     CFRelease(flavor_data);
292                  }
293               }
294
295               CFRelease(flavor_type_array);
296            }
297         }
298
299         if (success)
300            break;
301      }
302
303      CFRelease(pasteboard_ref);
304   }
305
306   return result;
307}
308
309//============================================================
310//  osd_stat
311//============================================================
312
313osd_directory_entry *osd_stat(const char *path)
314{
315   int err;
316   osd_directory_entry *result = NULL;
317   #if defined(SDLMAME_DARWIN) || defined(SDLMAME_NO64BITIO)
318   struct stat st;
319   #else
320   struct stat64 st;
321   #endif
322
323   #if defined(SDLMAME_DARWIN) || defined(SDLMAME_NO64BITIO)
324   err = stat(path, &st);
325   #else
326   err = stat64(path, &st);
327   #endif
328
329   if( err == -1) return NULL;
330
331   // create an osd_directory_entry; be sure to make sure that the caller can
332   // free all resources by just freeing the resulting osd_directory_entry
333   result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1);
334   strcpy(((char *) result) + sizeof(*result), path);
335   result->name = ((char *) result) + sizeof(*result);
336   result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE;
337   result->size = (UINT64)st.st_size;
338
339   return result;
340}
341
342//============================================================
343//  osd_get_volume_name
344//============================================================
345
346const char *osd_get_volume_name(int idx)
347{
348   if (idx!=0) return NULL;
349   return "/";
350}
351
352//============================================================
353//  osd_get_full_path
354//============================================================
355
356file_error osd_get_full_path(char **dst, const char *path)
357{
358   file_error err;
359   char path_buffer[512];
360
361   err = FILERR_NONE;
362
363   if (getcwd(path_buffer, 511) == NULL)
364   {
365      printf("osd_get_full_path: failed!\n");
366      err = FILERR_FAILURE;
367   }
368   else
369   {
370      *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3);
371
372      // if it's already a full path, just pass it through
373      if (path[0] == '/')
374      {
375         strcpy(*dst, path);
376      }
377      else
378      {
379         sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path);
380      }
381   }
382
383   return err;
384}
385
386//============================================================
387//  osd_get_emulator_directory
388//============================================================
389
390void osd_get_emulator_directory(char *dir, size_t dir_size)
391{
392   strncpy(dir, sdl_cwd, dir_size);
393}
src/osd/sdl/sdlos_win32.c
r7340r7341
1818
1919// MAME headers
2020#include "osdcore.h"
21#include "strconv.h"
2122
2223//============================================================
2324//  PROTOTYPES
r7340r7341
271272
272273   return result;
273274}
275
276
277//============================================================
278//  get_clipboard_text_by_format
279//============================================================
280
281static char *get_clipboard_text_by_format(UINT format, char *(*convert)(LPCVOID data))
282{
283   char *result = NULL;
284   HANDLE data_handle;
285   LPVOID data;
286
287   // check to see if this format is available
288   if (IsClipboardFormatAvailable(format))
289   {
290      // open the clipboard
291      if (OpenClipboard(NULL))
292      {
293         // try to access clipboard data
294         data_handle = GetClipboardData(format);
295         if (data_handle != NULL)
296         {
297            // lock the data
298            data = GlobalLock(data_handle);
299            if (data != NULL)
300            {
301               // invoke the convert
302               result = (*convert)(data);
303
304               // unlock the data
305               GlobalUnlock(data_handle);
306            }
307         }
308
309         // close out the clipboard
310         CloseClipboard();
311      }
312   }
313   return result;
314}
315
316
317
318//============================================================
319//  convert_wide
320//============================================================
321
322static char *convert_wide(LPCVOID data)
323{
324   return utf8_from_wstring((LPCWSTR) data);
325}
326
327
328
329//============================================================
330//  convert_ansi
331//============================================================
332
333static char *convert_ansi(LPCVOID data)
334{
335   return utf8_from_astring((LPCSTR) data);
336}
337
338
339
340//============================================================
341//  osd_get_clipboard_text
342//============================================================
343
344char *osd_get_clipboard_text(void)
345{
346   char *result = NULL;
347
348   // try to access unicode text
349   if (result == NULL)
350      result = get_clipboard_text_by_format(CF_UNICODETEXT, convert_wide);
351
352   // try to access ANSI text
353   if (result == NULL)
354      result = get_clipboard_text_by_format(CF_TEXT, convert_ansi);
355
356   return result;
357}
358
359//============================================================
360//  astring_from_utf8
361//============================================================
362
363CHAR *astring_from_utf8(const char *utf8string)
364{
365   WCHAR *wstring;
366   int char_count;
367   CHAR *result;
368
369   // convert MAME string (UTF-8) to UTF-16
370   char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0);
371   wstring = (WCHAR *)alloca(char_count * sizeof(*wstring));
372   MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count);
373
374   // convert UTF-16 to "ANSI code page" string
375   char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL);
376   result = (CHAR *)malloc(char_count * sizeof(*result));
377   if (result != NULL)
378      WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL);
379
380   return result;
381}
382
383//============================================================
384//  wstring_from_utf8
385//============================================================
386
387WCHAR *wstring_from_utf8(const char *utf8string)
388{
389   int char_count;
390   WCHAR *result;
391
392   // convert MAME string (UTF-8) to UTF-16
393   char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0);
394   result = (WCHAR *)malloc(char_count * sizeof(*result));
395   if (result != NULL)
396      MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count);
397
398   return result;
399}
400
401//============================================================
402//  win_attributes_to_entry_type
403//============================================================
404
405static osd_dir_entry_type win_attributes_to_entry_type(DWORD attributes)
406{
407   if (attributes == 0xFFFFFFFF)
408      return ENTTYPE_NONE;
409   else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
410      return ENTTYPE_DIR;
411   else
412      return ENTTYPE_FILE;
413}
414
415//============================================================
416//  osd_stat
417//============================================================
418
419osd_directory_entry *osd_stat(const char *path)
420{
421   osd_directory_entry *result = NULL;
422   TCHAR *t_path;
423   HANDLE find = INVALID_HANDLE_VALUE;
424   WIN32_FIND_DATA find_data;
425
426   // convert the path to TCHARs
427   t_path = tstring_from_utf8(path);
428   if (t_path == NULL)
429      goto done;
430
431   // attempt to find the first file
432   find = FindFirstFile(t_path, &find_data);
433   if (find == INVALID_HANDLE_VALUE)
434      goto done;
435
436   // create an osd_directory_entry; be sure to make sure that the caller can
437   // free all resources by just freeing the resulting osd_directory_entry
438   result = (osd_directory_entry *) malloc(sizeof(*result) + strlen(path) + 1);
439   if (!result)
440      goto done;
441   strcpy(((char *) result) + sizeof(*result), path);
442   result->name = ((char *) result) + sizeof(*result);
443   result->type = win_attributes_to_entry_type(find_data.dwFileAttributes);
444   result->size = find_data.nFileSizeLow | ((UINT64) find_data.nFileSizeHigh << 32);
445
446done:
447   if (t_path)
448      free(t_path);
449   return result;
450}
451
452//============================================================
453//  osd_get_volume_name
454//============================================================
455
456const char *osd_get_volume_name(int idx)
457{
458   static char szBuffer[128];
459   const char *p;
460
461   GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer);
462
463   p = szBuffer;
464   while(idx--) {
465      p += strlen(p) + 1;
466      if (!*p) return NULL;
467   }
468
469   return p;
470}
471
472//============================================================
473//  win_error_to_mame_file_error
474//============================================================
475
476static file_error win_error_to_mame_file_error(DWORD error)
477{
478   file_error filerr;
479
480   // convert a Windows error to a file_error
481   switch (error)
482   {
483      case ERROR_SUCCESS:
484         filerr = FILERR_NONE;
485         break;
486
487      case ERROR_OUTOFMEMORY:
488         filerr = FILERR_OUT_OF_MEMORY;
489         break;
490
491      case ERROR_FILE_NOT_FOUND:
492      case ERROR_PATH_NOT_FOUND:
493         filerr = FILERR_NOT_FOUND;
494         break;
495
496      case ERROR_ACCESS_DENIED:
497         filerr = FILERR_ACCESS_DENIED;
498         break;
499
500      case ERROR_SHARING_VIOLATION:
501         filerr = FILERR_ALREADY_OPEN;
502         break;
503
504      default:
505         filerr = FILERR_FAILURE;
506         break;
507   }
508   return filerr;
509}
510
511//============================================================
512//  osd_get_full_path
513//============================================================
514
515file_error osd_get_full_path(char **dst, const char *path)
516{
517   file_error err;
518   TCHAR *t_path;
519   TCHAR buffer[MAX_PATH];
520
521   // convert the path to TCHARs
522   t_path = tstring_from_utf8(path);
523   if (t_path == NULL)
524   {
525      err = FILERR_OUT_OF_MEMORY;
526      goto done;
527   }
528
529   // cannonicalize the path
530   if (!GetFullPathName(t_path, ARRAY_LENGTH(buffer), buffer, NULL))
531   {
532      err = win_error_to_mame_file_error(GetLastError());
533      goto done;
534   }
535
536   // convert the result back to UTF-8
537   *dst = utf8_from_tstring(buffer);
538   if (!*dst)
539   {
540      err = FILERR_OUT_OF_MEMORY;
541      goto done;
542   }
543
544   err = FILERR_NONE;
545
546done:
547   if (t_path != NULL)
548      free(t_path);
549   return err;
550}
551
552//============================================================
553//  win_get_module_file_name_utf8
554//============================================================
555
556static DWORD win_get_module_file_name_utf8(HMODULE module, char *filename, DWORD size)
557{
558   TCHAR t_filename[MAX_PATH];
559   char *utf8_filename;
560
561   if (GetModuleFileName(module, t_filename, ARRAY_LENGTH(t_filename)) == 0)
562      return 0;
563
564   utf8_filename = utf8_from_tstring(t_filename);
565   if (!utf8_filename)
566      return 0;
567
568   size = (DWORD) snprintf(filename, size, "%s", utf8_filename);
569   free(utf8_filename);
570   return size;
571}
572
573//============================================================
574//  osd_get_emulator_directory
575//============================================================
576
577void osd_get_emulator_directory(char *dir, size_t dir_size)
578{
579   char *s;
580   win_get_module_file_name_utf8(NULL, dir, dir_size);
581   s = strrchr(dir, '\\');
582   if (s)
583      s[1] = '\0';
584}
No newline at end of file
src/osd/sdl/sdl.mak
r7340r7341
9494else
9595ifeq ($(DISTRO),gcc44-generic)
9696CC = @gcc -V 4.4
97LD = @g++ -V 4.4
97LD = @g++-4.4
9898else
9999$(error DISTRO $(DISTRO) unknown)
100100endif
r7340r7341
257257   $(SDLOBJ)/strconv.o   \
258258   $(SDLOBJ)/sdldir.o   \
259259   $(SDLOBJ)/sdlfile.o    \
260   $(SDLOBJ)/sdlclip.o    \
261260   $(SDLOBJ)/sdlmisc_$(BASE_TARGETOS).o   \
262261   $(SDLOBJ)/sdlos_$(BASE_TARGETOS).o   \
263262   $(SDLOBJ)/sdlsync_$(SYNC_IMPLEMENTATION).o     \
src/osd/sdl/sdlos_unix.c
r7340r7341
1515#include <time.h>
1616#include <sys/time.h>
1717
18#include <SDL/SDL.h>
19#include <SDL/SDL_syswm.h>
20
1821// MAME headers
1922#include "osdcore.h"
2023
r7340r7341
119122{
120123   return setenv(name, value, overwrite);
121124}
125
126
127#if defined(SDL_VIDEO_DRIVER_X11) && defined(SDLMAME_X11)
128
129//============================================================
130//  osd_get_clipboard_text
131//============================================================
132
133char *osd_get_clipboard_text(void)
134{
135   SDL_SysWMinfo info;
136   Display* display;
137   Window our_win;
138   Window selection_win;
139   Atom data_type;
140   int data_format;
141   unsigned long nitems;
142   unsigned long bytes_remaining;
143   unsigned char* prop;
144   char* result;
145   XEvent event;
146   Uint32 t0, t1;
147   Atom types[2];
148   int i;
149
150   /* get & validate SDL sys-wm info */
151   SDL_VERSION(&info.version);
152   if ( ! SDL_GetWMInfo( &info ) )
153      return NULL;
154   if ( info.subsystem != SDL_SYSWM_X11 )
155      return NULL;
156   if ( (display = info.info.x11.display) == NULL )
157      return NULL;
158   if ( (our_win = info.info.x11.window) == None )
159      return NULL;
160
161   /* request data to owner */
162   selection_win = XGetSelectionOwner( display, XA_PRIMARY );
163   if ( selection_win == None )
164      return NULL;
165
166   /* first, try UTF-8, then latin-1 */
167   types[0] = XInternAtom( display, "UTF8_STRING", False );
168   types[1] = XA_STRING; /* latin-1 */
169
170   for ( i = 0; i < ARRAY_LENGTH(types); i++ )
171   {
172
173      XConvertSelection( display, XA_PRIMARY, types[i], types[i], our_win, CurrentTime );
174
175      /* wait for SelectionNotify, but no more than 100 ms */
176      t0 = t1 = SDL_GetTicks();
177      while ( 1 )
178      {
179         if (  XCheckTypedWindowEvent( display, our_win,  SelectionNotify, &event ) ) break;
180         SDL_Delay( 1 );
181         t1 = SDL_GetTicks();
182         if ( t1 - t0 > 100 )
183            return NULL;
184      }
185      if ( event.xselection.property == None )
186         continue;
187
188      /* get property & check its type */
189      if ( XGetWindowProperty( display, our_win, types[i], 0, 65536, False, types[i],
190                &data_type, &data_format, &nitems, &bytes_remaining, &prop )
191           != Success )
192         continue;
193      if ( ! prop )
194         continue;
195      if ( (data_format != 8) || (data_type != types[i]) )
196      {
197         XFree( prop );
198         continue;
199      }
200
201      /* return a copy & free original */
202      if (prop != NULL)
203      {
204         result = (char *) osd_malloc(strlen((char *)prop)+1);
205         strcpy(result, (char *)prop);
206      }
207      else
208         result = NULL;
209      XFree( prop );
210      return result;
211   }
212
213   return NULL;
214}
215
216#else
217//============================================================
218//  osd_get_clipboard_text
219//============================================================
220
221char *osd_get_clipboard_text(void)
222{
223   char *result = NULL;
224
225   return result;
226}
227#endif
228
229//============================================================
230//  osd_stat
231//============================================================
232
233osd_directory_entry *osd_stat(const char *path)
234{
235   int err;
236   osd_directory_entry *result = NULL;
237   #if defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD)
238   struct stat st;
239   #else
240   struct stat64 st;
241   #endif
242
243   #if defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD)
244   err = stat(path, &st);
245   #else
246   err = stat64(path, &st);
247   #endif
248
249   if( err == -1) return NULL;
250
251   // create an osd_directory_entry; be sure to make sure that the caller can
252   // free all resources by just freeing the resulting osd_directory_entry
253   result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1);
254   strcpy(((char *) result) + sizeof(*result), path);
255   result->name = ((char *) result) + sizeof(*result);
256   result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE;
257   result->size = (UINT64)st.st_size;
258
259   return result;
260}
261
262//============================================================
263//  osd_get_volume_name
264//============================================================
265
266const char *osd_get_volume_name(int idx)
267{
268   if (idx!=0) return NULL;
269   return "/";
270}
271
272//============================================================
273//  osd_get_full_path
274//============================================================
275
276file_error osd_get_full_path(char **dst, const char *path)
277{
278   file_error err;
279   char path_buffer[512];
280
281   err = FILERR_NONE;
282
283   if (getcwd(path_buffer, 511) == NULL)
284   {
285      printf("osd_get_full_path: failed!\n");
286      err = FILERR_FAILURE;
287   }
288   else
289   {
290      *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3);
291
292      // if it's already a full path, just pass it through
293      if (path[0] == '/')
294      {
295         strcpy(*dst, path);
296      }
297      else
298      {
299         sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path);
300      }
301   }
302
303   return err;
304}
305
306//============================================================
307//  osd_get_emulator_directory
308//============================================================
309
310void osd_get_emulator_directory(char *dir, size_t dir_size)
311{
312   strncpy(dir, sdl_cwd, dir_size);
313}
src/osd/sdl/sdlfile.c
r7340r7341
395395/*      osd_is_path_separator */
396396/*============================================================ */
397397
398int osd_is_path_separator(char c)
398static int osd_is_path_separator(char c)
399399{
400400        return (c == '/') || (c == '\\');
401401}
r7340r7341
422422                result = FALSE;
423423        return result;
424424}
425
426/* not used anywhere */
427#if 0
428//============================================================
429//  osd_mkdir
430//============================================================
431
432file_error osd_mkdir(const char *dir)
433{
434   #ifdef SDLMAME_WIN32
435   if (mkdir(dir) != 0)
436   #else
437   if (mkdir(dir, 0666) != 0)
438   #endif
439   {
440      return error_to_file_error(errno);
441   }
442
443   return FILERR_NONE;
444}
445
446//============================================================
447//  osd_rmdir
448//============================================================
449
450static file_error osd_rmdir(const char *dir)
451{
452   if (rmdir(dir) != 0)
453   {
454      return error_to_file_error(errno);
455   }
456
457   return FILERR_NONE;
458}
459#endif
460
461// these are MESS specific - DO NOT TOUCH!!@!
462#ifdef MESS
463
464//============================================================
465//  osd_copyfile
466//  FIXME: this will not work with blanks in filename ...
467//============================================================
468
469file_error osd_copyfile(const char *destfile, const char *srcfile)
470{
471   char command[1024];
472
473   sprintf(command, "cp %s %s\n", srcfile, destfile);
474   system(command);
475
476   return FILERR_NONE;
477}
478
479//============================================================
480//  osd_stat
481//============================================================
482
483#ifndef SDLMAME_WIN32
484osd_directory_entry *osd_stat(const char *path)
485{
486   int err;
487   osd_directory_entry *result = NULL;
488   #if defined(SDLMAME_DARWIN) || defined(SDLMAME_WIN32) || defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) || defined(SDLMAME_OS2)
489   struct stat st;
490   #else
491   struct stat64 st;
492   #endif
493
494   #if defined(SDLMAME_DARWIN) || defined(SDLMAME_WIN32) || defined(SDLMAME_NO64BITIO) || defined(SDLMAME_BSD) || defined(SDLMAME_OS2)
495   err = stat(path, &st);
496   #else
497   err = stat64(path, &st);
498   #endif
499
500   if( err == -1) return NULL;
501
502   // create an osd_directory_entry; be sure to make sure that the caller can
503   // free all resources by just freeing the resulting osd_directory_entry
504   result = (osd_directory_entry *) osd_malloc(sizeof(*result) + strlen(path) + 1);
505   strcpy(((char *) result) + sizeof(*result), path);
506   result->name = ((char *) result) + sizeof(*result);
507   result->type = S_ISDIR(st.st_mode) ? ENTTYPE_DIR : ENTTYPE_FILE;
508   result->size = (UINT64)st.st_size;
509
510   return result;
511}
512#endif
513#endif
514
515//============================================================
516//   osd_getcurdir
517//============================================================
518
519file_error osd_getcurdir(char *buffer, size_t buffer_len)
520{
521   file_error filerr = FILERR_NONE;
522
523   if (getcwd(buffer, buffer_len) == NULL)
524   {
525      filerr = FILERR_FAILURE;
526   }
527
528   if( filerr != FILERR_FAILURE )
529   {
530      if( strcmp( &buffer[strlen(buffer)-1], PATH_SEPARATOR ) != 0 )
531      {
532              strncat( buffer, PATH_SEPARATOR, buffer_len );
533      }
534   }
535
536   return filerr;
537}
538
539//============================================================
540//  osd_get_full_path
541//============================================================
542
543file_error osd_get_full_path(char **dst, const char *path)
544{
545   file_error err;
546   char path_buffer[512];
547
548   err = FILERR_NONE;
549
550   if (getcwd(path_buffer, 511) == NULL)
551   {
552      printf("osd_get_full_path: failed!\n");
553      err = FILERR_FAILURE;
554   }
555   else
556   {
557      *dst = (char *)malloc(strlen(path_buffer)+strlen(path)+3);
558
559      // if it's already a full path, just pass it through
560      if (path[0] == '/')
561      {
562         strcpy(*dst, path);
563      }
564      else
565      {
566         sprintf(*dst, "%s%s%s", path_buffer, PATH_SEPARATOR, path);
567      }
568   }
569
570   return err;
571}
572
573//============================================================
574//  osd_setcurdir
575//============================================================
576
577file_error osd_setcurdir(const char *dir)
578{
579   file_error filerr = FILERR_NONE;
580
581   if (chdir(dir) != 0)
582   {
583      filerr = FILERR_FAILURE;
584   }
585
586   return filerr;
587}
588
589//============================================================
590//  osd_basename
591//============================================================
592
593char *osd_basename(char *filename)
594{
595   char *c;
596
597   // NULL begets NULL
598   if (!filename)
599      return NULL;
600
601   // start at the end and return when we hit a slash or colon
602   for (c = filename + strlen(filename) - 1; c >= filename; c--)
603      if (*c == '\\' || *c == '/' || *c == ':')
604         return c + 1;
605
606   // otherwise, return the whole thing
607   return filename;
608}
609
610//============================================================
611//  osd_dirname
612//============================================================
613
614char *osd_dirname(const char *filename)
615{
616   char *dirname;
617   char *c;
618
619   // NULL begets NULL
620   if (!filename)
621      return NULL;
622
623   // allocate space for it
624   dirname = (char*)malloc(strlen(filename) + 1);
625   if (!dirname)
626      return NULL;
627
628   // copy in the name
629   strcpy(dirname, filename);
630
631   // search backward for a slash or a colon
632   for (c = dirname + strlen(dirname) - 1; c >= dirname; c--)
633      if (*c == '\\' || *c == '/' || *c == ':')
634      {
635         // found it: NULL terminate and return
636         *(c + 1) = 0;
637         return dirname;
638      }
639
640   // otherwise, return an empty string
641   dirname[0] = 0;
642   return dirname;
643}
644
645// no idea what these are for, just cheese them
646int osd_num_devices(void)
647{
648   return 1;
649}
650
651const char *osd_get_device_name(int idx)
652{
653   return "/";
654}
655
656//============================================================
657//  osd_get_temp_filename
658//============================================================
659
660file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename)
661{
662   char tempbuf[512];
663
664   if (!basename)
665      basename = "tempfile";
666
667   sprintf(tempbuf, "/tmp/%s", basename);
668   unlink(tempbuf);
669
670   strncpy(buffer, tempbuf, buffer_len);
671
672   return FILERR_NONE;
673}
674
675//============================================================
676//  osd_mkdir
677//============================================================
678
679file_error osd_mkdir(const char *dir)
680{
681   file_error filerr = FILERR_NONE;
682
683   #ifdef SDLMAME_WIN32
684   if (mkdir(dir) != 0)
685   #else
686   if (mkdir(dir, 0700) != 0)
687   #endif
688   {
689      filerr = FILERR_FAILURE;
690   }
691
692   return filerr;
693}
694
695//============================================================
696//  osd_rmdir
697//============================================================
698
699file_error osd_rmdir(const char *dir)
700{
701   file_error filerr = FILERR_NONE;
702
703   if (rmdir(dir) != 0)
704   {
705      filerr = FILERR_FAILURE;
706   }
707
708   return filerr;
709}
710
711
712//============================================================
713//  osd_get_emulator_directory
714//============================================================
715
716void osd_get_emulator_directory(char *dir, size_t dir_size)
717{
718   strncpy(dir, sdl_cwd, dir_size);
719}
src/osd/osdcore.h
r7340r7341
5050
5151#include "osdcomm.h"
5252
53
54
5355/***************************************************************************
5456    FILE I/O INTERFACES
5557***************************************************************************/
r7340r7341
8486/* osd_file is an opaque type which represents an open file */
8587typedef struct _osd_file osd_file;
8688
87enum
88{
89   OSD_FOPEN_READ,
90   OSD_FOPEN_WRITE,
91   OSD_FOPEN_RW,
92   OSD_FOPEN_RW_CREATE
93};
94
95
96struct _mame_file;
97
98
9989/*-----------------------------------------------------------------------------
10090    osd_open: open a new file.
10191
r7340r7341
819809-----------------------------------------------------------------------------*/
820810void osd_break_into_debugger(const char *message);
821811
822/***************************************************************************
823    FILE I/O INTERFACES
824***************************************************************************/
825812
826/*-----------------------------------------------------------------------------
827    osd_copyfile: copies a file
828
829    Parameters:
830
831        destfile - path to destination
832
833        srcfile - path to source file
834
835    Return value:
836
837        a file_error describing any error that occurred while copying
838        the file, or FILERR_NONE if no error occurred
839-----------------------------------------------------------------------------*/
840file_error osd_copyfile(const char *destfile, const char *srcfile);
841
842
843/*-----------------------------------------------------------------------------
844    osd_get_temp_filename: given a filename, return a full path with that
845    filename but in a temporary directory
846
847    Parameters:
848
849        buffer - pointer to buffer
850
851        buffer_len - length of buffer
852
853        basename - basename of file to create
854
855    Return value:
856
857        a file_error describing any error that occurred while copying
858        the file, or FILERR_NONE if no error occurred
859-----------------------------------------------------------------------------*/
860file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename);
861
862
863813/***************************************************************************
864814    DIRECTORY INTERFACES
865815***************************************************************************/
r7340r7341
879829-----------------------------------------------------------------------------*/
880830osd_directory_entry *osd_stat(const char *path);
881831
882
883/*-----------------------------------------------------------------------------
884    osd_mkdir: creates a directory
885
886    Parameters:
887
888        dir - path to directory to create
889
890    Return value:
891
892        a file_error describing any error that occurred while creating
893        the directory, or FILERR_NONE if no error occurred
894-----------------------------------------------------------------------------*/
895file_error osd_mkdir(const char *dir);
896
897
898/*-----------------------------------------------------------------------------
899    osd_rmdir: removes a directory
900
901    Parameters:
902
903        dir - path to directory to removes
904
905    Return value:
906
907        a file_error describing any error that occurred while deleting
908        the directory, or FILERR_NONE if no error occurred
909-----------------------------------------------------------------------------*/
910file_error osd_rmdir(const char *dir);
911
912
913/*-----------------------------------------------------------------------------
914    osd_getcurdir: retrieves the current working directory
915
916    Parameters:
917
918        buffer - place to store current working directory
919
920        buffer_len - length of buffer
921
922    Return value:
923
924        a file_error describing any error that occurred while deleting
925        the directory, or FILERR_NONE if no error occurred
926-----------------------------------------------------------------------------*/
927file_error osd_getcurdir(char *buffer, size_t buffer_len);
928
929
930/*-----------------------------------------------------------------------------
931    osd_setcurdir: sets the current working directory
932
933    Parameters:
934
935        dir - path to directory to which to change
936
937    Return value:
938
939        a file_error describing any error that occurred while deleting
940        the directory, or FILERR_NONE if no error occurred
941-----------------------------------------------------------------------------*/
942file_error osd_setcurdir(const char *dir);
943
944
945832/***************************************************************************
946833    PATH INTERFACES
947834***************************************************************************/
r7340r7341
959846void osd_get_emulator_directory(char *dir, size_t dir_size);
960847
961848/*-----------------------------------------------------------------------------
962    osd_is_path_separator: returns whether a character is a path separator
963
964    Parameters:
965
966        c - the character in question
967
968    Return value:
969
970        non-zero if the character is a path separator, zero otherwise
971
972-----------------------------------------------------------------------------*/
973int osd_is_path_separator(char c);
974
975
976/*-----------------------------------------------------------------------------
977    osd_dirname: returns the base directory of a file path
978
979    Parameters:
980
981        filename - the path in question
982
983    Return value:
984
985        an allocated path to the directory containing this file
986
987-----------------------------------------------------------------------------*/
988char *osd_dirname(const char *filename);
989
990
991/*-----------------------------------------------------------------------------
992    osd_basename: returns the file or directory name from a full path
993
994    Parameters:
995
996        filename - the path in question
997
998    Return value:
999
1000        a pointer to the base name of the file
1001
1002-----------------------------------------------------------------------------*/
1003char *osd_basename(char *filename);
1004
1005
1006/*-----------------------------------------------------------------------------
1007849    osd_get_full_path: retrieves the full path
1008850
1009851    Parameters:
r7340r7341
1019861file_error osd_get_full_path(char **dst, const char *path);
1020862
1021863
1022
1023864/***************************************************************************
1024865    UNCATEGORIZED INTERFACES
1025866***************************************************************************/
1026867
868/*-----------------------------------------------------------------------------
869    osd_get_clipboard_text: retrieves text from the clipboard
1027870
1028/* reads text from the clipboard - the returned string needs to be free()-ed! */
1029char *osd_get_clipboard_text(void);
871    Return value:
1030872
873        the returned string needs to be free()-ed!
1031874
875-----------------------------------------------------------------------------*/
876char *osd_get_clipboard_text(void);
1032877
1033/******************************************************************************
878/*-----------------------------------------------------------------------------
879    osd_get_volume_name: retrieves the volume name
1034880
1035  Device and file browsing
881  Parameters:
1036882
1037******************************************************************************/
883        idx - order number of volume
1038884
1039int osd_num_devices(void);
1040const char *osd_get_device_name(int idx);
885    Return value:
1041886
887        pointer to volume name
1042888
889-----------------------------------------------------------------------------*/
890const char *osd_get_volume_name(int idx);
891
1043892#endif   /* __OSDEPEND_H__ */
src/osd/windows/winfile.c
r7340r7341
413413   return filerr;
414414}
415415
416
417416//============================================================
418//  osd_get_temp_filename
419//============================================================
420
421file_error osd_get_temp_filename(char *buffer, size_t buffer_len, const char *basename)
422{
423   TCHAR tempbuf[MAX_PATH];
424   TCHAR *t_filename;
425   char *u_tempbuf;
426
427   if (!basename)
428      basename = "tempfile.tmp";
429
430   GetTempPath(ARRAY_LENGTH(tempbuf), tempbuf);
431   t_filename = tstring_from_utf8(basename);
432   _tcscat(tempbuf, t_filename);
433   free(t_filename);
434   DeleteFile(tempbuf);
435
436   u_tempbuf = utf8_from_tstring(tempbuf);
437   snprintf(buffer, buffer_len, "%s", u_tempbuf);
438   free(u_tempbuf);
439
440   return FILERR_NONE;
441}
442
443
444//============================================================
445//  osd_copyfile
446//============================================================
447
448file_error osd_copyfile(const char *destfile, const char *srcfile)
449{
450   // wrapper for win_copy_file_utf8()
451   if (!win_copy_file_utf8(srcfile, destfile, TRUE))
452      return win_error_to_mame_file_error(GetLastError());
453
454   return FILERR_NONE;
455}
456
457
458//============================================================
459417//  osd_stat
460418//============================================================
461419
r7340r7341
502460   return result;
503461}
504462
505
506
507463//============================================================
508//  osd_is_path_separator
509//============================================================
510
511int osd_is_path_separator(char c)
512{
513   return (c == '/') || (c == '\\');
514}
515
516
517
518//============================================================
519//  osd_dirname
520//============================================================
521
522char *osd_dirname(const char *filename)
523{
524   char *dirname;
525   char *c;
526
527   // NULL begets NULL
528   if (!filename)
529      return NULL;
530
531   // allocate space for it
532   dirname = (char*)malloc(strlen(filename) + 1);
533   if (!dirname)
534      return NULL;
535
536   // copy in the name
537   strcpy(dirname, filename);
538
539   // search backward for a slash or a colon
540   for (c = dirname + strlen(dirname) - 1; c >= dirname; c--)
541      if (*c == '\\' || *c == '/' || *c == ':')
542      {
543         // found it: NULL terminate and return
544         *(c + 1) = 0;
545         return dirname;
546      }
547
548   // otherwise, return an empty string
549   dirname[0] = 0;
550   return dirname;
551}
552
553
554//============================================================
555//  osd_basename
556//============================================================
557
558char *osd_basename(char *filename)
559{
560   char *c;
561
562   // NULL begets NULL
563   if (!filename)
564      return NULL;
565
566   // start at the end and return when we hit a slash or colon
567   for (c = filename + strlen(filename) - 1; c >= filename; c--)
568      if (*c == '\\' || *c == '/' || *c == ':')
569         return c + 1;
570
571   // otherwise, return the whole thing
572   return filename;
573}
574
575
576//============================================================
577464//  osd_get_full_path
578465//============================================================
579466
r7340r7341
614501   return err;
615502}
616503
617
618504//============================================================
619//  osd_mkdir
505//  osd_get_volume_name
620506//============================================================
621507
622file_error osd_mkdir(const char *dir)
508const char *osd_get_volume_name(int idx)
623509{
624   file_error filerr = FILERR_NONE;
625
626   TCHAR *tempstr = tstring_from_utf8(dir);
627   if (!tempstr)
628   {
629      filerr = FILERR_OUT_OF_MEMORY;
630      goto done;
631   }
632
633   if (!CreateDirectory(tempstr, NULL))
634   {
635      filerr = win_error_to_mame_file_error(GetLastError());
636      goto done;
637   }
638
639done:
640   if (tempstr)
641      free(tempstr);
642   return filerr;
643}
644
645
646//============================================================
647//  osd_rmdir
648//============================================================
649
650file_error osd_rmdir(const char *dir)
651{
652   file_error filerr = FILERR_NONE;
653
654   TCHAR *tempstr = tstring_from_utf8(dir);
655   if (!tempstr)
656   {
657      filerr = FILERR_OUT_OF_MEMORY;
658      goto done;
659   }
660
661   if (!RemoveDirectory(tempstr))
662   {
663      filerr = win_error_to_mame_file_error(GetLastError());
664      goto done;
665   }
666
667done:
668   if (tempstr)
669      free(tempstr);
670   return filerr;
671}
672
673
674//============================================================
675//  osd_getcurdir
676//============================================================
677
678file_error osd_getcurdir(char *buffer, size_t buffer_len)
679{
680   file_error filerr = FILERR_NONE;
681   char *tempstr = NULL;
682   TCHAR path[_MAX_PATH];
683
684   if (!GetCurrentDirectory(ARRAY_LENGTH(path), path))
685   {
686      filerr = win_error_to_mame_file_error(GetLastError());
687      goto done;
688   }
689
690   tempstr = utf8_from_tstring(path);
691   if (!tempstr)
692   {
693      filerr = FILERR_OUT_OF_MEMORY;
694      goto done;
695   }
696   snprintf(buffer, buffer_len, "%s", tempstr);
697
698done:
699   if (tempstr)
700      free(tempstr);
701   return filerr;
702}
703
704
705//============================================================
706//  osd_setcurdir
707//============================================================
708
709file_error osd_setcurdir(const char *dir)
710{
711   file_error filerr = FILERR_NONE;
712
713   TCHAR *tempstr = tstring_from_utf8(dir);
714   if (!tempstr)
715   {
716      filerr = FILERR_OUT_OF_MEMORY;
717      goto done;
718   }
719
720   if (!SetCurrentDirectory(tempstr))
721   {
722      filerr = win_error_to_mame_file_error(GetLastError());
723      goto done;
724   }
725
726done:
727   if (tempstr)
728      free(tempstr);
729   return filerr;
730}
731
732
733//============================================================
734//  DIRECTORIES
735//============================================================
736
737int osd_num_devices(void)
738{
739   char szBuffer[128];
740   char *p;
741   int i;
742
743   GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer);
744
745   i = 0;
746   for (p = szBuffer; *p; p += (strlen(p) + 1))
747      i++;
748   return i;
749}
750
751const char *osd_get_device_name(int idx)
752{
753510   static char szBuffer[128];
754511   const char *p;
755512
756513   GetLogicalDriveStringsA(ARRAY_LENGTH(szBuffer), szBuffer);
757514
758515   p = szBuffer;
759   while(idx--)
516   while(idx--) {
760517      p += strlen(p) + 1;
518      if (!*p) return NULL;
519   }
761520
762521   return p;
763522}
764523
524//============================================================
525//  osd_get_emulator_directory
526//============================================================
527
765528void osd_get_emulator_directory(char *dir, size_t dir_size)
766529{
767530   char *s;
src/mess/mess.c
r7340r7341
3030      "with these files is a violation of copyright law and should be promptly\n"
3131      "reported to the authors so that appropriate legal action can be taken.\n\n";
3232
33static char *filename_basename(char *filename)
34{
35   char *c;
36
37   // NULL begets NULL
38   if (!filename)
39      return NULL;
40
41   // start at the end and return when we hit a slash or colon
42   for (c = filename + strlen(filename) - 1; c >= filename; c--)
43      if (*c == '\\' || *c == '/' || *c == ':')
44         return c + 1;
45
46   // otherwise, return the whole thing
47   return filename;
48}
49     
3350/*-------------------------------------------------
3451    mess_predevice_init - initialize devices for a specific
3552    running_machine
r7340r7341
8097               /* FIXME: image_name is always empty in this message because of the image_unload_all() call */
8198               fatalerror_exitcode(machine, MAMERR_DEVICE, "Device %s load (%s) failed: %s\n",
8299                  info.name,
83                  osd_basename((char *) image_name),
100                  filename_basename((char *) image_name),
84101                  image_err);
85102            }
86103         }
src/mess/tools/dat2html/dat2html.c
r7340r7341
113113      return 1;
114114   }
115115
116   osd_mkdir(html_directory);
116   //osd_mkdir(html_directory);
117117
118118   /* Head of the index html file */
119119   fprintf(html, "<html>\n");
src/mess/tools/messtest/testimgt.c
r7340r7341
4141static const char *tempfile_name(void)
4242{
4343   static char buffer[256];
44   osd_get_temp_filename(buffer, ARRAY_LENGTH(buffer), NULL);
44//   osd_get_temp_filename(buffer, ARRAY_LENGTH(buffer), NULL);
4545   return buffer;
4646}
4747
src/mess/tools/messtest/testmess.c
r7340r7341
775775   {
776776      snprintf(buf, ARRAY_LENGTH(buf),   "%s.%s",
777777         current_testcase.name, file_extensions);
778      osd_get_temp_filename(buf, ARRAY_LENGTH(buf), buf);
778      //osd_get_temp_filename(buf, ARRAY_LENGTH(buf), buf);
779779      filename = buf;
780780   }
781781
src/mess/tools/messtest/core.c
r7340r7341
499499   file_error filerr;
500500   core_file *file;
501501   int result = -1;
502   char *script_directory;
502//   char *script_directory;
503503   xml_parse_options parse_options;
504504   xml_parse_error parse_error;
505505   xml_data_node *root_node = NULL;
r7340r7341
518518
519519   /* save the current working directory, and change to the test directory */
520520   saved_directory[0] = '\0';
521   if (!opts->preserve_directory)
521/*   if (!opts->preserve_directory)
522522   {
523523      script_directory = osd_dirname(opts->script_filename);
524524      if (script_directory)
r7340r7341
527527         osd_setcurdir(script_directory);
528528         free(script_directory);
529529      }
530   }
530   }*/
531531
532532   /* set up parse options */
533533   memset(&parse_options, 0, sizeof(parse_options));
r7340r7341
557557
558558done:
559559   /* restore the directory */
560   if (saved_directory[0])
561      osd_setcurdir(saved_directory);
560//   if (saved_directory[0])
561//      osd_setcurdir(saved_directory);
562562   if (file != NULL)
563563      core_fclose(file);
564564   if (root_node != NULL)
src/mess/tools/imgtool/windows/wimgtool.c
r7340r7341
7373   return info;
7474}
7575
76static DWORD win_get_file_attributes_utf8(const char *filename)
77{
78   DWORD result = ~0;
79   LPTSTR t_filename;
7680
81   t_filename = tstring_from_utf8(filename);
82   if (t_filename != NULL)
83   {
84      result = GetFileAttributes(t_filename);
85      free(t_filename);
86   }
87   return result;
88}
7789
7890struct foreach_entry
7991{
r7340r7341
176188   switch(ERRORSOURCE(err))
177189   {
178190      case IMGTOOLERR_SRC_IMAGEFILE:
179         source = osd_basename((char *) imagename);
191         source = imgtool_basename((char *) imagename);
180192         break;
181193      case IMGTOOLERR_SRC_FILEONIMAGE:
182194         source = filename;
r7340r7341
657669            file_title, imageinfo);
658670      }
659671
660      statusbar_text[0] = osd_basename((char *) info->filename);
672      statusbar_text[0] = imgtool_basename((char *) info->filename);
661673      statusbar_text[1] = imgtool_image_module(info->image)->description;
662674
663675      free(file_title);
r7340r7341
870882   return NULL;
871883}
872884
885//============================================================
886//  win_error_to_mame_file_error
887//============================================================
873888
889static file_error win_error_to_mame_file_error(DWORD error)
890{
891   file_error filerr;
874892
893   // convert a Windows error to a file_error
894   switch (error)
895   {
896      case ERROR_SUCCESS:
897         filerr = FILERR_NONE;
898         break;
899
900      case ERROR_OUTOFMEMORY:
901         filerr = FILERR_OUT_OF_MEMORY;
902         break;
903
904      case ERROR_FILE_NOT_FOUND:
905      case ERROR_PATH_NOT_FOUND:
906         filerr = FILERR_NOT_FOUND;
907         break;
908
909      case ERROR_ACCESS_DENIED:
910         filerr = FILERR_ACCESS_DENIED;
911         break;
912
913      case ERROR_SHARING_VIOLATION:
914         filerr = FILERR_ALREADY_OPEN;
915         break;
916
917      default:
918         filerr = FILERR_FAILURE;
919         break;
920   }
921   return filerr;
922}
923
924//============================================================
925//  win_mkdir
926//============================================================
927
928file_error win_mkdir(const char *dir)
929{
930   file_error filerr = FILERR_NONE;
931
932   TCHAR *tempstr = tstring_from_utf8(dir);
933   if (!tempstr)
934   {
935      filerr = FILERR_OUT_OF_MEMORY;
936      goto done;
937   }
938
939   if (!CreateDirectory(tempstr, NULL))
940   {
941      filerr = win_error_to_mame_file_error(GetLastError());
942      goto done;
943   }
944
945done:
946   if (tempstr)
947      free(tempstr);
948   return filerr;
949}
950
951
875952static imgtoolerr_t get_recursive_directory(imgtool_partition *partition, const char *path, LPCSTR local_path)
876953{
877954   imgtoolerr_t err;
r7340r7341
880957   const char *subpath;
881958   char local_subpath[MAX_PATH];
882959
883   if (osd_mkdir(local_path) != FILERR_NONE)
960   if (win_mkdir(local_path) != FILERR_NONE)
884961   {
885962      err = IMGTOOLERR_UNEXPECTED;
886963      goto done;
r7340r7341
16381715
16391716      // figure out the file/dir name on the image
16401717      snprintf(subpath, ARRAY_LENGTH(subpath), "%s%s",
1641         info->current_directory ? info->current_directory : "", osd_basename(filename));
1718         info->current_directory ? info->current_directory : "", imgtool_basename(filename));
16421719
16431720      if (GetFileAttributes(buffer) & FILE_ATTRIBUTE_DIRECTORY)
16441721         err = put_recursive_directory(info->partition, buffer, subpath);
src/mess/tools/imgtool/imgtool.c
r7340r7341
121121   return err;
122122}
123123
124char *imgtool_basename(char *filename)
125{
126   char *c;
124127
128   // NULL begets NULL
129   if (!filename)
130      return NULL;
125131
132   // start at the end and return when we hit a slash or colon
133   for (c = filename + strlen(filename) - 1; c >= filename; c--)
134      if (*c == '\\' || *c == '/' || *c == ':')
135         return c + 1;
136
137   // otherwise, return the whole thing
138   return filename;
139}
140
126141/*-------------------------------------------------
127142    internal_error - debug function for raising
128143    internal errors
r7340r7341
20952110   char *alloc_newfname = NULL;
20962111
20972112   if (!newfname)
2098      newfname = (const char *) osd_basename((char *) source);
2113      newfname = (const char *) imgtool_basename((char *) source);
20992114
21002115   charset = (imgtool_charset) (int) imgtool_partition_get_info_int(partition, IMGTOOLINFO_INT_CHARSET);
21012116   if (charset != IMGTOOL_CHARSET_UTF8)
src/mess/tools/imgtool/imgtool.h
r7340r7341
2727
2828#define FILENAME_BOOTBLOCK   ((const char *) 1)
2929
30enum
31{
32   OSD_FOPEN_READ,
33   OSD_FOPEN_WRITE,
34   OSD_FOPEN_RW,
35   OSD_FOPEN_RW_CREATE
36}; 
37
3038/* ---------------------------------------------------------------------------
3139 * Image calls
3240 *
r7340r7341
7583const imgtool_module *imgtool_find_module(const char *modulename);
7684imgtool_module_features imgtool_get_module_features(const imgtool_module *module);
7785void imgtool_warn(const char *format, ...) ATTR_PRINTF(1,2);
86char *imgtool_basename(char *filename);
7887
7988/* ----- image management ----- */
8089imgtoolerr_t imgtool_identify_file(const char *filename, imgtool_module **modules, size_t count);
src/mess/tools/imgtool/main.c
r7340r7341
3030{
3131   fprintf(f, "%s %s %s %s\n",
3232      (write_word_usage ? "Usage:" : "      "),
33      osd_basename(argv[0]),
33      imgtool_basename(argv[0]),
3434      c->name,
3535      c->usage ? c->usage : "");
3636}
r7340r7341
981981   fprintf(stderr, "<imagename> is the image filename; can specify a ZIP file for image name\n");
982982
983983   fprintf(stderr, "\nExample usage:\n");
984   fprintf(stderr, "\t%s dir %s myimageinazip.zip\n", osd_basename(argv[0]), sample_format);
985   fprintf(stderr, "\t%s get %s myimage.dsk myfile.bin mynewfile.txt\n", osd_basename(argv[0]), sample_format);
986   fprintf(stderr, "\t%s getall %s myimage.dsk\n", osd_basename(argv[0]), sample_format);
984   fprintf(stderr, "\t%s dir %s myimageinazip.zip\n", imgtool_basename(argv[0]), sample_format);
985   fprintf(stderr, "\t%s get %s myimage.dsk myfile.bin mynewfile.txt\n", imgtool_basename(argv[0]), sample_format);
986   fprintf(stderr, "\t%s getall %s myimage.dsk\n", imgtool_basename(argv[0]), sample_format);
987987   result = 0;
988988   goto done;
989989
src/mess/image.c
r7340r7341
12391239}
12401240
12411241
1242//============================================================
1243//  image_filename_basename
1244//============================================================
12421245
1246static char *image_filename_basename(char *filename)
1247{
1248   char *c;
1249
1250   // NULL begets NULL
1251   if (!filename)
1252      return NULL;
1253
1254   // start at the end and return when we hit a slash or colon
1255   for (c = filename + strlen(filename) - 1; c >= filename; c--)
1256      if (*c == '\\' || *c == '/' || *c == ':')
1257         return c + 1;
1258
1259   // otherwise, return the whole thing
1260   return filename;
1261}
1262
12431263/*-------------------------------------------------
12441264    image_basename
12451265-------------------------------------------------*/
12461266
12471267const char *image_basename(running_device *image)
12481268{
1249    return osd_basename((char *) image_filename(image));
1269    return image_filename_basename((char *) image_filename(image));
12501270}
12511271
12521272
src/mess/filemngr.c
r7340r7341
674674   const osd_directory_entry *dirent;
675675   const file_selector_entry *entry;
676676   const file_selector_entry *selected_entry = NULL;
677   int count, i;
677   int i;
678   const char *volume_name;
678679   image_device_info info;
679680   running_device *device = menustate->manager_menustate->selected_device;
680681   const char *path = astring_c(menustate->manager_menustate->current_directory);
r7340r7341
698699   }
699700
700701   /* add the drives */
701   count = osd_num_devices();
702   for (i = 0; i < count; i++)
702   i = 0;
703   while((volume_name = osd_get_volume_name(i))!=NULL)
703704   {
704705      append_file_selector_entry(menu, menustate, SELECTOR_ENTRY_TYPE_DRIVE,
705         osd_get_device_name(i), osd_get_device_name(i));
706         volume_name, volume_name);
707      i++;
706708   }
707709
708710   /* build the menu for each item */
src/mess/osd/sdl/winutils.c
r7340r7341
1//============================================================
2//
3//  winutils.c - Generic Win32 utility code
4//
5//============================================================
6
7// Needed for better file dialog
8#ifdef _WIN32_WINNT
9#undef _WIN32_WINNT
10#endif // _WIN32_WINNT
11#define _WIN32_WINNT 0x500
12
13// standard windows headers
14#define WIN32_LEAN_AND_MEAN
15#include <windows.h>
16#include <tchar.h>
17
18// MESS headers
19#include "winutils.h"
20#include "strconv.h"
21
22//============================================================
23//  win_get_file_attributes_utf8
24//============================================================
25
26DWORD win_get_file_attributes_utf8(const char *filename)
27{
28   DWORD result = ~0;
29   LPTSTR t_filename;
30
31   t_filename = tstring_from_utf8(filename);
32   if (t_filename != NULL)
33   {
34      result = GetFileAttributes(t_filename);
35      free(t_filename);
36   }
37   return result;
38}
src/mess/osd/sdl/winutils.h
r7340r7341
1//============================================================
2//
3//  winutils.h - Generic Win32 utility code in MESS
4//
5//============================================================
6
7#ifndef WINUTILS_H
8#define WINUTILS_H
9
10#include <windows.h>
11#include "osdcore.h"
12
13//============================================================
14//  MISC
15//============================================================
16
17DWORD win_get_file_attributes_utf8(const char *filename);
18
19#endif // WINUTILS_H
src/mess/osd/sdl/w32util.c
r7340r7341
1#define WIN32_LEAN_AND_MEAN
2#include <windows.h>
3#include <ctype.h>
4#include <tchar.h>
5
6#include "osdcore.h"
7#include "utils.h"
8#include "strconv.h"
9#include "emu.h"
10#include "unicode.h"
11
12//============================================================
13//  astring_from_utf8
14//============================================================
15
16CHAR *astring_from_utf8(const char *utf8string)
17{
18   WCHAR *wstring;
19   int char_count;
20   CHAR *result;
21
22   // convert MAME string (UTF-8) to UTF-16
23   char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0);
24   wstring = (WCHAR *)alloca(char_count * sizeof(*wstring));
25   MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count);
26
27   // convert UTF-16 to "ANSI code page" string
28   char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL);
29   result = (CHAR *)malloc(char_count * sizeof(*result));
30   if (result != NULL)
31      WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL);
32
33   return result;
34}
35
36//============================================================
37//  wstring_from_utf8
38//============================================================
39
40WCHAR *wstring_from_utf8(const char *utf8string)
41{
42   int char_count;
43   WCHAR *result;
44
45   // convert MAME string (UTF-8) to UTF-16
46   char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0);
47   result = (WCHAR *)malloc(char_count * sizeof(*result));
48   if (result != NULL)
49      MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count);
50
51   return result;
52}
53
54//============================================================
55//  win_attributes_to_entry_type
56//============================================================
57
58static osd_dir_entry_type win_attributes_to_entry_type(DWORD attributes)
59{
60   if (attributes == 0xFFFFFFFF)
61      return ENTTYPE_NONE;
62   else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
63      return ENTTYPE_DIR;
64   else
65      return ENTTYPE_FILE;
66}
67
68//============================================================
69//  osd_stat
70//============================================================
71
72osd_directory_entry *osd_stat(const char *path)
73{
74   osd_directory_entry *result = NULL;
75   TCHAR *t_path;
76   HANDLE find = INVALID_HANDLE_VALUE;
77   WIN32_FIND_DATA find_data;
78
79   // convert the path to TCHARs
80   t_path = tstring_from_utf8(path);
81   if (t_path == NULL)
82      goto done;
83
84   // attempt to find the first file
85   find = FindFirstFile(t_path, &find_data);
86   if (find == INVALID_HANDLE_VALUE)
87      goto done;
88
89   // create an osd_directory_entry; be sure to make sure that the caller can
90   // free all resources by just freeing the resulting osd_directory_entry
91   result = (osd_directory_entry *) malloc(sizeof(*result) + strlen(path) + 1);
92   if (!result)
93      goto done;
94   strcpy(((char *) result) + sizeof(*result), path);
95   result->name = ((char *) result) + sizeof(*result);
96   result->type = win_attributes_to_entry_type(find_data.dwFileAttributes);
97   result->size = find_data.nFileSizeLow | ((UINT64) find_data.nFileSizeHigh << 32);
98
99done:
100   if (t_path)
101      free(t_path);
102   return result;
103}
104
src/mess/osd/sdl/climain.c
r7340r7341
1#include <windows.h>
2
3int __declspec(dllimport) __cdecl main_(int argc, char **argv);
4
5int main(int argc, char *argv[])
6{
7   return main_(argc, argv);
8}
9
src/mess/osd/sdl/sdl.mak
r7340r7341
1515
1616$(LIBOSD): $(OSDOBJS)
1717
18ifeq ($(TARGETOS),win32)
19OSDCOREOBJS += \
20   $(MESS_SDLOBJ)/winutils.o \
21   $(MESS_SDLOBJ)/w32util.o
22endif
23
2418$(LIBOCORE): $(OSDCOREOBJS)
2519
2620$(LIBOCORE_NOMAIN): $(OSDCOREOBJS:$(WINOBJ)/main.o=)
src/mess/osd/windows/winutils.c
r7340r7341
219219      ScrollWindowEx(window, 0, si.nPos - scroll_pos, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE);
220220   }
221221}
222
223
224
225//============================================================
226//  win_get_file_attributes_utf8
227//============================================================
228
229DWORD win_get_file_attributes_utf8(const char *filename)
230{
231   DWORD result = ~0;
232   LPTSTR t_filename;
233
234   t_filename = tstring_from_utf8(filename);
235   if (t_filename != NULL)
236   {
237      result = GetFileAttributes(t_filename);
238      free(t_filename);
239   }
240   return result;
241}
242
src/mess/osd/windows/winutils.h
r7340r7341
4747//============================================================
4848
4949void win_scroll_window(HWND window, WPARAM wparam, int scroll_bar, int scroll_delta_line);
50DWORD win_get_file_attributes_utf8(const char *filename);
5150
5251#endif // WINUTILS_H
src/mess/osd/windows/menu.c
r7340r7341
718718}
719719
720720
721//============================================================
722//  win_dirname
723//============================================================
721724
725char *win_dirname(const char *filename)
726{
727   char *dirname;
728   char *c;
729
730   // NULL begets NULL
731   if (!filename)
732      return NULL;
733
734   // allocate space for it
735   dirname = (char*)malloc(strlen(filename) + 1);
736   if (!dirname)
737      return NULL;
738
739   // copy in the name
740   strcpy(dirname, filename);
741
742   // search backward for a slash or a colon
743   for (c = dirname + strlen(dirname) - 1; c >= dirname; c--)
744      if (*c == '\\' || *c == '/' || *c == ':')
745      {
746         // found it: NULL terminate and return
747         *(c + 1) = 0;
748         return dirname;
749      }
750
751   // otherwise, return an empty string
752   dirname[0] = 0;
753   return dirname;
754}
755
756
722757//============================================================
723758//  state_dialog
724759//============================================================
r7340r7341
735770
736771   if (state_filename[0])
737772   {
738      dir = osd_dirname(state_filename);
773      dir = win_dirname(state_filename);
739774   }
740775   else
741776   {
r7340r7341
23022337   }
23032338   return 0;
23042339}
2305
2306void win_mess_dummy()
2307{
2308   char mess_directory[1024];
2309   
2310    /* first set up the working directory to be the MESS directory */
2311    osd_get_emulator_directory(mess_directory, ARRAY_LENGTH(mess_directory));
2312}
src/mess/zippath.c
r7340r7341
5757    PATH OPERATIONS
5858***************************************************************************/
5959
60//============================================================
61//  is_path_separator
62//============================================================
63
64int is_path_separator(char c)
65{
66   return (c == '/') || (c == '\\');
67}
68
6069/*-------------------------------------------------
6170    parse_parent_path - parses out the parent path
6271-------------------------------------------------*/
r7340r7341
6877
6978   /* skip over trailing path separators */
7079   pos = length - 1;
71   while((pos > 0) && osd_is_path_separator(path[pos]))
80   while((pos > 0) && is_path_separator(path[pos]))
7281      pos--;
7382
7483   /* return endpos */
r7340r7341
7685      *endpos = pos;
7786
7887   /* now skip until we find a path separator */
79   while((pos >= 0) && !osd_is_path_separator(path[pos]))
88   while((pos >= 0) && !is_path_separator(path[pos]))
8089      pos--;
8190
8291   /* return beginpos */
r7340r7341
136145   {
137146      result = astring_cpyc(dst, path2);
138147   }
139   else if ((path1[0] != '\0') && !osd_is_path_separator(path1[strlen(path1) - 1]))
148   else if ((path1[0] != '\0') && !is_path_separator(path1[strlen(path1) - 1]))
140149   {
141150      result = astring_assemble_3(dst, path1, PATH_SEPARATOR, path2);
142151   }
r7340r7341
217226
218227done:
219228   if (ptr != NULL)
220      free(ptr);
229      global_free(ptr);
221230   return filerr;
222231}
223232
r7340r7341
372381      i += 2;
373382
374383   /* skip path separators */
375   while (osd_is_path_separator(path[i]))
384   while (is_path_separator(path[i]))
376385      i++;
377386
378387   return path[i] == '\0';
r7340r7341
412421
413422static int is_zip_path_separator(char c)
414423{
415   return is_zip_file_separator(c) || osd_is_path_separator(c);
424   return is_zip_file_separator(c) || is_path_separator(c);
416425}
417426
418427
r7340r7341
537546   {
538547      /* trim the path of trailing path separators */
539548      i = astring_len(apath);
540      while((i > 1) && osd_is_path_separator(astring_c(apath)[i - 1]))
549      while((i > 1) && is_path_separator(astring_c(apath)[i - 1]))
541550         i--;
542551      apath_trimmed = astring_cpysubstr(apath_trimmed, apath, 0, i);
543552
r7340r7341
549558      {
550559         /* get the entry type and free the stat entry */
551560         current_entry_type = current_entry->type;
552         free(current_entry);
561         global_free(current_entry);
553562         current_entry = NULL;
554563      }
555564      else
r7340r7341
703712   {
704713      dirlist = directory->returned_dirlist;
705714      directory->returned_dirlist = directory->returned_dirlist->next;
706      free(dirlist);
715      global_free(dirlist);
707716   }
708717
709   free(directory);
718   global_free(directory);
710719}
711720
712721

Previous 15994 Revisions Next


© 1998-2018 The MESS Team