Home / Software / BSD, Linux, and Unix / USB Drive sounds (Linux Mint 10)
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2  All - (Bottom) Print
Author Topic: USB Drive sounds (Linux Mint 10)  (Read 717 times)
BC_Programmer
Topic Starter
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« on: December 15, 2011, 06:34:40 AM »

So I googled my head off trying to find out if there is a way to make it so that Linux can play a short sound file when a USB drive/device is added or removed. I couldn't find anything, so I figured I'd write a python script to do it.

It uses the apparently deprecated DBUS api, because I couldn't figure ut how to install half the crap the "new" method wanted (pynotify's installation scripts had broken shebangs and crashed seemingly randomly when I tried to install it, and I have no idea how to get libnotify working). Also, so far it only seems to work for adding volumes, since dbus won't let me inspect the removed device in the remove hook to determine if it's a volume, and I got weird behaviour when I just played the removal sound for any removal without inspecting.

For sound, I tried about a dozen different APIs and packages, which either refused to install, had issues during ./configure that I couldn't resolve (particularly the one where it complained that I didn't have the header to compile python extensions which made sense except it was integrated into the core python includes and thus was extraneous and I wasn't able to find the part in the shell script to remove so I gave up). I had luck with pygame, mostly because I was able to simply apt-get it, but it crashed with audio errors when I tried to use pygame.mixer.init(). the Python-standard ossaudiodev was promising but OSS is outdated and I couldn't get it to even pretend to work.

I basically ended up just shelling out to mplayer to play the sound. Which works, I guess.

Anyway, here is the script. I now have it running off in another desktop so I get the standard windows USB add sound whenever I plug in a USB drive, heh.

Code: [Select]
#!/usr/bin/python
#by BC_Programming
import dbus
import gobject
import time
import subprocess

print "BASeCamp 'USBSounds' Simple USB Volume notification sound."

#playsound merely shells out to mplayer. I would have preferred an integrated solution but... meh.
def playsound(soundfile):

    subprocess.call(["mplayer", "hardwareinsert.wav"], stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
   
class DeviceAddedListener:
    def __init__(self):
        #get the system bus...
        self.bus = dbus.SystemBus()
        #get the manager
        self.hal_manager_obj = self.bus.get_object(
                                                  'org.freedesktop.Hal',
                                                  '/org/freedesktop/Hal/Manager')
        #get the interface for the manager                                         
        self.hal_manager = dbus.Interface(self.hal_manager_obj,'org.freedesktop.Hal.Manager')
        #connect to the appropriate signals.
        self.hal_manager.connect_to_signal('DeviceAdded', self._filteradd)
        self.hal_manager.connect_to_signal("DeviceRemoved",self._filterremove)
#note: I couldn't get DeviceRemoval sounds to work since it doesn't let you     #inspect whether the removed device is a volume via "QueryCapability"... since it's gone.
    def _filteradd(self, udi):
        device_obj = self.bus.get_object ('org.freedesktop.Hal', udi)
        device = dbus.Interface(device_obj, 'org.freedesktop.Hal.Device')
        #if it is a volume, call the do_add function...
        if device.QueryCapability("volume"):
            return self.do_add(device)
           
    def _filterremove(self,udi):
        try:
            #device_obj = self.bus.get_object('org.freedesktop.Hal',udi)
            #device = dbus.Interface(device_obj,'org.freedesktop.Hal.Device')

            #if device.QueryCapability("volume"):
            #    return self.do_remove(device)
        except:
            return
    #unused....
    def do_remove(self,volume):

        playsound("hardwareremove.wav")       
        #displays some info about the added device to the console (maybe future changes can pop stuff like volume label, device file, size, etc into a Notification box?)
    def do_add(self, volume):
        device_file = volume.GetProperty("block.device")
        label = volume.GetProperty("volume.label")
        fstype = volume.GetProperty("volume.fstype")
        mounted = volume.GetProperty("volume.is_mounted")
        mount_point = volume.GetProperty("volume.mount_point")
        try:
            size = volume.GetProperty("volume.size")
        except:
            size = 0

        print "New storage device detected:"
        print "  device_file: %s" % device_file
        print "  label: %s" % label
        print "  fstype: %s" % fstype
        if mounted:
            print "  mount_point: %s" % mount_point
        else:
            print "  not mounted"
        print "  size: %s (%.2fGB)" % (size, float(size) / 1024**3)
        #and play a sound.
        playsound("hardwareinsert.wav")

#main loop...
if __name__ == '__main__':
    from dbus.mainloop.glib import DBusGMainLoop
    DBusGMainLoop(set_as_default=True)
    loop = gobject.MainLoop()
    print "in __main__..."
    DeviceAddedListener()
    loop.run()
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Rob Pomeroy
Prodigy



Thanked: 119
Posts: 6,454

Experience: Expert
OS: Other


Web/Networking/Linux

Me
« Reply #1 on: December 19, 2011, 02:06:04 AM »

You'd think there'd be some kind of hook out of automount, wouldn't you?  Probably introducing such a feature into the code and recompiling would be the most elegant solution.  Heave you heard of inotifyincron builds on inotify and there's even an example of playing a sound on the basis of a change within the file system.
IP logged

Author of the fantasy thriller, Insensate - available for all ebook readers and iDevices. Find out more >here<. Only 99p/99¢!
BC_Programmer
Topic Starter
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #2 on: December 19, 2011, 02:17:45 AM »

I wanted a solution that I could share easily, most of the others, even assuming I was able to get them working, would generally need somebody to go off and download some other package (for example, pyinotify). I might explore that for a more robust solution, at least for myself, but this script has been working well enough for me.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Rob Pomeroy
Prodigy



Thanked: 119
Posts: 6,454

Experience: Expert
OS: Other


Web/Networking/Linux

Me
« Reply #3 on: December 19, 2011, 02:18:59 AM »

Ah, I see.  In that case, well done!
IP logged

Author of the fantasy thriller, Insensate - available for all ebook readers and iDevices. Find out more >here<. Only 99p/99¢!
Raptor
Guest
« Reply #4 on: January 08, 2012, 05:44:23 PM »

Why do you need a sound for that?
IP logged
BC_Programmer
Topic Starter
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #5 on: January 08, 2012, 06:50:57 PM »

Why do you need a sound for that?

So that I know the drive was detected.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
JJ 3000
Egghead



Thanked: 227
Posts: 3,459

Experience: Familiar
OS: Linux variant



« Reply #6 on: January 08, 2012, 07:00:56 PM »

So that I know the drive was detected.

Can't you just look?

If I recall correctly, the default behavior for Linux Mint is to auto-mount usb drives and show an icon on the desktop.

By the way, good job on the script!  :)
IP logged

