File: cdw_drive.c

package info (click to toggle)
cdw 0.7.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,952 kB
  • sloc: ansic: 31,667; sh: 1,496; makefile: 274
file content (806 lines) | stat: -rw-r--r-- 25,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/* cdw
 * Copyright (C) 2002 Varkonyi Balazs
 * Copyright (C) 2007 - 2012 Kamil Ignacak
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "cdw_utils.h"
#include "cdw_config.h"
#include "gettext.h"
#include "cdw_widgets.h" /* cdw_buttons_dialog() */
#include "cdw_drive.h"
#include "cdw_main_window.h"
#include "cdw_disc.h"
#include "cdw_fs.h"
#include "cdw_debug.h"
#include "cdw_processwin.h"
#include "cdw_read_disc_info.h"
#include "cdw_cdio_drives.h"
#include "cdw_string.h"



extern cdw_cdio_drive_t cdw_cdio_drives[];

/* main application configuration variable */
extern cdw_config_t global_config;

static int cdw_drive_toggle_tray2(const char *device_fullpath, bool with_processwin);
static int cdw_drive_execute_request(const char *device_fullpath, long unsigned int request);


static struct {
	CDW_DROPDOWN *drives_dropdown;
	int n_drives;
	/* cdio provides table that is indexed from zero - hence here is "ind"="index" */
	int cdio_default_drive_ind;
	/* cdw uses CDW_DRIVE_ID_CUSTOM == -1 to designate "custom", and
	   0 to N as regular indexes; because of using "-1" the name of
	   variable is "id"; for almost all cases id is a valid index;
	   except for -1; */
	cdw_id_t current_drive_id;
} drives;


static cdw_id_clabel_t drive_requests[] = {
	{ CDROMRESET,         "CDROMRESET" },
	{ CDROMEJECT,         "CDROMEJECT" },
	{ CDROMCLOSETRAY,     "CDROMCLOSETRAY" },
	{ CDROM_DRIVE_STATUS, "CDROM_DRIVE_STATUS" },
	{ CDROM_LOCKDOOR,     "CDROM_LOCKDOOR" },
	{ 0,                  (char *) NULL}}; /* guard */




void cdw_drive_init(void)
{
	drives.n_drives = cdw_cdio_drives_get_n_drives();
	if (drives.n_drives == 0) {
		drives.current_drive_id = CDW_DRIVE_ID_CUSTOM;
		return;
	}

	drives.cdio_default_drive_ind = cdw_cdio_drives_get_cdio_default_drive_ind();

	/* global_config.selected_drive is a string describing currently
	   selected drive; it can have three values:
	    - "default": use drive that is indicated as "default" by cdio
	       module; in practice this value happens only when there is
	       no cdw config file, and cdw runs with default settings;
	       there is no "select default drive" option;
	    - "custom": this value of "selected_drive" indicates that
	      the path to "real" drive is in global_config.custom_drive
	    - '/dev/xyz': a path to device file in form of "/dev/xyz";
	      the value is selected from a dropdown, and since the dropdown
	      is built from values provided by cdio module, the value
	      should be present in cdw_cdio_drives[] */

	/* find correct index to table of drives, regardless of value
	   of global_config.selected_drive */
	if (! strcmp(global_config.selected_drive, "default")) {
		drives.current_drive_id = drives.cdio_default_drive_ind;
	} else if (! strcmp(global_config.selected_drive, "custom")) {
		drives.current_drive_id = CDW_DRIVE_ID_CUSTOM;
	} else {
		/* we need to search for device path from cdw_drive
		   on list of drives detected by cdio */
		drives.current_drive_id = -1;
		for (int i = 0; i < drives.n_drives; i++) {
			if (!strcmp(cdw_cdio_drives[i].fullpath, global_config.selected_drive)) {
				/* here index is a valid id */
				drives.current_drive_id = i;
				break;
			}
		}
		if (drives.current_drive_id == -1) {
			/* can't find a drive from configuration on list
			   of drives detected by cdio */
			cdw_vdm ("ERROR: can't find a drive from configuration on list of drives detected by cdio: \"%s\"\n", global_config.selected_drive);
			/* fall back to safe value */
			if (drives.n_drives == 0) {
				drives.current_drive_id = CDW_DRIVE_ID_CUSTOM;
			} else {
				/* let's hope that there is some default drive */
				drives.current_drive_id = drives.cdio_default_drive_ind;
			}
		}
	}

	return;
}





