Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 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 | |
Arjan van der Ven | b43056b | 2007-05-24 20:47:42 +0000 | [diff] [blame] | 37 | char suggestion_key; |
| 38 | suggestion_func *suggestion_activate; |
| 39 | |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 40 | struct suggestion; |
| 41 | |
| 42 | struct suggestion { |
| 43 | struct suggestion *next; |
| 44 | |
| 45 | char *string; |
| 46 | int weight; |
| 47 | |
Arjan van der Ven | b43056b | 2007-05-24 20:47:42 +0000 | [diff] [blame] | 48 | char key; |
| 49 | char *keystring; |
| 50 | |
| 51 | suggestion_func *func; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | |
| 55 | static struct suggestion *suggestions; |
| 56 | static int total_weight; |
| 57 | |
Arjan van der Ven | 2b7db8e | 2007-06-23 19:03:34 +0000 | [diff] [blame] | 58 | static char previous[1024]; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 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); |
Arjan van der Ven | b43056b | 2007-05-24 20:47:42 +0000 | [diff] [blame] | 69 | free(ptr->keystring); |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 70 | free(ptr); |
| 71 | ptr = next; |
| 72 | } |
| 73 | suggestions = NULL; |
Arjan van der Ven | fcddf6e | 2007-06-23 05:33:16 +0000 | [diff] [blame] | 74 | strcpy(status_bar_slots[8],""); |
Arjan van der Ven | b5e41e5 | 2007-05-26 21:40:15 +0000 | [diff] [blame] | 75 | suggestion_key = 255; |
| 76 | suggestion_activate = NULL; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 77 | total_weight = 0; |
| 78 | } |
| 79 | |
Arjan van der Ven | b43056b | 2007-05-24 20:47:42 +0000 | [diff] [blame] | 80 | void add_suggestion(char *text, int weight, char key, char *keystring, suggestion_func *func) |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 81 | { |
| 82 | struct suggestion *new; |
| 83 | |
Arjan van der Ven | ef0e566 | 2007-05-24 21:10:36 +0000 | [diff] [blame] | 84 | if (!text) |
| 85 | return; |
| 86 | |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 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; |
Arjan van der Ven | 58156ad | 2007-05-24 21:04:04 +0000 | [diff] [blame] | 94 | if (keystring) |
| 95 | new->keystring = strdup(keystring); |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 96 | new->next = suggestions; |
Arjan van der Ven | b43056b | 2007-05-24 20:47:42 +0000 | [diff] [blame] | 97 | new->func = func; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 98 | suggestions = new; |
| 99 | total_weight += weight; |
| 100 | } |
| 101 | |
| 102 | void pick_suggestion(void) |
| 103 | { |
| 104 | int value, running = 0; |
| 105 | struct suggestion *ptr; |
Arjan van der Ven | 2b7db8e | 2007-06-23 19:03:34 +0000 | [diff] [blame] | 106 | int weight; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 107 | |
Arjan van der Ven | fcddf6e | 2007-06-23 05:33:16 +0000 | [diff] [blame] | 108 | strcpy(status_bar_slots[8],""); |
Arjan van der Ven | b43056b | 2007-05-24 20:47:42 +0000 | [diff] [blame] | 109 | suggestion_key = 255; |
| 110 | suggestion_activate = NULL; |
| 111 | |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 112 | if (total_weight==0 || suggestions==NULL) { |
| 113 | /* zero suggestions */ |
| 114 | show_suggestion(""); |
| 115 | return; |
| 116 | } |
| 117 | |
Arjan van der Ven | 2b7db8e | 2007-06-23 19:03:34 +0000 | [diff] [blame] | 118 | weight = total_weight; |
| 119 | if (strlen(previous) && displaytime > 0.0) |
| 120 | weight+=50; |
| 121 | value = rand() % weight; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 122 | ptr = suggestions; |
| 123 | while (ptr) { |
| 124 | running += ptr->weight; |
Arjan van der Ven | 2b7db8e | 2007-06-23 19:03:34 +0000 | [diff] [blame] | 125 | if (strcmp(ptr->string, previous)==0 && displaytime > 0.0) |
| 126 | running += 50; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 127 | if (running > value) { |
Arjan van der Ven | ef0e566 | 2007-05-24 21:10:36 +0000 | [diff] [blame] | 128 | if (ptr->keystring) |
Arjan van der Ven | 8fb611c | 2007-08-18 22:03:50 +0000 | [diff] [blame] | 129 | strncpy(status_bar_slots[8],ptr->keystring, 40); |
Arjan van der Ven | b43056b | 2007-05-24 20:47:42 +0000 | [diff] [blame] | 130 | suggestion_key = ptr->key; |
| 131 | suggestion_activate = ptr->func; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 132 | show_suggestion(ptr->string); |
Arjan van der Ven | 2b7db8e | 2007-06-23 19:03:34 +0000 | [diff] [blame] | 133 | if (strcmp(ptr->string, previous)) { |
| 134 | displaytime = 30.0; |
| 135 | strcpy(previous, ptr->string); |
| 136 | } |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | ptr = ptr->next; |
| 140 | } |
Arjan van der Ven | 2b7db8e | 2007-06-23 19:03:34 +0000 | [diff] [blame] | 141 | show_suggestion(""); |
| 142 | memset(previous, 0, sizeof(previous)); |
| 143 | displaytime = -1.0; |
Arjan van der Ven | 7087dfa | 2007-05-24 18:24:41 +0000 | [diff] [blame] | 144 | } |
Arjan van der Ven | fcddf6e | 2007-06-23 05:33:16 +0000 | [diff] [blame] | 145 | |
Arjan van der Ven | 0bddd72 | 2007-08-31 17:24:07 +0000 | [diff] [blame] | 146 | void print_all_suggestions(void) |
| 147 | { |
| 148 | struct suggestion *ptr; |
Arjan van der Ven | fcddf6e | 2007-06-23 05:33:16 +0000 | [diff] [blame] | 149 | |
Arjan van der Ven | 0bddd72 | 2007-08-31 17:24:07 +0000 | [diff] [blame] | 150 | for (ptr = suggestions; ptr; ptr = ptr->next) |
| 151 | printf("\n%s\n", ptr->string); |
| 152 | } |