xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

acpitemp.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009 by Tomi Tapper <tomi.o.tapper@jyu.fi>
3 //
4 // File based on lmstemp.* by
5 // Copyright (c) 2000, 2006 by Leopold Toetsch <lt@toetsch.at>
6 //
7 // This file may be distributed under terms of the GPL
8 //
9 //
10 //
11 
12 #include "acpitemp.h"
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <dirent.h>
19 #include <fstream>
20 #include <iostream>
21 #include <string>
22 
23 static const char PROC_ACPI_TZ[] = "/proc/acpi/thermal_zone";
24 static const char SYS_ACPI_TZ[] = "/sys/devices/virtual/thermal";
25 
26 
27 ACPITemp::ACPITemp( XOSView *parent, const char *tempfile, const char *highfile, const char *label, const char *caption )
28 : FieldMeter( parent, 3, label, caption, 1, 1, 0 ) {
29  metric_ = true;
30  if (!checkacpi(tempfile, highfile)) {
31  std::cerr << "Can not find file : ";
32  if (tempfile[0] == '/' && highfile[0] == '/')
33  std::cerr << tempfile << " or " << highfile;
34  else if (tempfile[0] == '/')
35  std::cerr << tempfile << ", or " << highfile << " under " << PROC_ACPI_TZ << " or " << SYS_ACPI_TZ;
36  else if (highfile[0] == '/')
37  std::cerr << tempfile << " under " << PROC_ACPI_TZ << " or " << SYS_ACPI_TZ << ", or " << highfile;
38  else
39  std::cerr << tempfile << " or " << highfile << " under " << PROC_ACPI_TZ << " or " << SYS_ACPI_TZ;
40  std::cerr << "." << std::endl;
41  parent_->done(1);
42  }
43  _high = 0;
44 }
45 
47 
48 }
49 
50 int ACPITemp::checkacpi( const char *tempfile, const char *highfile ) {
51  struct stat buf;
52  char temp[PATH_SIZE], high[PATH_SIZE];
53  bool temp_found = false, high_found = false;
54 
55  if (tempfile[0] == '/') {
56  if ( stat(tempfile, &buf) == 0 && S_ISREG(buf.st_mode) )
57  temp_found = true;
58  else
59  return false;
60  }
61  if (highfile[0] == '/') {
62  if ( stat(highfile, &buf) == 0 && S_ISREG(buf.st_mode) )
63  high_found = true;
64  else
65  return false;
66  }
67 
68  if (temp_found && high_found) {
69  strncpy(_tempfile, tempfile, PATH_SIZE);
70  strncpy(_highfile, highfile, PATH_SIZE);
71  return true;
72  }
73 
74  snprintf(temp, PATH_SIZE, "%s/%s", SYS_ACPI_TZ, tempfile);
75  snprintf(high, PATH_SIZE, "%s/%s", SYS_ACPI_TZ, highfile);
76 
77  if ( (stat(temp, &buf) == 0 && S_ISREG(buf.st_mode)) &&
78  (stat(high, &buf) == 0 && S_ISREG(buf.st_mode)) ) {
79  strncpy(_tempfile, temp, PATH_SIZE);
80  strncpy(_highfile, high, PATH_SIZE);
81  _usesysfs = true;
82  return true;
83  }
84 
85  _usesysfs = false;
86  snprintf(temp, PATH_SIZE, "%s/%s", PROC_ACPI_TZ, tempfile);
87  snprintf(high, PATH_SIZE, "%s/%s", PROC_ACPI_TZ, highfile);
88 
89  if ( (stat(temp, &buf) == 0 && S_ISREG(buf.st_mode)) &&
90  (stat(high, &buf) == 0 && S_ISREG(buf.st_mode)) ) {
91  strncpy(_tempfile, temp, PATH_SIZE);
92  strncpy(_highfile, high, PATH_SIZE);
93  return true;
94  }
95  return false;
96 }
97 
100 
101  _actcolor = parent_->allocColor( parent_->getResource( "acpitempActColor" ) );
102  _highcolor = parent_->allocColor( parent_->getResource( "acpitempHighColor" ) );
103  setfieldcolor( 0, _actcolor );
104  setfieldcolor( 1, parent_->getResource( "acpitempIdleColor" ) );
106  total_ = atoi( parent_->getResourceOrUseDefault( "acpitempHighest", "100" ) );
107  priority_ = atoi( parent_->getResource( "acpitempPriority" ) );
108  SetUsedFormat( parent_->getResource( "acpitempUsedFormat" ) );
109 }
110 
111 void ACPITemp::checkevent( void ) {
112  getacpitemp();
113  drawfields();
114 }
115 
116 void ACPITemp::getacpitemp( void ) {
117  std::ifstream temp_file(_tempfile);
118  std::ifstream high_file(_highfile);
119 
120  if (!temp_file) {
121  std::cerr << "Can not open file : " << _tempfile << std::endl;
122  parent_->done(1);
123  return;
124  }
125  if (!high_file) {
126  std::cerr << "Can not open file : " << _highfile << std::endl;
127  parent_->done(1);
128  return;
129  }
130 
131  float high;
132  std::string dummy;
133  bool do_legend = false;
134 
135  if (_usesysfs) {
136  high_file >> high;
137  high /= 1000.0;
138  temp_file >> fields_[0];
139  fields_[0] /= 1000.0;
140  }
141  else {
142  high_file >> dummy >> dummy >> high;
143  temp_file >> dummy >> fields_[0];
144  }
145 
146  if (high > total_ || high != _high) {
147  char l[16];
148  if (high > total_)
149  total_ = 10 * (int)((high * 1.25) / 10);
150  _high = high;
151  snprintf(l, 16, "ACT(\260C)/%d/%d", (int)high, (int)total_);
152  legend(l);
153  do_legend = true;
154  }
155 
156  setUsed( fields_[0], total_ );
157  if (fields_[0] < 0)
158  fields_[0] = 0.0;
159 
160  fields_[1] = high - fields_[0];
161  if (fields_[1] < 0) { // alarm: T > high
162  fields_[1] = 0;
163  if (colors_[0] != _highcolor) {
165  do_legend = true;
166  }
167  }
168  else {
169  if (colors_[0] != _actcolor) {
170  setfieldcolor( 0, _actcolor );
171  do_legend = true;
172  }
173  }
174 
175  fields_[2] = total_ - fields_[1] - fields_[0];
176  if (fields_[2] < 0)
177  fields_[2] = 0;
178 
179  if (do_legend)
180  drawlegend();
181 }
static const char SYS_ACPI_TZ[]
Definition: acpitemp.cc:24
static const char PROC_ACPI_TZ[]
Definition: acpitemp.cc:23
#define PATH_SIZE
Definition: acpitemp.h:18
int _high
Definition: acpitemp.h:37
void checkResources(void)
Definition: acpitemp.cc:98
unsigned long _actcolor
Definition: acpitemp.h:39
ACPITemp(XOSView *parent, const char *tempfile, const char *highfile, const char *label, const char *caption)
Definition: acpitemp.cc:27
char _tempfile[128]
Definition: acpitemp.h:35
int checkacpi(const char *tempfile, const char *highfile)
Definition: acpitemp.cc:50
void getacpitemp(void)
Definition: acpitemp.cc:116
unsigned long _highcolor
Definition: acpitemp.h:39
bool _usesysfs
Definition: acpitemp.h:38
~ACPITemp(void)
Definition: acpitemp.cc:46
char _highfile[128]
Definition: acpitemp.h:36
void checkevent(void)
Definition: acpitemp.cc:111
double total_
Definition: fieldmeter.h:41
virtual void checkResources(void)
Definition: fieldmeter.cc:54
void setfieldcolor(int field, const char *color)
Definition: fieldmeter.cc:108
bool metric_
Definition: fieldmeter.h:48
void drawlegend(void)
Definition: fieldmeter.cc:136
void setUsed(double val, double total)
Definition: fieldmeter.cc:76
virtual void drawfields(int mandatory=0)
Definition: fieldmeter.cc:221
double * fields_
Definition: fieldmeter.h:40
unsigned long * colors_
Definition: fieldmeter.h:43
void SetUsedFormat(const char *const str)
Definition: fieldmeter.cc:60
int priority_
Definition: meter.h:54
const char * legend(void)
Definition: meter.h:27
XOSView * parent_
Definition: meter.h:52
unsigned long allocColor(const char *name)
Definition: xwin.cc:383
const char * getResourceOrUseDefault(const char *name, const char *defaultVal)
Definition: xwin.cc:351
int done(void)
Definition: xwin.h:29
const char * getResource(const char *name)
Definition: xwin.cc:362