github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/display_control.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package builtin
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  	"path"
    26  	"path/filepath"
    27  
    28  	"github.com/snapcore/snapd/interfaces"
    29  	"github.com/snapcore/snapd/interfaces/apparmor"
    30  )
    31  
    32  const displayControlSummary = `allows configuring display parameters`
    33  
    34  const displayControlBaseDeclarationSlots = `
    35    display-control:
    36      allow-installation:
    37        slot-snap-type:
    38          - core
    39      deny-auto-connection: true
    40  `
    41  
    42  // gnome-settings-daemon also provides an API via setting the Brightness
    43  // property via a Set() method on the org.gnome.SettingsDaemon.Power.Screen
    44  // interface, but we can't mediate member data. This could instead be supported
    45  // via userd...
    46  const displayControlConnectedPlugAppArmor = `
    47  # Description: This interface allows getting information about a connected
    48  # display and setting parameters like backlight brightness.
    49  
    50  # keyboard backlight key
    51  /sys/class/leds/ r,
    52  /sys/devices/**/leds/**kbd_backlight/{,**} r,
    53  /sys/devices/**/leds/**kbd_backlight/brightness w,
    54  
    55  # upower
    56  #include <abstractions/dbus-strict>
    57  dbus (send)
    58      bus=system
    59      path=/org/freedesktop/UPower/KbdBacklight
    60      interface=org.freedesktop.DBus.Introspectable
    61      member=Introspect
    62      peer=(label=unconfined),
    63  dbus (send)
    64      bus=system
    65      path=/org/freedesktop/UPower/KbdBacklight
    66      interface=org.freedesktop.UPower.KbdBacklight
    67      member={GetBrightness,GetMaxBrightness,SetBrightness}
    68      peer=(label=unconfined),
    69  
    70  # gnome-settings-daemon
    71  #include <abstractions/dbus-session-strict>
    72  dbus (send)
    73      bus=session
    74      path=/org/gnome/SettingsDaemon/Power
    75      interface=org.freedesktop.DBus.Introspectable
    76      member=Introspect
    77      peer=(label=unconfined),
    78  dbus (send)
    79      bus=session
    80      path=/org/gnome/SettingsDaemon/Power
    81      interface=org.gnome.SettingsDaemon.Power.Screen
    82      member=Step{Down,Up}
    83      peer=(label=unconfined),
    84  
    85  /sys/class/backlight/ r,
    86  `
    87  
    88  type displayControlInterface struct {
    89  	commonInterface
    90  }
    91  
    92  func (iface *displayControlInterface) dereferencedBacklightPaths() []string {
    93  	var paths []string
    94  	sysClass := "/sys/class/backlight"
    95  	dirs, err := readDir(sysClass)
    96  	if err != nil {
    97  		return paths
    98  	}
    99  
   100  	for _, s := range dirs {
   101  		p, err := evalSymlinks(filepath.Join(sysClass, s.Name()))
   102  		if err != nil {
   103  			continue
   104  		}
   105  		paths = append(paths, filepath.Clean(p))
   106  	}
   107  	return paths
   108  }
   109  
   110  func (iface *displayControlInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
   111  	// add the static rules
   112  	spec.AddSnippet(displayControlConnectedPlugAppArmor)
   113  
   114  	// add the detected rules
   115  	for _, p := range iface.dereferencedBacklightPaths() {
   116  		var buf bytes.Buffer
   117  		fmt.Fprintf(&buf, "# autodetected backlight: %s\n", path.Base(p))
   118  		fmt.Fprintf(&buf, "%s/{,**} r,\n", p)
   119  		fmt.Fprintf(&buf, "%s/bl_power w,\n", p)
   120  		fmt.Fprintf(&buf, "%s/brightness w,\n", p)
   121  		spec.AddSnippet(buf.String())
   122  	}
   123  
   124  	return nil
   125  }
   126  
   127  func init() {
   128  	registerIface(&displayControlInterface{commonInterface{
   129  		name:                  "display-control",
   130  		summary:               displayControlSummary,
   131  		implicitOnCore:        true,
   132  		implicitOnClassic:     true,
   133  		baseDeclarationSlots:  displayControlBaseDeclarationSlots,
   134  		connectedPlugAppArmor: displayControlConnectedPlugAppArmor,
   135  	}})
   136  }