void cdw_drive_clean(void)
{
	return;
}





/**
   \brief Check for CD drive status

   Check for CD drive status using ioctl (with parameter CDROM_DRIVE_STATUS).
   Documentation on ioctl can be found in linux source code documentation in
   Documentation/ioctl/cdrom.txt.

   This function tries to open CD device described by \p device_fullpath.

   Return codes are mapped to new names in cdrom_ctrl.h, so there is no need
   to include linux/cdrom.h again in *c files.

   Function does not inform user about errors, caller function has to do it.

   \param device_fullpath - full path to device that you want to check

   \return -1 if cannot open() cdrom file, or ioctl() failed
   \return ioctl return code: CDS_NO_INFO (information not available)
   \return ioctl return code: CDS_NO_DISC
   \return ioctl return code: CDS_TRAY_OPEN
   \return ioctl return code: CDS_DRIVE_NOT_READY
   \return ioctl return code: CDS_DISC_OK
 */
int cdw_drive_status(const char *device_fullpath)
{
	return cdw_drive_execute_request(device_fullpath, CDROM_DRIVE_STATUS);
}





/**
   \brief Eject cdrom drive tray

   \param device_fullpath - full path to '/dev' file corresponding to device

   \return CDW_OK on success
   \return CDW_SYS_ERROR if function cannot open() device or if ioctl call for this device fails
 */
cdw_rv_t cdw_drive_eject_tray(const char *device_fullpath)
{
	/* sometimes call to ioctl(..., CDROMEJECT) fails, not sure why,
	   especially for DVD discs (and for DVD-RW in particular);
	   perhaps some other process is trying to access freshly burned
	   disc as well?
	   let's give the drive some time to calm down, but it should
	   take much less time than waiting for drive after closing a tray;
	   experiments show that it may take 2-3 seconds before call to
	   cdw_drive_execute_request(..., CDROMEJECT) is successful */
	const int arbitrary_limit = 10;
	for (int i = 0; i < arbitrary_limit; i++) {
		cdw_vdm ("INFO: trying to eject drive, second %d\n", i);
		int rv = cdw_drive_execute_request(device_fullpath, CDROMEJECT);
		if (rv == 0) {
			cdw_vdm ("INFO: ejected drive after %d seconds\n", i);
			return CDW_OK;
		} else {
#ifdef CDROM_LOCKDOOR
			usleep(200000);
			/* calls ioctl with 0 as a third argument -> unlock */
			cdw_drive_execute_request(device_fullpath, CDROM_LOCKDOOR);
#else
			/* some error message maybe? */
#endif
			sleep(1);
		}
	}
	cdw_vdm ("ERROR: failed to eject a drive\n");
	return CDW_ERROR;
}





/**
   \brief Close cdrom drive tray

   It is strongly advised to call sleep(x) (with x being e.g. 3) after
   calling this function. This gives the drive some time to recognize media.
   Otherwise any reads from disc in drive may yield incorrect results due to
   timeouts.

   \param device_fullpath - full path to '/dev' file corresponding to device

   \return CDW_OK on success
   \return CDW_ERROR if function cannot open() device or if ioctl call for this device fails
 */
