1 /* uimedia.c: Disk media UI routines
2    Copyright (c) 2013 Alex Badea
3    Copyright (c) 2015-2017 Gergely Szasz
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 
19    Author contact information:
20 
21    E-mail: vamposdecampos@gmail.com
22 
23 */
24 
25 #include <config.h>
26 #include <string.h>
27 
28 #ifdef HAVE_LIB_GLIB
29 #include <glib.h>
30 #endif
31 
32 #include "fuse.h"
33 #include "options.h"
34 #include "ui/ui.h"
35 #include "ui/uimedia.h"
36 #include "utils.h"
37 
38 #define DISK_TRY_MERGE(heads) \
39   ( option_enumerate_diskoptions_disk_try_merge() == 2 || \
40     ( option_enumerate_diskoptions_disk_try_merge() == 1 && heads == 1 ) )
41 
42 static GSList *registered_drives = NULL;
43 
44 static inline int
menu_item_valid(ui_menu_item item)45 menu_item_valid( ui_menu_item item )
46 {
47   return item != UI_MENU_ITEM_INVALID;
48 }
49 
50 int
ui_media_drive_register(ui_media_drive_info_t * drive)51 ui_media_drive_register( ui_media_drive_info_t *drive )
52 {
53   registered_drives = g_slist_append( registered_drives, drive );
54   return 0;
55 }
56 
57 void
ui_media_drive_end(void)58 ui_media_drive_end( void )
59 {
60   g_slist_free( registered_drives );
61   registered_drives = NULL;
62 }
63 
64 struct find_info {
65   int controller;
66   int drive;
67 };
68 
69 static gint
find_drive(gconstpointer data,gconstpointer user_data)70 find_drive( gconstpointer data, gconstpointer user_data )
71 {
72   const ui_media_drive_info_t *drive = data;
73   const struct find_info *info = user_data;
74 
75   return !( drive->controller_index == info->controller &&
76             drive->drive_index == info->drive );
77 }
78 
79 ui_media_drive_info_t *
ui_media_drive_find(int controller,int drive)80 ui_media_drive_find( int controller, int drive )
81 {
82   struct find_info info = {
83     /* .controller = */ controller,
84     /* .drive = */ drive,
85   };
86   GSList *item;
87   item = g_slist_find_custom( registered_drives, &info, find_drive );
88   return item ? item->data : NULL;
89 }
90 
91 
92 static gint
any_available(gconstpointer data,gconstpointer user_data)93 any_available( gconstpointer data, gconstpointer user_data )
94 {
95   const ui_media_drive_info_t *drive = data;
96 
97   return !( drive->is_available && drive->is_available() );
98 }
99 
100 int
ui_media_drive_any_available(void)101 ui_media_drive_any_available( void )
102 {
103   GSList *item;
104   item = g_slist_find_custom( registered_drives, NULL, any_available );
105   return item ? 1 : 0;
106 }
107 
108 
109 static void
update_parent_menus(gpointer data,gpointer user_data)110 update_parent_menus( gpointer data, gpointer user_data )
111 {
112   const ui_media_drive_info_t *drive = data;
113 
114   if( drive->is_available && menu_item_valid( drive->menu_item_parent ) )
115     ui_menu_activate( drive->menu_item_parent, drive->is_available() );
116 }
117 
118 void
ui_media_drive_update_parent_menus(void)119 ui_media_drive_update_parent_menus( void )
120 {
121   g_slist_foreach( registered_drives, update_parent_menus, NULL );
122 }
123 
124 static int
maybe_menu_activate(int id,int activate)125 maybe_menu_activate( int id, int activate )
126 {
127   if( !menu_item_valid( id ) )
128     return 0;
129   return ui_menu_activate( id, activate );
130 }
131 
132 void
ui_media_drive_update_menus(const ui_media_drive_info_t * drive,unsigned flags)133 ui_media_drive_update_menus( const ui_media_drive_info_t *drive,
134                              unsigned flags )
135 {
136   if( !drive->fdd )
137     return;
138 
139   if( flags & UI_MEDIA_DRIVE_UPDATE_TOP )
140     maybe_menu_activate( drive->menu_item_top,
141                          drive->fdd->type != FDD_TYPE_NONE );
142   if( flags & UI_MEDIA_DRIVE_UPDATE_EJECT )
143     maybe_menu_activate( drive->menu_item_eject, drive->fdd->loaded );
144   if( flags & UI_MEDIA_DRIVE_UPDATE_FLIP )
145     maybe_menu_activate( drive->menu_item_flip, !drive->fdd->upsidedown );
146   if( flags & UI_MEDIA_DRIVE_UPDATE_WP )
147     maybe_menu_activate( drive->menu_item_wp, !drive->fdd->wrprot );
148 }
149 
150 
151 int
ui_media_drive_flip(int controller,int which,int flip)152 ui_media_drive_flip( int controller, int which, int flip )
153 {
154   ui_media_drive_info_t *drive;
155 
156   drive = ui_media_drive_find( controller, which );
157   if( !drive )
158     return -1;
159   if( !drive->fdd->loaded )
160     return 1;
161 
162   fdd_flip( drive->fdd, flip );
163   ui_media_drive_update_menus( drive, UI_MEDIA_DRIVE_UPDATE_FLIP );
164   return 0;
165 }
166 
167 int
ui_media_drive_writeprotect(int controller,int which,int wrprot)168 ui_media_drive_writeprotect( int controller, int which, int wrprot )
169 {
170   ui_media_drive_info_t *drive;
171 
172   drive = ui_media_drive_find( controller, which );
173   if( !drive )
174     return -1;
175   if( !drive->fdd->loaded )
176     return 1;
177 
178   fdd_wrprot( drive->fdd, wrprot );
179   ui_media_drive_update_menus( drive, UI_MEDIA_DRIVE_UPDATE_WP );
180   return 0;
181 }
182 
183 
184 static int
drive_disk_write(const ui_media_drive_info_t * drive,const char * filename)185 drive_disk_write( const ui_media_drive_info_t *drive, const char *filename )
186 {
187   int error;
188 
189   drive->fdd->disk.type = DISK_TYPE_NONE;
190   if( filename == NULL )
191     filename = drive->fdd->disk.filename; /* write over original file */
192   else if( compat_file_exists( filename ) ) {
193     const char *filename1 = strrchr( filename, FUSE_DIR_SEP_CHR );
194     filename1 = filename1 ? filename1 + 1 : filename;
195 
196     ui_confirm_save_t confirm = ui_confirm_save(
197       "%s already exists.\n"
198       "Do you want to overwrite it?",
199       filename1
200     );
201 
202     switch( confirm ) {
203 
204     case UI_CONFIRM_SAVE_SAVE:
205       break;
206 
207     case UI_CONFIRM_SAVE_DONTSAVE: return 2;
208     case UI_CONFIRM_SAVE_CANCEL: return -1;
209 
210     }
211   }
212 
213   error = disk_write( &drive->fdd->disk, filename );
214 
215   if( error != DISK_OK ) {
216     ui_error( UI_ERROR_ERROR, "couldn't write '%s' file: %s", filename,
217               disk_strerror( error ) );
218     return 1;
219   }
220 
221   if( !drive->fdd->disk.filename ||
222       strcmp( filename, drive->fdd->disk.filename ) ) {
223     libspectrum_free( drive->fdd->disk.filename );
224     drive->fdd->disk.filename = utils_safe_strdup( filename );
225   }
226   return 0;
227 }
228 
229 
230 static int
drive_save(const ui_media_drive_info_t * drive,int saveas)231 drive_save( const ui_media_drive_info_t *drive, int saveas )
232 {
233   int err;
234   char *filename = NULL, title[80];
235 
236   if( drive->fdd->disk.filename == NULL )
237     saveas = 1;
238 
239   fuse_emulation_pause();
240 
241   snprintf( title, sizeof( title ), "Fuse - Write %s", drive->name );
242   if( saveas ) {
243     filename = ui_get_save_filename( title );
244     if( !filename ) {
245       fuse_emulation_unpause();
246       return 1;
247     }
248   }
249 
250   err = drive_disk_write( drive, filename );
251 
252   if( saveas )
253     libspectrum_free( filename );
254 
255   fuse_emulation_unpause();
256   if( err )
257     return 1;
258 
259   drive->fdd->disk.dirty = 0;
260   return 0;
261 }
262 
263 int
ui_media_drive_save(int controller,int which,int saveas)264 ui_media_drive_save( int controller, int which, int saveas )
265 {
266   ui_media_drive_info_t *drive;
267 
268   drive = ui_media_drive_find( controller, which );
269   if( !drive )
270     return -1;
271   return drive_save( drive, saveas );
272 }
273 
274 static int
drive_eject(const ui_media_drive_info_t * drive)275 drive_eject( const ui_media_drive_info_t *drive )
276 {
277   if( !drive->fdd->loaded )
278     return 0;
279 
280   if( drive->fdd->disk.dirty ) {
281 
282     ui_confirm_save_t confirm = ui_confirm_save(
283       "%s has been modified.\n"
284       "Do you want to save it?",
285       drive->name
286     );
287 
288     switch( confirm ) {
289 
290     case UI_CONFIRM_SAVE_SAVE:
291       if( drive_save( drive, 0 ) )
292         return 1;   /* first save it...*/
293       break;
294 
295     case UI_CONFIRM_SAVE_DONTSAVE: break;
296     case UI_CONFIRM_SAVE_CANCEL: return 1;
297 
298     }
299   }
300 
301   fdd_unload( drive->fdd );
302   disk_close( &drive->fdd->disk );
303   ui_media_drive_update_menus( drive, UI_MEDIA_DRIVE_UPDATE_EJECT );
304   return 0;
305 }
306 
307 int
ui_media_drive_eject(int controller,int which)308 ui_media_drive_eject( int controller, int which )
309 {
310   ui_media_drive_info_t *drive;
311 
312   drive = ui_media_drive_find( controller, which );
313   if( !drive )
314     return -1;
315   return drive_eject( drive );
316 }
317 
318 static gint
eject_all(gconstpointer data,gconstpointer user_data)319 eject_all( gconstpointer data, gconstpointer user_data )
320 {
321   const ui_media_drive_info_t *drive = data;
322 
323   return !drive_eject( drive );
324 }
325 
326 int
ui_media_drive_eject_all(void)327 ui_media_drive_eject_all( void )
328 {
329   GSList *item;
330   item = g_slist_find_custom( registered_drives, NULL, eject_all );
331   return item ? 1 : 0;
332 }
333 
334 
335 int
ui_media_drive_insert(const ui_media_drive_info_t * drive,const char * filename,int autoload)336 ui_media_drive_insert( const ui_media_drive_info_t *drive,
337                        const char *filename, int autoload )
338 {
339   int error;
340   const fdd_params_t *dt;
341 
342   /* Eject any disk already in the drive */
343   if( drive->fdd->loaded ) {
344     /* Abort the insert if we want to keep the current disk */
345     if( drive_eject( drive ) )
346       return 0;
347   }
348 
349   if( filename ) {
350     error = disk_open( &drive->fdd->disk, filename, 0,
351                        DISK_TRY_MERGE( drive->fdd->fdd_heads ) );
352     if( error != DISK_OK ) {
353       ui_error( UI_ERROR_ERROR, "Failed to open disk image: %s",
354                 disk_strerror( error ) );
355       return 1;
356     }
357   } else {
358     dt = drive->get_params();
359     error = disk_new( &drive->fdd->disk, dt->heads, dt->cylinders, DISK_DENS_AUTO,
360                       DISK_UDI );
361     if( error != DISK_OK ) {
362       ui_error( UI_ERROR_ERROR, "Failed to create disk image: %s",
363                 disk_strerror( error ) );
364       return 1;
365     }
366   }
367   if( drive->insert_hook ) {
368     error = drive->insert_hook( drive, !filename );
369     if( error )
370       return 1;
371   }
372 
373   fdd_load( drive->fdd, 0 );
374 
375   /* Set the 'eject' item active */
376   ui_media_drive_update_menus( drive, UI_MEDIA_DRIVE_UPDATE_ALL );
377 
378   if( filename && autoload && drive->autoload_hook ) {
379     return drive->autoload_hook();
380   }
381 
382   return 0;
383 }
384