-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mixer.cc
651 lines (553 loc) · 18.6 KB
/
Mixer.cc
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
//
// Mixer.app
//
// Copyright (c) 2022 Thinksilicon.de
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
#include <X11/Xlib.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <csignal>
#include "Xpm.h"
#include "Mixer.h"
//#include "AMixer/AMixer.h"
#include "PMixer/pulse.h"
#include "pixmaps/main.xpm"
#include "pixmaps/button.xpm"
#include "pixmaps/mutebutton.xpm"
#include "pixmaps/redlight.xpm"
#define ROUND_POS(x) (int ((x) + 0.5) > int (x)) ? (int) ((x) + 1) : (int) (x)
using namespace std;
static const int ButtonX[] = {6, 24, 42};
static const char* MixerSource;
static const char* active_profile;
int wait_cnt = 0;
extern Mixer* app;
void catchBrokenPipe(int sig)
{
app->saveVolumeSettings();
exit(0);
}
int positionToPercent(int position) {
return ROUND_POS(100 - (((position - BUTTON_MAX) * 100.0) / (BUTTON_MIN - BUTTON_MAX)));
}
int percentToPosition(int percent) {
return ROUND_POS(BUTTON_MIN - (percent * (BUTTON_MIN - BUTTON_MAX)) / 100.0);
}
void Mixer::GetProfile() {
ponymix->Populate();
ServerInfo defaults = ponymix->GetDefaults();
Card* card;
auto device = ponymix->GetDevice( defaults.GetDefault( DeviceType::SINK ), DeviceType::SINK );
card = ponymix->GetCard( *device );
active_profile = card->ActiveProfile().name.c_str();
}
Mixer::Mixer(int argc, char** argv)
{
XClassHint classHint;
XSizeHints sizeHints;
XWMHints wmHints;
Atom deleteWindow;
Xpm* image;
char* displayName = NULL;
//const char* card = "default";
mError = 0;
mInstanceName = INSTANCENAME;
mVolumeSource[0] = -1;
mVolumeSource[1] = -1;
mVolumeSource[2] = -1;
mVolumeMute[0] = 0;
mVolumeMute[1] = 0;
mVolumeMute[2] = 0;
mWheelButton = 1;
mLabelText = 0;
mSettingsFile = 0;
mSaveSettings = false;
mLoadSettings = false;
mCommand = NULL;
MixerSource = "Firefox";
// Parse command line
if (argc>1) {
for (int i=1; i<argc; i++) {
// Display
if(!strcmp(argv[i], "-d")) {
checkArgument(argv, argc, i);
displayName = argv[i+1];
i++;
}
// Sound source
else if( !strcmp( argv[i], "-3" ) ) {
checkArgument(argv, argc, i);
MixerSource = argv[ i + 1 ];
i++;
}
// Wheel binding
else if (!strcmp(argv[i], "-w")) {
checkArgument(argv, argc, i);
mWheelButton = atoi(argv[i+1]);
if (mWheelButton < 1 || mWheelButton > 3) {
cerr << APPNAME << ": invalid wheel binding, must be 1, 2 or 3, not " << argv[i+1] << endl;
tryHelp(argv[0]);
exit(0);
}
i++;
}
// Label text
else if (!strcmp(argv[i], "-l")) {
checkArgument(argv, argc, i);
mLabelText = argv[i+1];
i++;
}
// Save settings on exit
else if (!strcmp(argv[i], "-S")) {
mSaveSettings = true;
}
// Load settings on startup
else if (!strcmp(argv[i], "-L")) {
mLoadSettings = true;
}
// Load/Save settings file
else if (!strcmp(argv[i], "-f")) {
checkArgument(argv, argc, i);
mSettingsFile = argv[i+1];
i++;
}
// Execute command on middle click
else if (!strcmp(argv[i], "-e")) {
checkArgument(argv, argc, i);
mCommand = argv[i + 1];
i++;
}
// Instance name
else if (!strcmp(argv[i], "-n")) {
checkArgument(argv, argc, i);
mInstanceName = argv[i+1];
i++;
}
// Version
else if (!strcmp(argv[i], "-v")) {
cerr << APPNAME << " version " << VERSION << endl;
exit(0);
}
// Help
else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
showHelp();
exit(0);
}
// Unknown option
else {
cerr << APPNAME << ": invalid option '" << argv[i] << "'" << endl;
tryHelp(argv[0]);
exit(0);
}
}
}
// default settings file
if (!mSettingsFile) {
char* home = getenv("HOME");
if (home) {
mSettingsFile = new char[strlen(home) + strlen(SETTINGS) + 1];
strcpy(mSettingsFile, home);
strcat(mSettingsFile, SETTINGS);
} else {
cerr << APPNAME << ": $HOME not set, could not find saved settings" << endl;
}
}
// init mixer
ponymix = new PulseClient( "ponymix" );
ponymix->Populate();
// Open display
if ((mDisplay = XOpenDisplay(displayName)) == NULL) {
cerr << APPNAME << ": could not open display " << displayName << endl;
exit(0);
}
// Get root window
mRoot = RootWindow(mDisplay, DefaultScreen(mDisplay));
// Create windows
mAppWin = XCreateSimpleWindow(mDisplay, mRoot, 1, 1, 64, 64, 0, 0, 0);
mIconWin = XCreateSimpleWindow(mDisplay, mAppWin, 0, 0, 64, 64, 0, 0, 0);
// Set classhint
classHint.res_name = mInstanceName;
classHint.res_class = CLASSNAME;
XSetClassHint(mDisplay, mAppWin, &classHint);
// Create delete atom
deleteWindow = XInternAtom(mDisplay, "WM_DELETE_WINDOW", False);
XSetWMProtocols(mDisplay, mAppWin, &deleteWindow, 1);
XSetWMProtocols(mDisplay, mIconWin, &deleteWindow, 1);
// Set windowname
XStoreName(mDisplay, mAppWin, APPNAME);
XSetIconName(mDisplay, mAppWin, APPNAME);
// Set sizehints
sizeHints.flags= USPosition;
sizeHints.x = 0;
sizeHints.y = 0;
XSetWMNormalHints(mDisplay, mAppWin, &sizeHints);
// Set wmhints
wmHints.initial_state = WithdrawnState;
wmHints.icon_window = mIconWin;
wmHints.icon_x = 0;
wmHints.icon_y = 0;
wmHints.window_group = mAppWin;
wmHints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
XSetWMHints(mDisplay, mAppWin, &wmHints);
// Set command
XSetCommand(mDisplay, mAppWin, argv, argc);
// Set background image
image = new Xpm(mDisplay, mRoot, main_xpm);
if (mLabelText) {
image->drawString(LABEL_X, LABEL_Y, mLabelText);
}
image->setWindowPixmapShaped(mIconWin);
delete image;
// Create buttons
mButton[0] = XCreateSimpleWindow(mDisplay, mIconWin, ButtonX[0], BUTTON_MIN, 5, 5, 0, 0, 0);
mButton[1] = XCreateSimpleWindow(mDisplay, mIconWin, ButtonX[1], BUTTON_MIN, 5, 5, 0, 0, 0);
mButton[2] = XCreateSimpleWindow(mDisplay, mIconWin, ButtonX[2], BUTTON_MIN, 5, 5, 0, 0, 0);
image = new Xpm(mDisplay, mRoot, button_xpm);
image->setWindowPixmap(mButton[0]);
image->setWindowPixmap(mButton[1]);
image->setWindowPixmap(mButton[2]);
delete image;
XSelectInput(mDisplay, mButton[0], ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
XSelectInput(mDisplay, mButton[1], ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
XSelectInput(mDisplay, mButton[2], ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
XSelectInput(mDisplay, mIconWin, ButtonPressMask);
XMapWindow(mDisplay, mButton[0]);
XMapWindow(mDisplay, mButton[1]);
XMapWindow(mDisplay, mButton[2]);
XMapWindow(mDisplay, mIconWin);
XMapWindow(mDisplay, mAppWin);
XSync(mDisplay, False);
// Catch broker pipe signal
signal(SIGPIPE, catchBrokenPipe);
// Check if error
if (mError) {
showErrorLed();
} else {
getVolume();
if (mLoadSettings)
loadVolumeSettings();
}
}
void Mixer::tryHelp(char* appname)
{
cerr << "Try `" << appname << " --help' for more information" << endl;
}
void Mixer::showHelp()
{
cerr << APPNAME << "PulseMixer.app by Thinksilicon 2022, huge thanks to the authors of the" << endl
<< "original AlsaMixer.app Copyright (c) 1998-2002 by Per Liden ([email protected]), Petr Hlavka ([email protected])" << endl
<< "and Ponymix by Dave Reisner <[email protected]>, Simon Gomizelj <[email protected]>" << endl
<< "options:" << endl
<< " -3 <app> set sound source for control 3 (default is Firefox)" << endl
<< " -w 1|2|3 bind a control button to the mouse wheel (default is 1)" << endl
<< " -l <text> set label text" << endl
<< " -S save volume settings on exit" << endl
<< " -L load volume settings on start up" << endl
<< " -f <file> use setting <file> instead of ~/GNUstep/Defaults/PulseMixer" << endl
<< " -e <command> execute <command> on middle click" << endl
<< " -n <name> set client instance name" << endl
<< " -d <disp> set display" << endl
<< " -v print version and exit" << endl
<< " -h, --help display this help and exit" << endl << endl;
}
void Mixer::checkArgument(char** argv, int argc, int index)
{
if (argc-1 < index+1) {
cerr << APPNAME << ": option '" << argv[index] << "' requires an argument" << endl;
tryHelp(argv[0]);
exit(0);
}
}
void Mixer::showErrorLed()
{
Window led;
Xpm* image;
led = XCreateSimpleWindow(mDisplay, mIconWin, LED_X, LED_Y, 3, 2, 0, 0, 0);
// Set background image
image = new Xpm(mDisplay, mRoot, redlight_xpm);
image->setWindowPixmap(led);
delete image;
// Show window
XMapWindow(mDisplay, led);
mError = 1;
}
void Mixer::loadVolumeSettings()
{
if (mSettingsFile) {
ifstream file(mSettingsFile);
if (file) {
// This could fail if the user has edited the file by hand and destroyed the structure
char dummy[1024];
file >> dummy; // {
file >> dummy; // Volume1
file >> dummy; // =
file >> mVolume[0];
file >> dummy; // ;
file >> dummy; // Volume2
file >> dummy; // =
file >> mVolume[1];
file >> dummy; // ;
file >> dummy; // Volume3
file >> dummy; // =
file >> mVolume[2];
file.close();
for (int i = 0; i < 3; i++) {
setVolume(i, mVolume[i]);
setButtonPosition(i, percentToPosition(mVolume[i]));
}
}
}
}
int Mixer::GetInputSinkDevice() {
int dev_id = 0;
// Loop through all devices, assume that last added
// sink-input is the active one.
for( const auto& s: ponymix->GetSinkInputs() ) {
if( !strcmp( s.Desc().c_str(), MixerSource ) ) {
dev_id = s.Index();
}
}
return dev_id;
}
void Mixer::saveVolumeSettings()
{
if (mSaveSettings) {
ofstream file(mSettingsFile);
if (file) {
// Files in ~/GNUstep/Defaults/ should follow the property list format
file << "{" << endl
<< " Volume1 = " << mVolumePos[0] << ";" << endl
<< " Volume2 = " << mVolumePos[1] << ";" << endl
<< " Volume3 = " << mVolumePos[2] << ";" << endl
<< "}" << endl;
file.close();
} else {
cerr << APPNAME << ": failed to save volume settings in " << mSettingsFile << endl;
}
}
}
void Mixer::getVolume()
{
static int lastVolume[3] = {-1, -1, -1};
static int lastVolumeMute[3] = {-1, -1, -1};
if (mError) {
return;
}
ServerInfo defaults = ponymix->GetDefaults();
for( int i=0; i<3; i++ ) {
if( i == 0 ) {
//cerr << "GetDevice SINK" << endl;
auto device = ponymix->GetDevice( defaults.GetDefault( DeviceType::SINK ), DeviceType::SINK );
mVolume[i] = device->Volume();
} else if( i == 1 ) {
//cerr << "GetDevice SOURCE" << endl;
auto device = ponymix->GetDevice( defaults.GetDefault( DeviceType::SOURCE ), DeviceType::SOURCE );
mVolume[i] = device->Volume();
} else if( i == 2 ) {
int dev_id = GetInputSinkDevice();
// don't do anything if we didn't find a sink-input.
if( dev_id > 0 ) {
auto device = ponymix->GetDevice( dev_id, DeviceType::SINK_INPUT );
mVolume[i] = device->Volume();
}
}
//cerr << "Read volume" << mVolume[i] << endl;
if (lastVolume[i] != mVolume[i]) {
int y;
// Set button position
if (mError) {
y = BUTTON_MIN;
} else {
y = percentToPosition(mVolume[i]);
}
setButtonPosition(i, y);
lastVolume[i] = mVolume[i];
}
// set buttom type muted/unmuted
if (lastVolumeMute[i] != mVolumeMute[i]) {
setButtonType(i);
lastVolumeMute[i] = mVolumeMute[i];
}
}
if (mError) {
cerr << APPNAME << ": unable to read from " << mMixerDevice << endl;
showErrorLed();
return;
}
}
void Mixer::setVolume(int button, int volume)
{
if (mError) {
return;
}
// Store volume
mVolume[button] = volume;
GetProfile();
//cerr << active_profile << endl;
// Write to device
//aMixer->itemSetVolume(button, mVolume[button]);
ServerInfo defaults = ponymix->GetDefaults();
if( button == 0 ) {
auto device = ponymix->GetDevice( defaults.GetDefault( DeviceType::SINK ), DeviceType::SINK );
ponymix->SetVolume( *device, mVolume[button] );
} else if( button == 1 ) {
auto device = ponymix->GetDevice( defaults.GetDefault( DeviceType::SOURCE ), DeviceType::SOURCE );
ponymix->SetVolume( *device, mVolume[button] );
} else if( button == 2 ) {
int dev_id = GetInputSinkDevice();
// don't do anything if we didn't find a sink-input.
if( dev_id > 0 ) {
auto device = ponymix->GetDevice( dev_id, DeviceType::SINK_INPUT );
ponymix->SetVolume( *device, mVolume[button] );
}
}
}
void Mixer::toggleMute(int button)
{
mVolumeMute[button] = !mVolumeMute[button];
//aMixer->itemToggleMute(button);
ServerInfo defaults = ponymix->GetDefaults();
if( button == 0 ) {
auto device = ponymix->GetDevice( defaults.GetDefault( DeviceType::SINK ), DeviceType::SINK );
bool ismuted = ponymix->IsMuted( *device );
ponymix->SetMute( *device, !ismuted );
} else if( button == 1 ) {
auto device = ponymix->GetDevice( defaults.GetDefault( DeviceType::SOURCE ), DeviceType::SOURCE );
bool ismuted = ponymix->IsMuted( *device );
ponymix->SetMute( *device, !ismuted );
} else {
int dev_id = GetInputSinkDevice();
// don't do anything if we didn't find a sink-input.
if( dev_id > 0 ) {
auto device = ponymix->GetDevice( dev_id, DeviceType::SINK_INPUT );
bool ismuted = ponymix->IsMuted( *device );
ponymix->SetMute( *device, !ismuted );
}
}
setButtonType(button);
}
void Mixer::setButtonType(int button)
{
Xpm* image;
if (mVolumeMute[button] == 1) { // muted
image = new Xpm(mDisplay, mRoot, mutebutton_xpm);
image->setWindowPixmap(mButton[button]);
delete image;
XClearWindow(mDisplay, mButton[button]);
} else {
image = new Xpm(mDisplay, mRoot, button_xpm);
image->setWindowPixmap(mButton[button]);
delete image;
XClearWindow(mDisplay, mButton[button]);
}
}
void Mixer::setButtonPosition(int button, int position) {
if (position > BUTTON_MIN) {
position = BUTTON_MIN;
} else if (position < BUTTON_MAX) {
position = BUTTON_MAX;
}
XMoveWindow(mDisplay, mButton[button], ButtonX[button], position);
mVolumePos[button] = position;
}
void Mixer::setButtonPositionRelative(int button, int relativePosition)
{
int y;
// Calc new button position
y = mVolumePos[button] + relativePosition;
if (y > BUTTON_MIN) {
y = BUTTON_MIN;
} else if (y < BUTTON_MAX) {
y = BUTTON_MAX;
}
// Set button position and volume
XMoveWindow(mDisplay, mButton[button], ButtonX[button], y);
mVolumePos[button] = y;
// set volume
setVolume(button, positionToPercent(y));
}
void Mixer::run()
{
XEvent event;
int buttonDown = 0;
int buttonDownPosition = 0;
// Start handling events
while(1) {
while(XPending(mDisplay) || buttonDown) {
XNextEvent(mDisplay, &event);
switch(event.type) {
case ButtonPress:
if (event.xbutton.button == Button4 || event.xbutton.button == Button5) {
// Wheel scroll
setButtonPositionRelative(mWheelButton - 1, event.xbutton.button == Button5? 3: -3);
} else if (event.xbutton.button == Button1 && event.xbutton.window != mIconWin) {
// Volume change
buttonDown = 1;
buttonDownPosition = event.xbutton.y;
} else if (event.xbutton.button == Button3 && buttonDown == 0 && event.xbutton.window != mIconWin) {
// Mute
for (int i=0; i<3; i++) {
if (mButton[i] == event.xbutton.window) {
toggleMute(i);
break;
}
}
} else if (event.xbutton.button == Button2) {
// Load defaults or execute command
if (mCommand) {
char command[512];
int status;
snprintf(command, 512, "%s &", mCommand);
status = system(command);
if (status != 0 )
Mixer::showErrorLed();
} else
loadVolumeSettings();
}
break;
case ButtonRelease:
if (event.xbutton.button == Button1) {
buttonDown = 0;
}
break;
case MotionNotify:
if (buttonDown) {
// Find button
for (int i=0; i<3; i++) {
if (mButton[i] == event.xmotion.window) {
setButtonPositionRelative(i, event.xmotion.y - buttonDownPosition);
break;
}
}
}
break;
}
}
// Idle for a moment
usleep(100000);
if( wait_cnt++ > 10 ) {
// poll for new profiles and volumes every second
GetProfile();
getVolume();
wait_cnt = 0;
}
XSync(mDisplay, False);
}
}