Choose style:

Author Topic: Python Volume changing script query - not sure where to ask?!  (Read 97 times)

0 Members and 1 Guest are viewing this topic.

Offline wrecche

  • nOOb
  • *
  • Posts: 2
  • Karma: 0
  • New Forum User
    • View Profile
  • Peppermint version(s): OS 8
Python Volume changing script query - not sure where to ask?!
« on: October 10, 2017, 02:49:49 am »
Hi,

Bit of background first, I've always felt compelled to use a Linux OS, but over the years, small issues have always lead me to return to that demon OS, Windows. Usually, just out of frustration. Recently, I've been just having non stop issues on a lower spec machine with that, and decided to try Linux again. I trued Ubuntu again, the latest release, but it was sluggish and felt crippled. Sadly.. But I saw on another forum, the mention of Peppermint, from a number of people.. not having tried it, I went ahead. And all I can say is this is the OS I've been looking for !! Fast, quick, and everything just works. So far, any issues I've had, have not left me trailing through hundreds of pages of different scenarios, and I find any questions I have, answered concisely and pleasantly, just by reading...

But still, as I trapse through the daily use of it, I find there are those little niggling things I came to rely on in Windows, that I feel are important.. one of those being an application I used called Volume2. The simplicity of having a multimedia machine that I can change the volume or mute simply by moving the mouse to an edge (or both) is something I find I cannot get over. from a distance, finding the volume icon, or after a few beverages, it's a chore...

However, I did find a python script that does almost this, except it only lets you go to screen corners. And only lets you change the volume, not mute it. So as the tinkerer that I am, I did my due diligence and poked around the annals of google cobbling together something that is ALMOST exactly what I need.

I can move the mouse to an edge of my choosing, and control the audio exactly as I could in WIndows with Volume2. BUT, and this is where my brain falls away like wet cake, the scroll wheel unfortunately also scrolls the browser (or any scrollable window) along with the volume.

I tried to implement a sort of empty form 20px in width on the side of the screen that would be set to show once the mouse entered into this area, but my coding skill are no ninjas. I'd end up with multiple windows and accompanying taskbar (panel?) icons accordingly. Not too good. One attempt (I didn't think ahead) had the script simply put an infinite number of them there, and I had to reboot. *dont gasp, I'm new*...

But the idea would work, it would take the focus off the browser or other window, and allow me to use the scroll wheel to change the volume, and not affect things. My idea was to show the form when entering the 20px area, and hide the form on leaving... but python is a slippery snake to learn, until yesterday I had no idea that tabs and indentation dont act the same. and how it relies on indentation for nests..

Basically, tl;dr

Can anyone provide any ideas on how to take the focus off the main top application so when I move the mouse to a border, and use the scroll wheel to alter volume levels, but not actually scroll the browser?

I will include the full script if anyone can advice on it... ultimately, I would also like to replicate Volume2 in that it would show an empty form (or other object capable of taking focus temporarily) that would increase in height according to the volume level. But this is far down the track, if anyone can even grok what I'm asking..

If this is the wrong place, or wrong forum, advice on that would also be appreciated !!!

Steve.

(code attribution to Mehmet Nural Altintas, I simply added the middle click for must and right border detection)

Code: [Select]
#!/usr/bin/env python
#-*-coding:utf-8-*-
# mehmet nural altintas <mehmet.nrl@hotmail.com>
# This script is edited version of  "record_demo.py -- demonstrate record extension" that can be found here: http://python-xlib.svn.sourceforge.net/viewvc/python-xlib/trunk/examples/
# python-xlib package must be installed to run this script
# sudo apt-get install python-xlib

import os
from Xlib import X, display
from Xlib.ext import record
from Xlib.protocol import rq

Margin = 20     # The number of pixels to catch the mouse entering to the border.

# Set up the commands
def volume_up():
    os.system("amixer -q sset Master 3%+ & pid=$!")
def volume_down():
    os.system("amixer -q sset Master 3%- & pid=$!")
def volume_toggle():
    os.system("amixer -q sset Master toggle")

record_dpy = display.Display()

ctx = record_dpy.record_create_context(
        0,
        [record.AllClients],
        [{
                'core_requests': (0, 0),
                'core_replies': (0, 0),
                'ext_requests': (0, 0, 0, 0),
                'ext_replies': (0, 0, 0, 0),
                'delivered_events': (0, 0),
                'device_events': (X.KeyPress, X.MotionNotify),
                'errors': (0, 0),
                'client_started': False,
                'client_died': False,
        }])

def record_callback(reply):
    data = reply.data
    while len(data):
        event, data = rq.EventField(None).parse_binary_value(data, record_dpy.display, None, None)
       
        if event.type == X.ButtonPress:

    # Test for Right side Mouse event
    if all( [event.root_x < Margin] ):
                   
               # Event.detail is 2 means wheel click event
               if event.detail is 2 :
                  print "Toggle Mute!"
                  volume_toggle()
             
               # event.detail is 4 means wheel up event
               if event.detail is 4 :
                  print "Volume Up!"
                  volume_up()

               # event.detail is 5 means wheel down event
               if event.detail is 5 :
                  print "Volume Down!"
                  volume_down()

record_dpy.record_enable_context(ctx, record_callback)

Offline wrecche

  • nOOb
  • *
  • Posts: 2
  • Karma: 0
  • New Forum User
    • View Profile
  • Peppermint version(s): OS 8
Re: Python Volume changing script query - not sure where to ask?!
« Reply #1 on: October 10, 2017, 07:41:24 am »
As an addition, in the interim, I have found using Plank dock on the right of the screen with no icons added and undocked icons turned off, set to autohide, works, even if it is using something in an unintended way.

Hopefully someone with some Python knowledge can point me in the right direction. not a great fan of docks in general, but then I've lived in the microsoft universe for a long time.. ;)