📄 batterygauge.c
字号:
new_me->batteryGauge.status = current_me->batteryGauge.status; new_me->batteryGauge.lifetime = current_me->batteryGauge.lifetime; repaint = setPosition(new_me, current_me->batteryGauge.status); if(new_me->batteryGauge.highColor != current_me->batteryGauge.highColor || new_me->batteryGauge.lowColor != current_me->batteryGauge.lowColor || new_me->batteryGauge.criticalColor != current_me->batteryGauge.criticalColor || new_me->batteryGauge.chargingColor != current_me->batteryGauge.chargingColor) repaint = True; setFlashTimeout(new_me); } return repaint;}/*}}}*//*}}}*//*{{{ actions*//*{{{ static void Notify(Widget w, XEvent *event, String *params, Cardinal *num_params)*/static void Notify(Widget w, XEvent *event, String *params, Cardinal *num_params)/* notify of flashing */{ BatteryGaugeWidget me; me = (BatteryGaugeWidget)w; me->batteryGauge.notified = me->batteryGauge.flash; if(me->batteryGauge.flashTimeout != (XtIntervalId)0) { XtRemoveTimeOut(me->batteryGauge.flashTimeout); me->batteryGauge.flashTimeout = (XtIntervalId)0; } if(me->batteryGauge.inverted) { me->batteryGauge.inverted = 0; redraw(me); } if(!me->batteryGauge.popup) /*{{{ create popup*/ { me->batteryGauge.popup = XtCreatePopupShell("popup", overrideShellWidgetClass, (Widget)me, NULL, 0); me->batteryGauge.label = XtCreateManagedWidget("label", labelWidgetClass, me->batteryGauge.popup, NULL, 0); } /*}}}*/ updateLabel(me); /*{{{ position popup*/ { /*{{{ static Arg setArgs[] =*/ static Arg setArgs[] = { {XtNx, 0}, {XtNy, 0}, }; /*}}}*/ /*{{{ static Arg getArgs[] =*/ static Arg getArgs[] = { {XtNwidth, 0}, {XtNheight, 0}, }; /*}}}*/ int x, y; Dimension w, h; int got; int screenW; /*{{{ get position from event*/ switch(event->type) { case ButtonPress: case ButtonRelease: x = event->xbutton.x_root; y = event->xbutton.y_root; got = 1; break; case MotionNotify: x = event->xmotion.x_root; y = event->xmotion.y_root; got = 1; break; case EnterNotify: case LeaveNotify: x = event->xcrossing.x_root; y = event->xcrossing.y_root; got = 1; break; default: x = y = 0; got = 0; } /*}}}*/ XtRealizeWidget(me->batteryGauge.popup); getArgs[0].value = (XtArgVal)&w; getArgs[1].value = (XtArgVal)&h; XtGetValues(me->batteryGauge.popup, getArgs, 2); y -= h + 4; x -= w / 2; screenW = WidthOfScreen(XtScreen((Widget)me)); if(x < 0) x = 0; if(y < 0) y = 0; if(x > (int)(screenW - w)) x = (int)(screenW - w); setArgs[0].value = (XtArgVal)x; setArgs[1].value = (XtArgVal)y; XtSetValues(me->batteryGauge.popup, setArgs, 2); } /*}}}*/ XtPopup(me->batteryGauge.popup, XtGrabNone); me->batteryGauge.popped = 1; return;}/*}}}*//*{{{ static void Release(Widget w, XEvent *event, String *params, Cardinal *num_params)*/static void Release(Widget w, XEvent *event, String *params, Cardinal *num_params){ BatteryGaugeWidget me; me = (BatteryGaugeWidget)w; if(me->batteryGauge.popup) XtPopdown(me->batteryGauge.popup); me->batteryGauge.popped = 0; return;}/*}}}*//*}}}*//*{{{ helpers*//*{{{ static void redraw(BatteryGaugeWidget me)*/static void redraw(BatteryGaugeWidget me)/* Draw the batteryGauge position. If charging then background color is * charging color, otherwise it's normal background. If value <= emptyValue, * foreground is emptyColor, else if value >= warningValue, foreground is * normalColor, else foreground is warning foreground. If inverted then swap * foreground and background. */{ Pixel foreground; Pixel background; XRectangle foreRect; XRectangle backRect; background = me->core.background_pixel; /*{{{ set foreground color*/ switch(me->batteryGauge.status) { case 0: foreground = me->batteryGauge.inverted ? me->batteryGauge.chargingColor : me->batteryGauge.highColor; break; case 1: foreground = me->batteryGauge.inverted ? background : me->batteryGauge.lowColor; break; case 2: foreground = me->batteryGauge.criticalColor; if(me->batteryGauge.inverted) { foreground = background; background = me->batteryGauge.criticalColor; } break; case 3: foreground = me->batteryGauge.inverted ? me->batteryGauge.highColor : me->batteryGauge.chargingColor; break; default: foreground = background; } /*}}}*/ foreRect.x = backRect.x = 0; foreRect.y = backRect.y = 0; foreRect.width = backRect.width = me->core.width; foreRect.height = backRect.height = me->core.height; if(me->batteryGauge.orientation == XtorientHorizontal) /*{{{ horizontal*/ { foreRect.width = me->batteryGauge.position; backRect.width -= me->batteryGauge.position; backRect.x = foreRect.width; } /*}}}*/ else /*{{{ vertical*/ { foreRect.height = me->batteryGauge.position; backRect.height -= me->batteryGauge.position; foreRect.y = backRect.height; } /*}}}*/ XSetForeground(XtDisplay((Widget)me), me->batteryGauge.gc, foreground); XFillRectangle(XtDisplay((Widget)me), XtWindow((Widget)me), me->batteryGauge.gc, foreRect.x, foreRect.y, foreRect.width, foreRect.height); XSetForeground(XtDisplay((Widget)me), me->batteryGauge.gc, background); XFillRectangle(XtDisplay((Widget)me), XtWindow((Widget)me), me->batteryGauge.gc, backRect.x, backRect.y, backRect.width, backRect.height); return;}/*}}}*//*{{{ static int setPosition(BatteryGaugeWidget me, int oldStatus)*/static int setPosition(BatteryGaugeWidget me, int oldStatus){ int changed = 0; int pos; int flash; if(me->batteryGauge.level < 0) me->batteryGauge.level = 0; if(me->batteryGauge.level > 100) me->batteryGauge.level = 100; pos = (me->batteryGauge.length * me->batteryGauge.level + 50) / 100; changed |= me->batteryGauge.position != pos; me->batteryGauge.position = pos; if(me->batteryGauge.level <= me->batteryGauge.criticalLevel && me->batteryGauge.status != 3) me->batteryGauge.status = 2; changed |= me->batteryGauge.status != oldStatus; flash = me->batteryGauge.flash; if(me->batteryGauge.status == 2 && oldStatus != 2) /*{{{ gone critical*/ { me->batteryGauge.notified = 0; flash = -1; } /*}}}*/ else if(me->batteryGauge.status == 1 && oldStatus != 1) /*{{{ gone low*/ { me->batteryGauge.notified = 0; flash = 1; } /*}}}*/ else if(flash < 1 && me->batteryGauge.level == 100 && (me->batteryGauge.status == 3 || (me->batteryGauge.status == 0 && oldStatus == 3))) /*{{{ charged*/ { flash = 1; me->batteryGauge.notified = 0; } /*}}}*/ else if(me->batteryGauge.level != 100 && me->batteryGauge.status != 2 && me->batteryGauge.status != 1) /*{{{ stop flashing*/ { flash = 0; } /*}}}*/ if(!flash) { changed |= me->batteryGauge.inverted; me->batteryGauge.inverted = 0; } me->batteryGauge.flash = flash; return changed;}/*}}}*//*{{{ static void setFlashTimeout(BatteryGaugeWidget me)*/static void setFlashTimeout(BatteryGaugeWidget me){ if(!me->batteryGauge.flashTimeout && me->batteryGauge.flash != me->batteryGauge.notified) me->batteryGauge.flashTimeout = XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)me), (unsigned long)me->batteryGauge.flashDelay, flashTimeout, (XtPointer)me); return;}/*}}}*//*{{{ static void flashTimeout(XtPointer client, XtIntervalId *id)*/static void flashTimeout(XtPointer client, XtIntervalId *id){ BatteryGaugeWidget me; me = (BatteryGaugeWidget)client; me->batteryGauge.flashTimeout = (XtIntervalId)0; if(me->batteryGauge.flash) { me->batteryGauge.inverted ^= 1; redraw(me); setFlashTimeout(me); } return;}/*}}}*//*{{{ static void updateLabel(BatteryGaugeWidget me)*/static void updateLabel(BatteryGaugeWidget me){ if(me->batteryGauge.label) { static char buffer[24]; static char const *states[] = {"high", "low", "critical", "charging"}; static Arg arg = {XtNlabel, (XtArgVal)buffer}; if(me->batteryGauge.lifetime < 0) sprintf(buffer, "%d%% (%s)", me->batteryGauge.level, states[me->batteryGauge.status]); else sprintf(buffer, "%d%% %s (%s)", me->batteryGauge.level, apm_time_nosec(me->batteryGauge.lifetime), states[me->batteryGauge.status]); XtSetValues(me->batteryGauge.label, &arg, 1); }}/*}}}*//*{{{ static void updateTimeout(XtPointer client, XtIntervalId *id)*/static void updateTimeout(XtPointer client, XtIntervalId *id){ BatteryGaugeWidget me = (BatteryGaugeWidget)client; apm_info info; int oldStatus = me->batteryGauge.status; unsigned long delay; me->batteryGauge.updateTimeout = 0; apm_read(&info); me->batteryGauge.level = info.battery_percentage; me->batteryGauge.status = info.battery_status; me->batteryGauge.lifetime = info.battery_time < 0 ? -1 : info.using_minutes ? info.battery_time * 60 : info.battery_time; if(setPosition(me, oldStatus) && XtIsRealized((Widget)me)) { redraw(me); setFlashTimeout(me); } if(me->batteryGauge.popped) updateLabel(me); delay = me->batteryGauge.updateDelay * 100; if(me->batteryGauge.status & 1) delay *= 10; me->batteryGauge.updateTimeout = XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)me), delay + 500, updateTimeout, (XtPointer)me);}/*}}}*//*}}}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -