summaryrefslogtreecommitdiff
path: root/core/applets/vtapplet/vt.cpp
blob: 7885f835c1da9b3fe164e3f9b46110c2ad080dd8 (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
/**********************************************************************
** Copyright (C) 2003-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 "vt.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/oresource.h>

#include <qpe/applnk.h>

using namespace Opie::Core;

/* QT */
#include <qpopupmenu.h>

/* STD */
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/vt.h>

VTApplet::VTApplet()
         :QObject( 0, "VTApplet" ), m_ourVT( 0 )
{
}

VTApplet::~VTApplet()
{
}

int VTApplet::position() const
{
    return 2;
}

QString VTApplet::name() const
{
    return tr( "VT shortcut" );
}

QString VTApplet::text() const
{
    return tr( "Terminal" );
}

/*
QString VTApplet::tr( const char* s ) const
{
    return qApp->translate( "VTApplet", s, 0 );
}

QString VTApplet::tr( const char* s, const char* p ) const
{
    return qApp->translate( "VTApplet", s, p );
}
*/

QIconSet VTApplet::icon() const
{
    QPixmap pix = Opie::Core::OResource::loadPixmap( "terminal", Opie::Core::OResource::SmallIcon );
    return pix;
}

QPopupMenu *VTApplet::popup( QWidget* parent ) const
{
    odebug << "VTApplet::popup" << oendl;

    struct vt_stat vtstat;
#ifdef QT_QWS_DEVFS
    int fd = ::open( "/dev/vc/0", O_RDWR );
#else
    int fd = ::open( "/dev/tty0", O_RDWR );
#endif
    if ( fd == -1 ) return 0;
    if ( ioctl( fd, VT_GETSTATE, &vtstat ) == -1 ) return 0;

    m_subMenu = new QPopupMenu( parent );
    m_subMenu->setCheckable( true );
    for ( int i = 1; i < 10; ++i )
    {
        int id = m_subMenu->insertItem( QString::number( i ), 500+i );
        m_subMenu->setItemChecked( id, id-500 == vtstat.v_active );
    }
    ::close( fd );

    m_ourVT = vtstat.v_active;

    connect( m_subMenu, SIGNAL( activated(int) ), this, SLOT( changeVT(int) ) );
    connect( m_subMenu, SIGNAL( aboutToShow() ), this, SLOT( updateMenu() ) );

    return m_subMenu;
}


void VTApplet::changeVT( int index )
{
    //odebug << "VTApplet::changeVT( " << index-500 << " )" << oendl;

#ifdef QT_QWS_DEVFS
    int fd = ::open("/dev/vc/0", O_RDWR);
#else
    int fd = ::open("/dev/tty0", O_RDWR);
#endif
    if ( fd == -1 ) return;
    ioctl( fd, VT_ACTIVATE, index-500 );
    if ( m_ourVT )
    {
        odebug << "VTApplet::waiting for user to return to VT " << m_ourVT << oendl;
        ioctl( fd, VT_WAITACTIVE, m_ourVT );
    }
}


void VTApplet::updateMenu()
{
    //odebug << "VTApplet::updateMenu()" << oendl;

    int fd = ::open( "/dev/console", O_RDONLY );
    if ( fd == -1 ) return;

    for ( int i = 1; i < 10; ++i )
    {
        int result = ioctl( fd, VT_DISALLOCATE, i );

        /*
        if ( result == -1 )
            odebug << "VT " << i << " disallocated == free" << oendl;
        else
            odebug << "VT " << i << " _not_ disallocated == busy" << oendl;
        */

        m_subMenu->setItemEnabled( 500+i, result == -1 );
    }

    ::close( fd );
}


void VTApplet::activated()
{
    odebug << "VTApplet::activated()" << oendl;
}


QRESULT VTApplet::queryInterface( const QUuid &uuid, QUnknownInterface **iface )
{
    *iface = 0;
    if ( uuid == IID_QUnknown )
        *iface = this;
    else if ( uuid == IID_MenuApplet )
        *iface = this;
    else
        return QS_FALSE;

    if ( *iface )
        (*iface)-> addRef ( );
    return QS_OK;
}

Q_EXPORT_INTERFACE( )
{
    Q_CREATE_INSTANCE( VTApplet )
}