-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathptt.py
More file actions
executable file
·86 lines (71 loc) · 2.03 KB
/
ptt.py
File metadata and controls
executable file
·86 lines (71 loc) · 2.03 KB
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
#!/usr/bin/env python3
from pynput import keyboard
import subprocess
import wx
import wx.adv
import wx.lib.newevent
import sys
import os.path
import threading
scriptdir = os.path.dirname(os.path.abspath(sys.argv[0]))
red, green = os.path.join(scriptdir, "red.png"), os.path.join(scriptdir, "green.png")
l_key = keyboard.Key.f8
source = "alsa_input.usb-0c76_USB_PnP_Audio_Device-00.mono-fallback"
MuteSetEvent, EVT_MUTE_SET_EVENT = wx.lib.newevent.NewEvent()
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.red_icon = wx.Icon(red)
self.green_icon = wx.Icon(green)
self.frame = frame
super(TaskBarIcon, self).__init__()
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
self.Bind(EVT_MUTE_SET_EVENT, self.on_mute_set)
def on_mute_set(self, event):
self.set_icon(self.red_icon if event.muted else self.green_icon)
def on_left_down(self, event):
toggle()
def set_icon(self, icon):
self.SetIcon(icon, "PTT")
class App(wx.App):
def OnInit(self):
frame=wx.Frame(None)
self.SetTopWindow(frame)
self.taskbar_icon = TaskBarIcon(frame)
return True
app = App(False)
muted = False
def mute():
global muted
if not muted:
muted = True
print("muted")
subprocess.run(["pactl", "set-source-mute", source, "1"])
# this could call set_icon on the taskbar icon object directly, but that seems to inconsistently cause mysterious crashes in cairo
wx.PostEvent(app.taskbar_icon, MuteSetEvent(muted=muted))
def unmute():
global muted
if muted:
muted = False
print("unmuted")
subprocess.run(["pactl", "set-source-mute", source, "0"])
wx.PostEvent(app.taskbar_icon, MuteSetEvent(muted=muted))
def toggle():
if muted: unmute()
else: mute()
def on_press(key):
if key == l_key: unmute()
def on_release(key):
if key == l_key: mute()
mute()
threading.Thread(target=app.MainLoop).start()
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
try:
listener.join()
except KeyboardInterrupt:
listener.stop()
print("shutting down")
unmute()
app.Destroy()
sys.exit(0)