cdw_rv_t cdw_drive_close_tray(const char *device_fullpath)
{
	int rv = cdw_drive_execute_request(device_fullpath, CDROMCLOSETRAY);
	if (rv == -1) {
		cdw_vdm ("ERROR: ioctl(%s, CDROMCLOSETRAY, ...) returns -1\n", device_fullpath);
		return CDW_ERROR;
	} else {
		/* give your drive some time to scan disc after closing
		   tray; the drive really may need 30 seconds; on my
		   not-so-old machine a drive with DVD+RW in it settles
		   its state in 17 seconds */
		const int arbitrary_limit = 30;
		for (int i = 0; i < arbitrary_limit; i++) {
			rv = cdw_drive_status(device_fullpath);
			if (rv == -1) {
				cdw_vdm ("ERROR: can't get drive status\n");
				return CDW_ERROR;
			} else if (rv == CDS_DRIVE_NOT_READY) {
				cdw_vdm ("INFO: waiting for drive, second %d\n", i);
				sleep(1);
			} else {
				cdw_vdm ("INFO: drive is ready after %d seconds\n", i);
				break;
			}
		}
		return CDW_OK;
	}
}





/* TODO: make the function return errno, so that caller can
   act upon an error */
int cdw_drive_execute_request(const char *device_fullpath, long unsigned int request)
{
	/* yes, I know about sloppy casting of long unsigned int to
	   long signed int, but this is "only" debug code;
	   FIXME: fix this somehow; but how?
	   1. make int field in cdw_id_clabel_t of unsigned type, but
	      IDs can be negative
	   2. make two cdw_id_clabel_t types - for singed and unsigned int
	      field - maintenance nightmare */

	int cddev = open(device_fullpath, O_RDONLY | O_NONBLOCK);
	int e = errno;
	if (cddev == -1) {
		cdw_vdm ("ERROR: failed to open() device \"%s\", for request %s, error = \"%s\"\n",
			 device_fullpath,
			 cdw_utils_id_label_table_get_label(drive_requests, (long int) request),
			 strerror(e));
		return -1;
	}

	int rv = ioctl(cddev, request, 0);
	e = errno;
	close(cddev);
	if (rv == -1) {
		cdw_vdm ("ERROR: ioctl(%s, %s, ...) returns -1 / \"%s\"\n",
			 device_fullpath,
			 cdw_utils_id_label_table_get_label(drive_requests, (long int) request),
			 strerror(e));
	}

	return rv;
}





/**
   \brief Check availability of optical drive and disc

   This function provides information if there is a drive and a disc
   in the drive, and informs user (with dialog window messages) about
   potential problems with finding drive or disc.

   \return CDW_CANCEL if there is no optical drive available
   \return CDW_NO if there is a drive, but there is no disc in it
   \return CDW_OK if there is a drive, and there is a disc in it
 */
