A Lightweight Low-Battery Warning Monitor for GTK+ 3.x desktops

Today, I decided to help someone out and, as is often the case with coding, I got carried away.

I set out to help fix up a shell script but, instead, I decided to “do it right” and produced a simple Python script which…

  • Uses a combination of GLib.timeout_add_seconds and UPower events to react to power supply changes immediately while still putting a limit on how often battery percentage changes should be able to cause wakeups.
  • Uses GStreamer and notify2 to provide audio and visual low-battery warnings without the weight and delay of calling subprocesses.
  • Is compact and easy to customize because it’s written in Python

By design, it doesn’t really have much of a UI (just a persistent notification popup and alert sound that come and go in response to changes in power state), but here’s what the popup looks like when using xfce4-notifyd:

It does require the 1.0 UPower API for its “Display Device” abstraction, so an 0.99.x-series version of UPower or newer is required, but for people on a new enough Debian/Ubuntu/Mint release (Vivid, in Ubuntu’s case), the dependencies can be installed as follows:

sudo apt-get install python-notify2 python-gi gir1.2-upowerglib-1.0 gir1.2-gstreamer-1.0 gir1.2-gst-plugins-base-1.0

It hasn’t been tested under Python 3.x but it looks like it should work perfectly if you just replace python-notify2 python-gi with python3-notify2 python3-gi in the dependencies and edit the shebang line at the top of the file.

I only wish I’d realized what I was doing sooner so I could start tracking its evolution properly in git. As-is, all I could do was translate the snapshots from Dropbox’s revision history.

As usual for small, single-file things, here’s the direct download link followed by a GitHub Gist embed.

Could not embed GitHub Gist af69a99908a5bc001495: API rate limit exceeded for (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)
Posted in Geek Stuff | 10 Comments

“Rubber Matrix” and “Sparse Montage” wrapper for ImageMagick

Last night, I was working on a bunch of images and I really wanted to just get a visual overview along the lines of a scatter plot, but with images rather than dots. Unfortunately, no matter what I searched for, I couldn’t find anything to do what I wanted.

ImageMagick’s montage tool can do it… but you have to manually fill in the empty cells with the special filename null:

…so I wrote a script to do just that.

Oh, and I got carried away and wrote a “rubber matrix” class to abstract away the actual generation of the grid. Basically, it’s a DictMixin-based class that insists on consistently sized tuples as keys and then adds some extra methods to traverse them as sparse points in a matrix dynamically sized to only the rows/columns/etc. which exist.

I haven’t yet decided on what command-line syntax I want, so you have to import it, but it works perfectly in my preliminary tests.

Posted in Geek Stuff | Leave a comment

Exporting Firefox Tab Groups

TL;DR: Use this script. Non-Linux users, feed it the path to a sessionstore JSON file. Try --tiddlywiki for more copy-pastable output.

Because of the relatively high UX overhead of things like Firefox’s built-in bookmarking or Delicious.com, I recently found myself with over 1000 tabs in my Firefox tab groups.

Lazy-loading after a browser restart or not, this was very far from ideal so I whipped up a quick little script which contains not only the code I used to dump my sessionstore.js, but also the code I used as an aid to reverse-engineering the tab groups part of it since, as far as I can tell, the Mozilla guys didn’t bother to document that.

The code’s definitely not pretty and I can’t guarantee the --make-schema and --check-schema parts still work, but the dumping works perfectly and I didn’t want to spend too much time on it because I’ve heard Mozilla wants to move away from JSON for saved sessions in favour of a custom binary format specifically designed to help them minimize the amount of time internal data structures need to stay locked in order to write them out on a potentially memory-constrained device. (Remember, Firefox on Desktop shares code with Firefox OS.)

It was originally developed to operate on sessionstore.js but, between when I started developing it and now, things changed a bit so, now, it reads sessionstore-backups/recovery.js instead.

It can take one or more paths but, if you don’t provide one, it’ll assume a Linux desktop and try to export every tab group from every Firefox profile in your user account. As written, it always dumps to the tabs.html file in the current working directory.

