File: window.c

package info (click to toggle)
procmeter3 3.6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,444 kB
  • sloc: ansic: 16,600; makefile: 428; sh: 13
file content (272 lines) | stat: -rw-r--r-- 5,393 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***************************************
  $Header: /home/amb/CVS/procmeter3/log/window.c,v 1.3 2003-04-12 13:05:41 amb Exp $

  ProcMeter - A system monitoring program for Linux - Version 3.3c.

  Log to a file output.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1997,98,99,2000,02 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/


#include <stdlib.h>
#include <stdio.h>

#include <sys/time.h>

#include "procmeter.h"
#include "procmeterp.h"

/* Local functions */

static void output_labels(void);
static void output_data(time_t now);

/*+ A list of the outputs that are currently visible. +*/
static Output *displayed=NULL;
static int ndisplayed=0;
static short *new_data=NULL;
static short *graph_data=NULL;
static char **text_data=NULL;

/*+ A flag to indicate that we are still initialising and not to output anything. +*/
static int initialising=1;


/*++++++++++++++++++++++++++++++++++++++
  Start the logging to a file.

  int *argc The number of command line arguments.

  char **argv The actual command line arguments.
  ++++++++++++++++++++++++++++++++++++++*/

void Start(int *argc,char **argv)
{

 /* Do nothing since we have nothing to initialise. */

}


/*++++++++++++++++++++++++++++++++++++++
  Stop the logging to a file.
  ++++++++++++++++++++++++++++++++++++++*/

void Stop(void)
{

 /* Do nothing since we have nothing to finalise. */

}


/*++++++++++++++++++++++++++++++++++++++
  Sleep for the specified interval in seconds.

  time_t until The time to sleep until.
  ++++++++++++++++++++++++++++++++++++++*/

void Sleep(time_t until)
{
 struct timeval now;
 struct timespec delay;

 /* Before we sleep the first time, output the labels. */

 if(initialising)
   {
    output_labels();
    initialising=0;
   }
 else if(!displayed[0]->first)
    output_data(until);

 /* Sleep */

 gettimeofday(&now,NULL);

 delay.tv_sec=until-now.tv_sec-1;
 delay.tv_nsec=1000*(1000000-now.tv_usec);

 if(delay.tv_sec>=0)
   {
    nanosleep(&delay,NULL);
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Add or remove an output

  Output output The output to be added or removed.
  ++++++++++++++++++++++++++++++++++++++*/

void AddRemoveOutput(Output output)
{
 if(output->output_widget)
   {
    int i,found=0;

    output->output_widget=NULL;

    for(i=0;i<ndisplayed;i++)
       if(displayed[i]==output)
          found=1;
       else if(found)
          displayed[i-1]=displayed[i];
    ndisplayed--;
   }
 else
   {
    output->output_widget=(void*)1;
    output->first=2;

    displayed=(Output*)realloc((void*)displayed,sizeof(Output)*(ndisplayed+1));
    displayed[ndisplayed]=output;
    ndisplayed++;

    new_data=(short*)realloc((void*)new_data,sizeof(short)*ndisplayed);
    graph_data=(short*)realloc((void*)graph_data,sizeof(short)*ndisplayed);
    text_data=(char**)realloc((void*)text_data,sizeof(char*)*ndisplayed);
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Update a graph output.

  Output output The output to update.

  short value The new value.
  ++++++++++++++++++++++++++++++++++++++*/

void UpdateGraph(Output output,short value)
{
 int i;

 for(i=0;i<ndisplayed;i++)
    if(displayed[i]==output)
      {
       new_data[i]=1;
       graph_data[i]=value;
       break;
      }
}


/*++++++++++++++++++++++++++++++++++++++
  Update a text output.

  Output output The output to update.

  char *value The new value.
  ++++++++++++++++++++++++++++++++++++++*/

void UpdateText(Output output,char *value)
{
 int i;

 for(i=0;i<ndisplayed;i++)
    if(displayed[i]==output)
      {
       new_data[i]=2;
       text_data[i]=value;
       break;
      }
}


/*++++++++++++++++++++++++++++++++++++++
  Update a bar output.

  Output output The output to update.

  short value The new value.
  ++++++++++++++++++++++++++++++++++++++*/

void UpdateBar(Output output,short value)
{
 int i;

 for(i=0;i<ndisplayed;i++)
    if(displayed[i]==output)
      {
       new_data[i]=1;
       graph_data[i]=value;
       break;
      }
}


/*++++++++++++++++++++++++++++++++++++++
  Output the labels for the selected outputs.
  ++++++++++++++++++++++++++++++++++++++*/

static void output_labels(void)
{
 int i;

 printf("UNIX Time");

 for(i=0;i<ndisplayed;i++)
   {
    Output *outputp=NULL;
    Module *modulep,module=NULL;

    for(modulep=Modules;*modulep;modulep++)
      {
       for(outputp=(*modulep)->outputs;*outputp;outputp++)
          if(displayed[i]==*outputp)
            {
             module=*modulep;
             break;
            }
       if(module)
          break;
      }

    printf("\t");

    printf("%s.%s",module->module->name,displayed[i]->output->name);
   }

 printf("\n");

 fflush(stdout);
}


/*++++++++++++++++++++++++++++++++++++++
  Output the data for the selected outputs.

  time_t now The current time.
  ++++++++++++++++++++++++++++++++++++++*/

static void output_data(time_t now)
{
 int i;

 printf("%ld",(long)now);

 for(i=0;i<ndisplayed;i++)
   {
    printf("\t");

    if(new_data[i]==1)
       printf("%d",graph_data[i]);
    else if(new_data[i]==2)
       printf("%s",text_data[i]);

    new_data[i]=0;
   }

 printf("\n");

 fflush(stdout);
}