~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/Linux/apm.c

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>
 
5
 
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of version 2 of the GNU General Public
 
8
    License as published by the Free Software Foundation.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 
 
19
*/
 
20
 
 
21
#include <fcntl.h>
 
22
#include <stdio.h>
 
23
#include <sys/stat.h>
 
24
#include <sys/types.h>
 
25
#include <unistd.h>
 
26
 
 
27
#include "Command.h"
 
28
#include "ksysguardd.h"
 
29
 
 
30
#include "apm.h"
 
31
 
 
32
static int ApmOK = 0;
 
33
static int BatFill, BatTime;
 
34
 
 
35
#define APMBUFSIZE 128
 
36
static char ApmBuf[ APMBUFSIZE ];
 
37
static int Dirty = 0;
 
38
 
 
39
static void processApm( void )
 
40
{
 
41
  sscanf( ApmBuf, "%*f %*f %*x %*x %*x %*x %d%% %d min",
 
42
          &BatFill, &BatTime );
 
43
  Dirty = 0;
 
44
}
 
45
 
 
46
/*
 
47
================================ public part =================================
 
48
*/
 
49
 
 
50
void initApm( struct SensorModul* sm )
 
51
{
 
52
  if ( updateApm() < 0 ) {
 
53
    ApmOK = -1;
 
54
    return;
 
55
  } else
 
56
    ApmOK = 1;
 
57
 
 
58
  registerMonitor( "apm/batterycharge", "integer", printApmBatFill, printApmBatFillInfo, sm );
 
59
  registerMonitor( "apm/remainingtime", "integer", printApmBatTime, printApmBatTimeInfo, sm );
 
60
}
 
61
 
 
62
void exitApm( void )
 
63
{
 
64
  ApmOK = -1;
 
65
}
 
66
 
 
67
int updateApm( void )
 
68
{
 
69
  size_t n;
 
70
  int fd;
 
71
 
 
72
  if ( ApmOK < 0 )
 
73
    return -1;
 
74
 
 
75
  if ( ( fd = open( "/proc/apm", O_RDONLY ) ) < 0 ) {
 
76
    if ( ApmOK != 0 )
 
77
      print_error( "Cannot open file \'/proc/apm\'!\n"
 
78
                   "The kernel needs to be compiled with support\n"
 
79
                   "for /proc file system enabled!\n" );
 
80
    return -1;
 
81
  }
 
82
 
 
83
  n = read( fd, ApmBuf, APMBUFSIZE - 1 );
 
84
  if ( n == APMBUFSIZE - 1 ) {
 
85
    log_error( "Internal buffer too small to read \'/proc/apm\'" );
 
86
    close( fd );
 
87
    return -1;
 
88
  }
 
89
 
 
90
  close( fd );
 
91
  ApmBuf[ n ] = '\0';
 
92
  Dirty = 1;
 
93
 
 
94
  return 0;
 
95
}
 
96
 
 
97
void printApmBatFill( const char* cmd )
 
98
{
 
99
  (void)cmd;
 
100
 
 
101
  if ( Dirty )
 
102
    processApm();
 
103
 
 
104
  output( "%d\n", BatFill );
 
105
}
 
106
 
 
107
void printApmBatFillInfo( const char* cmd )
 
108
{
 
109
  (void)cmd;
 
110
  output( "Battery charge\t0\t100\t%%\n" );
 
111
}
 
112
 
 
113
void printApmBatTime( const char* cmd )
 
114
{
 
115
  (void)cmd;
 
116
 
 
117
  if ( Dirty )
 
118
    processApm();
 
119
 
 
120
  output( "%d\n", BatTime );
 
121
}
 
122
 
 
123
void printApmBatTimeInfo( const char* cmd )
 
124
{
 
125
  (void)cmd;
 
126
  output( "Remaining battery time\t0\t0\tmin\n" );
 
127
}