summaryrefslogtreecommitdiff
path: root/noncore/applets/wirelessapplet/wireless.cpp
blob: 9ce6618371f6c822f86b4d922564c96a6bcfd84d (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
/**********************************************************************
** Copyright (C) 2002-2004 Michael 'Mickey' Lauer <mickey@vanille.de>
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
**********************************************************************/

#include "wireless.h"
#include "mgraph.h"
#include "advancedconfig.h"
#include "connect0.xpm"
#include "connect1.xpm"
#include "connect2.xpm"
#include "connect3.xpm"
#include "connect4.xpm"
#include "connect5.xpm"
#include "nowireless.xpm"

/* OPIE */
#include <opie2/onetwork.h>
#include <opie2/otaskbarapplet.h>
#include <qpe/config.h>

/* QT */
#include <qradiobutton.h>
#include <qpushbutton.h>
#include <qpainter.h>
#include <qlabel.h>
#include <qslider.h>
#include <qbuttongroup.h>
#include <qlayout.h>
#include <qfile.h>
#include <qtextstream.h>

/* STD */
#include <sys/types.h>
#include <signal.h>

#define STYLE_BARS 0
#define STYLE_ANTENNA 1

//#define MDEBUG
#undef MDEBUG

WirelessControl::WirelessControl( WirelessApplet *applet, QWidget *parent, const char *name )
        : QFrame( parent, name, WStyle_StaysOnTop | WType_Popup ), applet( applet )
{

    readConfig();
    writeConfigEntry( "UpdateFrequency", updateFrequency );
    writeConfigEntry( "DisplayStyle", displayStyle );

    setFrameStyle( QFrame::PopupPanel | QFrame::Raised );
    QGridLayout *grid = new QGridLayout( this, 3, 2, 6, 2, "top layout" );

    /* status label */

    statusLabel = new QLabel( this, "statuslabel" );
    QString text( "Wireless Status:<br>"
                  "*** Unknown ***<br>"
                  "Card not inserted ?<br>"
                  "Or Sharp ROM ?<br>"
                  "CELL: 00:00:00:00:00:00" );
    /*    QString text( "Station: Unknown<br>"
    	                "ESSID: Unknown<br>"
                        "MODE: Unknown<br>"
                        "FREQ: Unknown<br>"
                        "CELL: AA:BB:CC:DD:EE:FF" ); */
    statusLabel->setText( text );
    statusLabel->setFixedSize( statusLabel->sizeHint() );
    grid->addWidget( statusLabel, 0, 0 );

    /* visualization group box */

    QButtonGroup* group = new QButtonGroup( 1, Qt::Horizontal, "Visualization", this );
    QRadioButton* r1 = new QRadioButton( "Color Bars", group );
    QRadioButton* r2 = new QRadioButton( "Antenna", group );
    r1->setFocusPolicy( QWidget::NoFocus );
    r2->setFocusPolicy( QWidget::NoFocus );
    group->setFocusPolicy( QWidget::NoFocus );
    group->setButton( displayStyle );
    grid->addWidget( group, 0, 1 );

    /* quality graph */

    mgraph = new MGraph( this );
    mgraph->setFrameStyle( QFrame::Panel | QFrame::Sunken );
    mgraph->setMin( 0 );
    mgraph->setMax( 92 );
    grid->addWidget( mgraph, 1, 0 );
    mgraph->setFocusPolicy( QWidget::NoFocus );

    /* advanced configuration Button */

    QPushButton* advanced = new QPushButton( "Advanced...", this );
    advanced->setFocusPolicy( QWidget::NoFocus );
    grid->addWidget( advanced, 2, 0, Qt::AlignCenter );
    connect( advanced, SIGNAL( clicked() ),
             this, SLOT( advancedConfigClicked() ) );

    /* update Frequency Label */

    updateLabel = new QLabel( this );
    text.sprintf( "Update every %d s", updateFrequency );
    updateLabel->setText( text );
    grid->addWidget( updateLabel, 2, 1 );

    /* update Frequency Slider */

    QSlider* updateSlider = new QSlider( QSlider::Horizontal, this );
    updateSlider->setRange( 0, 9 );
    updateSlider->setValue( updateFrequency );
    updateSlider->setTickmarks( QSlider::Both );
    updateSlider->setTickInterval( 1 );
    updateSlider->setSteps( 1, 1 );
    updateSlider->setFocusPolicy( QWidget::NoFocus );
    grid->addWidget( updateSlider, 1, 1 );
    connect( updateSlider, SIGNAL( valueChanged( int ) ),
             this, SLOT( updateDelayChange( int ) ) );

    setFixedSize( sizeHint() );
    setFocusPolicy( QWidget::NoFocus );

    applet->displayStyleChange( displayStyle );
    applet->updateDelayChange( updateFrequency );

    connect( group, SIGNAL( clicked( int ) ),
             this, SLOT( displayStyleChange( int ) ) );

    applet->updateDHCPConfig( rocESSID, rocFREQ, rocAP, rocMODE );
}

void WirelessControl::advancedConfigClicked()
{
    AdvancedConfig * a = new AdvancedConfig( this, "dialog", TRUE );
    int result = a->exec();
    a->hide();
    delete a;
    if ( result == QDialog::Accepted )
    {
        readConfig();
        applet->updateDHCPConfig( rocESSID, rocFREQ, rocAP, rocMODE );
    }
}

void WirelessControl::updateDelayChange( int delay )
{
    QString text;
    text.sprintf( "Update every %d s", delay );
    updateLabel->setText( text );
    applet->updateDelayChange( delay );
    writeConfigEntry( "UpdateFrequency", delay );
}

void WirelessControl::displayStyleChange( int style )
{
    applet->displayStyleChange( style );
    writeConfigEntry( "DisplayStyle", style );
}

void WirelessControl::show ( bool )
{
    QPoint curPos = applet->mapToGlobal( QPoint ( 0, 0 ) );

    int w = sizeHint().width();
    int x = curPos.x() - ( w / 2 );

    if ( ( x + w ) > QPEApplication::desktop() ->width() )
        x = QPEApplication::desktop ( ) -> width ( ) - w;

    move( x, curPos.y () - sizeHint().height () );
    QFrame::show();
}

void WirelessControl::readConfig()
{
    Config cfg( "qpe" );
    cfg.setGroup( "Wireless" );

    updateFrequency = cfg.readNumEntry( "UpdateFrequency", 2 );
    displayStyle = cfg.readNumEntry( "DisplayStyle", STYLE_ANTENNA );
    rocESSID = cfg.readBoolEntry( "renew_dhcp_on_essid_change", false );
    rocFREQ = cfg.readBoolEntry( "renew_dhcp_on_freq_change", false );
    rocAP = cfg.readBoolEntry( "renew_dhcp_on_ap_change", false );
    rocMODE = cfg.readBoolEntry( "renew_dhcp_on_mode_change", false );
}

void WirelessControl::writeConfigEntry( const char *entry, int val )
{
    Config cfg( "qpe" );
    cfg.setGroup( "Wireless" );
    cfg.writeEntry( entry, val );
}

//===========================================================================

WirelessApplet::WirelessApplet( QWidget *parent, const char *name )
        : QWidget( parent, name ), visualStyle( STYLE_ANTENNA ),
        timer( 0 ), interface( 0 ), oldiface( 0 ),
        rocESSID( false ), rocFREQ( false ), rocAP( false ), rocMODE( false )
{
    setFixedHeight( 18 );
    setFixedWidth( 14 );
    status = new WirelessControl( this, this, "wireless status" );
}

void WirelessApplet::checkInterface()
{
    interface = 0L;
    ONetwork* net = ONetwork::instance();
    net->synchronize();
    ONetwork::InterfaceIterator it = net->iterator();

    while ( it.current() && !it.current()->isWireless() ) ++it;

    if ( it.current() && it.current()->isWireless() )
        interface = static_cast<OWirelessNetworkInterface*>( it.current() );

    if ( interface )
    {
#ifdef MDEBUG
        qDebug( "WIFIAPPLET: using interface '%s'", ( const char* ) interface->name() );
#endif

    }
    else
    {
#ifdef MDEBUG
        qDebug( "WIFIAPPLET: D'oh! No Wireless interface present... :(" );
#endif
        hide();
    }
}

void WirelessApplet::renewDHCP()
{
#ifdef MDEBUG
    qDebug( "WIFIAPPLET: Going to request a DHCP configuration renew." );
#endif

    QString pidfile;
    if ( !interface )
        return ;
    QString ifacename( interface->name() );

    // At first we are trying dhcpcd

    pidfile.sprintf( "/var/run/dhcpcd-%s.pid", ( const char* ) ifacename );
#ifdef MDEBUG
    qDebug( "WIFIAPPLET: dhcpcd pidfile is '%s'", ( const char* ) pidfile );
#endif
    int pid;
    QFile pfile( pidfile );
    bool hasFile = pfile.open( IO_ReadOnly );
    QTextStream s( &pfile );
    if ( hasFile )
    {
        s >> pid;
#ifdef MDEBUG
        qDebug( "WIFIAPPLET: sent SIGALARM to pid %d", pid );
#endif
        kill( pid, SIGALRM );
        return ;
    }

    // No dhcpcd, so we are trying udhcpc
#ifdef MDEBUG
    qDebug( "WIFIAPPLET: dhcpcd not available." );
#endif
    pidfile.sprintf( "/var/run/udhcpc.%s.pid", ( const char* ) ifacename );
#ifdef MDEBUG
    qDebug( "WIFIAPPLET: udhcpc pidfile is '%s'", ( const char* ) pidfile );
#endif
    QFile pfile2( pidfile );
    hasFile = pfile2.open( IO_ReadOnly );
    QTextStream s2( &pfile2 );
    if ( hasFile )
    {
        s2 >> pid;
#ifdef MDEBUG
        qDebug( "WIFIAPPLET: sent SIGUSR1 to pid %d", pid );
#endif
        kill( pid, SIGUSR1 );
        return ;
    }
}

void WirelessApplet::updateDHCPConfig( bool ESSID, bool FREQ, bool AP, bool MODE )
{
    rocESSID = ESSID;
    rocFREQ = FREQ;
    rocAP = AP;
    rocMODE = MODE;
}

void WirelessApplet::updateDelayChange( int delay )
{
    if ( timer )
        killTimer( timer );
    delay *= 1000;
    if ( delay == 0 )
        delay = 50;
    timer = startTimer( delay );
}

void WirelessApplet::displayStyleChange( int style )
{
    visualStyle = style;
    repaint();
}

WirelessApplet::~WirelessApplet()
{}

void WirelessApplet::timerEvent( QTimerEvent* )
{
#ifdef MDEBUG
    qDebug( "WirelessApplet::timerEvent" );
#endif
    if ( interface )
    {
        if ( !ONetwork::instance()->isPresent( (const char*) interface->name() ) )
        {
#ifdef MDEBUG
            qDebug( "WIFIAPPLET: Interface no longer present." );
#endif
            interface = 0L;
            mustRepaint();
            return;
        }

        if ( mustRepaint() )
        {
#ifdef MDEBUG
            qDebug( "WIFIAPPLET: A value has changed -> repainting." );
#endif
            repaint();
        }

        if ( status->isVisible() )
        {
            updatePopupWindow();
        }
    }
    else
    {
        checkInterface();
    }
}

void WirelessApplet::mousePressEvent( QMouseEvent * )
{
    if ( status->isVisible() )
        status->hide();
    else
        status->show( true );
}

bool WirelessApplet::mustRepaint()
{
    // check if there are enough changes to justify a (flickering) repaint

    // has the interface changed?

    if ( interface != oldiface )
    {
        oldiface = interface;
        if ( interface )
        {
#ifdef MDEBUG
            qDebug( "WIFIAPPLET: We had no interface but now we have one! :-)" );
#endif
            show();
        }
        else
        {
#ifdef MDEBUG
            qDebug( "WIFIAPPLET: We had a interface but now we don't have one! ;-(" );
#endif
            hide();
            return true;
        }
    }

    const char** pixmap = getQualityPixmap();

    if ( pixmap && ( pixmap != oldpixmap ) )
    {
        oldpixmap = pixmap;
        return true;
    }

    int noiseH = 50; // iface->noisePercent() * ( height() - 3 ) / 100;
    int signalH = interface->signalStrength() * ( height() - 3 ) / 100;
    int qualityH = 50; // iface->qualityPercent() * ( height() - 3 ) / 100;

    if ( ( noiseH != oldnoiseH )
            || ( signalH != oldsignalH )
            || ( qualityH != oldqualityH ) )
    {
        oldnoiseH = noiseH;
        oldsignalH = signalH;
        oldqualityH = qualityH;
        return true;
    }

    if ( rocESSID && ( oldESSID != interface->SSID() ) )
    {
#ifdef MDEBUG
        qDebug( "WIFIAPPLET: ESSID has changed." );
#endif
        renewDHCP();
    }
    else if ( rocFREQ && ( oldFREQ != interface->frequency() ) )
    {
#ifdef MDEBUG
        qDebug( "WIFIAPPLET: FREQ has changed." );
#endif
        renewDHCP();
    }
    else if ( rocAP && ( oldAP != interface->associatedAP().toString() ) )
    {
#ifdef MDEBUG
        qDebug( "WIFIAPPLET: AP has changed." );
#endif
        renewDHCP();
    }
    else if ( rocMODE && ( oldMODE != interface->mode() ) )
    {
#ifdef MDEBUG
        qDebug( "WIFIAPPLET: MODE has changed." );
#endif
        renewDHCP();
    }

    oldESSID = interface->SSID();
    oldMODE = interface->mode();
    oldFREQ = interface->frequency();
    oldAP = interface->associatedAP().toString();

    return false;
}

void WirelessApplet::updatePopupWindow()
{
    int qualityH = interface->signalStrength();

    if ( status->mgraph )
        status->mgraph->addValue( qualityH, false );

    QString freqString;
    QString cell = ( interface->mode() == "Managed" ) ? "AP: " : "Cell: ";
    freqString.sprintf( "%.3f GHz", interface->frequency() );
    status->statusLabel->setText( "Station: " + interface->nickName() + "<br>" +
                                  "ESSID: " + interface->SSID() + "<br>" +
                                  "MODE: " + interface->mode() + "<br>" +
                                  "FREQ: " + freqString + "<br>" +
                                  cell + " " + interface->associatedAP().toString() );
}

const char** WirelessApplet::getQualityPixmap()
{
    if ( !interface ) return ( const char** ) nowireless_xpm;
    int qualityH = interface->signalStrength();
    if ( qualityH < 0 ) return ( const char** ) nowireless_xpm;

    if ( visualStyle == STYLE_ANTENNA )
    {
        if ( qualityH < 1 ) return ( const char** ) connect0_xpm;
        if ( qualityH < 17 ) return ( const char** ) connect1_xpm;
        if ( qualityH < 34 ) return ( const char** ) connect2_xpm;
        if ( qualityH < 50 ) return ( const char** ) connect3_xpm;
        if ( qualityH < 65 ) return ( const char** ) connect4_xpm;
        return ( const char** ) connect5_xpm;
    }

    return 0; // please draw your bars
}

void WirelessApplet::paintEvent( QPaintEvent* )
{
    QPainter p( this );
    QColor color;

    const char** pixmap = getQualityPixmap();

    if ( pixmap )
        p.drawPixmap( 0, 1, pixmap );
    else
    {

        int noiseH = 30; // iface->noisePercent() * ( height() - 3 ) / 100;
        int signalH = 50; // iface->signalPercent() * ( height() - 3 ) / 100;
        int qualityH = interface->signalStrength(); // iface->qualityPercent() * ( height() - 3 ) / 100;

        double intensity;
        int pixelHeight;
        int pixelWidth = 2;
        int Hue;
        int barSpace = 3;
        int leftoffset = 0;
        int bottomoffset = 2;

        // draw noise indicator
        pixelHeight = noiseH;
        Hue = 50;
        for ( int i = 0; i < pixelHeight; ++i )
        {
            intensity = 50 + ( ( double ) i / ( double ) pixelHeight ) * 205;
            color.setHsv( Hue, 255, intensity );
            p.setPen ( color );
            p.drawLine( leftoffset, height() - bottomoffset - i, pixelWidth + leftoffset, height() - bottomoffset - i );
        }

        // draw signal indicator
        pixelHeight = signalH;
        Hue = 100;
        leftoffset += pixelWidth + barSpace;
        for ( int i = 0; i < pixelHeight; ++i )
        {
            intensity = 50 + ( ( double ) i / ( double ) pixelHeight ) * 205;
            color.setHsv( Hue, 255, intensity );
            p.setPen ( color );
            p.drawLine( leftoffset, height() - bottomoffset - i, pixelWidth + leftoffset, height() - bottomoffset - i );
        }

        // draw quality indicator
        pixelHeight = qualityH;
        Hue = 250;
        leftoffset += pixelWidth + barSpace;
        for ( int i = 0; i < pixelHeight; ++i )
        {
            intensity = 50 + ( ( double ) i / ( double ) pixelHeight ) * 205;
            color.setHsv( Hue, 255, intensity );
            p.setPen ( color );
            p.drawLine( leftoffset, height() - bottomoffset - i, pixelWidth + leftoffset, height() - bottomoffset - i );
        }
    }
}


int WirelessApplet::position()
{
    return 6;
}


Q_EXPORT_INTERFACE()
{
    Q_CREATE_INSTANCE( OTaskbarAppletWrapper<WirelessApplet> );
}