Normally, it produces a nice-looking HTML output, complete with favicons for list bullets but, If you pass the --tiddlywiki option, it’ll generate a nice compromise that can be copy-pasted into TiddlyWiki. (Including hidden !!! prefixes for the headers so that you can copy-paste a bunch of groups in one go.)

Enjoy. 🙂

Posted in Geek Stuff | Leave a comment

A confirmation dialog for URxvt’s Print Screen key

I use urxvt’s kuake plugin as a lighter replacement for Yakuake and, as a result, I’m using it with F12 as my “toggle terminal” keybind.

…unfortunately, that means that, every time my finger slips, I hit Print Screen.

As such, I wrote this little script you can use to Print Screen to file, with a confirmation dialog to avoid the need to keep deleting unwanted printouts.

confirm_print.sh (fixed commit ID)

To use it:

  1. Copy it to somewhere like ~/bin/confirm_print.sh
  2. Make sure zenity is installed
  3. Add these lines to your ~/.Xresources
    Rxvt.print-pipe: ~/bin/confirm_print.sh
    URxvt.print-pipe: ~/bin/confirm_print.sh
  4. Run xrdb -merge ~/.Xresources
  5. Restart any terminals you may have. (You are using screen or tmux, right?)
Posted in Geek Stuff | Leave a comment

Useful bits for GOG Linux Users

Over the last little while, I’ve written or researched a few little things to make my life as a Linux-using player of GOG.com games a bit simpler. As such, I thought I’d share them.

1. GOG.com – Updated Thread Count in Title

This Greasemonkey script adds the unread count for your favourited forum threads to the favicon so you can select “Pin Tab” and have an alternative to manually polling for new replies.

It’s not RSS, but it still takes a load off your mind.

2. find_broken_symlinks.sh

While not specifically focused on GOG.com or even gaming, this multi-mode script has proven very useful.

Modes of operation:
  Show all symlinks targeted outside the current directory
 Show all symlinks targeted inside /home
 Show all symlinks targeted inside the current user's home directory
(anything else)
 Show all broken symlinks originating inside the current directory

I originally wrote it because:

  • Some of my Linux-native ports store their save files in the game’s application folder
  • I symlinked the save files into my homedir so nightly backups cover them
  • My games folder is on an external USB drive
  • Supposing I ever want to let a friend try the games on their computer, I’ll need a way to identify the folders I need to mkdir -p to unbreak the symlinks.

Caution: It’s a bit of a hack and will get confused by folder/filenames containing newlines. (Doing it properly and efficiently is very awkward and painful without reinventing /usr/bin/find in something with more support for structured data than shell scripting.)

3. aptitude search ~o
A quick explanation of “aptitude” for Ubuntu users:

  1. Debian invented apt-get
  2. apt-get didn’t get the lovin’ it deserved
  3. aptitude was invented as an apt-get that doesn’t suck and Debian decided to include it in the base install.
  4. The more popular aspects of aptitude eventually got added to apt-get
  5. Ubuntu decided that apt-get was enough and you have to sudo apt-get install aptitude if you want it.

Basically, aptitude is apt-get on steroids.

What does aptitude search ~o do? It lists all installed packages which aren’t backed by a repository. (Packages you installed using dpkg -i or gdebi, packages which are still installed after you removed the PPA, etc.)

I used it to identify which Humble Bundle games had been installed as .debs so I could replace them with tarballs and move them to my new games drive.


4. lgogd_uri

A simple GTK+ GUI for lgogdownloader that I wrote so Linux users could take advantage of GOG’s gogdownloader:// URIs.

5. makeself_safeextract

A tool for unpacking GOG’s new MojoSetup-based Linux installers without running any 3rd-party code.

(eg. Good for scripting the extraction of DOSBox- or ScummVM-based games for use with your non-x86 DOSBox or ScummVM install.)

Includes a proof-of-concept script demonstrating how to automatically download a game’s Linux build and then convert the installer to a cleaned-up Zip file. (Since MojoSetup already uses a Zip file internally to store the game so the file permissions are already constrained to what Zip’s support for POSIX permissions can represent.)


