gnome-power-manager and IDLETIME (setting alarms using GDK)



I'm looking to implement X11 IDLETIME counter support for
gnome-power-manager. To do this I need to setup alarms on the new X11
counter (see attached code[1]) but obviously need to do this in the
gtk/gdk framework.

I've looked through the gdk API and can't seem to find any functionality
for alarms, but feel free to tell me to RTFM :-)

I'm not sure if this belongs in GDK, or how to integrate the code into
gnome-power-manager. Comments and suggestions please. Thanks.

Richard.

[1] Compile with "gcc -o idlecounter-demo idlecounter-demo.c `pkg-config
--cflags --libs gtk+-2.0`"
#include <X11/Xlib.h>
#include <X11/extensions/sync.h>

#include <stdio.h>
#include <string.h>

static void
setAlarm (Display *dpy, XSyncAlarm *alarm, XSyncCounter counter, XSyncTestType test, XSyncValue value)
{
	XSyncAlarmAttributes attr;
	XSyncValue delta;
	unsigned int flags;

	XSyncIntToValue (&delta, 0);

	attr.trigger.counter	 = counter;
	attr.trigger.value_type  = XSyncAbsolute;
	attr.trigger.test_type   = test;
	attr.trigger.wait_value  = value;
	attr.delta               = delta;

	flags = XSyncCACounter | XSyncCAValueType | XSyncCATestType | XSyncCAValue | XSyncCADelta;

	if (*alarm)
		XSyncChangeAlarm (dpy, *alarm, flags, &attr);
	else
		*alarm = XSyncCreateAlarm (dpy, flags, &attr);
}


int
main (int argc, char **argv)
{
	Display *dpy = XOpenDisplay (NULL);
	int sync_event, sync_error;
	int sync_major, sync_minor;
	int ncounters;
	XSyncSystemCounter  *counters;
	XSyncCounter idleCounter = None;
	XSyncAlarm timeoutAlarm = None;
	XSyncAlarm resetAlarm = None;
	XSyncValue timeout;
	int i;

	if (!XSyncQueryExtension (dpy, &sync_event, &sync_error)) {
		fprintf (stderr, "No Sync extension.\n");
		return 1;
	}

	XSyncInitialize (dpy, &sync_major, &sync_minor);
	counters = XSyncListSystemCounters (dpy, &ncounters);

	for (i = 0; i < ncounters && !idleCounter; i++) {
		if (!strcmp(counters[i].name, "IDLETIME"))
			idleCounter = counters[i].counter;
	}

	if (!idleCounter) {
		fprintf (stderr, "No idle counter.\n");
		return 1;
	}

	/* 3 second timeout */
	XSyncIntToValue (&timeout, 7000);
	setAlarm (dpy, &timeoutAlarm, idleCounter, XSyncPositiveComparison, timeout);

	printf ("Waiting for timeout.\n");
	int count = 0;

	while (1) {
		XEvent event;
		XSyncAlarmNotifyEvent *alarmEvent;

		XNextEvent (dpy, &event);

		if (event.type != sync_event + XSyncAlarmNotify)
			continue;

		alarmEvent = (XSyncAlarmNotifyEvent *)&event;

		if (alarmEvent->alarm == timeoutAlarm) {
			printf ("Timeout %i.\n", count++);

			/* Don't match on the current value because
			 * XSyncNegativeComparison means less or equal. */
			int overflow;
			XSyncValue add;
			XSyncValue plusone;
			XSyncIntToValue (&add, -1);
			XSyncValueAdd (&plusone, alarmEvent->counter_value, add, &overflow);

			/* Set the reset alarm to fire the next time
			 * idleCounter < the current counter value */
			setAlarm (dpy, &resetAlarm, idleCounter, XSyncNegativeComparison, plusone);

		} else if (alarmEvent->alarm == resetAlarm) {

			printf ("Reset.\n");
			XSyncIntToValue (&timeout, 7000);
			setAlarm (dpy, &timeoutAlarm, idleCounter, XSyncPositiveComparison, timeout);
		}
	}
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]