-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdvd_drive.c
138 lines (112 loc) · 2.66 KB
/
dvd_drive.c
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
#include "dvd_drive.h"
#ifdef __linux__
/**
* @file dvd_drive.h
*
* Functions to get status of DVD drive hardware tray
*
* There's two types of functions: ones that can be used externally to see if
* there is a DVD or not, and others internally to get a closer look at the
* actual status of the drive.
*
* To simply check if everything is READY TO GO, use dvd_drive_has_media()
*
* For example:
*
* if(dvd_drive_has_media("/dev/dvd"))
* printf("Okay to go!\n");
* else
* printf("Need a DVD before I can proceed.\n");
*
*/
/**
* Get CDROM_DRIVE_STATUS from the kernel
* See 'linux/cdrom.h'
*
* Should be used by internal functions to get drive status, and then set
* booleans based on the return value.
*
*/
int dvd_drive_get_status(const char *device_filename) {
int dvd_fd;
int drive_status;
dvd_fd = open(device_filename, O_RDONLY | O_NONBLOCK);
drive_status = ioctl(dvd_fd, CDROM_DRIVE_STATUS);
close(dvd_fd);
return drive_status;
}
/**
* Check if the DVD tray IS CLOSED and HAS A DVD
*
*/
bool dvd_drive_has_media(const char *device_filename) {
if(dvd_drive_get_status(device_filename) == CDS_DISC_OK)
return true;
else
return false;
}
/**
* Check if the DVD tray IS OPEN
*
*/
bool dvd_drive_is_open(const char *device_filename) {
if(dvd_drive_get_status(device_filename) == CDS_TRAY_OPEN)
return true;
else
return false;
}
/**
* Check if the DVD tray IS CLOSED
*
*/
bool dvd_drive_is_closed(const char *device_filename) {
int drive_status;
drive_status = dvd_drive_get_status(device_filename);
if(drive_status == CDS_NO_DISC || drive_status == CDS_DISC_OK)
return true;
else
return false;
}
/**
* Check if the DVD tray IS READY TO QUERY FOR STATUS
*
* Should be used internally only. All other functions run this anytime it is
* doing a check to see if it's okay to query the status.
*
* This function is necessary for handling those states where a drive tray is
* being opened or closed, and it hasn't finished initilization. Once this
* returns true, everything else has the green light to go.
*
*/
bool dvd_drive_is_ready(const char* device_filename) {
if(dvd_drive_get_status(device_filename) != CDS_DRIVE_NOT_READY)
return true;
else
return false;
}
/**
* Human-friendly print-out of the dvd drive status
*
*/
void dvd_drive_display_status(const char *device_filename) {
const char *status;
switch(dvd_drive_get_status(device_filename)) {
case 1:
status = "no disc";
break;
case 2:
status = "tray open";
break;
case 3:
status = "drive not ready";
break;
case 4:
status = "drive ok";
break;
default:
status = "no info";
break;
}
printf("%s\n", status);
}
#endif