cdw_rv_t cdw_drive_disc_availability(void)
{
	const char *drive = cdw_drive_get_drive_fullpath();
	if (!drive) {
		cdw_vdm ("WARNING: no drive\n");
		cdw_buttons_dialog(_("Warning"), _("No drive.\n"), CDW_BUTTONS_OK, CDW_COLORS_WARNING);
		return CDW_CANCEL;
	}

	/* 2TRANS: this is title of dialog window:
	   some problem occurred while trying to access disc */
	const char *title = _("CD drive error");
	char *message = (char *) NULL;
	int buttons = 0;
	for (int i = 0; i < 10; i++) {
		/* 2TRANS: this is message displayed in process window */
		cdw_processwin_display_sub_info(_("Getting drive status"));
		cdw_processwin_wrefresh();
		int rv = cdw_drive_status(drive);
		switch (rv) {
			case CDS_DISC_OK:
				return CDW_OK;
			case CDS_NO_DISC:
				/* 2TRANS: this is message in dialog window: no optical disc
				   found in drive; user can select OK or CANCEL button */
				message =  _("No disc in CD/DVD drive. Please insert disc.");
				buttons = CDW_BUTTONS_OK_CANCEL;
				break;
			case CDS_TRAY_OPEN:
				/* 2TRANS: this is message in dialog window; user
				   can select OK or CANCEL button */
				message = _("Please close CD/DVD drive tray.");
				buttons = CDW_BUTTONS_OK_CANCEL;
				break;
			case CDS_DRIVE_NOT_READY:
				sleep(2);
				/* 2TRANS: this is message in dialog window;
				   user can select OK or CANCEL button */
				message = _( "CD/DVD drive doesn't respond (timeout). Try again?");
				buttons = CDW_BUTTONS_OK_CANCEL;
				break;
			case CDS_NO_INFO:
				/* 2TRANS: this is message in dialog window;
				   cannot read some meta-data from cd; user
				   can select OK or CANCEL button */
				message = _("Can't get CD/DVD drive info. Continue?");
				buttons = CDW_BUTTONS_OK_CANCEL;
				break;
			case -1: /* cannot open device file or ioctl() failed */
				/* 2TRANS: this is message in dialog window;
				   for some reason program cannot open CD device.
				   User should check his settings */
				message = _("Can't open CD/DVD drive device. Please check your configuration.");
				buttons = CDW_BUTTONS_OK;
				break;
			default: /* all return values from ioctl() and open() covered, so this */
				/* 2TRANS: this is message in dialog window:
				   unknown error occurred */
				message = _("Can't open CD/DVD drive device. Unknown error.");
				buttons = CDW_BUTTONS_OK;
				break;
		} /* switch */

		cdw_rv_t crv = cdw_buttons_dialog(title, message,
						  buttons, CDW_COLORS_ERROR);
		cdw_main_ui_main_window_wrefresh();
		if (buttons == CDW_BUTTONS_OK) {
			break; /* break loop, go to final error message */
		} else {
			if (crv == CDW_OK) {
				; /* continue loop */
			} else {
				return CDW_NO;
			}
		}

		sleep(5);
	} /* for () */

	cdw_buttons_dialog(title,
			   /* 2TRANS: this is message in dialog window: some errors
			      occurred while accessing drive */
			   _("Something went wrong.\nPlease check settings in Configuration -> Hardware and try once more."),
			   CDW_BUTTONS_OK, CDW_COLORS_ERROR);
	cdw_main_ui_main_window_wrefresh();

	return CDW_NO; /* user couldn't decide what to do or hardware settings in cdw options are invalid */
}





/**
   \brief Reload or eject disc, update disc info view

   Eject tray of cd drive. If \p final_eject is true, check value
   of config.eject to see if tray should stay open. If \p final_eject is
   false, close tray.

   Values displayed in disc info view are reset after ejecting tray and
   updated with valid values after closing tray. Valid values are
   collected by cdw_disc_get().

   This code would be used two or three times in main(), so
   I decided to extract it to separate function.

   \param device_fullpath - path to device that you want to control
   \param final_eject - should the function look at config.eject to check if tray should be left open?
 */
void cdw_drive_reload_tray_with_ui_update(const char *device_fullpath, bool final_eject)
{
	cdw_drive_eject_tray_with_ui_update(device_fullpath);
	if (final_eject) {
		if (global_config.eject) { /* leave tray open */
			return;
		}
	}

	/* close tray: either user don't want to keep it opened after
	   performing writing operation (this is when config.eject == false)
	   or cdw will be doing something more on the disc (this is when
	   final_eject == false) */
	cdw_drive_close_tray_with_ui_update(device_fullpath);

	return;
}





void cdw_drive_eject_tray_with_ui_update(const char *device_fullpath)
{
	/* 2TRANS: this is message displayed in process window */
	cdw_processwin_display_sub_info(_("Ejecting tray"));
	cdw_processwin_wrefresh();
	cdw_drive_eject_tray(device_fullpath);

	/* update information about disc size and usage:
	   0, 0 = sectors used, sectors total */
	cdw_main_window_volume_info_view_update(0, 0, false, global_config.follow_symlinks);

	return;
}





void cdw_drive_close_tray_with_ui_update(const char *device_fullpath)
{
	/* 2TRANS: this is message displayed in process window;
	   "tray" is an optical drive tray */
	cdw_processwin_display_sub_info(_("Closing tray, waiting for drive"));
	cdw_processwin_force_refresh();
	/* to avoid sending to drive "eject" and "close" commands
	   in very short time  */
	usleep(500000);
	cdw_drive_close_tray(device_fullpath);

	cdw_disc_t *disc = cdw_disc_new();
	cdw_disc_get(disc);
	cdw_main_ui_disc_info_view_display_data(disc);
	/* update information about disc size and usage */
	cdw_main_window_volume_info_view_update(disc->capacity.sectors_used, disc->capacity.sectors_total, false, global_config.follow_symlinks);
	cdw_disc_delete(&disc);

	return;
}





