⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osdcpufreq.c

📁 能够动态显示cpu工作频率的软件源代码
💻 C
字号:
/*  * OSD CPU frequency monitor (osdcpufreq) - It displays an OSD-like window * with information about the CPU current status: frequency, CPU driver, * governor... It is very useful for laptops equipped with * mobile technology processors (like the Intel Centrino), which can change the * speed or the governor on the fly. * Copyright 2005 V韈tor Mart韓ez Romanos. * web site: http://osdcpufreq.sourceforge.net * * Based on the idea of osdbattery program written by Maxim Uvarov <uvarov@hotbox.ru>. *  * This file is part of osdcpufreq. * Written by V韈tor Mart韓ez Romanos <vmromanos@gmail.com> *  * osdcpufreq is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * osdcpufreq is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *   */#include <stdio.h>#include <errno.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include "osdcpufreq.h"#include <xosd.h>#define FILE_CONF ".osdcpufreq.conf"#define CPUFREQD_ERROR "\nDo you have a processor that supports frequency scaling, like the Intel Centrino?\nDoes your kernel support CPU Frequency scaling? See zcat /proc/config.gz | grep CPU_FREQ output\nDoes your kernel support CPU frequency translation statistics? See zcat /proc/config.gz | grep CPU_FREQ_STAT output\nIs your CPUFreq driver loaded? If you have a centrino processor, as root try modprobe speedstep-centrino. If you have an AMD64 processor with Cool and Quiet enabled, try modprobe powernow-k8.\n\n"#define XOSD_ERROR "Is xosd installed?\n\n"#define CONTACT_ERROR "-->The program doesn't work properly.\n-->Please send me the error message and a short description about your computer:\n-->hardware, OS, xosd version, kernel version, etc.\n-------->to vmromanos@gmail.com (English or Spanish)<--------\n" #define version 0.3void get_cpu_info(cpufreq_info_struct *cpufreq_info, char cpu_num) {	char file[54];		FILE *fp;	sprintf(file, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu_num);	fp = fopen (file, "r");	if (fp == NULL ) {		printf("Error reading scaling_cur_freq file. ");		printf("%s",CPUFREQD_ERROR);		printf("%s",CONTACT_ERROR);		exit(1);	} else {		fscanf (fp,"%i",&(cpufreq_info->cur_freq));		fclose (fp);	}		sprintf(file, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu_num);		fp = fopen (file, "r");	if (fp == NULL ) {		printf("Error reading cpuinfo_max_freq file. ");		printf("%s",CPUFREQD_ERROR);		printf("%s",CONTACT_ERROR);		exit(1);	} else {		fscanf (fp,"%i",&(cpufreq_info->max_freq));		fclose (fp);	}			sprintf(file, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu_num);	fp = fopen (file, "r");	if (fp == NULL ) {		printf("Error reading cpuinfo_min_freq file. ");		printf("%s",CPUFREQD_ERROR);		printf("%s",CONTACT_ERROR);		exit(1);	} else {		fscanf (fp,"%i",&(cpufreq_info->min_freq));		fclose (fp);	}		sprintf(file, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", cpu_num);	fp = fopen (file, "r");	if (fp == NULL ) {		printf("Error reading scaling_driver file. ");		printf("%s",CPUFREQD_ERROR);		printf("%s",CONTACT_ERROR);		exit(1);	} else {		fscanf (fp,"%s",(char *)&(cpufreq_info->driver));		fclose (fp);	}	sprintf(file, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpu_num);	fp = fopen (file, "r");	if (fp == NULL ) {		printf("Error reading scaling_driver file ");		printf("%s",CPUFREQD_ERROR);		printf("%s",CONTACT_ERROR);		exit(1);	} else {		fscanf (fp,"%s",(char *)&(cpufreq_info->governor));		fclose (fp);	}}void configure(config_struct *config) {	char line[256];	char value[64];	char *the_home;	char config_file[48];	int chars_number, i;	FILE *fp;		/* getting user home directory */	the_home =getenv("HOME");	sprintf(config_file, "%s/%s",the_home,FILE_CONF);	/* Load default values. If user doesn't set properly one or more values, 	 * or if no config file found, I use program default values  */	config->cpu_num = 0;	config->align = 2;	config->vertical_offset = 5;	config->horizontal_offset = 5;	strncpy(config->font, "-misc-fixed-medium-r-semicondensed--*-*-*-*-c-*-*-*", 63);	strncpy(config->colour, "green", 63);	config->shadow_offset = 1;	strncpy(config->shadow_colour, "black", 63);	config->bar_length = 52;	config->on_time = 3;	config->refresh = 1;		fp = fopen(config_file,"r");	if (fp == NULL) {		printf("WARNING: no config file found at %s, using default settings.\n\n", config_file);	}	else {		while (!feof(fp)) {			/* get a line of the config file */			if (fgets(line, 256, fp) == NULL) {				fclose(fp);				break;			}			/* Delete ' ' chars at the start of a line */			while (*line==' ') {				chars_number=strlen(line);				for (i=0;i<=chars_number;i++)					*(line+i)=*(line+i+1);			}			/* set config values from the config file */				if (*line!='#') { //uncommented lines				if (sscanf(line, "display cpu = %s", value) != errno) 					config->cpu_num = atoi(value);								if (sscanf(line, "align = %s", value) != errno) 					config->align = atoi(value);				if (sscanf(line, "vertical offset = %s", value) != errno) 					config->vertical_offset = atoi(value);									if (sscanf(line, "horizontal offset = %s", value) != errno) 					config->horizontal_offset = atoi(value);					if (sscanf(line, "font = %s", value) != errno) 					strcpy(config->font, value);				if (sscanf(line, "colour = %s", value) != errno)					strcpy(config->colour, value);				if (sscanf(line, "shadow offset = %s", value) != errno) 					config->shadow_offset = atoi(value);				if (sscanf(line, "shadow colour = %s", value) != errno) 					strcpy(config->shadow_colour, value);				if (sscanf(line, "bar length = %s", value) != errno) 					config->bar_length = atoi(value);				if (sscanf(line, "on time = %s", value) != errno) 					config->on_time = atoi(value);					if (sscanf(line, "refresh = %s", value) != errno) 					config->refresh = atoi(value);		//	printf("%s",line); //Debug info: show cleared config file			}		}	 }}void strup(char *cadena) {	char i=0;	do {		if ( (*(cadena+i)>='a') && (*(cadena+i)<='z') )        		*(cadena+i)-=32;		i++;        } while (*(cadena+i)!=0);}int main () {	cpufreq_info_struct *cpufreq_info;	config_struct *config;	char freq_perc;	/* OSD drawing vars */	xosd *osd = NULL;	char *string;	/* Parse configuration (file or default values) */	config=malloc(sizeof(config_struct));	if (config == NULL) {		printf("Error: malloc failed. Can't allocate dinamic memory for config.\n");		printf ("%s", CONTACT_ERROR);		exit(1);	}	configure(config);		/* Allocate dinamic memory for cpufreq_info */	cpufreq_info=malloc(sizeof(cpufreq_info_struct));	if (cpufreq_info==NULL) {		printf ("Error: malloc failed. Can't allocate dinamic memory for cpufreq_info.\n");		printf ("%s", CONTACT_ERROR);		exit(1);	}				/* create OSD object */	osd = xosd_create(3);	if (osd == NULL) {		printf("Error creating xosd object.\n");		printf ("%s", XOSD_ERROR);		printf ("%s", CONTACT_ERROR);		exit(1);	}	xosd_set_font(osd, config->font);	xosd_set_align (osd, config->align);	xosd_set_vertical_offset (osd, config->vertical_offset);	xosd_set_horizontal_offset (osd, config->horizontal_offset);	xosd_set_bar_length(osd, config->bar_length);;	xosd_set_colour(osd, config->colour);	xosd_set_shadow_colour(osd, config->shadow_colour);	xosd_set_shadow_offset(osd, config->shadow_offset);	string=(char *)malloc(64);	do {		/* getting cpu info */		get_cpu_info(cpufreq_info,config->cpu_num);		freq_perc=(((cpufreq_info->cur_freq)-(cpufreq_info->min_freq)))*100/((cpufreq_info->max_freq)-(cpufreq_info->min_freq));		/* Display CPU Current freq */		sprintf(string, "CPU current frequency: %dMHz", cpufreq_info->cur_freq/1000);		xosd_display (osd, 0, XOSD_string, string);		/* Display CPU freq percent bar */		xosd_display (osd, 1, XOSD_percentage, freq_perc);		/* Display CPU general info */		strup(cpufreq_info->driver);		strup(cpufreq_info->governor);		sprintf(string, "driver: %s  governor: %s", cpufreq_info->driver, cpufreq_info->governor);		xosd_display (osd, 2, XOSD_string, string);		/* Wait */		switch (config->on_time) {			case 0: sleep(config->refresh); //always on top				break;			default: sleep(config->on_time);				 break;		}		xosd_hide(osd);	} while (config->on_time==0); /* Always on top? */					/* Clear memory */	free (string);	free (config);	free (cpufreq_info);			return (0); //Bye ;-)}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -