blob: c5639bb31bdb119080efcef68e3b41b7e7e3f289 [file] [log] [blame]
Arjan van der Ven7087dfa2007-05-24 18:24:41 +00001/*
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
Arjan van der Venb43056b2007-05-24 20:47:42 +000037char suggestion_key;
38suggestion_func *suggestion_activate;
39
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000040struct suggestion;
41
42struct suggestion {
43 struct suggestion *next;
44
45 char *string;
46 int weight;
47
Arjan van der Venb43056b2007-05-24 20:47:42 +000048 char key;
49 char *keystring;
50
51 suggestion_func *func;
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000052};
53
54
55static struct suggestion *suggestions;
56static int total_weight;
57
Arjan van der Ven2b7db8e2007-06-23 19:03:34 +000058static char previous[1024];
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000059
60
61void 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);
Arjan van der Venb43056b2007-05-24 20:47:42 +000069 free(ptr->keystring);
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000070 free(ptr);
71 ptr = next;
72 }
73 suggestions = NULL;
Arjan van der Venfcddf6e2007-06-23 05:33:16 +000074 strcpy(status_bar_slots[8],"");
Arjan van der Venb5e41e52007-05-26 21:40:15 +000075 suggestion_key = 255;
76 suggestion_activate = NULL;
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000077 total_weight = 0;
78}
79
Arjan van der Venb43056b2007-05-24 20:47:42 +000080void add_suggestion(char *text, int weight, char key, char *keystring, suggestion_func *func)
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000081{
82 struct suggestion *new;
83
Arjan van der Venef0e5662007-05-24 21:10:36 +000084 if (!text)
85 return;
86
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000087 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;
Arjan van der Ven58156ad2007-05-24 21:04:04 +000094 if (keystring)
95 new->keystring = strdup(keystring);
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000096 new->next = suggestions;
Arjan van der Venb43056b2007-05-24 20:47:42 +000097 new->func = func;
Arjan van der Ven7087dfa2007-05-24 18:24:41 +000098 suggestions = new;
99 total_weight += weight;
100}
101
102void pick_suggestion(void)
103{
104 int value, running = 0;
105 struct suggestion *ptr;
Arjan van der Ven2b7db8e2007-06-23 19:03:34 +0000106 int weight;
Arjan van der Ven7087dfa2007-05-24 18:24:41 +0000107
Arjan van der Venfcddf6e2007-06-23 05:33:16 +0000108 strcpy(status_bar_slots[8],"");
Arjan van der Venb43056b2007-05-24 20:47:42 +0000109 suggestion_key = 255;
110 suggestion_activate = NULL;
111
Arjan van der Ven7087dfa2007-05-24 18:24:41 +0000112 if (total_weight==0 || suggestions==NULL) {
113 /* zero suggestions */
114 show_suggestion("");
115 return;
116 }
117
Arjan van der Ven2b7db8e2007-06-23 19:03:34 +0000118 weight = total_weight;
119 if (strlen(previous) && displaytime > 0.0)
120 weight+=50;
121 value = rand() % weight;
Arjan van der Ven7087dfa2007-05-24 18:24:41 +0000122 ptr = suggestions;
123 while (ptr) {
124 running += ptr->weight;
Arjan van der Ven2b7db8e2007-06-23 19:03:34 +0000125 if (strcmp(ptr->string, previous)==0 && displaytime > 0.0)
126 running += 50;
Arjan van der Ven7087dfa2007-05-24 18:24:41 +0000127 if (running > value) {
Arjan van der Venef0e5662007-05-24 21:10:36 +0000128 if (ptr->keystring)
Arjan van der Ven8fb611c2007-08-18 22:03:50 +0000129 strncpy(status_bar_slots[8],ptr->keystring, 40);
Arjan van der Venb43056b2007-05-24 20:47:42 +0000130 suggestion_key = ptr->key;
131 suggestion_activate = ptr->func;
Arjan van der Ven7087dfa2007-05-24 18:24:41 +0000132 show_suggestion(ptr->string);
Arjan van der Ven2b7db8e2007-06-23 19:03:34 +0000133 if (strcmp(ptr->string, previous)) {
134 displaytime = 30.0;
135 strcpy(previous, ptr->string);
136 }
Arjan van der Ven7087dfa2007-05-24 18:24:41 +0000137 return;
138 }
139 ptr = ptr->next;
140 }
Arjan van der Ven2b7db8e2007-06-23 19:03:34 +0000141 show_suggestion("");
142 memset(previous, 0, sizeof(previous));
143 displaytime = -1.0;
Arjan van der Ven7087dfa2007-05-24 18:24:41 +0000144}
Arjan van der Venfcddf6e2007-06-23 05:33:16 +0000145
Arjan van der Ven0bddd722007-08-31 17:24:07 +0000146void print_all_suggestions(void)
147{
148 struct suggestion *ptr;
Arjan van der Venfcddf6e2007-06-23 05:33:16 +0000149
Arjan van der Ven0bddd722007-08-31 17:24:07 +0000150 for (ptr = suggestions; ptr; ptr = ptr->next)
151 printf("\n%s\n", ptr->string);
152}