6. How to use overlayfs to force games to separate out stuff you want to back up

While many native Linux games are well-behaved and cleanly separate out their save files, some games (some native, some DOSBox-wrapped DOS games, and some in Wine prefixes rendered self-contained to keep games from scribbling all over $HOME) aren’t so helpful.

Rather than fighting with symlinks and permissions, the simple, easy, and proper solution is to use an overlay filesystem so that any changes made after installation will be redirected into a folder covered by your backup system and the game won’t even realize anything’s changed.

Historically, Linux has had various overlay filesystems, like unionfs and aufs, but they haven’t made it into the mainline kernel. UnionFS (in Ubuntu patchsets since 11.10 and mainlined in kernel 3.18) changes that.

Just cook up an OverlayFS mount command like this and you’re ready to go.

sudo mount -t overlayfs -o lowerdir=/mnt/buffalo_ext/games_inst,upperdir=~/.local/share/games/ overlayfs /mnt/buffalo_ext/games_play

(lowerdir is where you install your games to. upperdir is where files created/modified after install will end up. That last path on the line is where you run your games from.)

Posted in Geek Stuff | Leave a comment

Working around a MonoGame joystick crash

TL;DR: Try adding unset SDL_JOYSTICK_DEVICE to your game’s launcher script.

Have you ever encountered an error that starts out like this when trying to start a Linux port based on MonoGame?

Number of joysticks: 5
Number of buttons for joystick: 0 - 11
Number of axes for joystick: 0 - 8
Number of PovHats for joystick: 0 - 0
Number of buttons for joystick: 1 - 11
Number of axes for joystick: 1 - 8
Number of PovHats for joystick: 1 - 0
Number of buttons for joystick: 2 - 11
Number of axes for joystick: 2 - 8
Number of PovHats for joystick: 2 - 0
Number of buttons for joystick: 3 - 11
Number of axes for joystick: 3 - 8
Number of PovHats for joystick: 3 - 0

Unhandled Exception:
System.IndexOutOfRangeException: Array index is out of range.
  at Microsoft.Xna.Framework.Input.GamePad.AutoConfig () [0x00000] in :0
  at Microsoft.Xna.Framework.Input.GamePad.PrepSettings () [0x00000] in :0
  at Microsoft.Xna.Framework.Input.GamePad.GetState (PlayerIndex playerIndex, GamePadDeadZone deadZoneMode) [0x00000] in :0
  at Microsoft.Xna.Framework.Input.GamePad.GetState (PlayerIndex playerIndex) [0x00000] in :0

It’s basically saying that it found five joysticks, then got their characteristics wrong, then died when trying to access item 5 out of 4 in the listing.

In my case, the problem was as follows:

  1. On Linux, there are two APIs for accessing joysticks. An old, deprecated, joystick-specific one, and the generic “any input device” API which has no concept of calibration.
  2. My first joystick device is always my 3DConnexion SpaceNavigator, which isn’t a joystick but has stupid USB HID tables which cause the Linux kernel to expose a useless joystick device as the first joystick on the system.
  3. Some games only listen to the first joystick.

My solution for a lot of games was to set this environment variable in my .xsessionrc to force SDL to see my XBox 360 gamepad as the first joystick and to access it via the calibration-aware /dev/input/jsX API rather than the “expect hardware to not need calibration” /dev/input/eventX API.

# Make sure SDL uses a joystick device which can be calibrated
export SDL_JOYSTICK_DEVICE=/dev/input/js3

Adding unset SDL_JOYSTICK_DEVICE to the launcher script for the game fixed it… at the risk of requiring joystick support to be had via antimicro (a joy2key-like utility).

UPDATE: For the really stubborn bugs, you may also need a switched USB hub off eBay for a few bucks.

Posted in Geek Stuff | Leave a comment

FocusWriter… whether or not you need “distraction-free”

Those who know me well know that I’m a bit of a paradox when it comes to writing. On the one hand, I do have issues with distraction and I do approve of the concept of separating composition and typesetting, but, on the other, I can’t use an ordinary distraction-free writing tool for two reasons:

  1. The sheer novelty of it sends me spiralling off into the “technology geek” side of my mind, which is generally quite disjoint from the creative side.
  2. They’re simply too spartan

