Skip to content

Commit

Permalink
Fetch file descriptors for the SVGs, not paths
Browse files Browse the repository at this point in the history
This allows us to read SVGs when we're sandboxed and don't have direct access
to the file system.

This requires an up-to-date ratbagd to get access to GetSvgFd()
See libratbag/libratbag#523

Fixes #245
  • Loading branch information
whot authored and Hjdskes committed Sep 4, 2018
1 parent d7f1a14 commit 09439cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
14 changes: 8 additions & 6 deletions piper/devicerow.py
Expand Up @@ -14,14 +14,13 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import os
import sys

from .gi_composites import GtkTemplate

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GdkPixbuf, GObject, Gtk, Rsvg
from gi.repository import GdkPixbuf, GObject, Gtk, Rsvg, Gio


@GtkTemplate(ui="/org/freedesktop/Piper/ui/DeviceRow.ui")
Expand All @@ -40,9 +39,12 @@ def __init__(self, device, *args, **kwargs):
self._device = device
self.title.set_text(device.name)

svg_path = device.get_svg("gnome")
if os.path.isfile(svg_path):
handle = Rsvg.Handle.new_from_file(svg_path)
try:
fd = device.get_svg_fd("gnome")
uis = Gio.UnixInputStream.new(fd.fileno(), False)
handle = Rsvg.Handle.new_from_stream_sync(uis, None,
Rsvg.HandleFlags.FLAGS_NONE,
None)
svg = handle.get_pixbuf_sub("#Device")
handle.close()
if svg is None:
Expand All @@ -53,7 +55,7 @@ def __init__(self, device, *args, **kwargs):
print("Cannot resize device SVG", file=sys.stderr)
else:
self.image.set_from_pixbuf(svg)
else:
except FileNotFoundError:
print("Device {} has no image or its path is invalid".format(device.name), file=sys.stderr)

self.show_all()
Expand Down
18 changes: 11 additions & 7 deletions piper/mousemap.py
Expand Up @@ -16,14 +16,13 @@

import cairo
import gi
import os
import sys
from lxml import etree

gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
gi.require_version("Rsvg", "2.0")
from gi.repository import Gdk, GLib, Gtk, GObject, Rsvg
from gi.repository import Gdk, GLib, Gtk, GObject, Rsvg, Gio

"""This module contains the MouseMap widget (and its helper class
_MouseMapChild), which is central to the button and LED configuration stack
Expand Down Expand Up @@ -106,8 +105,16 @@ def __init__(self, layer, ratbagd_device, spacing=10, *args, **kwargs):
raise ValueError("Layer cannot be None")
if ratbagd_device is None:
raise ValueError("Device cannot be None")
svg_path = ratbagd_device.get_svg("gnome")
if not os.path.isfile(svg_path):
try:
fd = ratbagd_device.get_svg_fd("gnome")
uis = Gio.UnixInputStream.new(fd.fileno(), False)
self._handle = Rsvg.Handle.new_from_stream_sync(uis, None,
Rsvg.HandleFlags.FLAGS_NONE,
None)
# separate fd for the XML parsing to avoid file offset issues
fd = ratbagd_device.get_svg_fd("gnome")
self._svg_data = etree.parse(fd)
except FileNotFoundError:
raise ValueError("Device has no image or its path is invalid")

Gtk.Container.__init__(self, *args, **kwargs)
Expand All @@ -119,9 +126,6 @@ def __init__(self, layer, ratbagd_device, spacing=10, *args, **kwargs):
self._children = []
self._highlight_element = None

self._handle = Rsvg.Handle.new_from_file(svg_path)
self._svg_data = etree.parse(svg_path)

# TODO: remove this when we're out of the transition to toned down SVGs
device = self._handle.has_sub("#Device")
buttons = self._handle.has_sub("#Buttons")
Expand Down

0 comments on commit 09439cb

Please sign in to comment.