summaryrefslogtreecommitdiffstats
path: root/kmix/kmix.cpp
blob: 20e9c58fa6bfa32ceaf3013bbbe7f3c219c2cab5 (plain)
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
/*
 * KMix -- KDE's full featured mini mixer
 *
 * Copyright (C) 2000 Stefan Schimanski <schimmi@kde.org>
 * Copyright (C) 2001 Preston Brown <pbrown@kde.org>
 * Copyright (C) 2003 Sven Leiber <s.leiber@web.de>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// include files for QT
#include <tqmap.h>
#include <tqhbox.h>
#include <tqcheckbox.h>
#include <tqradiobutton.h>
#include <tqpushbutton.h>
#include <tqwidgetstack.h>
#include <tqlayout.h>
#include <tqtooltip.h>

// include files for KDE
#include <kcombobox.h>
#include <kiconloader.h>
#include <tdemessagebox.h>
#include <tdemenubar.h>
#include <klineeditdlg.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <tdeaction.h>
#include <tdeapplication.h>
#include <kstdaction.h>
#include <kpanelapplet.h>
#include <tdepopupmenu.h>
#include <khelpmenu.h>
#include <kdebug.h>
#include <tdeaccel.h>
#include <kglobalaccel.h>
#include <kkeydialog.h>
#include <tdeconfigdialog.h>

// application specific includes
#include "mixertoolbox.h"
#include "kmix.h"
#include "kmixerwidget.h"
#include "kmixdockwidget.h"
#include "kmixtoolbox.h"
#include "kmixsettings.h"
#include "behaviorconfig.h"
#include "appearanceconfig.h"

#ifdef WITH_KMIX_EXPERIMENTAL
#include "experimental.h"
#endif


/**
 * Constructs a mixer window (KMix main window)
 */
KMixWindow::KMixWindow()
	: DCOPObject("kmix"), TDEMainWindow(0, 0, 0, 0), m_dockWidget( 0L )
{
	m_visibilityUpdateAllowed	= true;
	m_mixerWidgets.setAutoDelete(true);

#ifdef WITH_KMIX_EXPERIMENTAL
    MixerToolBox::initMixer(Mixer::mixers(), KMixSettings::multiDriver(), m_hwInfoString);
#else
    MixerToolBox::initMixer(Mixer::mixers(), false, m_hwInfoString);
#endif

    loadConfig();
	initActions();
	initWidgets();
	initMixerWidgets();
	updateDocking();

	if ( KMixSettings::visible() )
	{
		show();
	}
	else
	{
        hide();
	}
	connect( kapp, TQT_SIGNAL( aboutToQuit()), TQT_SLOT( saveSettings()) );
}


KMixWindow::~KMixWindow()
{
   MixerToolBox::deinitMixer();
}


void
KMixWindow::initActions()
{
	// file menu
	KStdAction::quit( TQT_TQOBJECT(this), TQT_SLOT(quit()), actionCollection());

	// settings menu
	KStdAction::showMenubar( TQT_TQOBJECT(this), TQT_SLOT(toggleMenuBar()), actionCollection());
        KStdAction::preferences( TQT_TQOBJECT(this), TQT_SLOT(showSettings()), actionCollection());
	new TDEAction( i18n( "Configure &Global Shortcuts..." ), "configure_shortcuts", 0, TQT_TQOBJECT(this),
		TQT_SLOT( configureGlobalShortcuts() ), actionCollection(), "settings_global" );
	KStdAction::keyBindings( guiFactory(), TQT_SLOT(configureShortcuts()), actionCollection());

	(void) new TDEAction( i18n( "Hardware &Information" ), 0, TQT_TQOBJECT(this), TQT_SLOT( slotHWInfo() ), actionCollection(), "hwinfo" );
	(void) new TDEAction( i18n( "Hide Mixer Window" ), Key_Escape, TQT_TQOBJECT(this), TQT_SLOT(hide()), actionCollection(), "hide_kmixwindow" );

	m_globalAccel = new TDEGlobalAccel(this, "KMix");
	m_globalAccel->insert( "Increase volume", i18n( "Increase Volume of Master Channel"), TQString(),
			TDEShortcut(), TDEShortcut(), TQT_TQOBJECT(this), TQT_SLOT( slotIncreaseVolume() ) );
	m_globalAccel->insert( "Decrease volume", i18n( "Decrease Volume of Master Channel"), TQString(),
			TDEShortcut(), TDEShortcut(), TQT_TQOBJECT(this), TQT_SLOT( slotDecreaseVolume() ) );
	m_globalAccel->insert( "Toggle mute", i18n( "Toggle Mute of Master Channel"), TQString(),
			TDEShortcut(), TDEShortcut(), TQT_TQOBJECT(this), TQT_SLOT( slotToggleMuted() ) );
	m_globalAccel->readSettings();
	m_globalAccel->updateConnections();

	createGUI( "kmixui.rc" );
}

