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
|
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2010 Артём Попов <artfwo@gmail.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
### END LICENSE
# TODO:
# import multiprocessing
# multiprocessing.cpu_count()
import gobject
import gtk
import appindicator
import locale
import dbus
from indicator_cpufreq import cpufreq
from indicator_cpufreq.indicator_cpufreqconfig import get_data_file, get_icon_path
import gettext
from gettext import gettext as _
#gettext.textdomain('indicator-cpufreq')
# FIXME: replace value with user-selectable unit
FIXME_CPU = 0
def readable_frequency(f):
return _("%s GHz") % locale.format(_("%.2f"), f / 1.0e6)
governor_names = {
'conservative': _("Conservative"),
'ondemand': _("Ondemand"),
#'userspace': _("Userspace"),
'powersave': _("Powersave"),
'performance': _("Performance"),
}
def readable_governor(g):
if governor_names.has_key(g):
return governor_names[g]
else:
return g
class MyIndicator(appindicator.Indicator):
def __init__(self):
appindicator.Indicator.__init__(self, "indicator-cpufreq",
"indicator-messages",
appindicator.CATEGORY_HARDWARE)
self.set_status(appindicator.STATUS_ACTIVE)
self.set_icon_theme_path("/usr/share/icons")
#print get_icon_path()
#self.set_icon(get_data_file('media', 'indicator-cpufreq.png'))
self.set_icon('indicator-cpufreq')
print self.get_icon()
menu = gtk.Menu()
self.select_items = {}
group = None
# frequency menu items
freqs = cpufreq.get_available_frequencies(FIXME_CPU)
for freq in freqs:
menu_item = gtk.RadioMenuItem(group, readable_frequency(freq))
if group is None:
group = menu_item
menu.append(menu_item)
menu_item.connect("activate", self.select_activated, 'frequency', freq)
self.select_items[freq] = menu_item
menu.append(gtk.SeparatorMenuItem())
# governor menu items
governors = cpufreq.get_available_governors(FIXME_CPU)
for governor in governors:
if governor == 'userspace':
continue
menu_item = gtk.RadioMenuItem(group, readable_governor(governor))
menu.append(menu_item)
menu_item.connect('activate', self.select_activated, 'governor', governor)
self.select_items[governor] = menu_item
menu.show_all()
self.set_menu(menu)
self.update_ui()
gobject.timeout_add(1500, self.poll_timeout)
def poll_timeout(self):
self.update_ui()
return True
def update_ui(self):
for i in self.select_items.values():
i.handler_block_by_func(self.select_activated)
fmin, fmax, governor = cpufreq.get_policy(FIXME_CPU)
freq = cpufreq.get_freq_kernel(FIXME_CPU)
ratio = min([25, 50, 75, 100], key=lambda x: abs(x - (float(freq) / fmax * 100)))
if freq < fmax and ratio == 100:
ratio = 75
#self.set_icon(get_data_file('media', 'indicator-cpufreq-%d.png' % ratio))
self.set_icon('indicator-cpufreq-%d' % ratio)
if governor == 'userspace':
self.select_items[freq].set_active(True)
else:
self.select_items[governor].set_active(True)
for i in self.select_items.values():
i.handler_unblock_by_func(self.select_activated)
#self.props.label = readable_frequency(freq)
def select_activated(self, menuitem, select, value):
if menuitem.active:
bus = dbus.SystemBus()
proxy = bus.get_object("com.ubuntu.IndicatorCpufreqSelector",
"/Selector",
introspect=False)
if select == 'frequency':
proxy.SetFrequency(dbus.UInt32(FIXME_CPU), dbus.UInt32(value),
dbus_interface='com.ubuntu.IndicatorCpufreqSelector')
else:
proxy.SetGovernor(dbus.UInt32(FIXME_CPU), value,
dbus_interface='com.ubuntu.IndicatorCpufreqSelector')
def can_set(self):
pass
gobject.type_register(MyIndicator)
if __name__ == "__main__":
ind = MyIndicator()
gtk.main()
|