Minor cleanups
[meego-developer-tools:powertop.git] / suggestions.c
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
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         total_weight = 0;
76 }
77
78 void reset_suggestions2(void)
79 {
80         suggestion_key = 255;
81         suggestion_activate = NULL;
82 }
83
84 void add_suggestion(char *text, int weight, char key, char *keystring, suggestion_func *func)
85 {
86         struct suggestion *new;
87
88         if (!text)
89                 return;
90
91         new = malloc(sizeof(struct suggestion));
92         if (!new)
93                 return;
94         memset(new, 0, sizeof(struct suggestion));
95         new->string = strdup(text);
96         new->weight = weight;
97         new->key = key;
98         if (keystring)
99                 new->keystring = strdup(keystring);
100         new->next = suggestions;
101         new->func = func;
102         suggestions = new;
103         total_weight += weight;
104 }
105
106 void pick_suggestion(void)
107 {
108         int value, running = 0;
109         struct suggestion *ptr;
110         int weight;
111
112         strcpy(status_bar_slots[8],"");
113         suggestion_key = 255;
114         suggestion_activate = NULL;
115
116         if (total_weight==0 || suggestions==NULL) {
117                 /* zero suggestions */
118                 show_suggestion("");
119                 return;
120         }
121
122         weight = total_weight;
123         if (strlen(previous) && displaytime > 0.0)
124                 weight+=50;
125         value = rand() % weight;
126         ptr = suggestions;
127         while (ptr) {
128                 running += ptr->weight;
129                 if (strcmp(ptr->string, previous)==0 && displaytime > 0.0)
130                         running += 50;
131                 if (running > value) {
132                         if (ptr->keystring)
133                                 strncpy(status_bar_slots[8],ptr->keystring, 40);
134                         suggestion_key = ptr->key;
135                         suggestion_activate = ptr->func;
136                         show_suggestion(ptr->string);
137                         if (strcmp(ptr->string, previous)) {
138                                 displaytime = 30.0;
139                                 strcpy(previous, ptr->string);
140                         }
141                         return;
142                 }
143                 ptr = ptr->next;
144         }
145         show_suggestion("");
146         memset(previous, 0, sizeof(previous));
147         displaytime = -1.0;
148 }
149
150 void print_all_suggestions(void)
151 {
152         struct suggestion *ptr;
153
154         for (ptr = suggestions; ptr; ptr = ptr->next)
155                 printf("\n%s\n", ptr->string);
156 }