void
KMixWindow::initWidgets()
{
	// Main widget and layout
	setCentralWidget( new TQWidget(  this, "qt_central_widget" ) );

	// Widgets layout
	widgetsLayout = new TQVBoxLayout(   centralWidget(), 0, 0, "widgetsLayout" );
	widgetsLayout->setResizeMode(TQLayout::Minimum); // works fine


	// Mixer widget line
	mixerNameLayout = new TQHBox( centralWidget(), "mixerNameLayout" );
	widgetsLayout->setStretchFactor( mixerNameLayout, 0 );
	TQSizePolicy qsp( TQSizePolicy::Ignored, TQSizePolicy::Maximum);
	mixerNameLayout->setSizePolicy(qsp);
	mixerNameLayout->setSpacing(KDialog::spacingHint());
	TQLabel *qlbl = new TQLabel( i18n("Current mixer:"), mixerNameLayout );
	qlbl->setFixedHeight(qlbl->sizeHint().height());
	m_cMixer = new KComboBox( FALSE, mixerNameLayout, "mixerCombo" );
	m_cMixer->setFixedHeight(m_cMixer->sizeHint().height());
	connect( m_cMixer, TQT_SIGNAL( activated( int ) ), TQT_TQOBJECT(this), TQT_SLOT( showSelectedMixer( int ) ) );
	TQToolTip::add( m_cMixer, i18n("Current mixer" ) );

	// Add first layout to widgets
	widgetsLayout->addWidget( mixerNameLayout );

	m_wsMixers = new TQWidgetStack( centralWidget(), "MixerWidgetStack" );
	widgetsLayout->setStretchFactor( m_wsMixers, 10 );
	widgetsLayout->addWidget( m_wsMixers );

	if ( KMixSettings::menubar() )
		menuBar()->show();
	else
		menuBar()->hide();

	widgetsLayout->activate();
}


void
KMixWindow::updateDocking()
{
	// delete old dock widget
	if (m_dockWidget)
	{
		delete m_dockWidget;
		m_dockWidget = 0L;
	}

	if ( KMixSettings::allowDocking() )
	{
		// create dock widget
		m_dockWidget = new KMixDockWidget( Mixer::mixers().first(), this, "mainDockWidget", KMixSettings::trayVolumeControl(), KMixSettings::dockIconMuting() );
		m_dockWidget->show();
	}
}

void
KMixWindow::saveSettings()
{
    saveConfig();
    saveVolumes();
}

void
KMixWindow::saveConfig()
{
   // make sure we don't start without any UI (in case the tray icon is disabled)
   bool startVisible = KMixSettings::visible();
   if ( !KMixSettings::allowDocking() )
   {
       startVisible = true;
   }

   KMixSettings::setSize(size());
   KMixSettings::setPosition(pos());
   KMixSettings::setVisible(startVisible);

   Mixer* mixerMasterCard = Mixer::masterCard();
   if ( mixerMasterCard != 0 )
   {
      KMixSettings::setMasterMixer(mixerMasterCard->id());
   }
   MixDevice* mdMaster = Mixer::masterCardDevice();
   if ( mdMaster != 0 )
   {
       KMixSettings::setMasterMixerDevice(mdMaster->getPK());
   }

   // save mixer widgets
   for ( KMixerWidget *mw = m_mixerWidgets.first(); mw != 0; mw = m_mixerWidgets.next() )
   {
	if ( mw->mixer()->isOpen() )
	{ // protect from unplugged devices (better do *not* save them)
		TQString grp;
		grp.sprintf( "%i", mw->id() );
		mw->saveConfig( TDESharedConfig::openConfig("kmixrc"), grp );
	}
   }

   KMixSettings::writeConfig();
}

void
KMixWindow::loadConfig()
{
   if (!KMixSettings::useDefaultMaster())
   {
       Mixer::setMasterCard(KMixSettings::masterMixer());
       Mixer::setMasterCardDevice(KMixSettings::masterMixerDevice());
   }

   m_toplevelOrientation = static_cast<TQt::Orientation>(KMixSettings::orientation());

   TDEToggleAction *a = static_cast<TDEToggleAction*>(actionCollection()->action("options_show_menubar"));
   if (a) a->setChecked( KMixSettings::menubar() );

   // restore window size and position
   if ( !kapp->isRestored() ) // done by the session manager otherwise
   {
        TQSize size = KMixSettings::size();
        if(!size.isEmpty())
        {
            resize(size);
        }
        move(KMixSettings::position());
    }
}


