golang: Get last character of a string

A very quick one here. I needed to get the last character of a string in golang, but thinking like a PHP developer I was looking for an equivalent to substr($var, -1);.

As it turns out, you actually do it using slices

newVal := val[len(val)-1:]

This says “start at string length, minus 1 and give me everything to the end”

Dell XPS13/XPS15 white noise via audio

Just a quick post to make a copy of a script that I’d rather not lose. By default, the XPS15 has a fairly quiet white noise hiss when using headphones without any sound playing. Someone linked me to this script and I wanted to reproduce it here just in case I need it in future.

Save it as white-noise-fix.py and run with sudo python white-noise-fix.py.

#!/usr/bin/env python

import os
import struct
from fcntl import ioctl

def __ioctl_val(val):
  # workaround for OverFlow bug in python 2.4
  if val & 0x80000000:
    return -((val^0xffffffff)+1)
  return val

IOCTL_INFO = __ioctl_val(0x80dc4801)
IOCTL_PVERSION = __ioctl_val(0x80044810)
IOCTL_VERB_WRITE = __ioctl_val(0xc0084811)

def set(nid, verb, param):
  verb = (nid << 24) | (verb << 8) | param
  res = ioctl(FD, IOCTL_VERB_WRITE, struct.pack('II', verb, 0))  

FD = os.open("/dev/snd/hwC1D0", os.O_RDONLY)
info = struct.pack('Ii64s80si64s', 0, 0, '', '', 0, '')
res = ioctl(FD, IOCTL_INFO, info)
name = struct.unpack('Ii64s80si64s', res)[3]
if not name.startswith('HDA Codec'):
  raise IOError, "unknown HDA hwdep interface"
res = ioctl(FD, IOCTL_PVERSION, struct.pack('I', 0))
version = struct.unpack('I', res)
if version < 0x00010000:    # 1.0.0
  raise IOError, "unknown HDA hwdep version"

# initialization sequence starts here...

set(0x0c, 0x300, 0x5180) # 0x0c035180 (SET_AMP_GAIN_MUTE)
set(0x0c, 0x300, 0x6180) # 0x0c036180 (SET_AMP_GAIN_MUTE)


xmonad: Bind XF86Audio keys with EZConfig

I used to just run gnome-settings-daemon to handle my media keys, but as of Ubuntu 14.10 it’s stopped working. I figured that it was about time that I managed them properly by binding keys in my Xmonad config.

myBindings =
    [ ("<XF86AudioMute>"  , spawn "amixer -q -D pulse sset Master toggle")
    , ("<XF86AudioRaiseVolume>"  , spawn "amixer -q -D pulse sset Master 2000+ unmute")
    , ("<XF86AudioLowerVolume>"  , spawn "amixer -q -D pulse sset Master 2000- unmute")
    , ("<XF86AudioPlay>"  , spawn "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause")

There are other bindings in there too, but these are the ones we’re interested in. We use amixer to set the volume on our audio device. It’s all fairly straightforward, but you might have to work out what your output device is called if you have multiple. For me, it was called Master (which is the default).

As a little bonus, I even managed to get the play/pause button to control Spotify via dbus. To achive this, we use dbus-send and send the PlayPause command

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause