Battery replicant returns correct widget

This commit is contained in:
Sasha Koshka 2023-11-14 16:22:01 -05:00
parent caa3d1d57b
commit daa80065c6
3 changed files with 151 additions and 1 deletions

View File

@ -115,7 +115,7 @@ Widget Battery_XmdReplicantCreate (
XtManageChild(this->layout); XtManageChild(this->layout);
XtManageChild(this->root); XtManageChild(this->root);
return this->layout; return this->root;
} }
BatteryInfo getBatteryInfo (void) { BatteryInfo getBatteryInfo (void) {

2
replicants/Clock/build.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/sh
../../scripts/buildreplicant.sh Clock "$@"

148
replicants/Clock/src/main.c Normal file
View File

@ -0,0 +1,148 @@
#define _XOPEN_SOURCE
#include <Xm/Xm.h>
#include <Xm/Frame.h>
#include <Xm/Label.h>
#include <Xm/DrawingA.h>
#include <Xmd/Icon.h>
#include <Xmd/Exec.h>
#include <Xmd/Replicant.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
typedef struct {
unsigned long pollInterval;
Bool analog;
String format;
XtAppContext application;
Widget root;
Widget face;
GC gc;
XtIntervalId timer;
} ClockReplicant;
static void clockPoll (XtPointer, XtIntervalId *);
static void resetClockPollTimeout (ClockReplicant *);
static void handleDestroy (Widget, XtPointer, XtPointer);
int Clock_XmdReplicantVersion (void) {
return XmdREPLICANT_VERSION;
}
Widget Clock_XmdReplicantCreate (
XtAppContext application,
Widget parent,
XmdReplicantState *state
) {
(void)(state);
ClockReplicant *this = XtNew(ClockReplicant);
memset(this, 0, sizeof(*this));
this->application = application;
/* read state */
String pollIntervalString = XmdReplicantStateQuery(state, "Interval");
if (pollIntervalString != NULL) {
this->pollInterval = 1000 * atoi(pollIntervalString);
}
if (this->pollInterval == 0) this->pollInterval = 1000;
XtFree(pollIntervalString);
String typeString = XmdReplicantStateQuery(state, "Type");
this->analog = strcmp(typeString, "Analog") == 0;
XtFree(typeString);
this->format = XmdReplicantStateQuery(state, "Format");
if (this->format == NULL) this->format = XtNewString("%H:%M:%S");
/* create widgets */
this->root = XtVaCreateWidget (
"clock", xmFrameWidgetClass, parent,
XmNshadowType, XmSHADOW_ETCHED_IN,
NULL);
XtAddCallback(this->root, XmNdestroyCallback, handleDestroy, NULL);
if (this->analog) {
/* analog */
this->face = XtVaCreateWidget (
"clockFace", xmDrawingAreaWidgetClass, this->root,
XmNwidth, 48,
XmNheight, 48,
XmNresizePolicy, XmRESIZE_NONE,
NULL);
XGCValues values = { 0 };
XtVaGetValues (parent,
XmNforeground, &values.foreground,
XmNbackground, &values.background,
NULL);
this->gc = XCreateGC (
XtDisplay(this->face),
RootWindowOfScreen(XtScreen(this->face)),
GCForeground | GCBackground, &values);
XtManageChild(this->face);
} else {
/* digital */
this->face = XtVaCreateManagedWidget (
"clockFace", xmLabelWidgetClass, this->root,
XmNmarginWidth, 8,
XmNmarginHeight, 8,
NULL);
}
clockPoll(this, NULL);
XtManageChild(this->root);
return this->root;
}
void clockPoll (XtPointer clientData, XtIntervalId *timer) {
(void)(clientData);
(void)(timer);
ClockReplicant *this = clientData;
if (this->analog) {
/*Display *display = XtDisplay(this->face);
Window window = XtWindow(this->face);
XDrawLine(display, window, this->gc, 0, 0, 32, 32);
XClearWindow(display, window);*/
} else {
time_t now = time(NULL);
struct tm *local = localtime(&now);
char timeBuffer[64];
strftime(timeBuffer, XtNumber(timeBuffer), this->format, local);
XmString timeString = XmStringCreateLocalized(timeBuffer);
XtUnmanageChild(this->face);
XtVaSetValues (
this->face,
XmNlabelString, timeString,
NULL);
XtManageChild(this->face);
XmStringFree(timeString);
}
resetClockPollTimeout(this);
}
void resetClockPollTimeout (ClockReplicant *this) {
this->timer = XtAppAddTimeOut (
this->application, this->pollInterval, clockPoll, this);
}
static void handleDestroy (
Widget widget,
XtPointer clientData,
XtPointer callData
) {
(void)(callData);
(void)(widget);
ClockReplicant *this = clientData;
XtRemoveTimeOut(this->timer);
XtFree(this->format);
XtFree((char *)(this));
}