1 /* daemon.c for GP2X Version 2.0
2 Copyright (C) 2006 jannis harder
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <sys/types.h>
31 #include "gp2xminilib.h"
38 extern unsigned COLORFONDO; // background-color
39 extern unsigned WHITE;
40 extern unsigned TEXTBACK;
42 extern unsigned char cad[256];
44 extern unsigned short *gp2x_memregs;
45 extern pthread_t gp2x_sound_thread;
48 unsigned int minimal_cpu_speed, unsigned int maximal_cpu_speed, unsigned int start_cpu_speed, int cpu_speed_step,
49 unsigned long hotkey, unsigned long incrementkey, unsigned long decrementkey,
50 int speed_display, int foreground,
56 kill_running_daemon();
59 FILE * pidfile = fopen("/tmp/cpu_daemon.pid","w");
62 printf("couldn't write pidfile\r\n");
69 fprintf(pidfile,"%i\n",pid);
88 printf("daemon ready\r\n");
90 nano_setup(); // loading the full minilib would be overkill and i guess some games/emus wouldn't like it
93 unsigned int current_cpu_speed = start_cpu_speed;
97 unsigned long keystate = gp2x_joystick_read();
99 unsigned int last_cpu_speed = 0;
102 (hotkey && (keystate & hotkey) == hotkey) ||
104 (incrementkey && (keystate & incrementkey) == incrementkey) ||
105 (decrementkey && (keystate & decrementkey) == decrementkey)
108 if(foreground && !last_cpu_speed)
109 printf("cpu daemon activated!\r\n");
111 if(incrementkey && (keystate & incrementkey) == incrementkey) {
112 current_cpu_speed += cpu_speed_step;
113 while((keystate & incrementkey) == incrementkey) usleep(100000),keystate = gp2x_joystick_read();
115 else if(decrementkey && (keystate & decrementkey) == decrementkey) {
116 current_cpu_speed -= cpu_speed_step;
117 while((keystate & decrementkey) == decrementkey) usleep(100000),keystate = gp2x_joystick_read();
120 if(current_cpu_speed < minimal_cpu_speed)
121 current_cpu_speed = minimal_cpu_speed;
122 if(current_cpu_speed > maximal_cpu_speed)
123 current_cpu_speed = maximal_cpu_speed;
127 if(last_cpu_speed != current_cpu_speed) {
128 set_FCLK(current_cpu_speed);
130 last_cpu_speed = current_cpu_speed;
131 keystate = gp2x_joystick_read();
138 int kill_running_daemon() {
140 FILE * pidfile = fopen("/tmp/cpu_daemon.pid","r");
149 printf("found pidfile\r\n");
150 fgets(&(pid_buffer[5]),10,pidfile);
152 int return_code = system(pid_buffer);
154 printf("daemon wasn't running\r\n");
156 printf("killed old daemon\r\n");
157 unlink("/tmp/cpu_daemon.pid");
165 if(!gp2x_sound_thread) {
166 gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, open("/dev/mem", O_RDWR), 0xc0000000);
171 void cmd_daemon(int argc, char *argv[]) {
173 int cpu_div = get_920_Div();
174 int sysfreq=get_freq_920_CLK();
176 int cpufreq=sysfreq/1000000;
178 unsigned int minimal_value = 33;
179 unsigned int maximal_value = 260;
180 unsigned int start_value = cpufreq;
181 unsigned int step = 10;
182 unsigned long hotkey = GP2X_L | GP2X_R;
183 unsigned long downkey = GP2X_VOL_UP;
184 unsigned long upkey = GP2X_VOL_DOWN;
190 for( i = 2; i < argc; i++) {
191 if(!strcmp(argv[i],"--min")) {
192 if(i+1 == argc){printf ("%s is missing it's parameter\r\n",argv[i]);gp2x_deinit();exit(1);}
193 minimal_value = atoi(argv[i+1]);
194 if(minimal_value < 33)
197 else if(!strcmp(argv[i],"--max")) {
198 if(i+1 == argc){printf ("%s is missing it's parameter\r\n",argv[i]);gp2x_deinit();exit(1);}
199 maximal_value = atoi(argv[i+1]);
200 if(maximal_value > 340)
203 else if(!strcmp(argv[i],"--start")) {
204 if(i+1 == argc){printf ("%s is missing it's parameter\r\n",argv[i]);gp2x_deinit();exit(1);}
205 start_value = atoi(argv[i+1]);
207 else if(!strcmp(argv[i],"--step")) {
208 if(i+1 == argc){printf ("%s is missing it's parameter\r\n",argv[i]);gp2x_deinit();exit(1);}
209 step = atoi(argv[i+1]);
211 else if(!strcmp(argv[i],"--hotkey")) {
212 if(i+1 == argc){printf ("%s is missing it's parameter\r\n",argv[i]);gp2x_deinit();exit(1);}
213 hotkey = parse_key_sequence(argv[i+1]);
215 else if(!strcmp(argv[i],"--incr")) {
216 if(i+1 == argc){printf ("%s is missing it's parameter\r\n",argv[i]);gp2x_deinit();exit(1);}
217 upkey = parse_key_sequence(argv[i+1]);
219 else if(!strcmp(argv[i],"--decr")) {
220 if(i+1 == argc){printf ("%s is missing it's parameter\r\n",argv[i]);gp2x_deinit();exit(1);}
221 downkey = parse_key_sequence(argv[i+1]);
223 else if(!strcmp(argv[i],"--delay")) {
224 if(i+1 == argc){printf ("%s is missing it's parameter\r\n",argv[i]);gp2x_deinit();exit(1);}
225 delay = atof(argv[i+1]);
227 else if(!strcmp(argv[i],"--no-incr")) {
230 else if(!strcmp(argv[i],"--no-decr")) {
233 else if(!strcmp(argv[i],"--no-hotkey")) {
236 else if(!strcmp(argv[i],"--foreground")) {
239 else if(!strcmp(argv[i],"--background")) {
242 else if(!strcmp(argv[i],"--display")) {
245 else if(!strcmp(argv[i],"--no-display")) {
250 if((hotkey & downkey) == downkey)
251 printf("warning: hotkey includes decrement keypress!\r\n");
252 if((hotkey & upkey) == upkey)
253 printf("warning: hotkey includes increment keypress!\r\n");
255 int pid = start_daemon(minimal_value, maximal_value, start_value, step, hotkey, upkey, downkey, display, foreground, delay* 1000000);
258 printf("couldn't start daemon\r\n");
262 printf("daemon started\r\n");
267 unsigned long parse_key_sequence(char *key_sequence) {
268 unsigned long hotkey = 0;
269 if(!strcmp(key_sequence,"None"))
271 char *mask = key_sequence;
299 hotkey |= GP2X_VOL_DOWN;
302 hotkey |= GP2X_VOL_UP;
306 hotkey |= GP2X_START;
309 hotkey |= GP2X_SELECT;
317 printf("unknown key %c\r\n",*mask);
324 int daemonsettings[8];
330 void formatkey(char * base, unsigned long keyseq) {
347 if(keyseq & GP2X_VOL_DOWN)
349 if(keyseq & GP2X_VOL_UP)
351 if(keyseq & GP2X_START)
353 if(keyseq & GP2X_SELECT)
355 if(keyseq & GP2X_PUSH)
361 #define VALID_KEYS ((GP2X_L) | (GP2X_R) | (GP2X_X) | (GP2X_Y) | (GP2X_A) | (GP2X_B) | (GP2X_START) | (GP2X_SELECT) | (GP2X_VOL_UP) | (GP2X_VOL_DOWN) | (GP2X_PUSH) )
365 void daemon_itemhelp(int menuitem)
369 v_putcad(26,8,0xffffff,COLORFONDO,"Choose a");
370 v_putcad(26,9,0xffffff,COLORFONDO,"minimal");
371 v_putcad(26,10,0xffffff,COLORFONDO,"clockspeed");
372 v_putcad(26,11,0xffffff,COLORFONDO,"with R/L or");
373 v_putcad(26,12,0xffffff,COLORFONDO,"Vol UP/Down.");
374 v_putcad(26,14,0xffffff,COLORFONDO,"Valid speeds");
375 v_putcad(26,15,0xffffff,COLORFONDO,"are:");
376 v_putcad(26,16,0xffffff,COLORFONDO,"33 to 340Mhz");
379 v_putcad(26,8,0xffffff,COLORFONDO,"Choose a");
380 v_putcad(26,9,0xffffff,COLORFONDO,"maximal");
381 v_putcad(26,10,0xffffff,COLORFONDO,"clockspeed");
382 v_putcad(26,11,0xffffff,COLORFONDO,"with R/L or");
383 v_putcad(26,12,0xffffff,COLORFONDO,"Vol UP/Down.");
384 v_putcad(26,14,0xffffff,COLORFONDO,"Valid speeds");
385 v_putcad(26,15,0xffffff,COLORFONDO,"are:");
386 v_putcad(26,16,0xffffff,COLORFONDO,"33 to 340Mhz");
389 v_putcad(26,8,0xffffff,COLORFONDO,"Choose a step");
390 v_putcad(26,9,0xffffff,COLORFONDO,"width for");
391 v_putcad(26,10,0xffffff,COLORFONDO,"changing the");
392 v_putcad(26,11,0xffffff,COLORFONDO,"clockspeed.");
393 v_putcad(26,13,0xffffff,COLORFONDO,"Use R/L or");
394 v_putcad(26,14,0xffffff,COLORFONDO,"Vol UP/Down.");
397 v_putcad(26,8,0xffffff,COLORFONDO,"Choose a");
398 v_putcad(26,9,0xffffff,COLORFONDO,"delay between");
399 v_putcad(26,10,0xffffff,COLORFONDO,"each hotkey");
400 v_putcad(26,11,0xffffff,COLORFONDO,"check");
401 v_putcad(26,13,0xffffff,COLORFONDO,"Use R/L or");
402 v_putcad(26,14,0xffffff,COLORFONDO,"Vol UP/Down.");
405 v_putcad(26,8,0xffffff,COLORFONDO,"Choose a");
406 v_putcad(26,9,0xffffff,COLORFONDO,"hotkey.");
407 v_putcad(26,10,0xffffff,COLORFONDO,"Add or delete");
408 v_putcad(26,11,0xffffff,COLORFONDO,"a button by");
409 v_putcad(26,12,0xffffff,COLORFONDO,"pressing it.");
410 v_putcad(26,14,0x0000DD,COLORFONDO,"Joystick is");
411 v_putcad(26,15,0x0000DD,COLORFONDO,"not allowed.");
414 v_putcad(26,8,0xffffff,COLORFONDO,"Choose a");
415 v_putcad(26,9,0xffffff,COLORFONDO,"key for");
416 v_putcad(26,10,0xffffff,COLORFONDO,"incrementing");
417 v_putcad(26,11,0xffffff,COLORFONDO,"the clkspeed.");
418 v_putcad(26,12,0xffffff,COLORFONDO,"Add or delete");
419 v_putcad(26,13,0xffffff,COLORFONDO,"a button by");
420 v_putcad(26,14,0xffffff,COLORFONDO,"pressing it.");
421 v_putcad(26,16,0x0000DD,COLORFONDO,"Joystick is");
422 v_putcad(26,17,0x0000DD,COLORFONDO,"not allowed.");
425 v_putcad(26,8,0xffffff,COLORFONDO,"Choose a");
426 v_putcad(26,9,0xffffff,COLORFONDO,"key for");
427 v_putcad(26,10,0xffffff,COLORFONDO,"decrementing");
428 v_putcad(26,11,0xffffff,COLORFONDO,"the clkspeed.");
429 v_putcad(26,12,0xffffff,COLORFONDO,"Add or delete");
430 v_putcad(26,13,0xffffff,COLORFONDO,"a button by");
431 v_putcad(26,14,0xffffff,COLORFONDO,"pressing it.");
432 v_putcad(26,16,0x0000DD,COLORFONDO,"Joystick is");
433 v_putcad(26,17,0x0000DD,COLORFONDO,"not allowed.");
436 /* v_putcad(26,8,0xffffff,COLORFONDO,"Enable or");
437 v_putcad(26,9,0xffffff,COLORFONDO,"disable");
438 v_putcad(26,10,0xffffff,COLORFONDO,"on screen");
439 v_putcad(26,11,0xffffff,COLORFONDO,"display.");
440 v_putcad(26,13,0x0000DD,COLORFONDO,"May cause");
441 v_putcad(26,14,0x0000DD,COLORFONDO,"conflicts");
442 v_putcad(26,15,0x0000DD,COLORFONDO,"with");
443 v_putcad(26,16,0x0000DD,COLORFONDO,"some apps!");*/
444 v_putcad(26,8,0x0000DD,COLORFONDO,"COMING SOON");
448 v_putcad(26,8,0xffffff,COLORFONDO,"Press B to");
449 v_putcad(26,9,0xffffff,COLORFONDO,"kill the");
450 v_putcad(26,10,0xffffff,COLORFONDO,"running");
451 v_putcad(26,11,0xffffff,COLORFONDO,"daemon");
452 v_putcad(26,12,0xffffff,COLORFONDO,"process.");
455 v_putcad(26,8,0xffffff,COLORFONDO,"Press B to");
456 v_putcad(26,9,0xffffff,COLORFONDO,"start the ");
457 v_putcad(26,10,0xffffff,COLORFONDO,"daemon in the");
458 v_putcad(26,11,0xffffff,COLORFONDO, "background.");
467 running = !access("/tmp/cpu_daemon.pid",R_OK);
470 unsigned long gp2x_nKeys;
473 if(daemonsettings[0] < 33)
474 daemonsettings[0] = 33;
475 if(daemonsettings[1] > 340)
476 daemonsettings[1] = 340;
477 if(daemonsettings[1] < daemonsettings[0])
478 daemonsettings[1] = daemonsettings[0];
479 if(daemonsettings[0] > daemonsettings[1])
480 daemonsettings[0] = daemonsettings[1];
481 if(daemonsettings[2] < 1)
482 daemonsettings[2] = 1;
483 if(daemonsettings[3] < 1)
484 daemonsettings[3] = 1;
485 //if(daemonsettings[7] == 10 || daemonsettings[7] == -10)
486 // daemonsettings[7] = 1;
487 //if(daemonsettings[7] == 11 || daemonsettings[7] == -9)
488 daemonsettings[7] = 0;
493 v_putcad(13,2,WHITE,COLORFONDO,"Daemon Setup");
495 v_putcad(2,5,0xffff00,COLORFONDO,"CPU Clockspeed:");
497 sprintf(cad,"From: %huMhz",daemonsettings[0]);
499 v_putcad(2,7,0xffff,COLORFONDO,cad);
501 v_putcad(2,7,0xffff,TEXTBACK,cad);
503 sprintf(cad,"To: %huMhz",daemonsettings[1]);
505 v_putcad(2,8,0xffff,COLORFONDO,cad);
507 v_putcad(2,8,0xffff,TEXTBACK,cad);
509 sprintf(cad,"Step: %huMhz",daemonsettings[2]);
511 v_putcad(2,9,0xffff,COLORFONDO,cad);
513 v_putcad(2,9,0xffff,TEXTBACK,cad);
516 v_putcad(2,11,0xffff00,COLORFONDO,"Buttons:");
518 sprintf(cad,"Delay: %0.1fsec",daemonsettings[3]/10.0f);
520 v_putcad(2,13,0xffff,COLORFONDO,cad);
522 v_putcad(2,13,0xffff,TEXTBACK,cad);
524 sprintf(cad,"Hotkey: ");
526 formatkey(cad,daemonsettings[4]);
528 v_putcad(2,15,0xffff,COLORFONDO,cad);
530 v_putcad(2,15,0xffff,TEXTBACK,cad);
533 sprintf(cad,"IncrKey: ");
535 formatkey(cad,daemonsettings[5]);
537 v_putcad(2,16,0xffff,COLORFONDO,cad);
539 v_putcad(2,16,0xffff,TEXTBACK,cad);
541 sprintf(cad,"DecrKey: ");
543 formatkey(cad,daemonsettings[6]);
545 v_putcad(2,17,0xffff,COLORFONDO,cad);
547 v_putcad(2,17,0xffff,TEXTBACK,cad);
549 if(menupoint >= 4 && menupoint <=6)
550 v_putcad(2,26,WHITE,COLORFONDO,"---------- Stick:UP/DOWN");
552 v_putcad(2,19,0xffff00,COLORFONDO,"Misc:");
555 v_putcad(2,21,0xffff,COLORFONDO,(daemonsettings[7] ? "On Screen Display: On" : "On Screen Display: Off"));
557 v_putcad(2,21,0xffff,TEXTBACK,(daemonsettings[7] ? "On Screen Display: On" : "On Screen Display: Off"));
560 v_putcad(2,23,0xffff,COLORFONDO,(running ? "Kill Running Daemon" : "Start Daemon"));
562 v_putcad(2,23,0xffff,TEXTBACK,(running ? "Kill Running Daemon" : "Start Daemon"));
566 daemon_itemhelp(menupoint);
571 gp2x_nKeys=gp2x_joystick_read();
575 if((gp2x_nKeys & GP2X_DOWN))
578 if(menupoint>8) menupoint=0;
583 if((gp2x_nKeys & GP2X_UP))
586 if(menupoint<0) menupoint=8;
591 if((menupoint >= 4) && (menupoint <= 6) && (gp2x_nKeys & VALID_KEYS))
593 daemonsettings[menupoint] ^= (gp2x_nKeys & VALID_KEYS);
598 if(menupoint < 8 &&(gp2x_nKeys & GP2X_R))
600 daemonsettings[menupoint] += 10;
605 if(menupoint < 4 && (gp2x_nKeys & GP2X_VOL_UP))
607 daemonsettings[menupoint] -= 1;
612 if(menupoint < 4 && (gp2x_nKeys & GP2X_VOL_DOWN))
614 daemonsettings[menupoint] += 1;
619 if(menupoint < 8 && (gp2x_nKeys & GP2X_L))
621 daemonsettings[menupoint] -= 10;
625 if(menupoint == 8 && (gp2x_nKeys & GP2X_B))
628 kill_running_daemon();
630 int cpu_div = get_920_Div();
631 int sysfreq=get_freq_920_CLK();
633 int cpufreq=sysfreq/1000000;
635 start_daemon_by_settings();
638 running = !access("/tmp/cpu_daemon.pid",R_OK);
643 if((gp2x_nKeys & GP2X_START))
647 gp2x_nKeys=gp2x_joystick_read();
648 if(!(gp2x_nKeys & GP2X_START)) break;
651 if(running) { // update values!
652 start_daemon_by_settings();
663 void start_daemon_by_settings() {
664 int cpu_div = get_920_Div();
665 int sysfreq=get_freq_920_CLK();
667 int cpufreq=sysfreq/1000000;
669 start_daemon(daemonsettings[0], daemonsettings[1], cpufreq, daemonsettings[2], daemonsettings[4], daemonsettings[5],
670 daemonsettings[6], daemonsettings[7], 0, daemonsettings[3] * 100000);