@@ -342,25 +342,19 @@ bool disk_control_set_eject_state(
342342 size_t _len ;
343343
344344 if (!disk_control || !disk_control -> cb .set_eject_state )
345- return false ;
345+ return err ;
346346
347347 /* Set eject state */
348348 if (disk_control -> cb .set_eject_state (eject ))
349- _len = strlcpy (
350- msg ,
351- eject
352- ? msg_hash_to_str (MSG_DISK_EJECTED )
353- : msg_hash_to_str (MSG_DISK_CLOSED ),
354- sizeof (msg ));
349+ _len = strlcpy (msg ,
350+ eject ? msg_hash_to_str (MSG_DISK_EJECTED ) : msg_hash_to_str (MSG_DISK_CLOSED ),
351+ sizeof (msg ));
355352 else
356353 {
357354 err = true;
358- _len = strlcpy (
359- msg ,
360- eject
361- ? msg_hash_to_str (MSG_VIRTUAL_DISK_TRAY_EJECT )
362- : msg_hash_to_str (MSG_VIRTUAL_DISK_TRAY_CLOSE ),
363- sizeof (msg ));
355+ _len = strlcpy (msg ,
356+ eject ? msg_hash_to_str (MSG_VIRTUAL_DISK_TRAY_EJECT ) : msg_hash_to_str (MSG_VIRTUAL_DISK_TRAY_CLOSE ),
357+ sizeof (msg ));
364358 }
365359
366360 if (_len > 0 )
@@ -398,9 +392,9 @@ bool disk_control_set_eject_state(
398392/**
399393 * disk_control_set_index:
400394 *
401- * Sets currently selected disk index
402- *
403- * NOTE: Will fail if disk is not currently ejected
395+ * Sets currently selected disk index.
396+ * Does silent eject and delayed insert with
397+ * 'runloop_st->pending_disk_control_insert' if tray is closed.
404398 **/
405399bool disk_control_set_index (
406400 disk_control_interface_t * disk_control ,
@@ -414,17 +408,26 @@ bool disk_control_set_index(
414408
415409 msg [0 ] = '\0' ;
416410
417- if (!disk_control )
418- return false;
411+ if ( !disk_control
412+ || !disk_control -> cb .get_eject_state
413+ || !disk_control -> cb .get_num_images
414+ || !disk_control -> cb .get_image_index
415+ || !disk_control -> cb .set_image_index )
416+ return err ;
419417
420- if ( !disk_control -> cb .get_eject_state
421- || !disk_control -> cb .get_num_images
422- || !disk_control -> cb .set_image_index )
423- return false;
418+ /* Do nothing if the desired disc is already in */
419+ if (disk_control -> cb .get_image_index () == index )
420+ return !err ;
424421
425- /* Ensure that disk is currently ejected */
422+ /* Do delayed disk insert if changing while not ejected */
426423 if (!disk_control -> cb .get_eject_state ())
427- return false;
424+ {
425+ runloop_state_t * runloop_st = runloop_state_get_ptr ();
426+
427+ if ( runloop_st
428+ && disk_control_set_eject_state (disk_control , true, false))
429+ runloop_st -> pending_disk_control_insert = 100 ;
430+ }
428431
429432 /* Get current number of disk images */
430433 num_images = disk_control -> cb .get_num_images ();
@@ -466,7 +469,6 @@ bool disk_control_set_index(
466469 && disk_control -> cb .get_image_path )
467470 {
468471 char new_image_path [PATH_MAX_LENGTH ] = {0 };
469- /* Get current image index + path */
470472 unsigned new_image_index = disk_control -> cb .get_image_index ();
471473 bool image_path_valid = disk_control -> cb .get_image_path (
472474 new_image_index , new_image_path , sizeof (new_image_path ));
@@ -618,22 +620,22 @@ bool disk_control_append_image(
618620
619621 if ((new_index = disk_control -> cb .get_num_images ()) < 1 )
620622 goto error ;
621- new_index -- ;
622623
624+ new_index -- ;
623625 info .path = image_path ;
624- if (!disk_control -> cb .replace_image_index (new_index , & info ))
625- goto error ;
626626
627- /* Set new index */
628- if (!disk_control_set_index (disk_control , new_index , false))
627+ if (!disk_control -> cb .replace_image_index (new_index , & info ))
629628 goto error ;
630629
631- /* If tray was initially closed, insert disk
632- * (i.e. leave system in the state we found it) */
630+ /* If tray was initially closed, insert disk */
633631 if ( !initial_disk_ejected
634632 && !disk_control_set_eject_state (disk_control , false, false))
635633 goto error ;
636634
635+ /* Set new index */
636+ if (!disk_control_set_index (disk_control , new_index , false))
637+ goto error ;
638+
637639 /* Display log */
638640 _len = strlcpy (msg , msg_hash_to_str (MSG_APPENDED_DISK ), sizeof (msg ) - 3 );
639641 msg [ _len ] = ':' ;
@@ -658,11 +660,7 @@ bool disk_control_append_image(
658660 * NOTE: If this fails then it's game over -
659661 * just display the error notification and
660662 * hope for the best... */
661- if (!disk_control -> cb .get_eject_state ())
662- disk_control_set_eject_state (disk_control , true, false);
663663 disk_control_set_index (disk_control , initial_index , false);
664- if (!initial_disk_ejected )
665- disk_control_set_eject_state (disk_control , false, false);
666664
667665 _len = strlcpy (msg ,
668666 msg_hash_to_str (MSG_FAILED_TO_APPEND_DISK ), sizeof (msg ) - 3 );
@@ -672,7 +670,7 @@ bool disk_control_append_image(
672670 _len += strlcpy (msg + _len , image_filename , sizeof (msg ) - _len );
673671
674672 runloop_msg_queue_push (msg , _len , 2 , 180 , true, NULL ,
675- MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_WARNING );
673+ MESSAGE_QUEUE_ICON_DEFAULT , MESSAGE_QUEUE_CATEGORY_ERROR );
676674
677675 return false;
678676}
0 commit comments