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
|
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Vendor.h>
#include <X11/xpm.h>
#include <stdio.h>
#include "SindicatorP.h"
#define SINDC_WIDTH (37)
#define SINDC_HEIGHT (14)
#define sDisp XtDisplay(iw)
#define sScreen XtScreen(iw)
#define sRoot XRootWindowOfScreen(XtScreen(iw))
#define sWidth iw->core.width
#define sHeight iw->core.height
#define sPixmap iw->sindicator.pixmap
#define sBGC iw->sindicator.gc[0]
#define sGC(i) iw->sindicator.gc[i]
static char* colors[] =
{
"#282828", /* dark gray */
"#00287A", /* dark blue */
"#00A7FF", /* blue */
"#0C5B00", /* dark green */
"#49DC00", /* green */
"#615600", /* dark yellow */
"#EBD000", /* yellow */
"#613D00", /* dark orange */
"#EB9C00", /* orange */
"#620000", /* dark red */
"#E10000", /* red */
};
static struct st_meter {
long threshold;
int color;
} meter[] = {
{ 0, 1}, { 1, 1}, { 2, 1}, { 3, 1},
{ 4, 1}, { 5, 1}, { 7, 1},
{ 10, 3}, { 13, 3}, { 19, 3}, { 26, 3},
{ 37, 3}, { 51, 3}, { 71, 3},
{ 100, 5}, { 138, 5}, { 193, 5}, { 268, 5},
{ 372, 5}, { 517, 5}, { 719, 5},
{ 1000, 7}, { 1389, 7}, { 1930, 7}, { 2682, 7},
{ 3727, 7}, { 5179, 7}, { 7196, 7},
{10000, 9}, {13894, 9}, {19306, 9}, {26827, 9},
{37275, 9}, {51794, 9}, {71968, 9},
};
#define offset(field) XtOffsetOf(SindicatorRec, field)
static XtResource resources[] = {
{ XtNinRate, XtCInRate, XtRInt, sizeof(int),
offset(sindicator.in_rate), XtRImmediate, (XtPointer)0 },
{ XtNoutRate, XtCOutRate, XtRInt, sizeof(int),
offset(sindicator.out_rate), XtRImmediate, (XtPointer)0 },
};
#undef offset
static void Initialize();
static void Resize();
static void Redisplay();
static Boolean SetValues();
static void Destroy();
static XtGeometryResult QueryGeometry();
static char defaultTranslation[] = "";
static XtActionsRec actions[] = {{NULL, NULL}};
static void ShowSindicator();
SindicatorClassRec sindicatorClassRec = {
{
/* superclass */ (WidgetClass) &coreClassRec,
/* class_name */ "Sindicator",
/* widget_size */ sizeof(SindicatorRec),
/* class_initialize */ NULL,
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize */ Initialize,
/* initialize_hook */ NULL,
/* realize */ XtInheritRealize,
/* actions */ actions,
/* num_actions */ XtNumber(actions),
/* resources */ resources,
/* num_resourece */ XtNumber(resources),
/* xrm_class */ NULLQUARK,
/* compress_motion */ TRUE,
/* compress_exposure */ TRUE,
/* compress_enterleave */ TRUE,
/* visible_interest */ FALSE,
/* destroy */ Destroy,
/* resize */ Resize,
/* expose */ Redisplay,
/* set_values */ SetValues,
/* set_values_hook */ NULL,
/* set_values_almoset */ XtInheritSetValuesAlmost,
/* get_values_hook */ NULL,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback_private */ NULL,
/* tm_table */ defaultTranslation,
/* query_geometry */ QueryGeometry,
/* display_accelator */ XtInheritDisplayAccelerator,
/* extention */ NULL
},{
/* foo */ 0
}
};
WidgetClass sindicatorWidgetClass = (WidgetClass)&sindicatorClassRec;
static GC
GetGC(SindicatorWidget iw, XColor fg)
{
XGCValues values;
XtGCMask mask;
values.foreground = fg.pixel;
values.background = iw->core.background_pixel;
values.graphics_exposures = False;
mask = GCForeground | GCBackground | GCGraphicsExposures;
return XCreateGC(sDisp, sPixmap, mask, &values);
}
static void
GetAllGC(SindicatorWidget iw)
{
int i;
for(i=0; i<11; i++) {
sGC(i) = GetGC(iw, iw->sindicator.color[i]);
}
}
static void
GetColor(SindicatorWidget iw)
{
XColor exact;
Colormap cmap;
int i;
cmap=DefaultColormapOfScreen(sScreen);
for(i=0; i<11; i++) {
XAllocNamedColor(sDisp, cmap, colors[i],
&(iw->sindicator.color[i]), &exact);
}
}
static void
Initialize(Widget wreq, Widget wnew)
{
SindicatorWidget iw = (SindicatorWidget) wnew;
Pixmap Mask;
XpmAttributes Attr;
if (sWidth == 0) {
sWidth = SINDC_WIDTH;
}
if (sHeight == 0) {
sHeight = SINDC_HEIGHT;
}
sPixmap = XCreatePixmap(sDisp, sRoot, sWidth, sHeight,
DefaultDepthOfScreen(sScreen));
GetColor(iw);
GetAllGC(iw);
XFillRectangle(sDisp, sPixmap, sBGC, 0, 0, sWidth, sHeight);
ShowSindicator(iw);
}
static void
Redisplay(Widget w, XExposeEvent *event)
{
SindicatorWidget iw = (SindicatorWidget) w;
int x,y;
int width, height;
if (XtIsRealized(w)) {
if (event) {
x = event->x;
y = event->y;
width = event->width;
height = event->height;
} else {
x = 0;
y = 0;
width = sWidth;
height = sHeight;
}
XCopyArea(sDisp, sPixmap, XtWindow(iw), sBGC,
x, y, width, height, x, y);
}
}
static Boolean
SetValues(Widget wcur, Widget wreq, Widget wnew)
{
SindicatorWidget cur = (SindicatorWidget) wcur;
SindicatorWidget new = (SindicatorWidget) wnew;
Boolean redraw = False;
if (cur->sindicator.in_rate != new->sindicator.in_rate ||
cur->sindicator.out_rate != new->sindicator.out_rate) {
ShowSindicator(new);
}
return redraw;
}
static void
Destroy(Widget w)
{
SindicatorWidget iw = (SindicatorWidget) w;
Colormap cmap;
int i;
cmap=DefaultColormapOfScreen(sScreen);
if (sPixmap)
XFreePixmap(sDisp, sPixmap);
for(i=0; i<11; i++) {
if (sGC(i)) XFreeGC(sDisp, sGC(i));
XFreeColors(sDisp, cmap,
&iw->sindicator.color[i].pixel, 1, 0);
}
}
static void Resize(Widget w)
{
SindicatorWidget iw = (SindicatorWidget) w;
return;
}
static XtGeometryResult
QueryGeometry(Widget w, XtWidgetGeometry *proposed, XtWidgetGeometry *answer)
{
SindicatorWidget iw = (SindicatorWidget) w;
answer->request_mode = CWWidth | CWHeight;
answer->width = sWidth;
answer->height = sHeight;
return XtGeometryAlmost;
}
static void
indicate(SindicatorWidget iw, long num, long old, long threshold,
int x, int y, GC offgc, GC ongc)
{
if (num > old && num > threshold) {
XFillRectangle(sDisp, sPixmap, ongc, x+1, y*7+1, 1, 5);
}
if (num < old && num <= threshold) {
XFillRectangle(sDisp, sPixmap, offgc, x+1, y*7+1, 1, 5);
}
}
static void
ShowSindicator(SindicatorWidget iw)
{
static old_in = 100000;
static old_out = 100000;
long in = iw->sindicator.in_rate;
long out = iw->sindicator.out_rate;
XExposeEvent rrect;
int i;
if (old_in != in) {
for(i=0; i<35; i++) {
indicate(iw, in, old_in, meter[i].threshold, i, 0,
sGC(meter[i].color), sGC(meter[i].color+1));
}
rrect.x = 1;
rrect.y = 1;
rrect.width = 35;
rrect.height = 5;
Redisplay((Widget)iw, &rrect);
old_in = in;
}
if (old_out != out) {
for(i=0; i<35; i++) {
indicate(iw, out, old_out, meter[i].threshold, i, 1,
sGC(meter[i].color), sGC(meter[i].color+1));
}
rrect.x = 1;
rrect.y = 8;
rrect.width = 35;
rrect.height = 5;
Redisplay((Widget) iw, &rrect);
old_out = out;
}
}
|