"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/linux/acpitemp.cc" (11 Jul 2020, 4895 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "acpitemp.cc" see the Fossies "Dox" file reference documentation.

    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 
   46 ACPITemp::~ACPITemp( void ) {
   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 
   98 void ACPITemp::checkResources( void ) {
   99   FieldMeter::checkResources();
  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" ) );
  105   setfieldcolor( 2, _highcolor );
  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) {
  164       setfieldcolor( 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 }