summaryrefslogtreecommitdiff
path: root/src/ui/alsa/alsatray.cpp
blob: 46ff59ff7452166b6833bde3de8f0c10c6e71ed2 (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
#include "alsatray.h"
#include "alsapopup.h"
#include "alsamixerwindow.h"
#include "../../model/alsamodel.h"
#include "../../model/alsadevice.h"

#include <tqevent.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <kstandarddirs.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <tdepopupmenu.h>
#include <twin.h>

AlsaTray::AlsaTray( AlsaModel *model, AlsaMixerWindow *win, TQWidget *parent )
    : KSystemTray(parent), m_model(model), m_win(win),
      m_output(model->preferredOutput()), m_popup(0)
{
    setCaption( i18n("Volume Control") );

    connect(model, TQ_SIGNAL(deviceAdded(AlsaDevice*)),              this, TQ_SLOT(updateIcon()));
    connect(model, TQ_SIGNAL(preferredOutputChanged(AlsaDevice*)),   this, TQ_SLOT(onPreferredChanged(AlsaDevice*)));

    if ( m_output ) {
        connect(m_output, TQ_SIGNAL(volumeChanged(int)), this, TQ_SLOT(updateIcon()));
        connect(m_output, TQ_SIGNAL(muteChanged(bool)),  this, TQ_SLOT(updateIcon()));
    }

    updateIcon();
}

void AlsaTray::onPreferredChanged( AlsaDevice *dev )
{
    if ( m_output ) {
        disconnect(m_output, TQ_SIGNAL(volumeChanged(int)), this, TQ_SLOT(updateIcon()));
        disconnect(m_output, TQ_SIGNAL(muteChanged(bool)),  this, TQ_SLOT(updateIcon()));
    }
    m_output = dev;
    if ( m_output ) {
        connect(m_output, TQ_SIGNAL(volumeChanged(int)), this, TQ_SLOT(updateIcon()));
        connect(m_output, TQ_SIGNAL(muteChanged(bool)),  this, TQ_SLOT(updateIcon()));
    }
    updateIcon();
}

void AlsaTray::updateIcon()
{
    if ( !m_output )
        m_output = m_model->preferredOutput();

    int  vol  = m_output ? m_output->volume() : 0;
    bool mute = m_output ? m_output->muted()  : true;

    const char *pic;
    if ( mute || vol == 0 )  pic = "audio-volume-muted.png";
    else if ( vol < 34 )     pic = "audio-volume-low.png";
    else if ( vol < 67 )     pic = "audio-volume-medium.png";
    else                     pic = "audio-volume-high.png";

    TQString path = TDEGlobal::dirs()->findResource("data",
        TQString("tmix/pics/crystal/") + pic);
    if ( !path.isEmpty() )
        setPixmap( TQPixmap(path) );
}

void AlsaTray::mousePressEvent( TQMouseEvent *e )
{
    if ( e->button() == TQt::LeftButton ) {
        TDEConfig *cfg = TDEGlobal::config();
        cfg->setGroup("Tray");
        int mode = cfg->readNumEntry("PopupMode", 1);
        if ( mode == 1 ) {
            if ( !m_popup )
                m_popup = new AlsaPopup( m_model, this );
            if ( m_popup->justHidden() ) return;
            m_popup->toggleAt( mapToGlobal( TQPoint(0,0) ), size() );
        } else {
            m_win->bringToFront();
        }
        e->accept();
        return;
    }
    if ( e->button() == TQt::MidButton ) {
        middleClickMute();
        e->accept();
        return;
    }
    KSystemTray::mousePressEvent(e);
}

void AlsaTray::middleClickMute()
{
    if ( !m_output ) return;
    TDEConfig *cfg = TDEGlobal::config();
    cfg->setGroup("Tray");
    if ( cfg->readBoolEntry("MiddleClickMute", false) )
        m_output->setMuted( !m_output->muted() );
}

void AlsaTray::wheelEvent( TQWheelEvent *e )
{
    if ( !m_output ) return;
    TDEConfig *cfg = TDEGlobal::config();
    cfg->setGroup("Tray");
    int step  = cfg->readNumEntry("ScrollStep", 5);
    int delta = e->delta() > 0 ? step : -step;
    int vol   = m_output->volume() + delta;
    if ( vol < 0 )   vol = 0;
    if ( vol > 100 ) vol = 100;
    m_output->setVolume(vol);
}

void AlsaTray::contextMenuAboutToShow( TDEPopupMenu *menu )
{
    while ( menu->count() > 1 )
        menu->removeItemAt(1);

    menu->insertItem( SmallIcon("tmix"), i18n("&Open Mixer"),
                      m_win, TQ_SLOT(bringToFront()) );
    menu->insertSeparator();
    menu->insertItem( SmallIcon("configure"), i18n("&Configure TMix..."),
                      m_win, TQ_SLOT(showPreferences()) );
    menu->insertItem( SmallIcon("help"), i18n("&About TMix"),
                      m_win, TQ_SLOT(showAbout()) );
    menu->insertSeparator();
    menu->insertItem( SmallIcon("application-exit"), i18n("&Quit"),
                      m_win, TQ_SLOT(quit()) );
}

#include "alsatray.moc"