Reputation: 11
I want to write a python to give a warning if I try to play music without headphones/earphones plugged in. How can I detect whether they are connected or not?
Upvotes: 1
Views: 1051
Reputation: 1787
Modern Linux systems use dbus messaging system to communicate between application. You can manipulate existing objects (that means: other running software) or you can listen to some signals telling about changes in the system (screen locked, USB driver inserted, headphones plugged in, etc). Depending on the interface provided by system components (to be exact: mostly by your desktop manager components) you can also ask about current state. You can use dbus-python library to communicate with dbus
from python
. That's the software development part.
What you need to find is the answer to question: how to ask desktop manager using dbus about the state of connected peripherals like headphones. There is a chance it is documented somewhere on freedesktop website. They aim at standardize the way components communicate with each other. So, there is high chance your solution would be desktop manager independent. However, if you decide to ask this question on some forum, please do not forget to mention your desktop manager (kde, gnome, etc). BTW, I'm not certain is such question would be on-topic here, on Stack Overflow, but Ask Ubuntu shall be fine.
Upvotes: 1