View Single Post
Old 11-28-2012, 11:27 AM   #184
twobob
( ͡° ͜ʖ ͡°){ʇlnɐɟ ƃǝs}Týr
twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.
 
twobob's Avatar
 
Posts: 6,552
Karma: 6021142
Join Date: Jun 2012
Location: uti gratia usura (Yao ying da ying; Mo ying da yieng)
Device: PW-WIFI|K5-3G+WIFI| K4|K3-3G|DXG|K2| Rooted Nook Touch
Got some input working now... later 2 posts

Spoiler:
it's the loop that handles the input.h

In essence you export the value into a stuct "&iev" so you can iev.type of whatever.

Quote:
while (read(inputfds[c], &iev, sizeof(struct input_event))
== sizeof(struct input_event)) {
But my TS code is expecting a unsigned char [16] for the event buffer...
maybe this is as simple as a cast or something... but I can't quite get it.

Quote:
while (read(inputfds[c], event_buffer, sizeof(struct input_event))
== sizeof(struct input_event)) {
So yeah I just want the input from inputfds[3] to go into event_buffer and the others [0-2] to go into your struct!!!

I made an unrolled version of the code to show the two bits I am welding together.

(YOU DON'T HAVE TO READ IT... Just showing what I have done)

Spoiler:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define WIDTH 600
#define HEIGHT 800
#define STRIDE 608

#include "input.h"
#include "rc.h"

#define TOUCH_SCREEN "/dev/input/event3"

#define UP 0
#define DOWN 1

static int inputfds[] = { -1, -1, -1 };

rcvar_t joy_exports[] = { RCV_END };

void joy_init() {
	int c;
	char dev[128];
	int deviceCount = 4;
	for (c = 0; c < deviceCount; c++) {
		sprintf(dev, "/dev/input/event%d", c);
		inputfds[c] = open(dev, O_RDONLY | O_NONBLOCK);
		if (inputfds[c] < 0) {
			fprintf(stderr, "couldn't open <%s>\n", dev);
		}
	}
}

void joy_close() {
	int c;
	int deviceCount = 4;
	for (c = 0; c < deviceCount; c++) {
		if (inputfds[c] < 0)
			continue;
		close(inputfds[c]);
	}
}

void update(int refresh) {   // #FIXME. Partial screen update. this is horrible
	if (refresh) {
		system("eips -f ''");
	} else {
		system("eips ''");
	}
}

void joy_poll() {
	event_t ev;

	struct input_event iev;
	int c;
	int deviceCount = 4;

	// Add in some TS initial values

	int touch_screen_fd = 0;
	unsigned char *frame_buffer = NULL;
	unsigned char event_buffer[16];

	/* First finger variables */
	int finger = UP;
	int x = 300;
	int y = 400;

	/* Second finger variables */
	int finger2 = UP;
	int x2 = 0;
	int y2 = 0;
	int f2_mode = 0;

	int i = 0;
	int j = 0;

	int sync = 0;  //? maybe

	//

	for (c = 0; c < deviceCount - 1; c++) {
		if (inputfds[c] < 0)
			continue;
		while (read(inputfds[c], &iev, sizeof(struct input_event))
				== sizeof(struct input_event)) {
		
			if (iev.type == EV_KEY) {

				if (iev.value == 1) {
					ev.type = EV_PRESS;
				} else if (iev.value == 0) {
					ev.type = EV_RELEASE;
				} else {
					continue;
				}
				switch (iev.code) {
				case 103:
				case 122: // FW_UP (K3, KDX)
					ev.code = K_JOYUP;
					break;
				case 108:
				case 123: // FW_DOWN (K3, KDX)
					ev.code = K_JOYDOWN;
					break;
				case 105: // FW_LEFT (both)
					ev.code = K_JOYLEFT;
					break;
				case 106: // FW_RIGHT (both)
					ev.code = K_JOYRIGHT;
					break;
				case 102:
				case 98:  // HOME
					ev.code = K_JOY6;
					break;
				case 44:  // Z
					ev.code = K_JOY1;
					break;
				case 45:  // X
					ev.code = K_JOY0;
					break;
				case 30:  // A
					ev.code = K_JOY2;
					break;
				case 31:  // S
					ev.code = K_JOY3;
					break;
				case 16:  // Q
					ev.code = K_JOY4;
					break;
				case 17:  // W
					ev.code = K_JOY5;
					break;
				default:
					// This would output the codes for unhandled key presses.
					// fprintf(stderr, "iev.code <%i>\n", iev.code);
					fprintf(stderr, "iev.code <%i>\n", iev.code);
					fprintf(stderr, "ev.code <%i>\n", ev.code);
					continue;
				}
				ev_postevent(&ev);
				fprintf(stderr, "sizeof(struct input_event) %i \n",
						sizeof(struct input_event));
			}
		}
	}

	// AGAIN  !!!!  <-- I would like to handle this loop again. or change the &iev to actually be read in as event_buffer
	// Then I can simply meld what is below into what is above.... 
	// (I did this once and realised the inconsistancy in the buffers types Uchar[16]? and a custom stuct.
	// So I unrolled it all again like this to show you what I meant.
	// Hope that is clear.

/*
	while (read(touch_screen_fd, event_buffer, 16)) {
		if (event_buffer[0x08] == 0x03) {
			switch (event_buffer[0x0A]) {
			case 0x2F:
				// When f2_mode is nonzero, data is treated as being for  the second finger 
				f2_mode = event_buffer[0x0C];
				break;
			case 0x35:
				// X Coordinate change  
				if (!f2_mode) {
					x = (WIDTH
							* (event_buffer[0x0C] + (event_buffer[0x0D] << 8)))
							/ 0x1000;
				} else {
					x2 = (WIDTH
							* (event_buffer[0x0C] + (event_buffer[0x0D] << 8)))
							/ 0x1000;
				}
				break;
			case 0x36:
				// Y Coordinate change  
				if (!f2_mode) {
					y = (HEIGHT
							* (event_buffer[0x0C] + (event_buffer[0x0D] << 8)))
							/ 0x1000;
				} else {
					y2 = (HEIGHT
							* (event_buffer[0x0C] + (event_buffer[0x0D] << 8)))
							/ 0x1000;
				}
				break;
			case 0x39:
				// Finger up / down  
				if (event_buffer[0x0C] == 0x00) {
					finger = DOWN;
				} else if (event_buffer[0x0C] == 0x01) {
					finger2 = DOWN;
				} else {
					if (finger2 == DOWN) {
						finger2 = UP;
					} else {
						finger = UP;
					}

				}
				break;
			default:
				break;
			}
		}
		if (event_buffer[0x08] == 0x00) {
			sync = 1;
		}

		if (sync) { // We have recieved a sync 

			// If finger 1 is down, draw for it 
			if (finger == DOWN) {
				if (finger2 == UP) {

				}
			}

			// Fingers are up. Did we just push exit / clear? 
			if (finger == UP) { // Release your finger in the top right to exit 
				if (x > 500 && y < 100) {
					printf("Exiting.\n");
					break;
				}
				if (x < 100 && y < 100) { // Release your finger in the top left to clear the screen 
					update(1);
					x = 300;
					y = 400;

				}

				if (x >= 50 && x <= 250 && y >= 140 && y <= 500) {

					// S
					ev.code = K_JOY3;

					sync = 0;
				}

			}
		}
		ev_postevent(&ev);
		fprintf(stderr, "ev.code = <%i>\n", ev.code);
	}
*/

}


And here is a version that squashes them together.
It needs to populate the event_buffer somehow. or cast my struct to an unsigned char [16] - or something : )

Spoiler:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define WIDTH 600
#define HEIGHT 800
#define STRIDE 608

#include "input.h"
#include "rc.h"

#define TOUCH_SCREEN "/dev/input/event3"

#define UP 0
#define DOWN 1

static int inputfds[] = { -1, -1, -1 };

rcvar_t joy_exports[] = { RCV_END };

void joy_init() {
	int c;
	char dev[128];

	for (c = 0; c < 3; c++) {
		sprintf(dev, "/dev/input/event%d", c);
		inputfds[c] = open(dev, O_RDONLY | O_NONBLOCK);
		if (inputfds[c] < 0) {
			fprintf(stderr, "couldn't open <%s>\n", dev);
		}
	}
}

void joy_close() {
	int c;
	for (c = 0; c < 3; c++) {
		if (inputfds[c] < 0)
			continue;
		close(inputfds[c]);
	}
}

void update(int refresh) {   // #FIXME. Just add the code for partial screen update on all devices. 

// this is horrible and 5 sets of headers and all the headaches they bring HAVE to be better than this...
	if (refresh) {
		system("eips -f ''");
	} else {
		system("eips ''");
	}
}

void joy_poll() {
	event_t ev;

	struct input_event iev;
	int c;

	// Add in some TS initial values

	int touch_screen_fd = 0;
	unsigned char *frame_buffer = NULL;
	unsigned char event_buffer[16];

	/* First finger variables */
	int finger = UP;
	int x = 300;
	int y = 400;

	/* Second finger variables */
	int finger2 = UP;
	int x2 = 0;
	int y2 = 0;
	int f2_mode = 0;

	int i = 0;
	int j = 0;

	int sync = 0;  //? maybe

	//

	for (c = 0; c < 3; c++) {
		if (inputfds[c] < 0)
			continue;
		while (read(inputfds[c], &iev, sizeof(struct input_event))
				== sizeof(struct input_event)) {
			
// Ha. I wish...
//event_buffer = (unsigned char[16])iev;  // or something?? I'm looking into it...

			// This test will obviously never match "event_buffer[0x08] == 0x03" 
			//since event buffer is currently unpopulated
			
				if (iev.type == EV_KEY || event_buffer[0x08] == 0x03 ) {

				if (event_buffer[0x08] == 0x03) {

					switch (event_buffer[0x0A]) {

					case 0x2F:
						/* When f2_mode is nonzero, data is treated as being for
						 the second finger */
						f2_mode = event_buffer[0x0C];
						break;
					case 0x35:
						/* X Coordinate change */
						if (!f2_mode) {
							x = (WIDTH
									* (event_buffer[0x0C]
											+ (event_buffer[0x0D] << 8)))
									/ 0x1000;
						} else {
							x2 = (WIDTH
									* (event_buffer[0x0C]
											+ (event_buffer[0x0D] << 8)))
									/ 0x1000;
						}
						break;
					case 0x36:
						/* Y Coordinate change */
						if (!f2_mode) {
							y = (HEIGHT
									* (event_buffer[0x0C]
											+ (event_buffer[0x0D] << 8)))
									/ 0x1000;
						} else {
							y2 = (HEIGHT
									* (event_buffer[0x0C]
											+ (event_buffer[0x0D] << 8)))
									/ 0x1000;
						}
						break;
					case 0x39:
						/* Finger up / down */
						if (event_buffer[0x0C] == 0x00) {
							finger = DOWN;
						} else if (event_buffer[0x0C] == 0x01) {
							finger2 = DOWN;
						} else {
							if (finger2 == DOWN) {
								finger2 = UP;
							} else {
								finger = UP;
							}

						}
						break;
					default:
						continue;

					}

					// End of Switch

					// TODO: This does what now???

					if (event_buffer[0x08] == 0x00) {
						sync = 1;
					}

					if (sync) { /* We have received a sync */

						// seemingly pointless code TODO: check relevance

						/* If finger 1 is down, draw for it */
						if (finger == DOWN) {
							if (finger2 == UP) {
							}
						}
						/* END OF: If finger 1 is down, draw for it */

						/* Fingers are up. Did we just push exit / clear? */
						if (finger == UP) { /* Release your finger in the top right to exit */
							if (x > 500 && y < 100) {
								printf("Exiting.\n");
								break;
							}
							if (x < 100 && y < 100) { /* Release your finger in the top left
							 to clear the screen */
								update(1);
								x = 300;
								y = 400;

							}

							if (x >= 50 && x <= 250 && y >= 140 && y <= 500) {

								// S
								ev.code = K_JOY3;

								sync = 0;
							}

						}

						/* END OF: Fingers are up. Did we just push exit / clear? */

					}
					// end of: if (sync)

				}
				// End of if (event_buffer[0x08] == 0x03)

				else if (iev.value == 1) {
					ev.type = EV_PRESS;
				} else if (iev.value == 0) {
					ev.type = EV_RELEASE;
				} else {
					continue;
				}
				switch (iev.code) {
				case 103:
				case 122: // FW_UP (K3, KDX)
					ev.code = K_JOYUP;
					break;
				case 108:
				case 123: // FW_DOWN (K3, KDX)
					ev.code = K_JOYDOWN;
					break;
				case 105: // FW_LEFT (both)
					ev.code = K_JOYLEFT;
					break;
				case 106: // FW_RIGHT (both)
					ev.code = K_JOYRIGHT;
					break;
				case 102:
				case 98:  // HOME
					ev.code = K_JOY6;
					break;
				case 44:  // Z
					ev.code = K_JOY1;
					break;
				case 45:  // X
					ev.code = K_JOY0;
					break;
				case 30:  // A
					ev.code = K_JOY2;
					break;
				case 31:  // S
					ev.code = K_JOY3;
					break;
				case 16:  // Q
					ev.code = K_JOY4;
					break;
				case 17:  // W
					ev.code = K_JOY5;
					break;
				default:
					// This would output the codes for unhandled key presses.
					// fprintf(stderr, "iev.code <%i>\n", iev.code);
					
					continue;
				}
				ev_postevent(&ev);
				// Let's have some output....
				fprintf(stderr, "iev.code <%i>\n", iev.code);
				fprintf(stderr, "ev.code <%i>\n", ev.code);
			}
		}
	}
}

Last edited by twobob; 11-28-2012 at 02:01 PM. Reason: added almost complete version.
twobob is offline   Reply With Quote