summaryrefslogtreecommitdiffstats
path: root/src/cpufreqd/cpufreqd.cpp
blob: b5f1ad0a66dfa27d1c8c2e2ad54a4befba1d2c40 (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
/***************************************************************************
 *   Copyright (C) 2006 by Valentine Sinitsyn                              *
 *   e_val@inbox.ru                                                        *
 *                                                                         *
 *   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 "cpufreqd.h"
#include <tqdir.h>
#include <tqkeysequence.h>
#include <tqstringlist.h>
#include <klocale.h>

#define CHUNK_SIZE 4096

/**
 * cpufreqd control module main class
 *@author: Valentine Sinitsyn (Valentine.Sinitsyn@usu.ru)
 *@todo: We assume that cpufreqd is in dynamic mode when applet is started because as for now it has
 * no means to detect mode. This could lead to inconsistency if, ex. applet is restarted after making
 * some changes to cpufreqd state.
 */

CPUFreqd::CPUFreqd() {
	m_menu = new TQPopupMenu();
	TQObject::connect(m_menu, TQT_SIGNAL(aboutToShow()), this, TQT_SLOT(updateMenu()));

	m_dynamic = new TQAction(i18n("Select dynamically"), TQKeySequence(), this);
	TQObject::connect(m_dynamic, TQT_SIGNAL(activated()), this, TQT_SLOT(setDynamic()));
	m_dynamic->setToggleAction(true);
	m_dynamic->setOn(true);

	m_items = new TQActionGroup(this);
	m_items->setExclusive(true);

	m_actions = new TQPtrList<TQAction>();
	m_actions->setAutoDelete(true);

	m_mapper = new TQSignalMapper(this);
	TQObject::connect(m_mapper, TQT_SIGNAL(mapped(int)), this, TQT_SLOT(setProfile(int)));
}

CPUFreqd::~CPUFreqd() {
}

bool CPUFreqd::enabled() const {
	return m_conn.available();
}

TQValueVector<CPUFreqdProfile> & CPUFreqd::getProfiles(bool reconnect) {
	char chunk[CHUNK_SIZE];
	TQString buffer;

	if (!m_profiles.empty())
		m_profiles.clear();

	if (!m_conn.open())
		if (reconnect) {
			m_dynamic->setOn(m_conn.lookup());
			return getProfiles(false);
		} else {
			return m_profiles;
		}
	
	if (!m_conn.write(CMD_LIST_PROFILES, 0))
		return m_profiles;

	int bytes = 0;
	while ( (bytes = m_conn.read(chunk, CHUNK_SIZE - 1)) ) {
		chunk[bytes] = '\0';
		buffer.append(chunk);
	}

	TQStringList profiles = TQStringList::split("\n", buffer);
	for (TQStringList::Iterator it = profiles.begin(); it != profiles.end(); it++)
		m_profiles.push_back( CPUFreqdProfile(*it) );

	m_conn.close();

	return m_profiles;
}

TQPopupMenu* CPUFreqd::menu() {
	return m_menu;
}

void CPUFreqd::updateMenu() {
	TQAction *cur;

	m_menu->clear();
	m_actions->clear();

	getProfiles(true);
	if (!m_profiles.isEmpty()) {
		m_dynamic->addTo(m_menu);
		m_menu->insertSeparator();

		for (unsigned int i = 0; i < m_profiles.count(); i++)
			if (m_profiles[i].isValid()) {
				cur = new TQAction(m_profiles[i].name(), TQKeySequence(), m_items);
				TQObject::connect(cur, TQT_SIGNAL(activated()), m_mapper, TQT_SLOT(map()));
				cur->setToggleAction(true);
				cur->setOn(m_profiles[i].active());

				m_mapper->setMapping(cur, i+1);
				m_actions->append(cur);
			}
		m_items->addTo(m_menu);
	} else {
		int errmsg = m_menu->insertItem(i18n("Can't talk to cpufreqd"), 0, 0);
		m_menu->setItemEnabled(errmsg, false);
	}
}

void CPUFreqd::setManual() {
	setMode(ARG_MANUAL);
	m_dynamic->setOn(false);
}

void CPUFreqd::setDynamic() {
	setMode(ARG_DYNAMIC);
	m_dynamic->setOn(true);
}

void CPUFreqd::setProfile(int id) {
	//@fixme: make it return bool and bail out if it falis
	if (m_dynamic->isOn())
		setManual();

	if (!m_conn.open())
		return;

	m_conn.write(CMD_SET_PROFILE, (uint32_t)id);
	m_conn.close();
}

void CPUFreqd::setMode(uint32_t mode) {
	if (!m_conn.open())
		return;

	m_conn.write(CMD_SET_MODE, mode);
	m_conn.close();
}