-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
180 lines (132 loc) · 5.25 KB
/
main.py
File metadata and controls
180 lines (132 loc) · 5.25 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import tkinter as tk
from tkinter import ttk, messagebox
import platform
import subprocess
from plyer import notification
from pynput import keyboard as pynput_keyboard
# windows detection
if platform.system() == "Windows":
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
keybindings = {}
hotkey_listener = None
#mute system
def toggle_mute(application_name):
system = platform.system()
if system == "Windows":
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
if session.Process and session.Process.name().lower() == application_name.lower():
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
current = volume.GetMute()
volume.SetMute(not current, None)
notification.notify(
title="PyMuteR",
message=f"{'Muted' if not current else 'Unmuted'} {application_name}",
timeout=0.2
)
return
elif system == "Linux":
try:
result = subprocess.check_output(["pactl", "list", "sink-inputs"]).decode()
blocks = result.split("Sink Input #")
for block in blocks[1:]:
if application_name.lower() in block.lower():
sink_id = block.split("\n")[0].strip()
mute_line = [l for l in block.split("\n") if "Mute:" in l][0]
is_muted = "yes" in mute_line.lower()
subprocess.call([
"pactl", "set-sink-input-mute",
sink_id,
"0" if is_muted else "1"
])
notification.notify(
title="PyMuteR",
message=f"{'Muted' if not is_muted else 'Unmuted'} {application_name}",
timeout=0.2
)
return
except Exception as e:
print("Error:", e)
#get process
def get_audio_sessions():
system = platform.system()
processes = set()
if system == "Windows":
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
if session.Process:
processes.add(session.Process.name())
elif system == "Linux":
try:
result = subprocess.check_output(["pactl", "list", "sink-inputs"]).decode()
for line in result.split("\n"):
if "application.name" in line:
name = line.split("=")[-1].strip().strip('"')
processes.add(name)
except:
pass
return list(processes)
#hoykei
def start_listener():
global hotkey_listener
if hotkey_listener:
hotkey_listener.stop()
hotkeys = {}
for combo, app in keybindings.items():
# convert "ctrl+k" -> "<ctrl>+k"
formatted = combo.lower().replace("ctrl", "<ctrl>") \
.replace("alt", "<alt>") \
.replace("shift", "<shift>")
hotkeys[formatted] = lambda a=app: toggle_mute(a)
if hotkeys:
hotkey_listener = pynput_keyboard.GlobalHotKeys(hotkeys)
hotkey_listener.start()
#buttonms
def refresh_programs():
programs = get_audio_sessions()
program_dropdown["values"] = programs
if programs:
program_var.set(programs[0])
def add_keybinding():
app_name = program_var.get()
key = keybind_var.get()
if not app_name or not key:
messagebox.showwarning("Error", "Select program and keybind")
return
if key in keybindings:
messagebox.showwarning("Error", "Key already used")
return
keybindings[key] = app_name
keybinding_list.insert(tk.END, f"{key} -> {app_name}")
keybind_var.set("")
start_listener()
def remove_keybinding():
key = keybind_var.get()
if key in keybindings:
del keybindings[key]
keybinding_list.delete(0, tk.END)
for k, app in keybindings.items():
keybinding_list.insert(tk.END, f"{k} -> {app}")
keybind_var.set("")
start_listener()
#rest of ui
root = tk.Tk()
root.title("PyMuteR")
root.geometry("500x400")
frame = ttk.Frame(root, padding="10")
frame.grid(row=0, column=0)
program_var = tk.StringVar()
ttk.Label(frame, text="Select Program:").grid(row=0, column=0)
program_dropdown = ttk.Combobox(frame, textvariable=program_var, state="readonly", width=40)
program_dropdown.grid(row=0, column=1)
ttk.Button(frame, text="Refresh", command=refresh_programs).grid(row=0, column=2)
keybind_var = tk.StringVar()
ttk.Label(frame, text="Keybind (ctrl+k):").grid(row=1, column=0)
ttk.Entry(frame, textvariable=keybind_var).grid(row=1, column=1)
ttk.Button(frame, text="Add", command=add_keybinding).grid(row=1, column=2)
ttk.Button(frame, text="Remove", command=remove_keybinding).grid(row=2, column=2)
ttk.Label(frame, text="Assigned Keybinds:").grid(row=3, column=0)
keybinding_list = tk.Listbox(frame, height=10, width=60)
keybinding_list.grid(row=4, column=0, columnspan=3)
refresh_programs()
root.mainloop()