cd inserted event

:confused: i am programming in c and i need to start a procedure automatically when a cd is inserted. Anyone knows how can i do it?

thanx

Sounds like you need to create an autorun.inf file that will run your executable, which is more of a windows question than a c programming question.

http://autorun.moonvalley.com/autoruninf.htm

no,i need to detect when a cd is inserted into the cd-rom device in order to make a c language program do certain things
i need to know if there is some kind of signal or interrupt received from a cd insert event or something similar...

Hi,

related to oomberas answer, you can start a small binary via autorun which sends you a signal.

regards
malcom

I believe you may need to keep polling the device manually.

Have a look at this code. Most of what you will need
is here if you are using Linux, BSD, etc.

www.hadess.net/files/patches/cdrom-test.c
  • Finnbarr

fpmurphy, that link did not work for me.

massimo_rati, you cannot have a process magically spring into existence because a cd has been inserted. You can write a daemon that will notice a cd insertion. SunOS comes with a volume manager that notices cd insertions and mounts the cd. Other versions of unix might have something similiar. This would be a problem for you. Most likely, only one such program could be running.

How this works will depend on the cd driver. You need to find the docs for your cd driver and read them.

Ideally, you will simply use open(2) on the device file for the cd. And you will not use O_NONBLOCK nor O_NDELAY. Thus the open will block until the device is ready.

But anything is possible. You're at the mercy of the driver's author.

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);
}

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 helping:)

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

Massimo:(