Based on a patch from: Sam Thursfield <ssssam@gmail.com> https://bugs.freedesktop.org/show_bug.cgi?id=51202