int cdw_drive_toggle_tray2(const char *device_fullpath, bool with_processwin)
{
	cdw_rv_t m = cdw_fs_check_device_mounted(device_fullpath);
	if (m == CDW_ERROR) {
		; /* let's try anyway */
	} else if (m == CDW_OK) { /* device is mounted */
		/* 2TRANS: this is title of dialog window */
		cdw_buttons_dialog(_("Message"),
				   /* 2TRANS: this is message in dialog window */
				   _("Cannot eject tray. Please unmount your drive."),
				   CDW_BUTTONS_OK, CDW_COLORS_WARNING);
		return CDW_ERROR;
	} else { /* crv == CDW_NO, not mounted */
		;
	}

	for (int i = 0; i < 10; ) {
		int rv = cdw_drive_status(device_fullpath);
		if (rv == CDS_DISC_OK
		    || rv == CDS_NO_DISC) {
			cdw_vdm ("INFO: tray is closed (CDS_DISC_OK || CDS_NO_DISC) - ejecting\n");
			if (with_processwin) {
				/* 2TRANS: this is message displayed in process window */
				cdw_processwin_display_sub_info(_("Ejecting tray"));
			}
			cdw_drive_eject_tray(device_fullpath);
			break;

		} else if (rv == CDS_TRAY_OPEN) {
			cdw_vdm ("INFO: tray is open (CDS_TRAY_OPEN) - closing\n");
			if (with_processwin) {
				/* 2TRANS: this is message displayed in process window */
				cdw_processwin_display_sub_info(_("Closing tray and waiting for drive"));
			}
			usleep(500000);
			cdw_drive_close_tray(device_fullpath);
			break;

		} else if (rv == -1 || rv == CDS_NO_INFO) {
			cdw_vdm ("ERROR: -1 || CDS_NO_INFO\n");
			sleep(1);
			break;

		} else { /* CDS_DRIVE_NOT_READY */
			cdw_vdm ("WARNING: CDS_DRIVE_NOT_READY\n");
			sleep(1);
			i++;
		}
	}

	/* get final status: if there were no problems this will be a status
	   after toggling, and if there were problems, the error value will
	   be passed to caller */
	int retval = cdw_drive_status(device_fullpath);

	if (retval == -1 || retval == CDS_NO_INFO) {
		cdw_vdm ("ERROR: CDW_SYS_ERROR || CDS_NO_INFO\n");
		/* 2TRANS: this is title of dialog window: some error occurred
		   while trying to access disc */
		cdw_buttons_dialog(_("Drive error"),
				   /* 2TRANS: this is message in dialog window:
				      some error occurred while accessing cdrom */
				   _("Can't open device. Please check your configuration."),
				   CDW_BUTTONS_OK, CDW_COLORS_ERROR);
	} else if (retval == CDS_DRIVE_NOT_READY) {
		cdw_vdm ("ERROR: CDS_DRIVE_NOT_READY\n");
		/* 2TRANS: this is title of dialog window: some error occurred
		   while trying to access disc */
		cdw_buttons_dialog(_("CD drive error"),
				   /* 2TRANS: this is message in dialog window */
				   _("CDROM doesn't respond (timeout)."),
				   CDW_BUTTONS_OK, CDW_COLORS_ERROR);
	} else {
		/* some non-error value */
	}

	return retval;
}





