console.systems

Handmade screen locker with fade out

Monday, October 10, 2016

Just wanted to share my solution for screen locking that proved itself over the years very practical. As I stepped away from full-blown desktop environments, here and there I had to search how to get some of the features back, but keep it very lightweight on resources. I wanted a very primitive solution with a few dependencies.

Imagine you are watching a long YouTube video and suddenly the screen is locked. Not very user friendly. So fading out notification with possibility to cancel screen locking is a must have feature.

For this recipe we will need:

Here goes the script that will start screen fading out:

#!/bin/bash

SEC=10
[ $# -eq 1 ] && SEC=$1
FRAMES=100
SLEEP=`echo $SEC / $FRAMES | bc -l`

trap "xcalib -clear" EXIT

sleep 0.1

LAST_IDLE=`xprintidle`
for (( i = 1; i <= $FRAMES; i++ )); do
  NEW_IDLE=`xprintidle`
  if [ $LAST_IDLE -gt $NEW_IDLE ]; then
    exit 0
  fi
  LAST_IDLE=$NEW_IDLE
  xcalib -co 95 -a
  sleep $SLEEP
done

xset dpms force off

When executed it will gradually lower the contrast to pitch black. If there is any mouse movement or a key press it will cancel fading out. If you like customization, it also accept a parameter, number of seconds during which screen will be fading out (10 by default). Save it somewhere in the home directory, for example in ~/.bin/.

Screen fader by itself is not enough, we also need a locker tool as well as the tool that will detect when there is no user activity. Add the following line to your script that runs at log in, in case of Openbox it is ~/.config/openbox/autostart.

$ xautolock -time 30 -locker "dm-tool lock" -notify 15 -notifier "$HOME/.bin/screen-fader.sh" &

In here 30 is the number of minutes after which the screen will be locked if no user activity detected. 15 is the number of seconds prior to screen locking when notifier will be executed. In our case the notifier is screen-fader.sh script from above. Replace dm-tool lock with locker of your choice, if you don't use lightdm.

Then we need to change the setting for the default X screen saver that it doesn't interfere with ours. So add this line as well to your autostart script:

$ xset s 1800 1800 dpms 1800 1800 1800 # screen blanking after 30 minutes

Finally, set a handler to lock the screen on demand via a hot key. For Openbox that would be in ~/.config/openbox/rc.xml:

<keybind key="C-A-L">
  <action name="Execute">
    <name>Lock Screen</name>
    <command>xautolock -locknow</command>
  </action>
</keybind>

That's all. Now you are in full control of screen locking. Sweet!

How to disable Power Button in Openbox and Debian 8 Jessie

Sunday, October 9, 2016

This is something that was bothering me for quite a while. Rarely but I happened to press accidentally the power button and shut down my computer, loosing all unsaved work. These few times I tried to search for solution, it never worked. After another undesired power off I decided to sit down and fix it.

Why I never managed to get it working properly is due to the combination of systemd and Openbox. Both are capable to set what the special buttons like power or sleep do. So fixing it in the either one kept the button misbehaving. And the trick was to configure it in both places.

Let's first disable the button in Openbox. Edit ~/.config/openbox/rc.xml and find the XF86PowerOff piece and comment it out by wrapping it between <!-- and -->:

<!--
<keybind key="XF86PowerOff">
  <action name="Execute">
    <command>systemctl poweroff</command>
  </action>
</keybind>
-->

Reload the Openbox config file:

$ openbox --reconfigure

Time for the systemd part. Open /etc/systemd/logind.conf and find the line with HandlePowerKey in it. By default the value for the power button handler is poweroff. Remove the preceding # symbol and change the value to ignore:

HandlePowerKey=ignore

Lastly, restart logind:

$ sudo systemctl restart systemd-logind

And we're done. Now the power button is harmless. Of course it will continue working to power on the computer or wake it up from the sleep.

How to remove all SoundCloud likes at once

Friday, September 30, 2016

UPDATE Nov 2016: It took only few years of whining to get Listening history. Good job SoundCloud!

How so there is no way to track which tracks I listened? There are various "SoundCloud History" browser extensions, but every other streaming service has that (I know only YouTube) out of the box.

So my workaround is to use the Like button. I basically like every track in my stream I listened. And to keep it clean and tidy once in a while I remove all the likes to start fresh. Yet even removing all the likes at once is not possible. I just love the tech support answer:

Hello David,
I will get straight to answering your question.
Here is how it goes: You can't do that.
Hope that helps.
IK

Helps a lot! The solution is to run a simple jQuery line. Go to soundcloud.com/you/likes, then open browser console (usually F12) and put:

$(".sc-button-like[title='Unlike']").click();

What it does is that it goes through every Like button that's been pressed already and presses it again, therefore removes the like.

It may take a while if you have many likes. In my case ~200 likes took about 10 seconds. You may even see a popup saying script execution takes too long.

Debian Time Machine

Tuesday, February 10, 2015

I wish there would be a standard tool in Debian or Ubuntu to list all the packages installed since certain date. So that I could use that information to rollback the system state to its original or even a fresh-install state.

Luckily there is a log written with every ever installed, upgraded or removed package by dpkg or apt-get, which can be found at /var/log/dpkg.log. I wrote a simple script that parses the logs and extracts the packages names.

So to list all the packages installed from the beginning of the year:

$ ./dpkg-since.py -n 2015-01-01

Or to clean up the system from anything installed during last two hours:

$ sudo apt-get remove `./dpkg-since.py 2 hours`

How to pair a Low Energy (LE) Bluetooth device in dual boot with Windows & Linux

Thursday, September 18, 2014

Those who dualboot know the pain of re-pairing your keyboard or mouse everytime you boot to a different OS. In this tutorial I will show how to pair a LE Bluetooth mouse in both Windows 8 and Debian simultaneously.

First pair the device in Debian, then reboot in Windows and pair the device there too. Yes, this will reset the paring in Debian, just carry on. Now we need to access the pairing keys in Windows. Download psexec.exe and open a command prompt with Administrator rights.

> cd Downloads
> psexec.exe -s -i regedit /e C:\BTKeys.reg HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters\Keys

The keys should be now exported to C:\BTKeys.reg. You should get something like:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters\Keys\7512a3185b2c\84abd4a25ee1]
"LTK"=hex:6c,54,ee,80,40,47,6c,cb,fc,8e,f3,f1,c6,b2,04,9e
"KeyLength"=dword:00000000
"ERand"=hex(b):1e,12,aa,37,39,cc,af,34
"EDIV"=dword:00003549
"CSRK"=hex:38,d7,aa,c1,42,06,31,25,12,b8,5a,6d,c3,90,98,f2

