1. Preface
Having recently made another attempt to master Linux a bit (specifically, Xubuntu 12.04), I noticed that the system suits me perfectly and I don’t want to spit on the idea and load Windows. It was easy to find analogues of familiar applications, the devices earned out of the box.
Problems that have arisen - it was not possible to configure the tv-tuner and the control of the console from it through lirc does not work at all like I used to in Windows. The first problem remains, the second one I solved with a small python script.
2. How I used the console in Windows
On Windows, I used the
SlyControl program as a WinLIRC server and
HIP for managing programs. That is, programs are not required to support WinLIRC - HIP serves as an interlayer, accepting commands from the console and emulating keystrokes (and not only, the possibilities are much wider) in the application. Moreover, the buttons for each application are configured separately and work only when it is in focus (when I configured lirc to control VLC and mplayer, I was unpleasantly surprised that all running applications accept commands, even if they are minimized). Plus, you can configure the "default" buttons, that is, if no button is defined for the active application, it performs the default action.
')
Also in the HIP there is an
Enhanced Master Control mode, by activating which, you can launch applications, switch between them, minimize / maximize / close, shut down / restart the computer and perform other similar actions. Oh yeah, you can still see the time, such a large, comfortable watch.
3. Write your HIP
I admit, I did not search for an alternative to HIP. I decided that I'd better do it myself, just the way I want it. I chose Python as the language. The second candidate was Perl - I know both languages ​​very, very superficially, but since I decided to try to learn Python, the choice fell on it. And easier, perhaps.
To emulate keystrokes (and other X server events), the
xdotool utility was selected. First, install it (banal sudo apt-get install xdotool in Ubuntu). By the way, lirc also provides the ability to emulate keys using irxevent.
So, connect to the socket / dev / lircd and read the data in an infinite loop:
lircd = socket.socket(socket.AF_UNIX) lircd.connect('/dev/lircd') while 1: comm = lircd.recv(128)
lircd sends lines like "scancode number_to press_name_key_name console_name", for example, "000400040000001c 00 Radio Chronos". We parse it with a regular expression (we need the name of the button and remote:
comm_parsed = re.search('([0-9A-Fa-f]+?) ([0-9]+?) (.+?) (.+)', comm) rc_key, rc_name = comm_parsed.group(3, 4)
Now you need to determine which application is active. It would be logical to get the name of the process, but I did not succeed, so we will use the name of the window. You can find out his id with the
xdotool getactivewindow command . Find out the window name by id -
xdotool getwindowname . Conveniently, xdotool allows you to use command chains.
We will call xdotool using the
subprocess module:
active_window_name = subprocess.check_output('xdotool getactivewindow getwindowname', shell=True)
Now you need to find in the config whether the action is set for the pressed button in this application. By the way, about the config - it is ini-like, the settings are divided into sections
[application_name] , in which the
button_plate: action pairs are stored.
Application_Name is the unchangeable part of the window title by which we will identify the application (for example, for “video.avi - SMPlayer” this will be “SMPlayer”). There is also a section for the actions of the default buttons
[Default] and the settings of the script
[Settings] . The config is called .lip-config and is stored in the user's folder. Read it elementary:
config = ConfigParser.ConfigParser() config.read(os.path.expanduser('~')+'/.lip-config')
So, we are looking for a program in the config, if found - the button
for section in config.sections()[2:]
If not found, we will try to look for the button in the
Default section (of course, if the default buttons are enabled in the config with the
use_default_keys: 1 option). Code similar:
if not key_found and use_default_keys and config.has_option('Default', rc_key): subprocess.call('xdotool '+config.get('Default', rc_key), shell=True)
Actually, that's all. The script turned out much easier than I thought. This is the merit of xdotool, which can do almost everything that is needed “out of the box”. For example, to "press" Ctrl + a, you need to run
xdotool key ctrl + a , right-click -
xdotool click 3 . Other examples of actions performed by it can be found in my .lip-config
4. Some remarks, shortcomings, plans
1. It is not
rational to use
xdotool exec to run an application. You can run directly.
2. Some applications in different modes have different window names. For example, Tano - in the window mode “{file} - Tano”, in full screen mode - “tano”.
3. A simple check for the occurrence of a substring to find an application in the config file can give a false result. For example, when playing the file "SMPlayer_example.avi" the name of the VLC window will be "SMPlayer_example.avi - VLC media player". If the
[SMPlayer] section in the config is earlier than the
[VLC media player] , it will be processed.
A possible solution to this and the previous item is to enter the
windowname: window_name option in the section of each application, in which it is possible to specify the possible options for the window name (in the form of regular expressions, for example):
windowname: tano | Tano - the name may include “tano” or “Tano” (solve the problem from item 2)
windowname: SMPlayer $ - the name should end with "SMPlayer" (solve the problem of paragraph 3)
The names of the sections are no longer used for searching and serve only for separation and clarity (you can write any text in them).
UPD: Done (with links below - updated code and config).
4. You can implement advanced mouse control (instead of writing a simple
mousemove_relative XY in the config) with acceleration depending on the length of the button press (it can be determined by the line issued by lirc):
- add processing design of the form {button}: lipmouse up (down, left, up-left, etc.)
- Add to [Settings] settings: the initial step of the cursor (in pixels), the step when the acceleration is turned on, the delay before the acceleration is turned on.
- when the corresponding key is pressed, we perform xdotool mousemove_relative XY , where X and Y will depend on direction, step and acceleration.
5. A mode similar to Enhanced HIP Master Control would not hurt. Although, it is easy to implement it by an external launcher, through the config, without any changes to the current script (but it is better to still take out a separate option).
Note 1: I do not know where it is better to store the code. So poured on pastebin: a
script , a
config .
Note 2: lip is similar to HIP, where l is linux. Or lirc. There must be a name. Even in such crafts.