void
KMixWindow::initMixerWidgets()
{
   m_mixerWidgets.clear();

	int id=0;
	Mixer *mixer;

	// Attention!! If Mixer::mixers() is empty, we behave stupid. We don't show nothing and there
        //             is not even a context menu.
	for ( mixer=Mixer::mixers().first(),id=0; mixer!=0; mixer=Mixer::mixers().next(),id++ )
	{
		//kdDebug(67100) << "Mixer number: " << id << " Name: " << mixer->mixerName() << endl ;
		ViewBase::ViewFlags vflags = ViewBase::HasMenuBar;
		if (KMixSettings::menubar()) {
			vflags |= ViewBase::MenuBarVisible;
		}

#ifdef WITH_KMIX_EXPERIMENTAL
        if (KMixSettings::experimental_ViewSurround()) {
			vflags |= ViewBase::Experimental_SurroundView;
		}
        if (KMixSettings::experimental_ViewGrid()) {
			vflags |= ViewBase::Experimental_GridView;
		}
#endif

		if ( m_toplevelOrientation == TQt::Vertical ) {
			vflags |= ViewBase::Vertical;
		}
        else {
			vflags |= ViewBase::Horizontal;
		}

	
		KMixerWidget *mw = new KMixerWidget( id, mixer, mixer->mixerName(),
						     MixDevice::ALL, this, "KMixerWidget", vflags );
		m_mixerWidgets.append( mw );

		// Add to Combo and Stack
		m_cMixer->insertItem( mixer->mixerName() );
		m_wsMixers->addWidget( mw, id );

		TQString grp;
		grp.sprintf( "%i", mw->id() );
        mw->loadConfig( TDESharedConfig::openConfig("kmixrc"), grp );

		mw->setTicks(KMixSettings::tickmarks());
		mw->setLabels(KMixSettings::labels());
		mw->setValueStyle(KMixSettings::valueStyle());
		// !! I am still not sure whether this works 100% reliably - chris
		mw->show();
	}

	if (id == 1)
	{
		// don't show the Current Mixer bit unless we have multiple mixers
		mixerNameLayout->hide();
	}
}



bool
KMixWindow::queryClose ( )
{
    if ( KMixSettings::allowDocking() && !kapp->sessionSaving() )
    {
        hide();
        return false;
    }
    return true;
}


void
KMixWindow::quit()
{
	kapp->quit();
}


void
KMixWindow::showSettings()
{
   if (TDEConfigDialog::showDialog("KMixConfigDialog"))
   {
       return;
   }

   TDEConfigDialog *cfg = new TDEConfigDialog(this, "KMixConfigDialog", KMixSettings::self());

   KMixBehaviorConfig *cfgb = new KMixBehaviorConfig(0, "Behavior");
   cfg->addPage(cfgb, i18n("Behavior"), "configure");

   KMixAppearanceConfig *cfga = new KMixAppearanceConfig(0, "Appearance");
   cfg->addPage(cfga, i18n("Appearance"), "kmix");

#ifdef WITH_KMIX_EXPERIMENTAL
   KMixExperimental *cfgx = new KMixExperimental(0, "Experimental");
   cfg->addPage(cfgx, i18n("Experimental"), "bug");
#endif

   connect(cfg, TQT_SIGNAL(settingsChanged()), this, TQT_SLOT(applyPrefs()));

   cfg->show();
}

void
KMixWindow::showHelp()
{
	actionCollection()->action( "help_contents" )->activate();
}


void
KMixWindow::showAbout()
{
   actionCollection()->action( "help_about_app" )->activate();
}

/**
 * Loads the volumes of all mixers from kmixctrlrc.
 * In other words:
 * Restores the default voumes as stored via saveVolumes() or the
 * execution of "kmixctrl --save"
 */
/* Currently this is not in use
void
KMixWindow::loadVolumes()
{
	TDEConfig *cfg = new TDEConfig( "kmixctrlrc", true );
	for (Mixer *mixer=Mixer::mixers().first(); mixer!=0; mixer=Mixer::mixers().next())
	{
		mixer->volumeLoad( cfg );
	}
	delete cfg;
}
*/

/**
 * Stores the volumes of all mixers  Can be restored via loadVolumes() or
 * the kmixctrl application.
 */
void
KMixWindow::saveVolumes()
{
    TDEConfig *cfg = new TDEConfig( "kmixctrlrc", false );
    for (Mixer *mixer=Mixer::mixers().first(); mixer!=0; mixer=Mixer::mixers().next()) {
	//kdDebug(67100) << "KMixWindow::saveConfig()" << endl;
	if ( mixer->isOpen() ) { // protect from unplugged devices (better do *not* save them)
		mixer->volumeSave( cfg );
	}
    }
    delete cfg;
}


