[c5c522c] | 1 | /* |
---|
| 2 | * This file contains status-gathering code *and* modescreen functions. |
---|
| 3 | * It's long, and messy. |
---|
| 4 | * |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | #include <stdlib.h> |
---|
| 8 | #include <stdio.h> |
---|
| 9 | #include <string.h> |
---|
| 10 | #ifdef IRIX |
---|
| 11 | #include <strings.h> |
---|
| 12 | #endif |
---|
| 13 | #include <fcntl.h> |
---|
| 14 | #include <unistd.h> |
---|
| 15 | #include <time.h> |
---|
| 16 | #include <sys/time.h> |
---|
| 17 | #include <sys/utsname.h> |
---|
| 18 | |
---|
| 19 | #include "../../shared/sockets.h" |
---|
| 20 | |
---|
| 21 | #include "mode.h" |
---|
| 22 | #include "main.h" |
---|
| 23 | |
---|
| 24 | #include "batt.h" |
---|
| 25 | #include "chrono.h" |
---|
| 26 | #include "cpu.h" |
---|
| 27 | #include "disk.h" |
---|
| 28 | #include "load.h" |
---|
| 29 | #include "mem.h" |
---|
| 30 | #include "net.h" |
---|
| 31 | |
---|
| 32 | int ELLIPSIS='-'; |
---|
| 33 | |
---|
| 34 | // TODO: Clean this up... Support multiple display sizes.. |
---|
| 35 | |
---|
| 36 | char buffer[1024]; |
---|
| 37 | char host[16]; |
---|
| 38 | |
---|
| 39 | void reread(int f, char *errmsg); |
---|
| 40 | int getentry(const char *tag, const char *bufptr); |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | int mode_init(mode *sequence) |
---|
| 44 | { |
---|
| 45 | int i; |
---|
| 46 | struct utsname Uname; |
---|
| 47 | |
---|
| 48 | // Grab the host name |
---|
| 49 | if(0 > uname(&Uname)) |
---|
| 50 | { |
---|
| 51 | perror("Can't get hostname"); |
---|
| 52 | strcpy(host, "host"); |
---|
| 53 | } |
---|
| 54 | else |
---|
| 55 | { |
---|
| 56 | strncpy(host, Uname.nodename, 15); |
---|
| 57 | host[16]=0; |
---|
| 58 | if(index(host,'.')) // truncate at the first dot |
---|
| 59 | *(index(host, '.'))='\0'; |
---|
| 60 | for(i=0; i<16; i++) |
---|
| 61 | if(host[i] == '\n') host[i]=0; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | // Init all the modescreens we need |
---|
| 65 | for(i=0; sequence[i].which > 1; i++) |
---|
| 66 | { |
---|
| 67 | switch(sequence[i].which) |
---|
| 68 | { |
---|
| 69 | case 'g': |
---|
| 70 | case 'G': // cpu_graph_screen |
---|
| 71 | cpu_init(); |
---|
| 72 | break; |
---|
| 73 | case 'n': |
---|
| 74 | case 'N': // cpu_graph_screen |
---|
| 75 | net_init(); |
---|
| 76 | break; |
---|
| 77 | case 'c': |
---|
| 78 | case 'C': // cpu_screen |
---|
| 79 | cpu_init(); |
---|
| 80 | break; |
---|
| 81 | case 'o': |
---|
| 82 | case 'O': // clock_screen |
---|
| 83 | chrono_init(); |
---|
| 84 | break; |
---|
| 85 | case 'k': |
---|
| 86 | case 'K': // big_clock_screen |
---|
| 87 | chrono_init(); |
---|
| 88 | break; |
---|
| 89 | case 'm': |
---|
| 90 | case 'M': // mem_screen |
---|
| 91 | mem_init(); |
---|
| 92 | break; |
---|
| 93 | case 's': |
---|
| 94 | case 'S': // mem_info_screen |
---|
| 95 | mem_init(); |
---|
| 96 | break; |
---|
| 97 | case 'u': |
---|
| 98 | case 'U': // uptime_screen |
---|
| 99 | chrono_init(); |
---|
| 100 | break; |
---|
| 101 | case 't': |
---|
| 102 | case 'T': // time_screen |
---|
| 103 | chrono_init(); |
---|
| 104 | break; |
---|
| 105 | case 'd': |
---|
| 106 | case 'D': // disk_screen |
---|
| 107 | disk_init(); |
---|
| 108 | break; |
---|
| 109 | case 'x': |
---|
| 110 | case 'X': // xload_screen |
---|
| 111 | load_init(); |
---|
| 112 | break; |
---|
| 113 | case 'b': |
---|
| 114 | case 'B': // battery_screen |
---|
| 115 | batt_init(); |
---|
| 116 | break; |
---|
| 117 | case 'a': |
---|
| 118 | case 'A': // credit_screen |
---|
| 119 | break; |
---|
| 120 | default: break; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | return 0; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void mode_close() |
---|
| 130 | { |
---|
| 131 | batt_close(); |
---|
| 132 | chrono_close(); |
---|
| 133 | cpu_close(); |
---|
| 134 | disk_close(); |
---|
| 135 | load_close(); |
---|
| 136 | mem_close(); |
---|
| 137 | |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | int update_screen(mode *m, int display) |
---|
| 142 | { |
---|
| 143 | static int status = -1; |
---|
| 144 | int old_status = status; |
---|
| 145 | |
---|
| 146 | if(m) |
---|
| 147 | { |
---|
| 148 | switch(m->which) |
---|
| 149 | { |
---|
| 150 | case 'g': |
---|
| 151 | case 'G': status = cpu_graph_screen(m->timer, display); break; |
---|
| 152 | case 'n': |
---|
| 153 | case 'N': status = net_screen(m->timer, display); break; |
---|
| 154 | case 'c': |
---|
| 155 | case 'C': status = cpu_screen(m->timer, display); break; |
---|
| 156 | case 'o': |
---|
| 157 | case 'O': status = clock_screen(m->timer, display); break; |
---|
| 158 | case 'k': |
---|
| 159 | case 'K': status = big_clock_screen(m->timer, display); break; |
---|
| 160 | case 'm': |
---|
| 161 | case 'M': status = mem_screen(m->timer, display); break; |
---|
| 162 | case 's': |
---|
| 163 | case 'S': status = mem_top_screen(m->timer, display); break; |
---|
| 164 | case 'u': |
---|
| 165 | case 'U': status = uptime_screen(m->timer, display); break; |
---|
| 166 | case 't': |
---|
| 167 | case 'T': status = time_screen(m->timer, display); break; |
---|
| 168 | case 'd': |
---|
| 169 | case 'D': status = disk_screen(m->timer, display); break; |
---|
| 170 | case 'x': |
---|
| 171 | case 'X': status = xload_screen(m->timer, display); break; |
---|
| 172 | case 'b': |
---|
| 173 | case 'B': status = battery_screen(m->timer, display); break; |
---|
| 174 | case 'a': |
---|
| 175 | case 'A': status = credit_screen(m->timer, display); break; |
---|
| 176 | default: break; |
---|
| 177 | } |
---|
| 178 | /* Debugging tool... |
---|
| 179 | if(display) |
---|
| 180 | { |
---|
| 181 | printf("Displayed Mode %c\n", m->which); |
---|
| 182 | } |
---|
| 183 | */ |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | if(status != old_status) |
---|
| 187 | { |
---|
| 188 | if(status == BACKLIGHT_OFF) |
---|
| 189 | { |
---|
| 190 | sock_send_string(sock, "backlight off\n"); |
---|
| 191 | } |
---|
| 192 | if(status == BACKLIGHT_ON) |
---|
| 193 | { |
---|
| 194 | sock_send_string(sock, "backlight on\n"); |
---|
| 195 | } |
---|
| 196 | if(status == BLINK_ON) |
---|
| 197 | { |
---|
| 198 | sock_send_string(sock, "backlight blink\n"); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | return status; |
---|
| 202 | |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | void reread(int f, char *errmsg) |
---|
| 207 | { |
---|
| 208 | if (lseek(f, 0L, 0) == 0 && read(f, buffer, sizeof(buffer) - 1 ) > 0 ) |
---|
| 209 | return; |
---|
| 210 | perror(errmsg); |
---|
| 211 | exit(1); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | int getentry(const char *tag, const char *bufptr) |
---|
| 215 | { |
---|
| 216 | char *tail; |
---|
| 217 | int retval, len = strlen(tag); |
---|
| 218 | |
---|
| 219 | while (bufptr) |
---|
| 220 | { |
---|
| 221 | if (*bufptr == '\n') bufptr++; |
---|
| 222 | if (!strncmp(tag, bufptr, len)) |
---|
| 223 | { |
---|
| 224 | retval = strtol(bufptr + len, &tail, 10); |
---|
| 225 | if (tail == bufptr + len) return -1; |
---|
| 226 | else return retval; |
---|
| 227 | } |
---|
| 228 | bufptr = strchr( bufptr, '\n'); |
---|
| 229 | } |
---|
| 230 | return -1; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | /////////////////////////////////////////////////////////////////////////// |
---|
| 235 | //////////////////////// Let the Modes Begin! ///////////////////////////// |
---|
| 236 | /////////////////////////////////////////////////////////////////////////// |
---|
| 237 | |
---|
| 238 | // Er, these have been moved.. :) |
---|
| 239 | |
---|
| 240 | char tmp[1024]; |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | //////////////////////////////////////////////////////////////////////// |
---|
| 246 | // Credit Screen shows who wrote this... |
---|
| 247 | // |
---|
| 248 | int credit_screen(int rep, int display) |
---|
| 249 | { |
---|
| 250 | static int first = 1; |
---|
| 251 | |
---|
| 252 | if(first) |
---|
| 253 | { |
---|
| 254 | first = 0; |
---|
| 255 | |
---|
| 256 | sock_send_string(sock, "screen_add A\n"); |
---|
| 257 | sock_send_string(sock, |
---|
| 258 | "screen_set A name {Credits for LCDproc}\n"); |
---|
| 259 | sock_send_string(sock, "widget_add A title title\n"); |
---|
| 260 | sprintf(tmp, "widget_set A title {LCDPROC %s}\n", version); |
---|
| 261 | sock_send_string(sock, tmp); |
---|
| 262 | if(lcd_hgt >= 4) |
---|
| 263 | { |
---|
| 264 | sock_send_string(sock, "widget_add A one string\n"); |
---|
| 265 | sock_send_string(sock, "widget_add A two string\n"); |
---|
| 266 | sock_send_string(sock, "widget_add A three string\n"); |
---|
| 267 | sock_send_string(sock, |
---|
| 268 | "widget_set A one 1 2 { for Linux }\n"); |
---|
| 269 | sock_send_string(sock, |
---|
| 270 | "widget_set A two 1 3 { by William Ferrell}\n"); |
---|
| 271 | sock_send_string(sock, |
---|
| 272 | "widget_set A three 1 4 { and Scott Scriven}\n"); |
---|
| 273 | } |
---|
| 274 | else |
---|
| 275 | { |
---|
| 276 | sock_send_string(sock, "widget_add A text scroller\n"); |
---|
| 277 | sock_send_string(sock, |
---|
| 278 | "widget_set A text 1 2 20 2 v 8 { for Linux by William Ferrell and Scott Scriven}\n"); |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | return 0; |
---|
| 283 | } // End credit_screen() |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | |
---|