7512a3185b2c is the MAC address of the Bluetooth adapter, which can also be written in standard format as 75:12:A3:18:5B:2C. 84abd4a25ee1 is the mouse address that was assigned during the pairing. We will need those numbers in the next steps.

Now boot again to Debian. The mouse wont paired automatically, because it is now assigned to a different address and with different keys. Let's fix it.

$ cd /var/lib/bluetooth/75:12:A3:18:5B:2C/
$ ls
cache 84:AB:D4:A2:5F:E1 settings

If you look closely, the mouse address is not the same as in Windows. In my case only the fifth group is different. We need the device addresses to match, so rename the file.

$ mv 84:AB:D4:A2:{5F,5E}:E1
$ cd 84:AB:D4:A2:5E:E1/
$ ls
attributes gatt info

Now open the info file for editing and update the keys values. The relation between Windows and Bluez keys format is as follows:

At the end you should get this.

[LocalSignatureKey]
Key=38D7AAC14206312512B85A6DC39098F2

[LongTermKey]
Key=6C54EE8040476CCBFC8EF3F1C6B2049E
Authenticated=0
EncSize=0
EDiv=13641
Rand=3796477557015712286

Save the changes and reboot. From now on Debian and Windows will connect the mouse automatically. Awesome!

Older Posts ->