void cdw_drive_toggle_tray_with_ui_update2(void)
{
	const char *drive = cdw_drive_get_drive_fullpath();
	if (drive == (const char *) NULL) {
		/* 2TRANS: this is title of dialog window */
		cdw_buttons_dialog(_("Error"),
				   /* 2TRANS: this is message in dialog window */
				   _("Can't find any drive. Please check your hardware configuration."),
				   CDW_BUTTONS_OK, CDW_COLORS_ERROR);
		return;
	}

	/* 2TRANS: this is title of process window;
	   "drive" is "optical drive" - a burner */
	cdw_processwin_create(_("Drive tray"),
			      /* 2TRANS: this is message in process window,
				  "drive" is "optical drive" - a burner */
			      _("Opening/closing drive tray"), false);

	int rv = cdw_drive_toggle_tray2(drive, true);

	cdw_disc_t *disc = cdw_disc_new();

	if (rv == CDS_DISC_OK) {
		cdw_rv_t crv = cdw_disc_get(disc);
		if (crv != CDW_OK) {
			/* 2TRANS: this is title of dialog window */
			cdw_buttons_dialog(_("Error"),
					   /* 2TRANS: this is message in dialog window */
					   _("Cannot get media info."),
					   CDW_BUTTONS_OK, CDW_COLORS_ERROR);

		}
	} else if (rv == CDS_NO_DISC || rv == CDS_TRAY_OPEN) {
		;
	} else if (rv == CDS_DRIVE_NOT_READY) {
		cdw_vdm ("WARNING: drive still not ready\n");
	} else {
		cdw_vdm ("ERROR: failed to toggle drive tray\n");
	}

	cdw_main_ui_disc_info_view_display_data(disc);
	/* update information about disc size and usage */
	cdw_main_window_volume_info_view_update(disc->capacity.sectors_used, disc->capacity.sectors_total, false, global_config.follow_symlinks);
	/* 2TRANS: this is message in process window,
	    "drive" is "optical drive" - a burner */
	cdw_processwin_destroy(_("Drive state changed"), false);

	cdw_disc_delete(&disc);

	return;
}





CDW_DROPDOWN *cdw_drive_make_drives_dropdown(WINDOW *window, int row, int col, int width)
{
#define LABEL_LEN 50

	CDW_DROPDOWN *dropdown = cdw_dropdown_new(window, row, col, width,
						  drives.n_drives + 1, CDW_COLORS_DIALOG);


	char label[LABEL_LEN + 1];
	size_t max_len = width > LABEL_LEN ? LABEL_LEN : (size_t) width;
	/* 2TRANS: this is a label in a dropdown; "custom path" is
	   "path to a device file, specified by user" */
	strncpy (label, _("Use custom path to drive"), max_len);
	label[LABEL_LEN] = '\0';
	cdw_rv_t crv = cdw_dropdown_add_item(dropdown, CDW_DRIVE_ID_CUSTOM, label);
	if (crv != CDW_OK) {
		cdw_vdm ("ERROR: failed to add first label \"%s\" to dropdown\n", label);
		cdw_dropdown_delete(&dropdown);
		return (CDW_DROPDOWN *) NULL;
	}

	for (int i = 0; i < drives.n_drives; i++) {
		snprintf(label, max_len + 1, "%s%s (%s / %s)",
			 i == drives.cdio_default_drive_ind ? "* " : "",
			 cdw_cdio_drives[i].fullpath,
			 cdw_cdio_drives[i].hw_info.psz_vendor,
			 cdw_cdio_drives[i].hw_info.psz_model);

		cdw_vdm ("INFO: adding dropdown label #%d = \"%s\"\n", i, label);
		crv = cdw_dropdown_add_item(dropdown, i, label);
		if (crv != CDW_OK) {
			cdw_vdm ("ERROR: failed to add first label \"%s\" to dropdown\n", label);
			cdw_dropdown_delete(&dropdown);
			return (CDW_DROPDOWN *) NULL;
		}
	}
	cdw_dropdown_finalize(dropdown);

	crv = cdw_dropdown_set_current_item_by_id(dropdown, drives.current_drive_id);
	if (crv != CDW_OK) {
		cdw_vdm ("ERROR: failed to set current item\n");
		/* what now? */
	}
	cdw_dropdown_display_current_item(dropdown);

	return dropdown;
}





