Skip to content

Instantly share code, notes, and snippets.

@thenonameguy
Forked from Monsterovich/headphone.py
Last active July 6, 2021 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thenonameguy/94151cc33867d2d1eabaea6151a6a6d1 to your computer and use it in GitHub Desktop.
Save thenonameguy/94151cc33867d2d1eabaea6151a6a6d1 to your computer and use it in GitHub Desktop.
NixOS Pipewire headphone jack fix
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE, STDOUT
import time
HEADPHONE_EVENT = "jack/headphone"
p = Popen(["/run/current-system/sw/sbin/acpi_listen"],
stdout=PIPE, stderr=STDOUT, bufsize=1)
with p.stdout:
for line in iter(p.stdout.readline, b''):
event = line.decode("utf-8").split(' ')
if event[0] == HEADPHONE_EVENT:
action = event[2].strip()
time.sleep(0.5)
if (action == "plug"):
# must have `pulseaudio` pkg installed
os.system("pactl set-sink-port 0 \"analog-output-headphones\"")
sys.stdout.write("Headphones plugged in\n")
elif (action == "unplug"):
os.system("pactl set-sink-port 0 \"analog-output-speaker\"")
sys.stdout.write("Headphones plugged out\n")
sys.stdout.flush()
p.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment