Programming:  Polling events

Why don't we move a rectangle over the screen using the pen? This sample will do that for us, provided that you move the pen :)

#include	<stdrom.h>
#include	"define.h"
#include	"libc.h"

Let's define screen and rect sizes

#define SCREEN_W 160
#define SCREEN_H 160

#define RECT_W 10
#define RECT_H 10

These are the events PollEvents can listen to.

#define EVENT_TCH    1
#define EVENT_CRADLE 4
#define EVENT_BLD1   8

This is PollEvents and its two params. We already met tsts. The event mask is an "Ored" combination of the flags we defined above.

void PollEvents(TCHSTS far *tsts, byte event_mask)
{
	union REGS reg;
	reg.x.ax = 0x0200 | event_mask;
	reg.x.di = FP_OFF(tsts);
	reg.x.es = FP_SEG(tsts);
	int86(0x50, &reg, &reg);
}

Let's compute the position of the rect according to the new (x, y) and draw the rect where the pen wants it to be :)

void DoMoveRect(int x, int y)
{
	int rect_x1, rect_y1;
	int rect_x2, rect_y2;

	if (x <= RECT_W / 2) 
		return;

	if (y <= RECT_H / 2) 
		return;

	if (x + RECT_W / 2 >= SCREEN_W) 
		return;

	if (y + RECT_H / 2 >= SCREEN_H) 
		return;

	rect_x1 = x - RECT_W / 2;
	rect_y1 = y - RECT_H / 2;
	rect_x2 = x + RECT_W / 2;
	rect_y2 = y + RECT_H / 2;

	LibClrDisp();
	LibGdsBox(rect_x1, rect_y1, rect_x2, rect_y2); 
	LibPutDisp();
}

This event loop differs from the ones we're used to. No more LibTchWait, PollEvents takes its place, but PollEvents is not blocking. So how do we know if something happened. Easy, just test tsts. If nothing happened tsts.x and tsts.y are both -1. Don't expect this function will fill the obj field. Why? I don't know ;) But did we provide a touch table? No, we didn't, so what obj should we look for? This time objects are all in our minds. But where is the ESC button? :| It's lost out there somewhere. It shows that when we use PollEvents our screen become larger!! So if tsts.y is greater than 159 we're touching the hard icons, but we have to figure out which icon we're on according to (tsts.x, tsts.y). Ok, let's take a ruler ;) Oh i don't have one, so i quit as soon as the pen touches a point whose y is greater than 159. OK i have a ruler, but i'm lazy :) To learn more about the geometry of the Hard Icons take a look at "PocketViewer Event control BIOS, page 7". bYe.

void main()
{
	TCHSTS tsts;
	bool bExit = FALSE;

	DoMoveRect(79, 79);

	for (;bExit == FALSE;)
	{
		PollEvents(&tsts, EVENT_TCH);

		if (tsts.y != -1 || tsts.x != -1)
		{
			DoMoveRect(tsts.x, tsts.y);
			if (tsts.y >= SCREEN_H) bExit = TRUE;
		}
	}

	LibJumpMenu();
}