Files
my_glut/sources/input/touch/touch_controller_L1_builder.c

163 lines
3.7 KiB
C
Raw Permalink Normal View History

#include "../../../headers/input/touch/touch_controller_L1_builder.h"
#include <stdlib.h>
#include <stdio.h>
/* otherwise
error: variably modified tracked at file scope
even if tracked_size gets declared as const
*/
#define tracked_size 255
/* could hold 256 using tracked_size + 1 but don't want to mess the copy tracked loop */
static touch_event *tracked[ tracked_size] = { NULL };
static uint8_t tracked_count = 0;
static touch_event *last_event = NULL;
static touch_event *current_event = NULL;
touch_event* tracked_find( uint64_t slot_number){
return tracked[slot_number];
}
void tracked_remove( touch_event *toremove){
tracked[toremove->slot_number] = NULL;
tracked_count--;
}
void tracked_add( touch_event *toadd){
tracked[toadd->slot_number] = toadd;
tracked_count++;
}
uint8_t tracked_get_first_free_slot_number(){
int first_free_slot_number = 0;
while(
tracked[first_free_slot_number]
&&
! ( tracked_size == first_free_slot_number)
){
first_free_slot_number++;
}
if( tracked_size == first_free_slot_number){
fprintf( stderr, "no more slot_numbers for tracked touches\n");
exit(1);
}
return first_free_slot_number;
}
/* yes int instead of uint */
/**
SCHIFO : il primo tocco non ha mai ABS_MT_SLOT
ma parte direttamente da ABS_MT_TRACKING_ID
QUINDI se viene settato senza ABS_MT_SLOT
*/
void TC_L1_builder_set_tracking_id( int64_t tracking_id){
/*
fix brutta
*/
if( ! current_event){
if( last_event){
printf("current was unset : setting as the last, which had slot number %ld\n", last_event->slot_number);
current_event = last_event;
}
else{
printf("current was unset : setting as NEW\n");
TC_L1_builder_set_slot_number( tracked_get_first_free_slot_number());
}
}
/* unsigned aghhh */
if( -1u == tracking_id){
printf("REMOVE\n");
tracked_remove( current_event);
free( current_event);
current_event = NULL;
}
else{
current_event->tracking_id = tracking_id;
}
}
void TC_L1_builder_set_position_x( uint64_t x_coord){
current_event->position.x = x_coord;
}
void TC_L1_builder_set_position_y( uint64_t y_coord){
current_event->position.y = y_coord;
}
void TC_L1_builder_set_major_axis( uint64_t width){
}
void TC_L1_builder_set_approaching_major_axis( uint64_t width){
}
void TC_L1_builder_set_slot_number( uint64_t number){
touch_event* target = tracked_find( number);
if( ! target){
printf("FETCHING DID NOT SUCCEED, allocating\n");
target = malloc( sizeof( touch_event));
target->slot_number = number;
tracked_add( target);
}
else{
printf("FETCHING SUCCEEDED\n");
target->slot_number = number;
}
last_event = current_event;
current_event = target;
}
void TC_L1_builder_copy_tracked_events( touch_event ***store, uint8_t *count){
//printf("TC_L1_builder_copy_tracked_events begins with %p, %d\n", (void*) *store, *count);
if( ! tracked_count){
//printf("TC_L1_b_c_t_e has nothing to copy");
*store = NULL;
*count = 0;
return;
}
//printf("tracked\n");
//for( uint8_t i = 0; i < tracked_count; i++){
// printf( "%ld\n", tracked[i]->tracking_id);
//}
*store = malloc( tracked_count * sizeof( touch_event* ));
touch_event *clone;
touch_event *current;
int index = 0;
for( int i = 0; i < tracked_size; i++){
current = tracked[i];
if( current){
clone = malloc( sizeof( touch_event));
touch_event_copy( clone, current);
(*store)[index++] = clone;
}
}
*count = index;
//printf("copied\n");
//for( uint8_t i = 0; i < *count; i++){
// printf( "%ld\n", (*store)[i]->tracking_id);
//}
//printf("TC_L1_builder_copy_tracked_events ends with %p, %d\n", (void*) *store, *count);
}
void TC_L1_builder_destroy_copy( touch_event **store, uint8_t count){
for( int i = 0; i < count; i++){
free(store[i]);
}
if( count){
free( store);
}
}