summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell/UnixIPC.qml
blob: e19ccfb162676618343145c17374c51809481c77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import Quickshell
import Quickshell.Io
import Quickshell.Services.Pipewire
import Quickshell.Services.Mpris
import qs.Services
import Custom as Custom
import QtQml

Scope {
  id: root

  SocketServer {
    active: true
    path: `${Quickshell.env("XDG_RUNTIME_DIR")}/shell.sock`
    handler: Socket {
      parser: SplitParser {
        onRead: line => {
	  const command = (() => {
	    try {
	      return JSON.parse(line);
	    } catch (e) {
	      console.warn("UnixIPC: Failed to parse command:", line, e);
	    }
	  })();
	  if (!command)
	    return;

	  if (command.Volume)
	    root.onCommandVolume(command.Volume);
	  else if (command.Brightness)
	    root.onCommandBrightness(command.Brightness);
	  else if (command.LockSession)
	    Custom.Systemd.lockSession();
	  else if (command.Suspend)
	    Custom.Systemd.suspend();
	  else if (command.Hibernate)
	    Custom.Systemd.hibernate();
	  else if (command.Mpris)
	    root.onCommandMpris(command.Mpris);
	  else if (command.Notifications)
	    root.onCommandNotifications(command.Notifications);
	  else
	    console.warn("UnixIPC: Command not handled:", JSON.stringify(command));
	}
      }

      onError: e => {
        if (e == 1)
	  return;
        console.warn("QLocalSocket::LocalSocketError", e);
      }
    }
  }

  PwObjectTracker {
    objects: [ Pipewire.defaultAudioSink, Pipewire.defaultAudioSource ]
  }
  function onCommandVolume(command) {
    if (command.muted === "toggle")
      Pipewire.defaultAudioSink.audio.muted = !Pipewire.defaultAudioSink.audio.muted;
    if (command.volume === "up")
      Pipewire.defaultAudioSink.audio.volume += 0.02;
    if (command.volume === "down")
      Pipewire.defaultAudioSink.audio.volume -= 0.02;

    if (command["mic-muted"] === "toggle")
      Pipewire.defaultAudioSource.audio.muted = !Pipewire.defaultAudioSource.audio.muted;
  }

  function onCommandBrightness(command) {
    if (command === "up")
      Brightness.currBrightness += 0.02
    if (command === "down")
      Brightness.currBrightness -= 0.02
  }

  function onCommandMpris(command) {
    if (command.PauseAll)
      Array.from(MprisProxy.players).forEach(player => {
	if (player.canPause && player.isPlaying)
	  player.pause();
      });
  }
  Component.onCompleted: { (_ => {})(MprisProxy.players); }

  function onCommandNotifications(command) {
    if (command.DismissGroup && !NotificationManager.displayInhibited) {
      if (NotificationManager.groups.length > 0)
        for (const notif of [...NotificationManager.groups[0]])
          notif.dismiss();
    }
    if (command.DismissAll && !NotificationManager.displayInhibited) {
      for (const notif of [...NotificationManager.trackedNotifications.values])
        notif.dismiss();
    }
  }
}