Save a Life!
Adopt a homeless pet.
http://www.petfinder.com/
BC_Programmer
Topic Starter
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #7 on: January 08, 2012, 07:05:56 PM »

Can't you just look?
If I recall correctly, the default behavior for Linux Mint is to auto-mount usb drives and show an icon on the desktop.
It doesn't always show up and opening nautilus or showing the desktop is a big pain in the *censored* compared to just being able to hear a sound.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Raptor
Guest
« Reply #8 on: January 09, 2012, 08:13:55 AM »

It's pretty cool that you wrote an entire script for that. I still think Firefox is the best browser, though.
IP logged
BC_Programmer
Topic Starter
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #9 on: January 09, 2012, 11:19:44 PM »

It's pretty cool that you wrote an entire script for that. I still think Firefox is the best browser, though.
What does firefox have to do with any of this?
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Veltas
Intermediate



Thanked: 7
Posts: 154

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows 7

1
« Reply #10 on: January 10, 2012, 04:05:57 AM »

What does firefox have to do with any of this?

At least it's not "or alternatively you could just install Windows, which plays the USB-add sound".
IP logged
BC_Programmer
Topic Starter
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #11 on: January 10, 2012, 04:14:38 AM »

At least it's not "or alternatively you could just install Windows, which plays the USB-add sound".

Windows is installed in a dual boot with it, so I can properly test my programs installers without fiddling with a VM :)

the Apache/MySQL/PHP stack  is a lot easier to install & administer via on Linux, and it is the primary role of the laptop.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Veltas
Intermediate



Thanked: 7
Posts: 154

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows 7

1
« Reply #12 on: January 10, 2012, 04:23:02 AM »

the Apache/MySQL/PHP stack  is a lot easier to install & administer via on Linux, and it is the primary role of the laptop.

Is it running a website?
IP logged
Raptor
Guest
« Reply #13 on: January 10, 2012, 04:31:47 AM »

What does firefox have to do with any of this?

Nothing. Just trolling!
IP logged
Veltas
Intermediate



Thanked: 7
Posts: 154

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows 7

1
« Reply #14 on: January 10, 2012, 04:34:08 AM »

Nothing. Just trolling!

Oh Raptor...
IP logged
Pages: [1] 2  All - (Top) Print 
Home / Software / BSD, Linux, and Unix / USB Drive sounds (Linux Mint 10) « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.145 seconds with 20 queries.