2 * Copyright 2009, Intel Corporation
3 * Copyright 2009, Sun Microsystems, Inc
5 * This file is part of PowerTOP
7 * This program file is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU General Public License
17 * along with this program in a file named COPYING; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
23 * Arjan van de Ven <arjan@linux.intel.com>
24 * Eric C Saxe <eric.saxe@sun.com>
25 * Aubrey Li <aubrey.li@intel.com>
31 * For the avoidance of doubt, except that if any license choice other
32 * than GPL or LGPL is available it will apply instead, Sun elects to
33 * use only the General Public License version 2 (GPLv2) at this time
34 * for any software where a choice of GPL license versions is made
35 * available with the language indicating that GPLv2 or any later
36 * version may be used, or where a choice of which version of the GPL
37 * is applied is otherwise unspecified.
47 * Default number of intervals we display a suggestion before moving
50 #define PT_SUGG_DEF_SLICE 3
53 * Global pointer to the current suggestion.
58 * Head of the list of suggestions.
63 * Add a new suggestion. Only one suggestion per text allowed.
66 pt_sugg_add(char *text
, int weight
, char key
, char *sb_msg
, sugg_func_t
*func
)
68 sugg_t
*new, *n
, *pos
= NULL
;
71 * Text is a required field for suggestions
78 * Creating first element
80 if ((new = calloc(1, sizeof (sugg_t
))) == NULL
)
84 new->sb_msg
= strdup(sb_msg
);
87 new->text
= strdup(text
);
98 for (n
= sugg
; n
!= NULL
; n
= n
->next
) {
99 if (strcmp(n
->text
, text
) == 0)
102 if (weight
> n
->weight
&& pos
== NULL
)
106 * Create a new element
108 if ((new = calloc(1, sizeof (sugg_t
))) == NULL
)
112 new->sb_msg
= strdup(sb_msg
);
114 new->text
= strdup(text
);
116 new->weight
= weight
;
123 * Ordering placed the new element at the end
125 for (n
= sugg
; n
->next
!= NULL
; n
= n
->next
)
134 * Ordering placed the new element at the start
142 * Ordering placed the new element somewhere in
146 new->prev
= pos
->prev
;
147 pos
->prev
->next
= new;
155 * Removes a suggestion, returning 0 if not found and 1 if so.
158 pt_sugg_remove(sugg_func_t
*func
)
163 for (n
= sugg
; n
!= NULL
; n
= n
->next
) {
164 if (n
->func
== func
) {
165 /* Removing the first element */
167 if (sugg
->next
== NULL
) {
168 /* Removing the only element */
175 if (n
->next
== NULL
) {
176 /* Removing the last element */
177 n
->prev
->next
= NULL
;
179 /* Removing an intermediate element */
180 n
->prev
->next
= n
->next
;
181 n
->next
->prev
= n
->prev
;
186 * If this suggestions is currently being suggested,
187 * remove it and update the screen.
189 if (n
== g_curr_sugg
) {
190 if (n
->sb_msg
!= NULL
) {
191 pt_display_mod_status_bar(n
->sb_msg
);
192 pt_display_status_bar();
195 pt_display_suggestions(NULL
);
207 * Chose a suggestion to display. The list of suggestions is ordered by weight,
208 * so we only worry about fariness here. Each suggestion, starting with the
209 * first (the 'heaviest') is displayed during PT_SUGG_DEF_SLICE intervals.
222 for (n
= sugg
; n
!= NULL
; n
= n
->next
) {
224 if (n
->slice
++ < PT_SUGG_DEF_SLICE
) {
227 * Don't need to re-suggest the current suggestion.
229 if (g_curr_sugg
== n
&& !g_sig_resize
)
233 * Remove the current suggestion from screen.
235 if (g_curr_sugg
!= NULL
) {
236 if (g_curr_sugg
->sb_msg
!= NULL
) {
237 pt_display_mod_status_bar(
238 g_curr_sugg
->sb_msg
);
239 pt_display_status_bar();
241 if (g_curr_sugg
->text
!= NULL
)
242 pt_display_suggestions(NULL
);
245 if (n
->sb_msg
!= NULL
) {
246 pt_display_mod_status_bar(n
->sb_msg
);
247 pt_display_status_bar();
250 pt_display_suggestions(n
->text
);
259 * All suggestions have run out of slice quotas, so we restart.
261 for (n
= sugg
; n
!= NULL
; n
= n
->next
)
268 pt_sugg_as_root(void)
270 pt_sugg_add("Suggestion: run as root to get suggestions"
271 " for reducing system power consumption", 40, NULL
, NULL
,