summaryrefslogtreecommitdiffstats
path: root/kmilo/kmilo_kvaio/kvaiodriverinterface.cpp
blob: 873633d88f2deacce30fab08b27dac101860b2af (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
/* -*- C++ -*-

   This file implements the KVaioDriverInterface class.
   It provides an event-oriented wrapper for the kernel sonypi driver.

   $ Author: Mirko Boehm $
   $ Copyright: (C) 1996-2003, Mirko Boehm $
   $ Contact: mirko@kde.org
         http://www.kde.org
         http://www.hackerbuero.org $
   $ License: LGPL with the following explicit clarification:
         This code may be linked against any version of the TQt toolkit
         from Troll Tech, Norway. $

   $Id$

   * Portions of this code are
   * (C) 2001-2002 Stelian Pop <stelian@popies.net> and
   * (C) 2001-2002 Alcove <www.alcove.com>.
   * Thanks to Stelian for the implementation of the sonypi driver.
*/

#include "kvaiodriverinterface.h"

extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <X11/Xlib.h>
//#include <X11/extensions/XTest.h>

#include "./sonypi.h"
}

#include <tqsocketnotifier.h>
#include <kdebug.h>

// Taken from Stelian Pop's spicctrl utility:

/* Have our own definition of ioctls... */

/* get/set brightness */
#define SONYPI_IOCGBRT		_IOR('v', 0, __u8)
#define SONYPI_IOCSBRT		_IOW('v', 0, __u8)

/* get battery full capacity/remaining capacity */
#define SONYPI_IOCGBAT1CAP	_IOR('v', 2, __u16)
#define SONYPI_IOCGBAT1REM	_IOR('v', 3, __u16)
#define SONYPI_IOCGBAT2CAP	_IOR('v', 4, __u16)
#define SONYPI_IOCGBAT2REM	_IOR('v', 5, __u16)

/* get battery flags: battery1/battery2/ac adapter present */
#define SONYPI_BFLAGS_B1	0x01
#define SONYPI_BFLAGS_B2	0x02
#define SONYPI_BFLAGS_AC	0x04
#define SONYPI_IOCGBATFLAGS	_IOR('v', 7, __u8)

/* get/set bluetooth subsystem state on/off */
#define SONYPI_IOCGBLUE         _IOR('v', 8, __u8)
#define SONYPI_IOCSBLUE         _IOW('v', 9, __u8)

KVaioDriverInterface::KVaioDriverInterface(TQObject *tqparent)
    : TQObject(tqparent),
      mFd(0),
      mNotifier(0)
{
}

bool KVaioDriverInterface::connectToDriver(bool listen)
{
    const char* DriverFile = "/dev/sonypi";

    mFd = open(DriverFile, O_RDONLY);

    // mFd = open(DriverFile, O_RDWR);

    if(mFd == -1)
    {
        kdDebug() << "KVaio: Failed to open /dev/sonypi: "
                  << strerror(errno) << "." << endl;
        return false;
    }

    fcntl(mFd, F_SETFL, fcntl(mFd, F_GETFL) | O_ASYNC);

    if(listen)
        {
            mNotifier = new TQSocketNotifier(mFd,  TQSocketNotifier::Read, this);
            connect(mNotifier, TQT_SIGNAL(activated(int)), TQT_SLOT(socketActivated(int)));
        }
    return true;
}

void KVaioDriverInterface::disconnectFromDriver()
{
    delete mNotifier;
    mNotifier = 0;

    if(mFd!=0)
    {
        close(mFd);
        mFd = 0;
    }
}

void KVaioDriverInterface::socketActivated(int)
{
    unsigned char events[8];
    int count;

    do {
        count = read(mFd, &events, sizeof(events));
        for(int index = 0; index<count; index++)
        {
            emit(vaioEvent(events[index]));
        }
    } while(count==sizeof(events));
}

int KVaioDriverInterface::brightness()
{
    unsigned char value = 0;
    int result=0;

    result = ioctl(mFd, SONYPI_IOCGBRT, &value);

    if (result >= 0)
    {
        return value;
    } else {
        return -1;
    }

}

void KVaioDriverInterface::setBrightness(int value)
{
    static unsigned char cache; // to avoid unnecessary updates
    int result;
    unsigned char value8 = 0;

    if(value<0) value=0;
    if(value>255) value=255;

    value8 = __u8(value);

    if(value8 != cache)
    {
        result = ioctl(mFd, SONYPI_IOCSBRT, &value8);

        if(result<0)
        {
            kdDebug() << "KVaioDriverInterface::setBrightness: ioctl failed."
                      << endl;
        }
        cache = value8;
    }
}

bool KVaioDriverInterface::getBatterytqStatus(
    bool& bat1Avail, int& bat1Remaining, int& bat1Max,
    bool& bat2Avail, int& bat2Remaining, int& bat2Max,
    bool& acConnected)
{
    unsigned char batFlags = 0;

    bool retval  = true;

    if(ioctl(mFd, SONYPI_IOCGBATFLAGS, &batFlags)<0)
    {
        retval = false;
    } else {
        __u16 rem1 = 0, rem2 = 0, cap1 = 0, cap2 = 0;

        bat1Avail = batFlags & SONYPI_BFLAGS_B1;
        bat2Avail = batFlags & SONYPI_BFLAGS_B2;
        acConnected = batFlags & SONYPI_BFLAGS_AC;

//         kdDebug() << "KVaioDriverInterface::getBatterytqStatus: battery 1:  "
//                   << (bat1Avail ? "available" : "not available") << endl
//                   << "                                        battery 2:  "
//                   << (bat2Avail ? "available" : "not available") << endl
//                   << "                                        AC adapter: "
//                   << (acConnected ? "connected" : "connected")
//                   << "." << endl;

        if(bat1Avail
           && ioctl(mFd, SONYPI_IOCGBAT1CAP, &cap1) >= 0
           && ioctl(mFd, SONYPI_IOCGBAT1REM, &rem1) >= 0)
        {
            bat1Max = cap1;
            bat1Remaining = rem1;
        } else {
            bat1Remaining = 0;
            bat1Max = 0;
            retval = false;
        }

        if(bat2Avail
           && ioctl(mFd, SONYPI_IOCGBAT2CAP, &cap2) >= 0
           && ioctl(mFd, SONYPI_IOCGBAT2REM, &rem2) >= 0)
        {
            bat2Max = cap2;
            bat2Remaining = rem2;
        } else {
            bat2Remaining = 0;
            bat2Max = 0;
            retval = false;
        }
    }

    return retval;
}

#include "kvaiodriverinterface.moc"