A simple app - MOCP music player control interface

A project log for Linux Control System

*moved to pyLCI project*An expandable control system for Linux with apps that lets you control your Linux devices using a screen and buttons

aryaArya 08/17/2015 at 18:410 Comments

Here, I will show you how to make a script that does actual work. Let's say I want to control a music player that is Ncurses-only. Fortunately, by passing some parameters to its executable it is possible to control its playback. So, we'll be executing commands:

from subprocess import call

We'll also need a menu. For now, it's imported like that:

from import Menu
As soon as I package my framework, it'll be something like:
from wcs.interfaces import Menu

But, as for now, it's not yet properly packaged so every script using menus is to be run from the working tree. I'm going to fix this soon.

The setup part is mainly the same. We import the wcs framework, create an application with a self-descriptive name, then a window and get interfaces from it.

application = wm.create_new_application("MOCP control")
Now is the fun part - getting working Python commands to change things. First, I have made two wrappers. One is for calling mocp executable:
def mocp_command(*command):
    return call(['mocp'] + list(command))
Then, all the other commands can be described like:
def mocp_next():
The second wrapper is for calling amixer program. It's called to change volume. MOCP can do it by itself, it's just that I don't have some component on my system so the control from inside the application is not really working. Fortunately, it doesn't matter if we use one more executable - it's just another wrapper:
def amixer_command(*command):
    return call(['amixer'] + list(command))
From which we can make commands like this:
def plus_volume():
    return amixer_command("-c", "0", "--", "sset", "PCM", "400+")

Now we've got the commands, all that is left is making a menu. We'll start with the contents:

main_menu_contents = [ 
["Toggle play/pause", mocp_toggle_play],
["Next song", mocp_next],
["Previous song", mocp_prev],
["Increase volume", plus_volume],
["Decrease volume", minus_volume],
["Toggle mute", toggle_mute]
It's as simple as that. A list of lists representing menu items, where first element is the menu item name and second element is a function to be called when said element is chosen.
Let's initialise this:

menu = Menu(main_menu_contents, output, input, "Main menu", daemon = wcs._daemon)
Now we're registering the menu object with the daemon, same as we did with our HelloWorld object:
For wrapper, we need a blocking call, and Menu.activate is just the thing. So - let's start the application!, application.shutdown)

The result is on the video above. The full code for the application can be seen here.

What do you think about it? Do you have an idea of how you'll be using that project?