Previous 15994 Revisions Next

r2305 Sunday 6th April, 2008 at 15:23:31 UTC by Nathan Woods
Device refactoring
[src/mess]device.c device.h image.c image.h

src/mess/image.c
r2304r2305
139139
140140
141141
142/*-------------------------------------------------
143    device_get_info_str_offline - return an integer
144   state value from an allocated device; can be
145   called offline
146-------------------------------------------------*/
147
148INLINE const char *device_get_info_string_offline(const device_config *device, UINT32 state)
149{
150   deviceinfo info;
151
152   assert(device != NULL);
153   assert(device->type != NULL);
154   assert(state >= DEVINFO_STR_FIRST && state <= DEVINFO_STR_LAST);
155
156   /* retrieve the value */
157   info.s = 0;
158   (*device->type)(device, state, &info);
159   return info.s;
160}
161
162
163
142164/***************************************************************************
143165    CORE IMPLEMENTATION
144166***************************************************************************/
r2304r2305
322344****************************************************************************/
323345
324346/*-------------------------------------------------
347    get_device_name - retrieves the name of a
348   device
349-------------------------------------------------*/
350
351static void get_device_name(const device_config *device, char *buffer, size_t buffer_len)
352{
353   const char *name = NULL;
354   device_get_name_func get_name;
355   
356   if (name == NULL)
357   {
358      /* first try a get_name function */
359      get_name = (device_get_name_func) device_get_info_fct_offline(device, DEVINFO_FCT_GET_NAME);
360      if (get_name != NULL)
361         name = get_name(device, buffer, buffer_len);
362   }
363
364   if (name == NULL)
365   {
366      /* next try DEVINFO_STR_NAME */
367      name = device_get_info_string_offline(device, DEVINFO_STR_NAME);
368   }
369
370   /* make sure that the name is put into the buffer */
371   if (name != buffer)
372      snprintf(buffer, buffer_len, "%s", (name != NULL) ? name : "");
373}
374
375
376
377/*-------------------------------------------------
325378    image_device_getinfo - returns info on a device;
326379   can be called by front end code
327380-------------------------------------------------*/
r2304r2305
344397   info.creatable = device_get_info_int_offline(device, DEVINFO_INT_IMAGE_CREATABLE) ? 1 : 0;
345398   info.must_be_loaded = device_get_info_int_offline(device, DEVINFO_INT_IMAGE_MUST_BE_LOADED) ? 1 : 0;
346399
400   /* retrieve name */
401   get_device_name(device, info.name, ARRAY_LENGTH(info.name));
402
347403   iodev = mess_device_from_core_device(device);
348404   if (iodev != NULL)
349405   {
350      /* retrieve name */
351      s = (*iodev->name)(iodev, iodev->index_in_device, info.name, ARRAY_LENGTH(info.name));
352      if (s != info.name)
353         snprintf(info.name, ARRAY_LENGTH(info.name), "%s", s);
354
355406      /* retrieve file extensions */
356407      for(i = 0; iodev->file_extensions[i] != '\0'; i += strlen(&iodev->file_extensions[i]) + 1)
357408         ;
r2304r2305
12651316
12661317const char *image_typename_id(const device_config *image)
12671318{
1268   const struct IODevice *dev;
1269   int id;
1270   static char buf[64];
1271
1272   dev = mess_device_from_core_device(image);
1273   id = image_index_in_device(image);
1274   return dev->name(dev, id, buf, sizeof(buf) / sizeof(buf[0]));
1319   static char buffer[64];
1320   get_device_name(image, buffer, ARRAY_LENGTH(buffer));
1321   return buffer;
12751322}
12761323
12771324
src/mess/image.h
r2304r2305
2727typedef void (*device_image_unload_func)(const device_config *image);
2828typedef void (*device_display_func)(const device_config *image);
2929typedef void (*device_image_partialhash_func)(char *, const unsigned char *, unsigned long, unsigned int);
30typedef const char *(*device_get_name_func)(const device_config *device, char *buffer, size_t buffer_length);
3031
3132typedef enum
3233{
r2304r2305
100101   DEVINFO_FCT_IMAGE_UNLOAD,                           /* R/O: device_image_unload_func */
101102   DEVINFO_FCT_DISPLAY,                              /* R/O: device_display_func */
102103   DEVINFO_FCT_IMAGE_PARTIAL_HASH,                        /* R/O: device_image_partialhash_func */
104   DEVINFO_FCT_GET_NAME,                              /* R/O: device_get_name_func */
103105   DEVINFO_FCT_IMAGE_LAST = DEVINFO_FCT_FIRST + 0x0fff,
104106
105107   /* --- the following bits of info are returned as NULL-terminated strings --- */
r2304r2305
270272#define DEVICE_IMAGE_UNLOAD_NAME(name)   device_unload_##name
271273#define DEVICE_IMAGE_UNLOAD(name)      void DEVICE_IMAGE_UNLOAD_NAME(name)(const device_config *image)
272274
275#define DEVICE_GET_NAME_NAME(name)      device_get_name_##name
276#define DEVICE_GET_NAME(name)         const char *DEVICE_GET_NAME_NAME(name)(const device_config *device, char *buffer, size_t buffer_length)
277
273278#endif /* __IMAGE_H__ */
src/mess/device.c
r2304r2305
187187
188188
189189
190static const char *default_device_name(const struct IODevice *dev, int id,
191   char *buf, size_t bufsize)
192{
193   const char *name;
194
195   /* use the cool new device string technique */
196   name = mess_device_get_info_string(&dev->devclass, MESS_DEVINFO_STR_DESCRIPTION+id);
197   if (name)
198   {
199      snprintf(buf, bufsize, "%s", name);
200      return buf;
201   }
202
203   name = device_uiname(dev->type);
204   if (dev->multiple)
205   {
206      /* for the average user counting starts at #1 ;-) */
207      snprintf(buf, bufsize, "%s #%d", name, id + 1);
208      name = buf;
209   }
210   return name;
211}
212
213
214
215190/*-------------------------------------------------
216191    DEVICE_START(mess_device) - device start
217192    callback
r2304r2305
253228
254229
255230/*-------------------------------------------------
231    DEVICE_GET_NAME(mess_device) - device get name
232    callback
233-------------------------------------------------*/
234
235static DEVICE_GET_NAME(mess_device)
236{
237   const char *name;
238   const struct IODevice *iodev = mess_device_from_core_device(device);
239   int id = iodev->index_in_device;
240
241   /* use the cool new device string technique */
242   name = mess_device_get_info_string(&iodev->devclass, MESS_DEVINFO_STR_DESCRIPTION + id);
243   if (name)
244   {
245      snprintf(buffer, buffer_length, "%s", name);
246      return buffer;
247   }
248
249   name = device_uiname(iodev->type);
250   if (iodev->multiple)
251   {
252      /* for the average user counting starts at #1 ;-) */
253      snprintf(buffer, buffer_length, "%s #%d", name, id + 1);
254      name = buffer;
255   }
256   return name;
257}
258
259
260
261/*-------------------------------------------------
256262    DEVICE_GET_INFO(mess_device) - device get info
257263    callback
258264-------------------------------------------------*/
r2304r2305
282288      case DEVINFO_FCT_IMAGE_UNLOAD:         info->f = mess_device_get_info_fct(&mess_device->io_device.devclass, MESS_DEVINFO_PTR_UNLOAD); break;
283289      case DEVINFO_FCT_DISPLAY:            info->f = mess_device_get_info_fct(&mess_device->io_device.devclass, MESS_DEVINFO_PTR_DISPLAY); break;
284290      case DEVINFO_FCT_IMAGE_PARTIAL_HASH:   info->f = mess_device_get_info_fct(&mess_device->io_device.devclass, MESS_DEVINFO_PTR_PARTIAL_HASH); break;
291      case DEVINFO_FCT_GET_NAME:            info->f = (genf *) DEVICE_GET_NAME_NAME(mess_device);   break;
285292
286293      /* --- the following bits of info are returned as NULL-terminated strings --- */
287294      case DEVINFO_STR_NAME:               info->s = "Legacy MESS Device";               break;
r2304r2305
426433      mess_device->io_device.imgverify         = (device_image_verify_func) mess_device_get_info_fct(&mess_device->io_device.devclass, MESS_DEVINFO_PTR_VERIFY);
427434      mess_device->io_device.partialhash         = (device_image_partialhash_func) mess_device_get_info_fct(&mess_device->io_device.devclass, MESS_DEVINFO_PTR_PARTIAL_HASH);
428435
429      mess_device->io_device.name               = default_device_name;
430
431436      mess_device->io_device.createimage_optguide   = (const struct OptionGuide *) mess_device_get_info_ptr(&mess_device->io_device.devclass, MESS_DEVINFO_PTR_CREATE_OPTGUIDE);
432437
433438      createimage_optcount = (int) mess_device_get_info_int(&mess_device->io_device.devclass, MESS_DEVINFO_INT_CREATE_OPTCOUNT);
src/mess/device.h
r2304r2305
234234   int (*imgverify)(const UINT8 *buf, size_t size);
235235   device_image_partialhash_func partialhash;
236236
237   /* cosmetic/UI callbacks */
238   const char *(*name)(const struct IODevice *dev, int id, char *buf, size_t bufsize);
239
240237   /* image creation options */
241238   const struct OptionGuide *createimage_optguide;
242239   const struct CreateImageOptions *createimage_options;

Previous 15994 Revisions Next


© 1998-2018 The MESS Team