const char *cdw_drive_get_drive_fullpath(void)
{
	if (drives.n_drives == 0) {
		/* there are zero drives detected by cdio, but there may be
		   some custom path entered in configuration window;
		   if there is no custom path, then function will return
		   empty string, which won't be an error, but will signify
		   that there are surely no drives available */
		const char *path = cdw_config_get_custom_drive();
		if (!strlen(path)) {
			path = (const char *) NULL;
		}
		cdw_vdm ("INFO: n_drives = 0, selecting custom drive path = \"%s\"\n", path);
		return path;
	}

	if (drives.current_drive_id == CDW_DRIVE_ID_CUSTOM) { /* use custom drive path */
		const char *path = cdw_config_get_custom_drive();
		cdw_vdm ("INFO: selecting custom drive path = \"%s\"\n", path);
		return path;
	} else {
		const char *path = cdw_cdio_drives[drives.current_drive_id].fullpath;
		cdw_vdm ("INFO: selecting cdio drive path %lld = \"%s\"\n", drives.current_drive_id, path);
		return path;
	}
}





char *cdw_drive_get_help_message(void)
{
#if 1
	char *message = cdw_string_concat
		/* 2TRANS: this is a help message */
		(drives.n_drives == 0 ? _("cdw can't detect any drives, you may want to enter and use custom path to a device.\n") : "",
		 /* here -1 is only an initial value */
		 drives.n_drives != 0 && drives.cdio_default_drive_ind != -1 ?
		 /* 2TRANS: this is a help message, describing content
		    of a dropdown */
		 _("'*' in dropdown denotes default drive.\n") : "",
		 /* 2TRANS: this is a help message, keep \"/dev/xxx\" unchanged */
		 _("Custom path to device should have form \"/dev/xxx\", without ending slash.\n\"SCSI device\" field is for experts only, and if filled, cdw may not work correctly.\n"),
		 (char *) NULL);
#else
	/* test message */
	char *message = strdup("dddddddddddwwwwwwwwwwwwiiiiiiiiiissssssllllllllllllbbbbbbbbboooooooooooeeeeeejjjjjjjd");
#endif
	return message;
}





/**
   \brief Save changes made in configuration window into "cdw drive" data structures

   Function saves some options of current configuration into
   variables that are private to "cdw drive" module.

   There are also other options in configuration window, but their values
   should be saved to config variable - use cdw_drive_set_config_fields
   to do this.

   \p id may be either a "real" ID (CDW_DRIVE_ID_CUSTOM), or non-negative
   index of one of dropdown items. CDW_DRIVE_ID_CUSTOM is an ID of first
   item in the dropdown, following IDs are indexed starting from zero.

   \param id - id of current item in drive dropdown in config window;
*/
void cdw_drive_save_configuration(cdw_id_t id)
{
	drives.current_drive_id = id;
	return;

}





/**
   \brief Save values of selected configuration fields in config variable

   Function saves value of drive dropdown in appropriate field of config
   variable.

   \param config - config variable in which to save state of "drive" dropdown
   \param id - id of current item of "drive" dropdown
*/
void cdw_drive_set_config_fields(cdw_config_t *config, cdw_id_t id)
{
	if (id == CDW_DRIVE_ID_CUSTOM) {
		/* current selection is "use custom drive path" */
		strncpy(config->selected_drive, "custom", OPTION_FIELD_LEN_MAX);
		config->selected_drive[OPTION_FIELD_LEN_MAX] = '\0';
		cdw_assert (!strcmp(config->selected_drive, "custom"), "ERROR: failed to correctly save \"custom\" value\n");
	} else {
		/* current selection is one of drives detected by cdio */
		strncpy(config->selected_drive, cdw_cdio_drives[id].fullpath, OPTION_FIELD_LEN_MAX);
		config->selected_drive[OPTION_FIELD_LEN_MAX] = '\0';
		cdw_assert (!strcmp(config->selected_drive, cdw_cdio_drives[id].fullpath), "ERROR: failed to correctly save non-custom value \"%s\"\n", cdw_cdio_drives[id].fullpath);
	}

	return;
}





cdw_id_t cdw_drive_get_current_drive_id(void)
{
	return drives.current_drive_id;
}