Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
731 lines (644 loc) · 27.4 KB
/
Copy pathinstall.sh
File metadata and controls
731 lines (644 loc) · 27.4 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#!/usr/bin/env bash
set -euo pipefail
bdir="${XDG_CONFIG_HOME:-$HOME/.config}/hyprkey"
vdir="$bdir/venv"
bindir="${XDG_BIN_HOME:-$HOME/.local/bin}"
appdir="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
pybin="$vdir/bin/python3"
pipbin="$vdir/bin/pip"
c0="\033[0m"
cb="\033[1m"
cd="\033[2m"
cw="\033[38;2;226;226;229m"
msg() {
printf " ${cd}::${c0} %b%s${c0}\n" "$cw" "$1"
}
err() {
printf " ${cd}!!${c0} \033[31m%s${c0}\n" "$1" >&2
exit 1
}
command -v python3 >/dev/null 2>&1 || err "python3 is not installed"
mkdir -p "$bdir" "$bindir" "$appdir"
msg "configuring udev rules for input devices"
if [ ! -f /etc/udev/rules.d/99-hyprkey.rules ]; then
if command -v sudo >/dev/null 2>&1; then
echo 'KERNEL=="event*", SUBSYSTEM=="input", MODE="0666"' | sudo tee /etc/udev/rules.d/99-hyprkey.rules >/dev/null
sudo udevadm control --reload-rules && sudo udevadm trigger
elif command -v pkexec >/dev/null 2>&1; then
echo 'KERNEL=="event*", SUBSYSTEM=="input", MODE="0666"' | pkexec tee /etc/udev/rules.d/99-hyprkey.rules >/dev/null
pkexec udevadm control --reload-rules && pkexec udevadm trigger
fi
fi
chmod 666 /dev/input/event* 2>/dev/null || true
msg "initializing python environment"
if [ ! -f "$pybin" ]; then
python3 -m venv "$vdir" || err "failed to create venv (install python3-venv)"
fi
msg "syncing dependencies"
"$pipbin" install --upgrade pip -q
"$pipbin" install -q PyQt6 numpy pygame evdev
msg "writing configuration files"
if [ ! -f "$bdir/config.json" ]; then
cat << 'EOF' > "$bdir/config.json"
{"muted": false, "preset": 0, "volume": 100, "mouse": true, "headset": false}
EOF
fi
if [ ! -f "$bdir/custom.json" ]; then
cat << 'EOF' > "$bdir/custom.json"
{"freq": 380, "click": 2, "warm": 35, "decay": 75, "drop": 30, "dur": 35, "soft": 20, "body": 30}
EOF
fi
msg "building synthesis daemon"
cat << 'EOF' > "$bdir/daemon.py"
import sys, os, signal, json, time, select
import numpy as np, pygame
from evdev import InputDevice, ecodes, list_devices
cfg_f = os.path.expanduser("~/.config/hyprkey/config.json")
cust_f = os.path.expanduser("~/.config/hyprkey/custom.json")
pid_f = os.path.expanduser("~/.config/hyprkey/daemon.pid")
with open(pid_f, "w") as f:
f.write(str(os.getpid()))
try:
pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=256)
pygame.mixer.set_num_channels(128)
except Exception:
sys.exit(1)
def make_pop(freq=480, dur=0.030, decay=0.0055, p_drop=0.40, click_mix=0.03, warm=0.30, vol=0.75, att=0.0018, body=0.0, hset=False):
if hset:
click_mix *= 0.15
warm *= 1.15
att = max(att, 0.0035)
samples = int(44100 * dur)
t = np.linspace(0, dur, samples, False)
f = freq * (1.0 - p_drop * (1.0 - np.exp(-t / 0.008)))
attack = np.clip(t / att, 0, 1)
env = np.exp(-t / decay) * np.sin(np.pi * 0.5 * attack)
wave = np.sin(2 * np.pi * f * t) + warm * np.sin(4 * np.pi * f * t) * np.exp(-t / (decay * 0.5))
if body > 0:
wave += body * np.sin(np.pi * f * t) * np.exp(-t / (decay * 0.8))
trans = np.random.uniform(-1, 1, samples) * np.exp(-t / (0.0012 if hset else 0.0008))
raw = (wave * (1.0 - click_mix) + trans * click_mix) * env
if hset:
filt = np.array([0.08, 0.25, 0.34, 0.25, 0.08])
raw = np.convolve(raw, filt, mode="same")
raw = np.clip(raw, -1.0, 1.0)
pcm = (raw / (np.max(np.abs(raw)) + 1e-6) * 32767 * vol).astype(np.int16)
stereo = np.column_stack((pcm, pcm))
return pygame.sndarray.make_sound(stereo)
def create_palette(base_f, dur, decay, drop, click, warm, vol, att=0.0018, body=0.0, hset=False):
return {
"normal": [
make_pop(base_f * 0.96, dur, decay, drop, click, warm, vol, att, body, hset),
make_pop(base_f * 0.99, dur, decay, drop, click, warm, vol, att, body, hset),
make_pop(base_f * 1.02, dur, decay, drop, click, warm, vol, att, body, hset),
make_pop(base_f * 1.05, dur, decay, drop, click, warm, vol, att, body, hset)
],
"space": make_pop(base_f * 0.70, dur * 1.35, decay * 1.5, drop * 0.9, click * 0.8, warm * 1.3, vol * 1.1, att * 1.2, body * 1.3, hset),
"back": make_pop(base_f * 0.88, dur * 0.95, decay * 0.9, drop * 0.8, click * 0.8, warm * 0.8, vol * 0.9, att, body * 0.8, hset),
"mouse": make_pop(base_f * 1.30, dur * 0.8, decay * 0.8, drop * 1.2, click * 1.3, warm * 0.5, vol * 0.9, att * 0.9, body * 0.5, hset)
}
specs = [
(480, 0.030, 0.0055, 0.45, 0.020, 0.25, 0.75, 0.0018, 0.15),
(340, 0.038, 0.0080, 0.28, 0.015, 0.45, 0.85, 0.0020, 0.30),
(440, 0.034, 0.0070, 0.52, 0.018, 0.35, 0.80, 0.0018, 0.20),
(380, 0.032, 0.0060, 0.18, 0.025, 0.55, 0.80, 0.0016, 0.35),
(270, 0.045, 0.0110, 0.20, 0.010, 0.25, 0.75, 0.0028, 0.20),
(210, 0.040, 0.0090, 0.10, 0.015, 0.65, 0.95, 0.0018, 0.45),
(550, 0.035, 0.0075, 0.60, 0.010, 0.18, 0.75, 0.0022, 0.10),
(460, 0.030, 0.0050, 0.32, 0.020, 0.32, 0.80, 0.0017, 0.25),
(310, 0.036, 0.0078, 0.24, 0.015, 0.50, 0.82, 0.0020, 0.30),
(280, 0.036, 0.0075, 0.22, 0.025, 0.45, 0.85, 0.0016, 0.35),
(230, 0.038, 0.0085, 0.20, 0.012, 0.55, 0.85, 0.0020, 0.40),
(290, 0.036, 0.0080, 0.18, 0.015, 0.45, 0.85, 0.0020, 0.25),
(220, 0.042, 0.0120, 0.06, 0.010, 0.65, 0.90, 0.0025, 0.35),
(350, 0.033, 0.0065, 0.28, 0.020, 0.42, 0.80, 0.0018, 0.25)
]
profs = []
state = {"muted": False, "preset": 0, "volume": 100, "mouse": True, "headset": False}
last_hset = [None]
def load_config(signum=None, frame=None):
global state, profs
try:
with open(cfg_f) as f:
state = json.load(f)
hset = state.get("headset", False)
if last_hset[0] != hset or not profs:
last_hset[0] = hset
profs = [create_palette(*p, hset=hset) for p in specs]
c = {}
if os.path.exists(cust_f):
with open(cust_f) as f:
c = json.load(f)
profs.append(create_palette(c.get("freq", 380), c.get("dur", 35) / 1000.0, c.get("decay", 75) / 10000.0, c.get("drop", 30) / 100.0, c.get("click", 2) / 100.0, c.get("warm", 35) / 100.0, 0.85, c.get("soft", 20) / 10000.0, c.get("body", 30) / 100.0, hset=hset))
elif state.get("preset", 0) == len(profs) - 1 and os.path.exists(cust_f):
with open(cust_f) as f:
c = json.load(f)
profs[-1] = create_palette(c.get("freq", 380), c.get("dur", 35) / 1000.0, c.get("decay", 75) / 10000.0, c.get("drop", 30) / 100.0, c.get("click", 2) / 100.0, c.get("warm", 35) / 100.0, 0.85, c.get("soft", 20) / 10000.0, c.get("body", 30) / 100.0, hset=hset)
vol = state.get("volume", 100) / 100.0
for pr in profs:
pr["space"].set_volume(vol)
pr["back"].set_volume(vol)
pr["mouse"].set_volume(vol)
for s in pr["normal"]:
s.set_volume(vol)
except Exception:
pass
signal.signal(signal.SIGUSR1, load_config)
load_config()
def get_kbs():
kbs = {}
for p in list_devices():
try:
d = InputDevice(p)
caps = d.capabilities()
if ecodes.EV_KEY in caps:
keys = caps[ecodes.EV_KEY]
if isinstance(keys, list) and (ecodes.KEY_A in keys or 272 in keys):
kbs[d.fd] = d
except Exception:
pass
return kbs
devs = get_kbs()
while True:
try:
if not devs:
time.sleep(1)
devs = get_kbs()
continue
r, _, _ = select.select(devs.keys(), [], [], 2.0)
for fd in r:
for ev in devs[fd].read():
if not state["muted"] and ev.type == ecodes.EV_KEY and ev.value == 1:
k = ev.code
p = profs[state["preset"]]
if k in (ecodes.KEY_SPACE, ecodes.KEY_ENTER):
p["space"].play()
elif k == ecodes.KEY_BACKSPACE:
p["back"].play()
elif k in (272, 273, 274, 275, 276):
if state.get("mouse", True):
p["mouse"].play()
elif k not in (ecodes.KEY_LEFTSHIFT, ecodes.KEY_RIGHTSHIFT, ecodes.KEY_LEFTCTRL, ecodes.KEY_LEFTALT, ecodes.KEY_CAPSLOCK, ecodes.KEY_TAB, ecodes.KEY_ESC):
p["normal"][k % len(p["normal"])].play()
except OSError:
devs = get_kbs()
except Exception:
time.sleep(1)
EOF
msg "building interface"
cat << 'EOF' > "$bdir/gui.py"
import sys, json, os, signal, math
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QComboBox, QSlider, QLabel, QFrame
from PyQt6.QtCore import Qt, QTimer, QVariantAnimation, QEasingCurve, QRectF, QPointF
from PyQt6.QtGui import QCursor, QPainter, QPainterPath, QColor, QFont, QFontMetrics, QPen, QPolygonF
cfg_f = os.path.expanduser("~/.config/hyprkey/config.json")
cust_f = os.path.expanduser("~/.config/hyprkey/custom.json")
pid_f = os.path.expanduser("~/.config/hyprkey/daemon.pid")
PRESETS = ["iPhone Bubble", "Creamy Butter", "Boba Jelly", "Wooden Block", "Marshmallow", "Thocky Mech", "Rain Drop", "Ceramic Pebble", "Matcha Latte", "Holy Panda", "Gateron Black Ink", "Alpaca Linear", "Topre Silent", "Coffee Bean", "Custom (Expert)"]
class JellyBtn(QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
self._s = 1.0
self.on = False
self.anim = QVariantAnimation(self)
self.anim.valueChanged.connect(self._set_s)
def _set_s(self, val):
self._s = val
self.update()
def mousePressEvent(self, e):
self.anim.stop()
self.anim.setDuration(110)
self.anim.setEasingCurve(QEasingCurve.Type.OutQuad)
self.anim.setStartValue(self._s)
self.anim.setEndValue(0.88)
self.anim.start()
super().mousePressEvent(e)
def mouseReleaseEvent(self, e):
self.anim.stop()
self.anim.setDuration(460)
self.anim.setEasingCurve(QEasingCurve.Type.OutBack)
self.anim.setStartValue(self._s)
self.anim.setEndValue(1.0)
self.anim.start()
super().mouseReleaseEvent(e)
class PwrBtn(JellyBtn):
def paintEvent(self, e):
p = QPainter(self)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
cx, cy = self.width() / 2.0, self.height() / 2.0
p.translate(cx, cy)
p.scale(self._s, self._s)
p.translate(-cx, -cy)
bg = QColor("#E2E2E5") if self.on else QColor("#2F3033")
fg = QColor("#1A1C1E") if self.on else QColor("#74777F")
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(bg)
p.drawRoundedRect(self.rect(), 32, 32)
p.setPen(fg)
f = p.font()
f.setBold(True)
f.setPixelSize(28)
p.setFont(f)
p.drawText(self.rect(), Qt.AlignmentFlag.AlignCenter, "⏻")
class MBtn(JellyBtn):
def __init__(self):
super().__init__()
self.on = True
def paintEvent(self, e):
p = QPainter(self)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
cx, cy = self.width() / 2.0, self.height() / 2.0
p.translate(cx, cy)
p.scale(self._s, self._s)
p.translate(-cx, -cy)
bg = QColor("#E2E2E5") if self.on else QColor("#2F3033")
fg = QColor("#1A1C1E") if self.on else QColor("#74777F")
path = QPainterPath()
r = QRectF(self.rect())
path.moveTo(r.left() + 14, r.top())
path.lineTo(r.right() - 32, r.top())
path.arcTo(QRectF(r.right() - 64, r.top(), 64, 64), 90, -180)
path.lineTo(r.left() + 14, r.bottom())
path.quadTo(r.left(), r.bottom(), r.left(), r.bottom() - 14)
path.lineTo(r.left(), r.top() + 14)
path.quadTo(r.left(), r.top(), r.left() + 14, r.top())
path.closeSubpath()
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(bg)
p.drawPath(path)
pen = QPen(fg, 2.2, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin)
p.setPen(pen)
p.setBrush(Qt.BrushStyle.NoBrush)
p.drawRoundedRect(QRectF(cx - 10, cy - 15, 20, 30), 10, 10)
p.drawLine(QPointF(cx, cy - 15), QPointF(cx, cy - 7))
p.setBrush(fg)
p.setPen(Qt.PenStyle.NoPen)
p.drawRoundedRect(QRectF(cx - 1.8, cy - 11, 3.6, 7), 1.8, 1.8)
class HBtn(JellyBtn):
def __init__(self):
super().__init__()
self.on = False
def paintEvent(self, e):
p = QPainter(self)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
cx, cy = self.width() / 2.0, self.height() / 2.0
p.translate(cx, cy)
p.scale(self._s, self._s)
p.translate(-cx, -cy)
bg = QColor("#E2E2E5") if self.on else QColor("#2F3033")
fg = QColor("#1A1C1E") if self.on else QColor("#74777F")
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(bg)
p.drawRoundedRect(self.rect(), 19, 19)
pen = QPen(fg, 2.2, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap)
p.setPen(pen)
p.setBrush(Qt.BrushStyle.NoBrush)
p.drawArc(QRectF(cx - 10, cy - 10, 20, 20), 0, 180 * 16)
p.setBrush(fg)
p.setPen(Qt.PenStyle.NoPen)
p.drawRoundedRect(QRectF(cx - 12, cy - 3, 4.5, 10), 2, 2)
p.drawRoundedRect(QRectF(cx + 7.5, cy - 3, 4.5, 10), 2, 2)
class WaveSlider(QSlider):
def __init__(self, is_accent=False, seed=0):
super().__init__(Qt.Orientation.Horizontal)
self.is_accent = is_accent
self.wlen = 44.0 + (seed * 3.7) % 13.0
self.speed = 0.026 + (seed * 0.0031) % 0.012
self.phase = (seed * 1.57) % (2.0 * math.pi)
self.amp = 2.3 + (seed * 0.14) % 0.5
self.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
self.setFixedHeight(26)
self.timer = QTimer(self)
self.timer.timeout.connect(self.tick)
self.timer.start(20)
def tick(self):
self.phase += self.speed
self.update()
def mousePressEvent(self, ev):
self.seek(ev.position().x())
super().mousePressEvent(ev)
def mouseMoveEvent(self, ev):
if ev.buttons() & Qt.MouseButton.LeftButton:
self.seek(ev.position().x())
super().mouseMoveEvent(ev)
def seek(self, mx):
pad = 9.0
uw = self.width() - 2 * pad
if uw <= 0: return
ratio = max(0.0, min(1.0, (mx - pad) / uw))
val = int(self.minimum() + ratio * (self.maximum() - self.minimum()))
self.setValue(val)
def paintEvent(self, e):
p = QPainter(self)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
w, h = float(self.width()), float(self.height())
cy = h / 2.0
pad = 9.0
uw = w - 2 * pad
val_rng = max(1, self.maximum() - self.minimum())
ratio = (self.value() - self.minimum()) / float(val_rng)
fx = pad + ratio * uw
p.setPen(QPen(QColor("#2F3033"), 5, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap))
if fx < w - pad:
p.drawLine(QPointF(fx, cy), QPointF(w - pad, cy))
c = QColor("#E2E2E5") if self.is_accent else QColor("#A0A0A5")
p.setPen(QPen(c, 4.5, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin))
if fx > pad:
path = QPainterPath()
path.moveTo(pad, cy)
x_start = int(pad)
x_end = int(fx)
for x in range(x_start, x_end + 1):
rel = x - pad
env = min(1.0, (x - pad) / 12.0, (fx - x) / 12.0) if (fx - pad) > 24 else (fx - pad) / 24.0
env = max(0.0, env)
y = cy - env * self.amp * math.sin(2.0 * math.pi * rel / self.wlen - self.phase)
path.lineTo(x, y)
p.drawPath(path)
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(c)
p.drawEllipse(QPointF(fx, cy), 6.5, 6.5)
class VIcon(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(20, 20)
def paintEvent(self, e):
p = QPainter(self)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
c = QColor("#E2E2E5")
p.setBrush(c)
p.setPen(Qt.PenStyle.NoPen)
poly = QPolygonF([QPointF(2, 7), QPointF(6, 7), QPointF(11, 3), QPointF(11, 17), QPointF(6, 13), QPointF(2, 13)])
p.drawPolygon(poly)
p.setBrush(Qt.BrushStyle.NoBrush)
p.setPen(QPen(c, 1.8, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap))
p.drawArc(QRectF(7, 6, 8, 8), -50 * 16, 100 * 16)
p.drawArc(QRectF(5, 3, 14, 14), -50 * 16, 100 * 16)
class JText(QWidget):
def __init__(self):
super().__init__()
self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
self.setFixedSize(210, 44)
self.setStyleSheet("outline: none; border: none;")
self.chars = []
self.c_blink = 0.0
self.t = QTimer()
self.t.timeout.connect(self.anim)
self.t.start(16)
self.ct = QTimer()
self.ct.setSingleShot(True)
self.ct.timeout.connect(self.clear_text)
def clear_text(self):
for c in self.chars:
c["op_t"] = 0.0
c["d"] = True
def keyPressEvent(self, e):
if e.key() == Qt.Key.Key_Backspace:
for c in reversed(self.chars):
if not c.get("d"):
c["op_t"] = 0.0
c["d"] = True
break
elif e.text() and e.text().isprintable():
fm = QFontMetrics(self.font())
cx = 15.0
for c in self.chars:
if not c.get("d"):
cx += fm.horizontalAdvance(c["c"])
start_x = cx
if cx > self.width() - 25:
start_x = self.width() - 25
self.chars.append({"c": e.text(), "x": float(start_x), "y": 15.0, "op": 0.0, "tx": 0.0, "op_t": 1.0, "d": False})
self.update_t()
self.ct.start(5000)
def update_t(self):
fm = QFontMetrics(self.font())
cx = 15.0
for c in self.chars:
if not c.get("d"):
c["tx"] = cx
cx += fm.horizontalAdvance(c["c"])
if cx > self.width() - 25:
shift = cx - (self.width() - 25)
for c in self.chars:
c["tx"] -= shift
def anim(self):
self.c_blink += 0.15
for c in self.chars[:]:
if abs(c["x"] - c["tx"]) > 0.1:
c["x"] += (c["tx"] - c["x"]) * 0.3
if c["op_t"] == 0.0:
c["y"] += (15.0 - c["y"]) * 0.2
else:
c["y"] += (0.0 - c["y"]) * 0.25
if abs(c["op"] - c["op_t"]) > 0.01:
c["op"] += (c["op_t"] - c["op"]) * 0.25
if c["op_t"] == 0.0 and c["op"] < 0.05:
self.chars.remove(c)
self.update()
def paintEvent(self, e):
p = QPainter(self)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
p.setClipRect(self.rect())
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor("#2F3033"))
p.drawRoundedRect(self.rect(), 22, 22)
fm = QFontMetrics(self.font())
cx = 15.0
for c in self.chars:
p.setOpacity(min(1.0, max(0.0, c["op"])))
p.setPen(QColor("#E2E2E5"))
p.drawText(int(c["x"]), int(self.height()/2 + fm.descent() + 3 + c["y"]), c["c"])
if not c.get("d"):
cx = max(cx, c["x"] + fm.horizontalAdvance(c["c"]))
if self.hasFocus():
p.setOpacity((math.sin(self.c_blink) + 1.0) / 2.0)
p.drawRect(int(cx) + 2, int(self.height()/2 - fm.ascent() + 3), 2, fm.height())
class App(QWidget):
def __init__(self):
super().__init__()
try:
with open(cfg_f) as f:
self.state = json.load(f)
except Exception:
self.state = {"muted": False, "preset": 0, "volume": 100, "mouse": True, "headset": False}
try:
with open(cust_f) as f:
self.cust = json.load(f)
except Exception:
self.cust = {}
self.init_ui()
def init_ui(self):
self.setWindowTitle("Hyprkey")
self.setFixedWidth(278)
self.setStyleSheet("background-color: #1A1C1E; font-family: sans-serif;")
layout = QVBoxLayout(self)
layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignHCenter)
layout.setSpacing(16)
layout.setContentsMargins(34, 26, 34, 26)
self.title = QLabel("HYPRKEY")
self.title.setAlignment(Qt.AlignmentFlag.AlignCenter)
tf = QFont("Arial Black")
tf.setFamilies(["Monument Extended", "Syne", "Cabinet Grotesk", "Haettenschweiler", "Impact", "Arial Black", "sans-serif"])
tf.setBold(True)
tf.setWeight(QFont.Weight.Black)
tf.setStretch(125)
tf.setLetterSpacing(QFont.SpacingType.AbsoluteSpacing, 4.0)
tf.setPixelSize(30)
self.title.setFont(tf)
self.title.setStyleSheet("color: #FFFFFF; font-weight: 900; letter-spacing: 4px; padding-bottom: 2px;")
layout.addWidget(self.title)
top_layout = QHBoxLayout()
top_layout.setSpacing(15)
self.pwr_btn = PwrBtn()
self.pwr_btn.setFixedSize(64, 64)
self.pwr_btn.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
self.pwr_btn.clicked.connect(self.toggle_mute)
self.mbtn = MBtn()
self.mbtn.setFixedSize(131, 64)
self.mbtn.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
self.mbtn.clicked.connect(self.toggle_mouse)
top_layout.addWidget(self.pwr_btn)
top_layout.addWidget(self.mbtn)
layout.addLayout(top_layout)
self.hbtn = HBtn()
self.hbtn.setFixedSize(210, 38)
self.hbtn.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
self.hbtn.clicked.connect(self.toggle_headset)
layout.addWidget(self.hbtn, alignment=Qt.AlignmentFlag.AlignCenter)
self.combo = QComboBox()
self.combo.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
self.combo.setFixedSize(210, 44)
self.combo.addItems(PRESETS)
self.combo.setCurrentIndex(self.state.get("preset", 0) if self.state.get("preset", 0) < len(PRESETS) else 0)
self.combo.currentIndexChanged.connect(self.change_preset)
self.combo.setStyleSheet("QComboBox { background-color: #2F3033; color: #E2E2E5; border-radius: 22px; padding-left: 20px; font-size: 13px; font-weight: bold; border: none; } QComboBox::drop-down { border: none; width: 40px; } QComboBox QAbstractItemView { background-color: #2F3033; color: #E2E2E5; selection-background-color: #E2E2E5; selection-color: #1A1C1E; border-radius: 12px; outline: none; padding: 5px; }")
layout.addWidget(self.combo, alignment=Qt.AlignmentFlag.AlignCenter)
sl_box = QHBoxLayout()
sl_box.setSpacing(10)
self.vicon = VIcon()
self.vol_sl = WaveSlider(is_accent=True, seed=0)
self.vol_sl.setRange(0, 100)
self.vol_sl.setValue(int(self.state.get("volume", 100)))
self.vol_sl.valueChanged.connect(self.change_vol)
sl_box.addWidget(self.vicon)
sl_box.addWidget(self.vol_sl)
layout.addLayout(sl_box)
self.test_box = JText()
fnt = self.test_box.font()
fnt.setPointSize(12)
fnt.setBold(True)
self.test_box.setFont(fnt)
layout.addWidget(self.test_box, alignment=Qt.AlignmentFlag.AlignCenter)
self.cpanel = QFrame()
self.cpanel.setStyleSheet("QFrame { border: none; }")
cl = QVBoxLayout(self.cpanel)
cl.setContentsMargins(0, 4, 0, 4)
cl.setSpacing(6)
def make_sl(name, minv, maxv, val, cb, idx=0):
r = QHBoxLayout()
r.setContentsMargins(2, 0, 2, 0)
r.setSpacing(8)
l = QLabel(name)
l.setFixedWidth(36)
l.setStyleSheet("color:#A0A0A5; font-size:11px; font-weight:bold;")
s = WaveSlider(is_accent=False, seed=idx)
s.setRange(minv, maxv)
s.setValue(int(val))
s.valueChanged.connect(cb)
r.addWidget(l)
r.addWidget(s)
cl.addLayout(r)
return s
self.sf = make_sl("Freq", 100, 1000, self.cust.get("freq", 380), self.save_c, 1)
self.sc = make_sl("Click", 0, 50, self.cust.get("click", 2), self.save_c, 2)
self.sw = make_sl("Warm", 0, 100, self.cust.get("warm", 35), self.save_c, 3)
self.sd = make_sl("Decay", 10, 200, self.cust.get("decay", 75), self.save_c, 4)
self.sp = make_sl("Drop", 0, 100, self.cust.get("drop", 30), self.save_c, 5)
self.su = make_sl("Dur", 15, 80, self.cust.get("dur", 35), self.save_c, 6)
self.st = make_sl("Soft", 5, 50, self.cust.get("soft", 20), self.save_c, 7)
self.sb = make_sl("Body", 0, 100, self.cust.get("body", 30), self.save_c, 8)
layout.addWidget(self.cpanel)
self.anim = QVariantAnimation(self)
self.anim.setDuration(350)
self.anim.setEasingCurve(QEasingCurve.Type.OutBack)
self.anim.valueChanged.connect(self.on_anim_step)
self.update_ui()
self.check_cpanel(False)
def on_anim_step(self, val):
self.cpanel.setFixedHeight(val)
self.adjustSize()
def toggle_mute(self):
self.state["muted"] = not self.state["muted"]
self.save_cfg()
self.update_ui()
def toggle_mouse(self):
self.state["mouse"] = not self.state.get("mouse", True)
self.save_cfg()
self.update_ui()
def toggle_headset(self):
self.state["headset"] = not self.state.get("headset", False)
self.save_cfg()
self.update_ui()
def change_preset(self, idx):
self.state["preset"] = idx
self.save_cfg()
self.check_cpanel(True)
def change_vol(self, val):
self.state["volume"] = int(val)
self.save_cfg()
def save_c(self):
self.cust = {"freq": int(self.sf.value()), "click": int(self.sc.value()), "warm": int(self.sw.value()), "decay": int(self.sd.value()), "drop": int(self.sp.value()), "dur": int(self.su.value()), "soft": int(self.st.value()), "body": int(self.sb.value())}
with open(cust_f, "w") as f:
json.dump(self.cust, f)
self.save_cfg()
def save_cfg(self):
with open(cfg_f, "w") as f:
json.dump(self.state, f)
try:
with open(pid_f) as f:
os.kill(int(f.read().strip()), signal.SIGUSR1)
except Exception:
pass
def check_cpanel(self, animate=False):
is_cust = (self.state.get("preset", 0) == len(PRESETS) - 1)
target = 252 if is_cust else 0
if animate:
self.anim.setStartValue(self.cpanel.height())
self.anim.setEndValue(target)
self.anim.start()
else:
self.cpanel.setFixedHeight(target)
self.adjustSize()
def update_ui(self):
self.pwr_btn.on = not self.state["muted"]
self.pwr_btn.update()
self.mbtn.on = self.state.get("mouse", True)
self.mbtn.update()
self.hbtn.on = self.state.get("headset", False)
self.hbtn.update()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = App()
win.show()
sys.exit(app.exec())
EOF
msg "generating executable and desktop entry"
cat << EOF > "$bindir/hyprkey"
#!/usr/bin/env bash
pkill -9 -f "daemon.py" >/dev/null 2>&1 || true
nohup "$pybin" "$bdir/daemon.py" >/dev/null 2>&1 &
"$pybin" "$bdir/gui.py"
EOF
cat << EOF > "$appdir/hyprkey.desktop"
[Desktop Entry]
Name=Hyprkey
Comment=ASMR Keyboard Sound Generator
Exec=$bindir/hyprkey
Terminal=false
Type=Application
Categories=Utility;Audio;
EOF
chmod +x "$bdir/daemon.py" "$bdir/gui.py" "$bindir/hyprkey"
msg "launching"
exec "$bindir/hyprkey"