Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
dvd_drive_status [2014/02/16 20:42] beandog |
dvd_drive_status [2021/10/17 17:50] (current) beandog |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== dvd_drive_status ====== | ====== dvd_drive_status ====== | ||
- | * [[https://raw2.github.com/beandog/dvd_tools/master/dvd_drive_status.c|dvd_drive_status.c]] | + | * [[dvd_info]] - main package |
+ | * [[https://github.com/torvalds/linux/blob/master/include/uapi/linux/cdrom.h|Linux cdrom.h]] | ||
+ | * [[https://github.com/beandog/dvd_info/blob/master/dvd_drive_status.c|dvd_drive_status.c]] | ||
- | This program is a successor to the small program [[trayopen]] (which also inspired this one). ''trayopen'' works great, but it doesn't poll two things: if the drive is busy or if it has media. ''dvd_drive_status'' adds that feature plus a few more. | + | This program is a successor to the small program [[trayopen]] (which also inspired this one). ''trayopen'' works great, but it doesn't poll two things: if the drive is busy or if it has media. ''dvd_drive_status'' adds that feature plus a few more. |
+ | |||
+ | It has support on and off for *BSD systems. I like to work on it, and then I don't. The kernels are different, and have different tray status checks and so on. I'm limited by that. | ||
+ | |||
+ | A man page is included with the ''dvd_info'' program (man dvd_drive_status). | ||
=== Context === | === Context === | ||
- | The reason this program was written, was because I was having problems having programs access the DVD drive before it is ready. Duplication is simple. | + | The reason this program was written, was because I was having problems having programs access the DVD drive before it is ready. Duplication is simple. |
* Open your DVD tray | * Open your DVD tray | ||
Line 16: | Line 22: | ||
* get frustrated and have to re-run your program once the drive is ready | * get frustrated and have to re-run your program once the drive is ready | ||
- | The problem here is that the OS will close the tray, and pass off control of the device back to the user, while the device is still in a non-ready status. So, using the above example, even closing it with ''eject'', it will still complain, because eject finishes before the drive is ready: | + | The problem here is that the OS will close the tray, and pass off control of the device back to the user, while the device is still in a non-ready status. So, using the above example, even closing it with ''eject'', it will still complain, because eject finishes before the drive is ready: |
<code> | <code> | ||
Line 23: | Line 29: | ||
</code> | </code> | ||
- | To fix this, we need to properly check if a drive is ready to be accessed or not. Just being "closed" doesn't mean it is. I wanted to solve that problem, so this is what I came up with. | + | To fix this, we need to properly check if a drive is ready to be accessed or not. Just being "closed" doesn't mean it is. I wanted to solve that problem, so this is what I came up with. |
=== Drive and Tray States === | === Drive and Tray States === | ||
Line 36: | Line 42: | ||
In every state, the DVD drive is ready to send commands to it, and not have your program puke, **except** for if it is opening or closing. | In every state, the DVD drive is ready to send commands to it, and not have your program puke, **except** for if it is opening or closing. | ||
- | I should clarify. When I say "opening or closing" I don't mean just the physical state of the tray, this also includes the time that either the OS or the drive has to poll the tray and see what's going on. If your drive has an LED on front, you'll see it lighting up after you close the tray. | + | I should clarify. When I say "opening or closing" I don't mean just the physical state of the tray, this also includes the time that either the OS or the drive has to poll the tray and see what's going on. If your drive has an LED on front, you'll see it lighting up after you close the tray. |
It's that state where the drive is //not ready// to be accessed. And when the drive's not ready to access, then automated programs will complain because it thinks there's not a DVD in the drive -- even if there is. Basically, you can't effectively know if something is in a proper state. | It's that state where the drive is //not ready// to be accessed. And when the drive's not ready to access, then automated programs will complain because it thinks there's not a DVD in the drive -- even if there is. Basically, you can't effectively know if something is in a proper state. | ||
Line 42: | Line 48: | ||
=== Checking Drive / Tray Status === | === Checking Drive / Tray Status === | ||
- | Anyway, inspired by [[trayopen]] and having a desire to learn C myself, I came up with this little program, ''dvd_drive_status''. And by little, I mean that. ALL it does is just query the state of the DVD drive using the Linux cdrom.h. | + | Anyway, inspired by [[trayopen]] and having a desire to learn C myself, I came up with this little program, ''dvd_drive_status''. And by little, I mean that. ALL it does is just query the state of the DVD drive using the Linux cdrom.h. |
- | A nice bonus from the CD driver is that it lets you see if there is media in the tray or not, while the tray is closed. So again, this is very helpful for scripts that want to automate querying the status of your DVD drive. | + | A nice bonus from the CD driver is that it lets you see if there is media in the tray or not, while the tray is closed. So again, this is very helpful for scripts that want to automate querying the status of your DVD drive. |
=== Scripting === | === Scripting === | ||
- | The [[https://raw2.github.com/beandog/dvd_tools/master/dvd_drive_status.c|program]] reports status to stdout, but returns with an exit code indicating status as well. | + | The [[https://raw2.github.com/beandog/dvd_info/master/dvd_drive_status.c|program]] reports status to stdout, but returns with an exit code indicating status as well. |
Here's the exit codes: | Here's the exit codes: | ||
Line 58: | Line 64: | ||
* 5 - device exists, but is NOT a DVD drive | * 5 - device exists, but is NOT a DVD drive | ||
* 6 - cannot access device | * 6 - cannot access device | ||
+ | |||
+ | If you want to see if the **tray is closed**, then exit codes 1 and 4 would indicate that. | ||
+ | |||
+ | If you want to see if there is **no disc**, then exit codes 1 and 2 would apply. | ||
+ | |||
+ | If you just want to see if the **tray is closed and has media**, that would be exit code 4. | ||
+ | |||
+ | === Sample Script === | ||
+ | |||
+ | Here's a really simple shell script that you could use to wait until the device is ready to access: | ||
+ | |||
+ | **wait_for_drive.sh**: | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | # Accept first argument as DVD device, or use /dev/dvd as default | ||
+ | device=$1 | ||
+ | if [[ -z "$1" ]]; then | ||
+ | device=/dev/dvd | ||
+ | fi | ||
+ | |||
+ | # Run dvd_drive_status, and dump all output to /dev/null for silence | ||
+ | dvd_drive_status &> /dev/null | ||
+ | |||
+ | # Get the exit code | ||
+ | exit_code=$? | ||
+ | |||
+ | # Quit if it's already ready already! :) | ||
+ | if [[ $exit_code -ne 3 ]]; then | ||
+ | exit 0 | ||
+ | fi | ||
+ | |||
+ | # Otherwise, wait patiently (one second) and try again, until it is ready | ||
+ | until [[ $exit_code -ne 3 ]]; do | ||
+ | dvd_drive_status &> /dev/null | ||
+ | exit_code=$? | ||
+ | sleep 1 | ||
+ | done | ||
+ | |||
+ | </code> |