cd inserted event


 
Thread Tools Search this Thread
Top Forums Programming cd inserted event
# 8  
Old 01-17-2004
The link worked for me, but the code base is small enough to post:

// gcc -o cdrom-test cdrom-test.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>
int
main (int argc, char **argv)
{
char *program;
char *device;
int fd; /* file descriptor for CD-ROM device */
int status; /* return status for system calls */
int verbose = 0;
program = argv[0];
++argv;
--argc;
if (argc < 1 || argc > 2) {
fprintf (stderr, "usage: %s [-v] <device>\n",
program);
exit (1);
}

if (strcmp (argv[0], "-v") == 0) {
verbose = 1;
++argv;
--argc;
}

device = argv[0];

/* open device */
fd = open(device, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
fprintf (stderr, "%s: open failed for `%s': %s\n",
program, device, strerror (errno));
exit (1);
}
/* Check CD player status */
printf ("Drive status: ");
status = ioctl (fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
if (status<0) {
perror(" CDROM_DRIVE_STATUS");
} else switch(status) {
case CDS_DISC_OK:
printf ("Ready.\n");
break;
case CDS_TRAY_OPEN:
printf ("Tray Open.\n");
break;
case CDS_DRIVE_NOT_READY:
printf ("Drive Not Ready.\n");
break;
default:
printf ("This Should not happen!\n");
break;
}
status = ioctl (fd, CDROM_DRIVE_STATUS, 0);
if (status<0) {
perror(" CDROM_DRIVE_STATUS");
} else switch(status) {
case CDS_DISC_OK:
printf ("Disc present.");
break;
case CDS_NO_DISC:
printf ("Empty slot.");
break;
case CDS_TRAY_OPEN:
printf ("CD-ROM tray open.\n");
break;
case CDS_DRIVE_NOT_READY:
printf ("CD-ROM drive not ready.\n");
break;
case CDS_NO_INFO:
printf ("No Information available.");
break;
default:
printf ("This Should not happen!\n");
break;
}
status = ioctl (fd, CDROM_DISC_STATUS);
if (status<0) {
perror(" CDROM_DISC_STATUS");
}
switch (status) {
case CDS_AUDIO:
printf ("\tAudio disc.\t");
break;
case CDS_DATA_1:
case CDS_DATA_2:
printf ("\tData disc type %d.\t", status-CDS_DATA_1+1);
break;
case CDS_XA_2_1:
case CDS_XA_2_2:
printf ("\tXA data disc type %d.\t", status-CDS_XA_2_1+1);
break;
default:
printf ("\tUnknown disc type 0x%x!\t", status);
break;
}
status = ioctl (fd, CDROM_MEDIA_CHANGED, 0);
if (status<0) {
perror(" CDROM_MEDIA_CHANGED");
}
switch (status) {
case 1:
printf ("Changed.\n");
break;
default:
printf ("\n");
break;
}
{
int foo;
dvd_struct dvd;
dvd.type = DVD_STRUCT_COPYRIGHT;
dvd.copyright.layer_num = 0;
status = ioctl( fd, DVD_READ_STRUCT, &dvd );
foo = dvd.copyright.cpst;
if (status < 0)
{
perror ("Not a DVD");
} else switch (foo) {
case 0:
printf ("DVD not encrypted\n");
break;
case 1:
printf ("DVD encrypted\n");
break;
default:
printf ("Shouldn't happen\n");
break;
}
printf ("RMI is: %#02x\n", dvd.copyright.rmi);
}
/* close device */
status = close (fd);
if (status != 0) {
fprintf (stderr, "%s: close failed for `%s': %s\n",
program, device, strerror (errno));
exit (1);
}

exit (0);
}
# 9  
Old 01-30-2004
the code works even in my system...anyway i suppose i should implement this request of cdrom status in a while loop in another thread to get whenever a disc is inserted...
thanx for helpingSmilie
# 10  
Old 02-10-2004
after many times of debugging i realized with not
great joy that this code doesn't work all the time(strange ah?) the point is that sometimes the
line with the instruction open wich i changed with:

#define CDROM_CONST "/dev/cdrom"
fd = open(CDROM_CONST, O_RDONLY );

returns me -1 ???!!!

i don't need a code that works just for one kind of cd driver....what can i do?
thanx

MassimoSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date from filename inserted into records

Hi Folks, I have below files in one directory: Spiross-MBP:AIRTEMP spirospap$ ls -1 CPK2004001 CPK2004002 CPK2004003 etc... JFK2003001 JFK2003002 JFK2003003 etc... TEB1999001 TEB1999002 TEB1999003 etc... Month/year is in Filename and also in the file Header, first line. (18 Replies)
Discussion started by: spirospap
18 Replies

2. Solaris

Copying existing OS to new inserted drives

Hi, I was wondering if there is an easy / recommended way to do the following: Copy existing OS to 2 NEW drives inserted to server. Netra 440 / Solaris 8 1. install the 2 new physical drives, 2. reconfigure the four drives to a RAID 1 array, and 3. copy the existing 2 drives... (4 Replies)
Discussion started by: kmac22068
4 Replies

3. UNIX for Dummies Questions & Answers

Output number of rows inserted, updated...

Hi all, I am new to Unix. I have written pl/sql script to be run in Unix. I have used Merge statement and subsequently would like to know the number of rows updated or inserted. Any suggestions in this regard would be great Thanks in advance Kushal (0 Replies)
Discussion started by: kushal_cog
0 Replies

4. UNIX for Dummies Questions & Answers

help needed with getting last inserted row id

Hi, I am working on a script that inserts one row of data at the time to a table. Among the fields of that table is the “serial” which is the auto increment. I need to be able to retrieve last inserted row id to use it for another insert. To retrieve last row id right after I do successful... (2 Replies)
Discussion started by: arushunter
2 Replies
Login or Register to Ask a Question