diff --git a/README.md b/README.md index 0d2f257..ced6f9a 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,5 @@ This comes with absolutely no guarantee of support or correct function, although * some weird thing which lets you use synonyms to get attributes on python objects * something which generates random vaguely human readable names in Elm. Please note that I do NOT endorse the use of Elm and this is provided mostly just to make the languages list at the side weirder. * F# kerbal name generator & very basic stack calculator -* importer part of an unfinished wikipedia database dump viewer \ No newline at end of file +* importer part of an unfinished wikipedia database dump viewer +* `ptt.py` - Python-based systemwide push to talk (mutes and unmutes microphone via PulseAudio) with tray icon \ No newline at end of file diff --git a/green.png b/green.png new file mode 100644 index 0000000..a0cf40e Binary files /dev/null and b/green.png differ diff --git a/ptt.py b/ptt.py new file mode 100755 index 0000000..4db1c4e --- /dev/null +++ b/ptt.py @@ -0,0 +1,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.KeyCode(65312) +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(["pacmd", "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(["pacmd", "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) diff --git a/red.png b/red.png new file mode 100644 index 0000000..d11e583 Binary files /dev/null and b/red.png differ