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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
/***************************************
$Header: /home/amb/procmeter/RCS/xwindow.c 1.14 1999/03/06 14:42:03 amb Exp $
ProcMeter - A performance metering/logging program for Linux - Version 2.5.1.
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1997,98,99 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 <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/SmeLine.h>
#include <X11/Xaw/Paned.h>
#include "ProcMeter.h"
#include "procmeter.h"
static void AddRemoveMeterCallback(Widget widget,XtPointer clientData,XtPointer callData);
static void SleepCallback(XtPointer p,XtIntervalId i);
static void ResizePaneCallback(Widget w,XtPointer va,XEvent* e,Boolean* vb);
static void CloseCallback(Widget w,XtPointer va,XEvent* e,Boolean* vb);
static XtAppContext app_context;/*+ The application context. +*/
static Display* display; /*+ The display that the meter is on. +*/
static Widget pane, /*+ The widget for the pane containing the meters. +*/
menu; /*+ The widget for the menu. +*/
static Dimension length; /*+ The length of the pane. +*/
static Widget meters_p[NProcStats], /*+ The list of /proc meter Widgets. +*/
*meters_o, /*+ The list of other meter Widgets. +*/
menus_p[NProcStats], /*+ The list of /proc menu Widgets. +*/
*menus_o; /*+ The list of other menu Widgets. +*/
static int sleeping; /*+ Set to true when we are sleeping waiting for a timeout. +*/
static int n_windows=0; /*+ The number of meter windows. +*/
/*+ A flag that is set to true when we are told to quit. +*/
int quit=0;
/*+ If the meters are aligned vertically. +*/
int vertical=1;
/*+ The name of the host that we are running on. +*/
extern char HostName[40];
/*++++++++++++++++++++++++++++++++++++++
Start the X-Windows part.
int *argc The number of command line arguments.
char **argv The actual command line arguments.
++++++++++++++++++++++++++++++++++++++*/
void StartX(int *argc,char **argv)
{
Atom close_atom;
Widget toplevel,menushell;
char hostlabel[81];
int i;
if(NOtherStats)
{
menus_o =(Widget*)malloc((NOtherStats+1)*sizeof(Widget));
meters_o=(Widget*)malloc((NOtherStats+1)*sizeof(Widget));
}
if(*HostName)
strcpy(hostlabel,HostName);
else
strcpy(hostlabel,"ProcMeter");
/* Initialise the display */
toplevel=XtVaAppInitialize(&app_context,"ProcMeter",
NULL,(Cardinal)0,argc,argv,NULL,
XtNtitle,"ProcMeter V2.5.1",
XtNiconName,hostlabel,
NULL);
display=XtDisplay(toplevel);
/* Create the pane widget */
pane=XtVaCreateManagedWidget("pane",panedWidgetClass,toplevel,
XtNwidth, vertical?100:200,
XtNheight,vertical?200:100,
XtNorientation,vertical?XtorientVertical:XtorientHorizontal,
NULL);
XtVaGetValues(pane,vertical?XtNwidth:XtNheight,&length,NULL);
XtAddEventHandler(pane,StructureNotifyMask,False,(XtEventHandler)ResizePaneCallback,NULL);
/* Create the menu widgets */
if(!vertical)
{
int i=strlen(hostlabel);
hostlabel[2*i-1]=0;
for(i--;i>0;i--)
{hostlabel[i*2]=hostlabel[i];hostlabel[i*2-1]='\n';}
}
menu=XtVaCreateManagedWidget("menu",menuButtonWidgetClass,pane,
XtNlabel,hostlabel,
XtNmenuName,"menushell",
XtNallowResize,False,
XtNresizeToPreferred,True,
XtNskipAdjust,True,
XtNshowGrip,False,
vertical?XtNwidth:XtNheight,length,
NULL);
menushell=XtCreatePopupShell("menushell",simpleMenuWidgetClass,menu,NULL,0);
for(i=0;i<NProcStats;i++)
if(ProcStats[i].avail)
{
menus_p[i]=XtVaCreateManagedWidget(ProcStats[i].name,smeBSBObjectClass,menushell,
XtNlabel,ProcStats[i].name,
XtNleftMargin,10,XtNheight,10,
NULL);
XtAddCallback(menus_p[i],XtNcallback,AddRemoveMeterCallback,(XtPointer)i);
}
for(i=1;i<=NOtherStats;i++)
if(OtherStats[i].avail)
{
if(i==1 || OtherStats[i].file!=OtherStats[i-1].file)
XtVaCreateManagedWidget("spacer",smeLineObjectClass,menushell,
XtNheight,5,
NULL);
menus_o[i]=XtVaCreateManagedWidget(OtherStats[i].name,smeBSBObjectClass,menushell,
XtNlabel,OtherStats[i].name,
XtNleftMargin,10,XtNheight,10,
NULL);
XtAddCallback(menus_o[i],XtNcallback,AddRemoveMeterCallback,(XtPointer)-i);
}
/* Show the widgets */
XtRealizeWidget(toplevel);
XFlush(display);
/* Put an action on the close button */
close_atom=XInternAtom(display,"WM_DELETE_WINDOW",False);
XSetWMProtocols(display,XtWindow(toplevel),&close_atom,1);
XtAddEventHandler(toplevel,0,True,CloseCallback,NULL);
}
/*++++++++++++++++++++++++++++++++++++++
Stop the X-Windows part.
++++++++++++++++++++++++++++++++++++++*/
void StopX(void)
{
XCloseDisplay(display);
}
/*++++++++++++++++++++++++++++++++++++++
Update the X-Windows display.
++++++++++++++++++++++++++++++++++++++*/
void UpdateMetersX(void)
{
int i;
for(i=0;i<NProcStats;i++)
if(ProcStats[i].used&2)
{
double value=ProcStats[i].value/(double)ProcStats[i].scale;
ProcMeterWidgetAddDatum(meters_p[i],(unsigned short)(ProcMeterWidgetScale*value));
}
for(i=1;i<=NOtherStats;i++)
if(OtherStats[i].used&2)
{
double value=OtherStats[i].value/(double)OtherStats[i].scale;
ProcMeterWidgetAddDatum(meters_o[i],(unsigned short)(ProcMeterWidgetScale*value));
}
}
/*++++++++++++++++++++++++++++++++++++++
Called when a meter is to be added or removed from the list of those displayed.
int type The type of meter to add or remove, +ve for a /proc one, -ve otherwise.
++++++++++++++++++++++++++++++++++++++*/
void AddRemoveMeterX(int type)
{
static Pixmap marker=(Pixmap)0;
if(!marker)
{
GC tempgc;
XGCValues gcxval;
marker=XCreatePixmap(XtDisplay(pane),RootWindowOfScreen(XtScreen(pane)),10,10,1);
gcxval.foreground=0;
gcxval.background=0;
tempgc=XCreateGC(XtDisplay(pane),marker,GCForeground|GCBackground,&gcxval);
XFillRectangle(XtDisplay(pane),marker,tempgc,0,0,10,10);
XSetForeground(XtDisplay(pane),tempgc,(unsigned long)~0);
XFillArc(XtDisplay(pane),marker,tempgc,1,1,7,7,0,360*64);
XFreeGC(XtDisplay(pane),tempgc);
}
if(type<0)
{
if(OtherStats[-type].used)
{
char label[20];
sprintf(label,"%s (%s)",OtherStats[-type].name,OtherStats[-type].units);
meters_o[-type]=XtVaCreateManagedWidget(OtherStats[-type].name,procMeterWidgetClass,pane,
vertical?XtNwidth:XtNheight,length,
XtNlabel,label,
XtNallowResize,True,
XtNshowGrip,False,
NULL);
XtVaSetValues(menus_o[-type],XtNleftBitmap,marker,NULL);
}
else
{
XtDestroyWidget(meters_o[-type]);
XtVaSetValues(menus_o[-type],XtNleftBitmap,NULL,NULL);
}
if(OtherStats[-type].used)
n_windows++;
else
n_windows--;
}
else
{
if(ProcStats[type].used)
{
char label[20];
sprintf(label,"%s (%s)",ProcStats[type].name,ProcStats[type].units);
meters_p[type]=XtVaCreateManagedWidget(ProcStats[type].name,procMeterWidgetClass,pane,
vertical?XtNwidth:XtNheight,length,
XtNlabel,label,
XtNallowResize,True,
XtNshowGrip,False,
NULL);
XtVaSetValues(menus_p[type],XtNleftBitmap,marker,NULL);
}
else
{
XtDestroyWidget(meters_p[type]);
XtVaSetValues(menus_p[type],XtNleftBitmap,NULL,NULL);
}
if(ProcStats[type].used)
n_windows++;
else
n_windows--;
}
ResizePaneCallback(NULL,NULL,NULL,NULL);
}
/*++++++++++++++++++++++++++++++++++++++
Sleep for the specified interval in seconds.
time_t until The time to sleep until.
++++++++++++++++++++++++++++++++++++++*/
void SleepX(time_t until)
{
XtIntervalId id;
struct timeval now;
int delay;
gettimeofday(&now,NULL);
delay=1000*(until-now.tv_sec)-now.tv_usec/1000;
if(delay>0)
{
id=XtAppAddTimeOut(app_context,(unsigned)delay,(XtTimerCallbackProc)SleepCallback,NULL);
sleeping=1;
while(sleeping)
{
struct timeval now2;
XtAppProcessEvent(app_context,XtIMAll);
gettimeofday(&now2,NULL);
if(now2.tv_sec<now.tv_sec) /* Ooops, we went back in time. Let's cancel timer */
{
XtRemoveTimeOut(id);
sleeping=0;
}
}
}
}
/*++++++++++++++++++++++++++++++++++++++
Called when a menu item is chosen.
Widget widget The widget causing the callback.
XtPointer clientData The type to change.
XtPointer callData Not used.
++++++++++++++++++++++++++++++++++++++*/
static void AddRemoveMeterCallback(Widget widget,XtPointer clientData,XtPointer callData)
{
int type=(int)clientData;
if(type<0)
OtherStats[-type].used=!OtherStats[-type].used;
else
ProcStats[type].used=!ProcStats[type].used;
AddRemoveMeterX(type);
}
/*++++++++++++++++++++++++++++++++++++++
The function called by the timeout to terminate the sleep.
XtPointer p Not used.
XtIntervalId i Not used.
++++++++++++++++++++++++++++++++++++++*/
static void SleepCallback(XtPointer p,XtIntervalId i)
{
sleeping=0;
}
/*++++++++++++++++++++++++++++++++++++++
A callback that is activated by a resize event on the parent pane.
Widget w The widget that caused the callback.
XtPointer va Not used.
XEvent* e The event that requires action.
Boolean* vb Not used.
This function is only ever called from the Xt Intrinsics routines.
++++++++++++++++++++++++++++++++++++++*/
static void ResizePaneCallback(Widget w,XtPointer va,XEvent* e,Boolean* vb)
{
Dimension fsize,msize,size;
int i;
XtVaGetValues(pane,vertical?XtNwidth:XtNheight,&length,NULL);
if(!n_windows)
return;
XtVaGetValues(pane,vertical?XtNheight:XtNwidth,&fsize,NULL);
XtVaGetValues(menu,vertical?XtNheight:XtNwidth,&msize,NULL);
size=(int)(fsize-msize)/n_windows;
XtVaSetValues(menu,vertical?XtNwidth:XtNheight,length,vertical?XtNheight:XtNwidth,msize,NULL);
for(i=0;i<NProcStats;i++)
if(ProcStats[i].used)
XtVaSetValues(meters_p[i],vertical?XtNwidth:XtNheight,length,vertical?XtNheight:XtNwidth,size,NULL);
for(i=1;i<=NOtherStats;i++)
if(OtherStats[i].used)
XtVaSetValues(meters_o[i],vertical?XtNwidth:XtNheight,length,vertical?XtNheight:XtNwidth,size,NULL);
}
/*++++++++++++++++++++++++++++++++++++++
A callback that is activated by a close window event on the toplevel window.
Widget w The widget that caused the callback.
XtPointer va Not used.
XEvent* e The event that requires action.
Boolean* vb Not used.
This function is only ever called from the Xt Intrinsics routines.
++++++++++++++++++++++++++++++++++++++*/
static void CloseCallback(Widget w,XtPointer va,XEvent* e,Boolean* vb)
{
XClientMessageEvent *cev=(XClientMessageEvent*)e;
Atom atom_type=XInternAtom(display,"WM_PROTOCOLS",False);
if(atom_type==cev->message_type)
{
Atom atom_proto=XInternAtom(display,"WM_DELETE_WINDOW",False);
if(cev->format==32 && atom_proto==(Atom)cev->data.l[0])
quit=1;
}
}
|