There’s not much I can do about the former, but I may not need to. When I’m writing something like a blog post or an essay, it’s the “sitting down to write” part that’s the biggest problem. (My troubles with writing fiction seem more oriented around mentally modelling the characters and world… and I’m in the process of  using a mockup to refine a potential solution for that.)

However, the latter point definitely is something worth mentioning in more detail. Distraction-free or not, word processors optimized for composition rather than typesetting are a greatly under-explored market. LyX works beautifully for academic papers, textbooks, and the like, but tools intended for fiction seem to either be too minimal (notepad.exe with chrome trim) or too heavy (MS Word with more task-specific features).

Perhaps the most visually-distinctive examples of the former category are “distraction-free” word processors, like WriteRoom, which take over your whole monitor and present no GUI elements.

Originally, I just assumed that I disliked them because thought these simply weren’t for me but, after reading someone else’s blog post, it dawned on me that, no, WriteRoom just sucks and some people are better at living with it than others.

That blog post introduces FocusWriter, a cross-platform, open-source writing tool that’s primarily intended to be distraction-free, but can still be run in a window if you want (Alt+Enter to switch) and has so many author-oriented features that, even if you don’t need distraction-free, you’ll probably still want it. (Payment is optional. Just choose “$0.00” from the Tip drop-down.)

Like most distraction-free writing tools, FocusWriter is highly themable [1] [2] [3] [4] and can be fine-tuned with “experience features” like playing typewriter sounds when you press keys.

However, where WriteRoom and its clones are the digital version of fancy stationery in a typewriter, FocusWriter asks and answers the question “What does the writer need to be more productive?”

Here’s the mix of answers it came up with:

  1. Minimize distraction without killing discoverability by using auto-hiding menus, toolbars, panels, and a statusbar.
  2. Not everyone needs a fullscreen writing tool. Allow the user to toggle windowed mode via F11 or the menu system.
  3. Keep the author “in the zone” by offering basic standard word processor functionality like rich text support. (Not everyone natively thinks in Markdown-like languages)
  4. Trust skilled authors to know to separate writing and proofreading. Don’t force them to open a whole other program just to run operations like spell check or search/replace.
  5. Provide a tab bar. “Distraction-free” doesn’t necessarily mean “single document”.
  6. Support saving and loading sessions. People who can only spare a few minutes at a time are authors too, as are people who juggle multiple projects.
  7. Acknowledge that some people may need extra help focusing. Offer tricks like greying out all but the line, three lines, or paragraph that the user is actively working on.
  8. Recognize that authors don’t always write in a linear fashion. Provide a jump-to-scene panel on the left and a scrollbar on the right.
  9. Help people meet their goals. Offer to track a daily goal, measured in minutes or words, and support tracking how many consecutive days the user has met their goals.
  10. Make it easy to check your word count, paragraph count, page count, character count, time, and progress toward your goal. Put them in a configurable statistics panel hiding at the bottom of the screen.
  11. Provide a timer system so users can let the outside world fade away without missing that upcoming appointment.
  12. Support auto-saving everything… right down to cursor position.
  13. Allow the user to export in plain text, RTF, DOCX, or ODT formats for easy compatibility with whatever their pre-readers may be using.
  14. Themes are a great way for authors to really get into the right mindset. They should be easy enough to create that anyone can do it. (Ink on Parchment? Typewriter on coarse paper? Phosphor on a VT100? A snowy field or futuristic slum? Google up a background image and a font, open up the “new theme” dialog, and go wild.)

There’s even a Portable Apps release of the Windows version for when you can’t get into the zone on your phone, but didn’t bring along anything else bigger than a thumbdrive.

In summary, don’t prejudge FocusWriter for its status as a “distraction-free word processor”. It’s a very elegantly designed, highly customizable word processor for creative projects that just happens to have distraction-free writing support as its most visible feature.

Posted in Writing | Leave a comment