Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to detect a signal when a headphone is connected or disconnected from the system. What is the best way to do this?

If there is a special board with drivers, that will be my preferred way.

share|improve this question
    
I'm not sure I understand the last part - do you intend to do this by connecting an additional hardware to your computer? –  rozcietrzewiacz Nov 30 '11 at 14:56
    
@rozcietrzewiacz - I have an java app that needs to get notified if the headphone is plugged or unplugged. That is my goal. I am open to any suggestions/recommendation with or without using additional hardware. –  user775633 Nov 30 '11 at 16:03
    
@Gilles - No, typical hardware does provide notification to software. I know this is U&L, but you're likely familiar with the classic annoying Windows notification: "You've plugged a device into the audio jack!" i51.tinypic.com/2w2hogw.jpg. Linux, thankfully, doesn't do this, but the information should be available from the hardware you've got. The question is how the OS makes this information available. –  Kevin Vermeer Dec 1 '11 at 14:44
    
@KevinVermeer I don't happen to be familiar with this particular Windows annoyance, no. Good to know that modern hardware does provide notification, thank you. Linux would typically provide the information through /sys then, and perhaps notifications via dbus. –  Gilles Dec 1 '11 at 15:30
    
This is not on a windows system. This issue is for LINUX os. I should have been more explicit. Sorry! Anyway looks like by default there is no easy way to trap that notification.. That is why I am even thinking of looking out if there is any custom cards that has a driver that will trap and make this signal available for apps running on linux. –  user775633 Dec 1 '11 at 17:26
show 2 more comments

3 Answers

This information is available in /proc/asound/card0/codec#0 and depends on the hardware. For my computer, it is in the section which captures this information:

Headphone connected:

[...]
Node 0x0d [Pin Complex] wcaps 0x400181: Stereo
  Control: name="Speaker Phantom Jack", index=0, device=0
  Pincap 0x00000014: OUT Detect
  Pin Default 0x90170110: [Fixed] Speaker at Int N/A
    Conn = Analog, Color = Unknown
    DefAssociation = 0x1, Sequence = 0x0
    Misc = NO_PRESENCE
  Pin-ctls: 0x00:
[...]

Headphone disconnected (see Pin-ctls):

[...]
Node 0x0d [Pin Complex] wcaps 0x400181: Stereo
  Control: name="Speaker Phantom Jack", index=0, device=0
  Pincap 0x00000014: OUT Detect
  Pin Default 0x90170110: [Fixed] Speaker at Int N/A
    Conn = Analog, Color = Unknown
    DefAssociation = 0x1, Sequence = 0x0
    Misc = NO_PRESENCE
  Pin-ctls: 0x40: OUT
[...]

You could use inotify to check if the file was modified and grep the information.

See also http://askubuntu.com/questions/133809/mute-sound-on-headphone-unplug.

share|improve this answer
add comment

Find the udev tagger of the jack on the System, connect a client (dbus-monitor) that monitors the bus using DBus for messages on jack connect & disconnections.

share|improve this answer
add comment

A tool called hda-verb can enable/disable the headphone jack using pins.

For example,

To enable headphone jack, use:

./hda-verb /dev/snd/hwC0D0 0x0f SET_PIN_WIDGET_CONTROL 0x40

To disable headphone jack, use:

./hda-verb /dev/snd/hwC0D0 0x0f SET_PIN_WIDGET_CONTROL 0

Since you just want to check its status, perhaps you can use some polling mechanism in your java program which can check the status of above pins using hda. For this, your java program should be able to call hda-verb. Alternatively, you can check the source of hda-verb as it is available and see how they have done it.

Hope this helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.