/* * Toggle tray. * * Written by Benjamin Schwenk <*****@*****.**> and * Sybren Stuvel <*****@*****.**> * * Not supported by older kernels because it might use * CloseTray(). * */ static void toggle_tray(int fd) { struct timeval time_start, time_stop; int time_elapsed; #ifdef CDROM_DRIVE_STATUS /* First ask the CDROM for info, otherwise fall back to manual. */ switch (ioctl(fd, CDROM_DRIVE_STATUS)) { case CDS_TRAY_OPEN: close_tray(fd); return; case CDS_NO_DISC: case CDS_DISC_OK: if (!eject_cdrom(fd)) err(EXIT_FAILURE, _("CD-ROM eject command failed")); return; case CDS_NO_INFO: warnx(_("no CD-ROM information available")); return; case CDS_DRIVE_NOT_READY: warnx(_("CD-ROM drive is not ready")); return; default: abort(); } #endif /* Try to open the CDROM tray and measure the time therefor * needed. In my experience the function needs less than 0.05 * seconds if the tray was already open, and at least 1.5 seconds * if it was closed. */ gettimeofday(&time_start, NULL); /* Send the CDROMEJECT command to the device. */ if (!eject_cdrom(fd)) err(EXIT_FAILURE, _("CD-ROM eject command failed")); /* Get the second timestamp, to measure the time needed to open * the tray. */ gettimeofday(&time_stop, NULL); time_elapsed = (time_stop.tv_sec * 1000000 + time_stop.tv_usec) - (time_start.tv_sec * 1000000 + time_start.tv_usec); /* If the tray "opened" too fast, we can be nearly sure, that it * was already open. In this case, close it now. Else the tray was * closed before. This would mean that we are done. */ if (time_elapsed < TRAY_WAS_ALREADY_OPEN_USECS) close_tray(fd); }
int main(void) { int fd_cdrom; int select; int track; fd_cdrom = open_cdrom(); do { printf("-1- CD-Tray oeffnen\n"); printf("-2- CD-Tray schliessen\n"); printf("-3- CDROM-Faehigkeiten\n"); printf("-4- Audio-CD abspielen (komplett)\n"); printf("-5- Einzelnen Track abspielen\n"); printf("-6- <PAUSE>\n"); printf("-7- <FORTFAHREN>\n"); printf("-8- <STOP>\n"); printf("-9- Aktuellen Status ermitteln\n"); printf("-10- CD-Inhalt ausgeben\n"); printf("-11- Programmende\n"); printf("Auswahl: "); scanf("%d", &select); switch (select) { case 1: open_tray(fd_cdrom); break; case 2: close_tray(fd_cdrom); break; case 3: capability_cdrom(fd_cdrom); break; case 4: play_cdrom(fd_cdrom, FULL); break; case 5: printf("Welchen Track: "); scanf("%d", &track); play_cdrom(fd_cdrom, track); break; case 6: pause_cdrom(fd_cdrom); break; case 7: resume_cdrom(fd_cdrom); break; case 8: stop_cdrom(fd_cdrom); break; case 9: get_audio_status(fd_cdrom); break; case 10: content_cdrom(fd_cdrom); break; case 11: printf("Bye!\n"); break; default: printf("Was?"); } } while (select != 11); stop_cdrom(fd_cdrom); close(fd_cdrom); return 0; }
/* main program */ int main(int argc, char **argv) { char *device = NULL; char *disk = NULL; char *mountpoint = NULL; int worked = 0; /* set to 1 when successfully ejected */ int fd; /* file descriptor for device */ setlocale(LC_ALL,""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); /* parse the command line arguments */ parse_args(argc, argv, &device); /* handle -d option */ if (d_option) { info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE); return EXIT_SUCCESS; } if (!device) { device = mnt_resolve_path(EJECT_DEFAULT_DEVICE, NULL); verbose(_("using default device `%s'"), device); } else { char *p; if (device[strlen(device)-1] == '/') device[strlen(device)-1] = '\0'; /* figure out full device or mount point name */ p = find_device(device); if (p) free(device); else p = device; device = mnt_resolve_spec(p, NULL); free(p); } if (!device) errx(EXIT_FAILURE, _("%s: unable to find device"), device); verbose(_("device name is `%s'"), device); device_get_mountpoint(&device, &mountpoint); if (mountpoint) verbose(_("%s: mounted on %s"), device, mountpoint); else verbose(_("%s: not mounted"), device); disk = get_disk_devname(device); if (disk) { verbose(_("%s: disc device: %s (disk device will be used for eject)"), device, disk); free(device); device = disk; disk = NULL; } else { struct stat st; if (stat(device, &st) != 0 || !S_ISBLK(st.st_mode)) errx(EXIT_FAILURE, _("%s: not found mountpoint or device " "with the given name"), device); verbose(_("%s: is whole-disk device"), device); } if (F_option == 0 && is_hotpluggable(device) == 0) errx(EXIT_FAILURE, _("%s: is not hot-pluggable device"), device); /* handle -n option */ if (n_option) { info(_("device is `%s'"), device); verbose(_("exiting due to -n/--noop option")); return EXIT_SUCCESS; } /* handle -i option */ if (i_option) { fd = open_device(device); manual_eject(fd, i_arg); return EXIT_SUCCESS; } /* handle -a option */ if (a_option) { if (a_arg) verbose(_("%s: enabling auto-eject mode"), device); else verbose(_("%s: disabling auto-eject mode"), device); fd = open_device(device); auto_eject(fd, a_arg); return EXIT_SUCCESS; } /* handle -t option */ if (t_option) { verbose(_("%s: closing tray"), device); fd = open_device(device); close_tray(fd); set_device_speed(device); return EXIT_SUCCESS; } /* handle -T option */ if (T_option) { verbose(_("%s: toggling tray"), device); fd = open_device(device); toggle_tray(fd); set_device_speed(device); return EXIT_SUCCESS; } /* handle -X option */ if (X_option) { verbose(_("%s: listing CD-ROM speed"), device); fd = open_device(device); list_speeds(device, fd); return EXIT_SUCCESS; } /* handle -x option only */ if (!c_option) set_device_speed(device); /* * Unmount all partitions if -m is not specified; or umount given * mountpoint if -M is specified, otherwise print error of another * partition is mounted. */ if (!m_option) { int ct = umount_partitions(device, M_option); if (ct == 0 && mountpoint) umount_one(mountpoint); /* probably whole-device */ if (M_option) { if (ct == 1 && mountpoint) umount_one(mountpoint); else if (ct) errx(EXIT_FAILURE, _("error: %s: device in use"), device); } } /* handle -c option */ if (c_option) { verbose(_("%s: selecting CD-ROM disc #%ld"), device, c_arg); fd = open_device(device); changer_select(fd, c_arg); set_device_speed(device); return EXIT_SUCCESS; } /* if user did not specify type of eject, try all four methods */ if (r_option + s_option + f_option + q_option == 0) r_option = s_option = f_option = q_option = 1; /* open device */ fd = open_device(device); /* try various methods of ejecting until it works */ if (r_option) { verbose(_("%s: trying to eject using CD-ROM eject command"), device); worked = eject_cdrom(fd); verbose(worked ? _("CD-ROM eject command succeeded") : _("CD-ROM eject command failed")); } if (s_option && !worked) { verbose(_("%s: trying to eject using SCSI commands"), device); worked = eject_scsi(fd); verbose(worked ? _("SCSI eject succeeded") : _("SCSI eject failed")); } if (f_option && !worked) { verbose(_("%s: trying to eject using floppy eject command"), device); worked = eject_floppy(fd); verbose(worked ? _("floppy eject command succeeded") : _("floppy eject command failed")); } if (q_option && !worked) { verbose(_("%s: trying to eject using tape offline command"), device); worked = eject_tape(fd); verbose(worked ? _("tape offline command succeeded") : _("tape offline command failed")); } if (!worked) errx(EXIT_FAILURE, _("unable to eject")); /* cleanup */ close(fd); free(device); free(mountpoint); mnt_unref_table(mtab); return EXIT_SUCCESS; }