void
KMixWindow::applyPrefs()
{
   auto old = static_cast<TQt::Orientation>(KMixSettings::orientation());
   if (m_toplevelOrientation != old)
   {
      TQString msg = i18n("The change of orientation will be adopted on the next start of KMix.");
      KMessageBox::information(0, msg);
   }

   this->setUpdatesEnabled(false);
   updateDocking();

   for (KMixerWidget *mw=m_mixerWidgets.first(); mw!=0; mw=m_mixerWidgets.next())
   {
      mw->setTicks( KMixSettings::tickmarks() );
      mw->setLabels( KMixSettings::labels() );
      mw->setValueStyle( KMixSettings::valueStyle() );
      mw->mixer()->readSetFromHWforceUpdate(); // needed, as updateDocking() has reconstructed the DockWidget
   }

   this->setUpdatesEnabled(true);

   this->repaint(); // make KMix look fast (saveConfig() often uses several seconds)
   kapp->processEvents();
   saveConfig();
}


void
KMixWindow::toggleMenuBar()
{
	if( menuBar()->isShown() )
	{
		menuBar()->hide();
	}
   else
	{
		menuBar()->show();
	}
}

void
KMixWindow::showEvent( TQShowEvent * )
{
    if ( m_visibilityUpdateAllowed )
	m_isVisible = isVisible();
    // !! could possibly start polling now (idea: use someting like ref() and unref() on Mixer instance
}

void
KMixWindow::hideEvent( TQHideEvent * )
{
    if ( m_visibilityUpdateAllowed )
    {
	m_isVisible = isVisible();
    }
    // !! could possibly stop polling now (idea: use someting like ref() and unref() on Mixer instance
    //    Update: This is a stupid idea, because now the views are responsible for updating. So it will be done in the Views.
    //    But the dock icon is currently no View, so that must be done first.
}


void
KMixWindow::stopVisibilityUpdates() {
    m_visibilityUpdateAllowed = false;
}

void
KMixWindow::slotHWInfo() {
	KMessageBox::information( 0, m_hwInfoString, i18n("Mixer Hardware Information") );
}

void
KMixWindow::showSelectedMixer( int mixer )
{
	m_wsMixers->raiseWidget( mixer );
}

void
KMixWindow::configureGlobalShortcuts()
{
	KKeyDialog::configure( m_globalAccel, 0, false ) ;
        m_globalAccel->writeSettings();
        m_globalAccel->updateConnections();
}

// KMixIface DCOP interface methods
void KMixWindow::setVolume(int percentage)
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      mixerMaster->setMasterVolume(percentage);
   }
}

void KMixWindow::increaseVolume(int percentage)
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      MixDevice *md = mixerMaster->masterDevice();
      if (md)
      {
         mixerMaster->increaseVolume(md->num(), percentage);
      }
   }
}

void KMixWindow::decreaseVolume(int percentage)
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      MixDevice *md = mixerMaster->masterDevice();
      if (md)
      {
         mixerMaster->decreaseVolume(md->num(), percentage);
      }
   }
}

int KMixWindow::volume()
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      return mixerMaster->masterVolume();
   }
   return -1;
}

void KMixWindow::setAbsoluteVolume(long absoluteVolume)
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      MixDevice *md = mixerMaster->masterDevice();
      if (md)
      {
         return mixerMaster->setAbsoluteVolume(md->num(), absoluteVolume);
      }
   }
}

long KMixWindow::absoluteVolume()
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      MixDevice *md = mixerMaster->masterDevice();
      if (md)
      {
         return mixerMaster->absoluteVolume(md->num());
      }
   }
   return -1L;
}

long KMixWindow::absoluteVolumeMin()
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      MixDevice *md = mixerMaster->masterDevice();
      if (md)
      {
         return mixerMaster->absoluteVolumeMin(md->num());
      }
   }
   return -1L;
}

long KMixWindow::absoluteVolumeMax()
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      MixDevice *md = mixerMaster->masterDevice();
      if (md)
      {
         return mixerMaster->absoluteVolumeMax(md->num());
      }
   }
   return -1L;
}

void KMixWindow::setMute(bool on)
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      mixerMaster->setMasterMute(on);
   }
}

void KMixWindow::toggleMute()
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      mixerMaster->toggleMasterMute();
   }
}

bool KMixWindow::mute()
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      return mixerMaster->masterMute();
   }
   return true;
}

TQString KMixWindow::mixerName()
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      return mixerMaster->mixerName();
   }
   return TQString::null;
}

int KMixWindow::deviceIndex()
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      return mixerMaster->masterDeviceIndex();
   }
   return -1;
}

void KMixWindow::setBalance(int balance)
{
   Mixer *mixerMaster = Mixer::masterCard();
   if (mixerMaster)
   {
      mixerMaster->setBalance(balance);
   }
}

#include "kmix.moc"