How to detect if the CD tray is open or closed?

Hi, All;

I am running 6.1, I need a way to check if the CD tray is opened or closed,
and also, if there is an actual CD in the tray without mounting it first.

Any help is well appreciated.

Xuedong