summaryrefslogtreecommitdiffstats
path: root/tdecore/tdehw/tdebacklightdevice.cpp
blob: b3f6964669d9a81a16bbe5fa2161756bba8dd31f (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
/* This file is part of the TDE libraries
   Copyright (C) 2012 Timothy Pearson <[email protected]>
             (C) 2013 Golubev Alexander <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "tdebacklightdevice.h"

#include <unistd.h>

#include <tqfile.h>

#include "config.h"

// uPower
#if defined(WITH_TDEHWLIB_DAEMONS)
	#include <tqdbusdata.h>
	#include <tqdbusmessage.h>
	#include <tqdbusproxy.h>
	#include <tqdbusvariant.h>
	#include <tqdbusconnection.h>
#endif // defined(WITH_TDEHWLIB_DAEMONS)

TDEBacklightDevice::TDEBacklightDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
}

TDEBacklightDevice::~TDEBacklightDevice() {
}

TDEDisplayPowerLevel::TDEDisplayPowerLevel TDEBacklightDevice::powerLevel() {
	return m_powerLevel;
}

void TDEBacklightDevice::internalSetPowerLevel(TDEDisplayPowerLevel::TDEDisplayPowerLevel pl) {
	m_powerLevel = pl;
}

void TDEBacklightDevice::internalSetMaximumRawBrightness(int br) {
	m_maximumBrightness = br;
}

void TDEBacklightDevice::internalSetCurrentRawBrightness(int br) {
	m_currentBrightness = br;
}

int TDEBacklightDevice::brightnessSteps() {
	return m_maximumBrightness + 1;
}

double TDEBacklightDevice::brightnessPercent() {
	return (((m_currentBrightness*1.0)/m_maximumBrightness)*100.0);
}

bool TDEBacklightDevice::canSetBrightness() {
	TQString brightnessnode = systemPath() + "/brightness";
	int rval = access (brightnessnode.ascii(), W_OK);
	if (rval == 0) {
		return TRUE;
	}

#ifdef WITH_TDEHWLIB_DAEMONS
	{
		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
		if (dbusConn.isConnected()) {
			TQT_DBusProxy hardwareControl("org.trinitydesktop.hardwarecontrol", "/org/trinitydesktop/hardwarecontrol", "org.trinitydesktop.hardwarecontrol.Brightness", dbusConn);
			if (hardwareControl.canSend()) {
				// can set brightness?
				TQValueList<TQT_DBusData> params;
				params << TQT_DBusData::fromString(brightnessnode);
				TQT_DBusMessage reply = hardwareControl.sendWithReply("CanSetBrightness", params);
				if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
					return reply[0].toBool();
				}
			}
		}
	}
#endif // WITH_TDEHWLIB_DAEMONS

	return FALSE;
}

int TDEBacklightDevice::rawBrightness() {
	return m_currentBrightness;
}

void TDEBacklightDevice::setRawBrightness(int br) {
	bool setRawBrightnessDone = FALSE;

	TQString brightnessnode = systemPath() + "/brightness";
	TQString brightnessCommand = TQString("%1").arg(br);
	TQFile file( brightnessnode );
	if ( file.open( IO_WriteOnly ) ) {
		TQTextStream stream( &file );
		stream << brightnessCommand;
		file.close();
		setRawBrightnessDone = TRUE;
	}

#ifdef WITH_TDEHWLIB_DAEMONS
	if ( !setRawBrightnessDone ) {
		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
		if (dbusConn.isConnected()) {
			TQT_DBusProxy hardwareControl("org.trinitydesktop.hardwarecontrol", "/org/trinitydesktop/hardwarecontrol", "org.trinitydesktop.hardwarecontrol.Brightness", dbusConn);
			if (hardwareControl.canSend()) {
				// set brightness
				TQValueList<TQT_DBusData> params;
				params << TQT_DBusData::fromString(brightnessnode) << TQT_DBusData::fromString(brightnessCommand);
				TQT_DBusMessage reply = hardwareControl.sendWithReply("SetBrightness", params);
				if (reply.type() == TQT_DBusMessage::ReplyMessage) {
					setRawBrightnessDone = TRUE;
				}
			}
		}
	}
#endif // WITH_TDEHWLIB_DAEMONS

}

#include "tdebacklightdevice.moc"