summaryrefslogtreecommitdiffstats
path: root/kmilo/delli8k/delli8k.cpp
blob: 74c0d6047c0ebd583a63744b3f2e4ad2ef11e53b (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
/*
   This file is part of the KDE project

   Copyright (c) 2004 Barry O'Donovan (barry.odonovan@kdemail.net)

   The code in this module is heavily based on:
    - the Asus Laptop plug-in for KMilo
      Copyright (c) 2004 Chris Howells (howells@kde.org)
    - the Thinkpad plug-in
      Copyright (c) 2004 Jonathan Riddell (jr@jriddell.org)
    - the i8k kernel module and utilities (http://www.debian.org/~dz/i8k/)
      Copyright (c) 2001-2003  Massimo Dal Zotto <dz@debian.org>

    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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <kgenericfactory.h>

#include "kmilointerface.h"

#include <fcntl.h>
#include <sys/ioctl.h>

#include "i8k.h"

/* For compatibilty with i8k kernel driver 1.1 */
#define I8K_VOL_MUTE_1_1 3

#include "delli8k.h"

namespace KMilo
{

DellI8kMonitor::DellI8kMonitor( QObject* parent, const char* name, const QStringList& args )
    : Monitor( parent, name, args )
{
}

DellI8kMonitor::~DellI8kMonitor()
{
}

bool DellI8kMonitor::init()
{
    kdDebug() << "KMilo: loading Dell I8k KMilo plugin" << endl;

    // Ensure the Dell I8k kernel module is installed/loaded
    if( ( m_fd = open( I8K_PROC, O_RDONLY ) ) < 0 )
    {
        kdDebug() << "KMilo: DellI8kMonitor: unable to open i8k proc file: " << I8K_PROC
                << " in init()" << endl;
        return false;
    }
	else
	{
        kmixClient = new DCOPRef( "kmix", "Mixer0" );
        kmixWindow = new DCOPRef( "kmix", "kmix-mainwindow#1" );
        retrieveVolume();
        retrieveMute();
	}

	return true;
}

Monitor::DisplayType DellI8kMonitor::poll()
{
    Monitor::DisplayType pollResult = None;

    if( ( m_status = fn_status( m_fd ) ) < 0 )
    {
        kdDebug() << "KMilo: DellI8kMonitor: unable to read fn status in poll()" << endl;
        return pollResult;
    }
    else
    {
        switch( m_status )
        {
            case I8K_VOL_UP:
                pollResult = Volume;
                retrieveVolume();
                setVolume( m_volume + DELLI8K_VOLUME_STEPSIZE );
                m_progress = m_volume;
                break;
            case I8K_VOL_DOWN:
                pollResult = Volume;
                retrieveVolume();
                setVolume( m_volume - DELLI8K_VOLUME_STEPSIZE );
                m_progress = m_volume;
                break;
            case I8K_VOL_MUTE:
            case I8K_VOL_MUTE_1_1:
                pollResult = None;
                retrieveMute();
                setMute( !m_mute );
                if( m_mute )
                    _interface->displayText( i18n( "Mute On" ) );
                else
                    _interface->displayText( i18n( "Mute Off" ) );
                break;
            case 0:
                break;
            default:
                kdDebug() << "KMile: DellI8kMonitor: invalid button status: " << m_status
                        << " in poll()" << endl;
                break;
        }
    }

	return pollResult;
}

int DellI8kMonitor::progress() const
{
    return m_progress;
}

bool DellI8kMonitor::retrieveVolume()
{
    bool kmix_error = false;

    DCOPReply reply = kmixClient->call( "masterVolume");

    if( reply.isValid() )
    {
        m_volume = reply;
    }
    else
    {
        kmix_error = true;
    }

    if (kmix_error)
    {
        // maybe the error occurred because kmix wasn't running
        if( kapp->startServiceByDesktopName( "kmix" ) == 0 )            // trying to start kmix
        {
            // trying again
            reply = kmixClient->call( "masterVolume");
            if( reply.isValid() )
            {
                m_volume = reply;
                kmix_error = false;
                kmixWindow->send( "hide" );
            }
        }
    }

    if (kmix_error)
    {
        kdError() << "KMilo: DellI8kMonitor could not access kmix/Mixer0 via dcop" << endl;
        return false;
    }

    return true;
}

void DellI8kMonitor::setVolume(int volume)
{
    if( !retrieveVolume() )
    {
        return;
    }

    if( volume > 100 )
    {
        m_volume = 100;
    }
    else if( volume < 0 )
    {
        m_volume = 0;
    }
    else
    {
        m_volume = volume;
    }

    kmixClient->send("setMasterVolume", m_volume);

    m_progress = m_volume;
}

bool DellI8kMonitor::retrieveMute()
{
    bool kmix_error = false;

    DCOPReply reply = kmixClient->call( "masterMute" );

    if( reply.isValid() )
    {
        m_mute = reply;
    }
    else
    {
        kmix_error = true;
    }

    if (kmix_error)
    {
        // maybe the error occurred because kmix wasn't running
        if( kapp->startServiceByDesktopName( "kmix" ) == 0 )            // trying to start kmix
        {
            // trying again
            reply = kmixClient->call( "masterVolume" );
            if( reply.isValid() )
            {
                m_volume = reply;
                kmix_error = false;
                kmixWindow->send( "hide" );
            }
        }
    }

    if (kmix_error)
    {
        kdError() << "KMilo: DellI8kMonitor could not access kmix/Mixer0 via dcop in isMute()" << endl;
        return false;
    }

    return true;
}

void DellI8kMonitor::setMute( bool b )
{
    m_mute = b;
    kmixClient->send( "setMasterMute", m_mute );
}

int DellI8kMonitor::fn_status( int fd )
{
    int args[1];
    int rc;

    if( ( rc = ioctl( fd, I8K_FN_STATUS, &args ) ) < 0 )
    {
        kdError() << "KMilo: DellI8kMonitor: ioctl failed in fn_status()" << endl;
        return rc;
    }

    return args[0];
}

} //close namespace

K_EXPORT_COMPONENT_FACTORY( kmilo_delli8k, KGenericFactory<KMilo::DellI8kMonitor>( "kmilo_delli8k" ) )