• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2007, Intel Corporation
3  *
4  * This file is part of PowerTOP
5  *
6  * This program file is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program in a file named COPYING; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301 USA
20  *
21  * Authors:
22  * 	Arjan van de Ven <arjan@linux.intel.com>
23  */
24 
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32 
33 #include "powertop.h"
34 
35 
36 
37 char suggestion_key;
38 suggestion_func *suggestion_activate;
39 
40 struct suggestion;
41 
42 struct suggestion {
43 	struct suggestion *next;
44 
45 	char *string;
46 	int weight;
47 
48 	char key;
49 	char *keystring;
50 
51 	suggestion_func *func;
52 };
53 
54 
55 static struct suggestion *suggestions;
56 static int total_weight;
57 
58 static char previous[1024];
59 
60 
reset_suggestions(void)61 void reset_suggestions(void)
62 {
63 	struct suggestion *ptr;
64 	ptr = suggestions;
65 	while (ptr) {
66 		struct suggestion *next;
67 		next = ptr->next;
68 		free(ptr->string);
69 		free(ptr->keystring);
70 		free(ptr);
71 		ptr = next;
72 	}
73 	suggestions = NULL;
74 	strcpy(status_bar_slots[8],"");
75 	suggestion_key = 255;
76 	suggestion_activate = NULL;
77 	total_weight = 0;
78 }
79 
add_suggestion(char * text,int weight,char key,char * keystring,suggestion_func * func)80 void add_suggestion(char *text, int weight, char key, char *keystring, suggestion_func *func)
81 {
82 	struct suggestion *new;
83 
84 	if (!text)
85 		return;
86 
87 	new = malloc(sizeof(struct suggestion));
88 	if (!new)
89 		return;
90 	memset(new, 0, sizeof(struct suggestion));
91 	new->string = strdup(text);
92 	new->weight = weight;
93 	new->key = key;
94 	if (keystring)
95 		new->keystring = strdup(keystring);
96 	new->next = suggestions;
97 	new->func = func;
98 	suggestions = new;
99 	total_weight += weight;
100 }
101 
pick_suggestion(void)102 void pick_suggestion(void)
103 {
104 	int value, running = 0;
105 	struct suggestion *ptr;
106 	int weight;
107 
108 	strcpy(status_bar_slots[8],"");
109 	suggestion_key = 255;
110 	suggestion_activate = NULL;
111 
112 	if (total_weight==0 || suggestions==NULL) {
113 		/* zero suggestions */
114 		show_suggestion("");
115 		return;
116 	}
117 
118 	weight = total_weight;
119 	if (strlen(previous) && displaytime > 0.0)
120 		weight+=50;
121 	value = rand() % weight;
122 	ptr = suggestions;
123 	while (ptr) {
124 		running += ptr->weight;
125 		if (strcmp(ptr->string, previous)==0 && displaytime > 0.0)
126 			running += 50;
127 		if (running > value) {
128 			if (ptr->keystring)
129 				strncpy(status_bar_slots[8],ptr->keystring, 40);
130 			suggestion_key = ptr->key;
131 			suggestion_activate = ptr->func;
132 			show_suggestion(ptr->string);
133 			if (strcmp(ptr->string, previous)) {
134 				displaytime = 30.0;
135 				strcpy(previous, ptr->string);
136 			}
137 			return;
138 		}
139 		ptr = ptr->next;
140 	}
141 	show_suggestion("");
142 	memset(previous, 0, sizeof(previous));
143 	displaytime = -1.0;
144 }
145 
print_all_suggestions(void)146 void print_all_suggestions(void)
147 {
148 	struct suggestion *ptr;
149 
150 	for (ptr = suggestions; ptr; ptr = ptr->next)
151 		printf("\n%s\n", ptr->string);
152 }
153