suggestions.c revision 9711:d309660fd989
1/*
2 * Copyright 2009, Intel Corporation
3 * Copyright 2009, Sun Microsystems, Inc
4 *
5 * This file is part of PowerTOP
6 *
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.
10 *
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
14 * for more details.
15 *
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
21 *
22 * Authors:
23 *      Arjan van de Ven <arjan@linux.intel.com>
24 *      Eric C Saxe <eric.saxe@sun.com>
25 *      Aubrey Li <aubrey.li@intel.com>
26 */
27
28/*
29 * GPL Disclaimer
30 *
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.
38 */
39
40#include <unistd.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include "powertop.h"
45
46struct suggestion;
47
48struct suggestion {
49	struct suggestion *next;
50
51	char 	*string;
52	int	weight;
53	char 	key;
54	char 	*keystring;
55
56	suggestion_func *func;
57};
58
59static struct suggestion 	*suggestions;
60static int 			total_weight;
61
62static char 	previous[1024];
63
64void
65reset_suggestions(void)
66{
67	struct suggestion *ptr;
68
69	ptr = suggestions;
70
71	while (ptr) {
72		struct suggestion *next;
73
74		next = ptr->next;
75		free(ptr->string);
76		free(ptr->keystring);
77		free(ptr);
78		ptr = next;
79	}
80
81	suggestions = NULL;
82	(void) strcpy(g_status_bar_slots[8], "");
83
84	g_suggestion_key 	= -1;
85	g_suggestion_activate 	= NULL;
86	total_weight 		= 0;
87}
88
89void
90add_suggestion(char *text, int weight, char key, char *keystring,
91    suggestion_func *func)
92{
93	struct suggestion *new;
94
95	if (!text)
96		return;
97
98	new = malloc(sizeof (struct suggestion));
99
100	if (!new)
101		return;
102
103	(void) memset(new, 0, sizeof (struct suggestion));
104
105	new->string = strdup(text);
106	new->weight = weight;
107	new->key = key;
108
109	if (keystring)
110		new->keystring = strdup(keystring);
111
112	new->next 	= suggestions;
113	new->func 	= func;
114	suggestions 	= new;
115	total_weight 	+= weight;
116}
117
118void
119pick_suggestion(void)
120{
121	int			weight, value, running = 0;
122	struct suggestion 	*ptr;
123
124	(void) strcpy(g_status_bar_slots[8], "");
125	g_suggestion_key 	= -1;
126	g_suggestion_activate 	= NULL;
127
128	if (total_weight == 0 || suggestions == NULL) {
129		show_suggestion("");
130		return;
131	}
132
133	weight = total_weight;
134
135	if (strlen(previous) && g_displaytime > 0.0)
136		weight += 50;
137
138	value 	= rand() % weight;
139	ptr 	= suggestions;
140
141	while (ptr) {
142		running += ptr->weight;
143
144		if (strcmp(ptr->string, previous) == 0 && g_displaytime > 0.0)
145			running += 50;
146
147		if (running > value) {
148			if (ptr->keystring)
149				(void) strncpy(g_status_bar_slots[8],
150				    ptr->keystring, PT_BAR_LENGTH);
151
152			g_suggestion_key 	= ptr->key;
153			g_suggestion_activate 	= ptr->func;
154
155			show_suggestion(ptr->string);
156
157			if (strcmp(ptr->string, previous)) {
158				g_displaytime = 30.0;
159				(void) strcpy(previous, ptr->string);
160			}
161			return;
162		}
163		ptr = ptr->next;
164	}
165
166	show_suggestion("");
167	(void) memset(previous, 0, sizeof (previous));
168	g_displaytime = -1.0;
169}
170
171void
172print_all_suggestions(void)
173{
174	struct suggestion *ptr;
175
176	for (ptr = suggestions; ptr; ptr = ptr->next)
177		(void) printf("\n%s\n", ptr->string);
178}
179