nyx_error_t nyx_utils_async_callback(nyx_device_t *device_in_ptr, nyx_device_callback_function_t callback, nyx_callback_status_t status, void *context) { if (NULL == device_in_ptr) { nyx_warn("%s: device pointer is null, no device to use in call", __FUNCTION__); return NYX_ERROR_INVALID_VALUE; } if (NULL == callback) { nyx_warn("%s: callback pointer is null, no callback to call", __FUNCTION__); return NYX_ERROR_INVALID_VALUE; } struct sched_param param; pthread_attr_t thread_attr; pthread_attr_init(&thread_attr); pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER); pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED); param.sched_priority = sched_get_priority_max(SCHED_OTHER); pthread_attr_setschedparam(&thread_attr, ¶m); if (pthread_attr_setstacksize(&thread_attr, 65536)) { nyx_error( "%s: Could not set thread stack size for display_brightness_thread.", __FUNCTION__); return NYX_ERROR_GENERIC; } pthread_t thread; struct callback_data *d = (struct callback_data *)calloc(sizeof( struct callback_data), 1); d->device_ptr = device_in_ptr; d->callback = callback; d->status = status; d->context = context; if (pthread_create(&thread, &thread_attr, callback_thread, d)) { nyx_error( "%s: Could not set thread stack size for display_brightness_thread.", __FUNCTION__); free(d); return NYX_ERROR_GENERIC; } return NYX_ERROR_NONE; }
static int read_input_event(void) { int numEvents = 0; int rd = 0; input_event_t pEvent; fds[0].fd = touchpanel_event_fd; fds[0].events = POLLIN; int ret_val = poll(fds,1,0); if(ret_val <= 0) { return 0; } if(fds[0].revents & POLLIN) { rd = read(fds[0].fd, &pEvent, sizeof(input_event_t)); if (rd<0 && errno!=EINTR) { nyx_error("Failed to read events from keypad event file"); return -1; } handle_new_event(&pEvent); } return numEvents; }
static nyx_touchpanel_event_item_t* touch_event_get_current_item( nyx_event_touchpanel_t* i_event_ptr) { nyx_touchpanel_event_item_t* item_ptr = NULL; assert(NULL != i_event_ptr); if (i_event_ptr->item_count > 0) { item_ptr = &i_event_ptr->item_array[i_event_ptr->item_count - 1]; } else { nyx_error("No touch items available! event %p\n", i_event_ptr); } return item_ptr; }
static int init_touchpanel(void) { struct input_absinfo abs; int maxX, maxY, ret = -1; touchpanel_event_fd = open("/dev/input/touchscreen0", O_RDWR); if(touchpanel_event_fd < 0) { nyx_error("Error in opening touchpanel event device"); return -1; } ret = ioctl(touchpanel_event_fd, EVIOCGABS(0), &abs); if(ret < 0) { nyx_error("Error in fetching screen horizontal limits"); goto error; } maxX = abs.maximum; ret = ioctl(touchpanel_event_fd, EVIOCGABS(1), &abs); if(ret < 0) { nyx_error("Error in fetching screen vertical limits"); goto error; } maxY = abs.maximum; init_gesture_state_machine(&sGeneralSettings, 1); scaleX = (float)SCREEN_HORIZONTAL_RES / (float)maxX; scaleY = (float)SCREEN_VERTICAL_RES / (float)maxY; return 0; error: if(touchpanel_event_fd >= 0) close(touchpanel_event_fd); return ret; }
static nyx_touchpanel_event_item_t* touch_event_get_next_item( nyx_event_touchpanel_t* i_event_ptr) { nyx_touchpanel_event_item_t* item_ptr = NULL; assert(NULL != i_event_ptr); if (i_event_ptr->item_count < NYX_MAX_TOUCH_EVENTS) { item_ptr = &i_event_ptr->item_array[i_event_ptr->item_count++]; } else { nyx_error("tried allocating too many touch items: event %p, item cnt %d, max %d\n", i_event_ptr, i_event_ptr->item_count, NYX_MAX_TOUCH_EVENTS); } return item_ptr; }
nyx_error_t nyx_device_open(nyx_device_type_t type, nyx_device_id_t id, nyx_device_handle_t* handle_out_ptr) { nyx_device_t* device = NULL; const char* type_str = nyx_core_device_type_to_string(type); if (NULL == type_str) { return NYX_ERROR_UNSUPPORTED_DEVICE_TYPE; } /* * let's open the library: * */ gchar **tokens = g_strsplit(id, ":", -1); if (NULL == tokens) return NYX_ERROR_INVALID_VALUE; // TODO: make sure typeStr and id do not generate overflow. gchar* lib_name_str = g_strdup_printf ("%s/%s%s%s%s", NYX_MODULE_DIR, NYX_MODULE_PREFIX, type_str, tokens[0], NYX_MODULE_SUFFIX); void* module_ptr = dlopen(lib_name_str, RTLD_NOW); g_free (lib_name_str); if (NULL == module_ptr) { lib_name_str = g_strdup_printf ("%s/%s%s%s%s", NYX_MODULE_MOCK_DIR, NYX_MODULE_PREFIX, type_str, tokens[0], NYX_MODULE_SUFFIX); module_ptr = dlopen(lib_name_str, RTLD_NOW); g_free (lib_name_str); } if (NULL == module_ptr) { nyx_error ("module (%s%s%s%s) does not exist", NYX_MODULE_PREFIX, type_str, id, NYX_MODULE_SUFFIX); nyx_error ("in %s", NYX_MODULE_DIR); nyx_error ("nor in %s", NYX_MODULE_MOCK_DIR); return NYX_ERROR_DEVICE_NOT_EXIST; } nyx_open_function_t open_ptr = dlsym(module_ptr, "nyx_module_open"); if (NULL == open_ptr) { nyx_error ("module does not implement \"nyx_module_open\" method"); dlclose (module_ptr); return NYX_ERROR_UNSUPPORTED_DEVICE_TYPE; } nyx_close_function_t close_ptr = dlsym(module_ptr, "nyx_module_close"); if (NULL == close_ptr) { nyx_error ("module does not implement \"nyx_module_close\" method"); dlclose (module_ptr); return NYX_ERROR_UNSUPPORTED_DEVICE_TYPE; } _nyx_module_get_api_version_major_function_t get_version_major_ptr = dlsym(module_ptr, "_nyx_module_get_api_version_major"); if (NULL == get_version_major_ptr) { nyx_error ("module was not declared with NYX_DECLARE_MODULE"); dlclose (module_ptr); return NYX_ERROR_UNSUPPORTED_DEVICE_TYPE; } _nyx_module_get_api_version_minor_function_t get_version_minor_ptr = dlsym(module_ptr, "_nyx_module_get_api_version_minor"); if (NULL == get_version_minor_ptr) { nyx_error ("module was not declared with NYX_DECLARE_MODULE"); dlclose (module_ptr); return NYX_ERROR_UNSUPPORTED_DEVICE_TYPE; } _nyx_module_get_name_function_t get_name_ptr = dlsym(module_ptr, "_nyx_module_get_name"); if (NULL == get_name_ptr) { nyx_error ("module was not declared with NYX_DECLARE_MODULE"); dlclose (module_ptr); return NYX_ERROR_UNSUPPORTED_DEVICE_TYPE; } _nyx_module_get_type_function_t get_device_type_ptr = dlsym(module_ptr, "_nyx_module_get_type"); if (NULL == get_device_type_ptr) { nyx_error ("module was not declared with NYX_DECLARE_MODULE"); dlclose (module_ptr); return NYX_ERROR_UNSUPPORTED_DEVICE_TYPE; } if (get_version_major_ptr() != NYX_API_VERSION_MAJOR) { nyx_error("module API version (%i) is different from the library API version (%i)", get_version_major_ptr(), NYX_API_VERSION_MAJOR); dlclose (module_ptr); return NYX_ERROR_INCOMPATIBLE_LIBRARY; } struct nyx_instance_data instance; instance.tokens = tokens; instance.token_count = 0; if (NULL != tokens) { while (tokens[instance.token_count] != NULL) instance.token_count++; } instance.module_ptr = module_ptr; nyx_error_t error = open_ptr ((void*)&instance, &device); if (NYX_ERROR_NONE != error || NULL == device) { nyx_error ("open device method failed"); dlclose (module_ptr); return error; } device->module_ptr = module_ptr; device->type = type; device->open_ptr = open_ptr; device->close_ptr = close_ptr; g_strfreev(instance.tokens); /* * We are done, so we can return success to the user and the device handle. */ *handle_out_ptr = (nyx_device_handle_t)device; return NYX_ERROR_NONE; }
nyx_error_t touchpanel_get_event(nyx_device_t* d, nyx_event_t** e) { int event_count = 0; int event_iter = 0; static int read_input = 0; nyx_event_t* p_generated = NULL; touchpanel_device_t* touch_device = (touchpanel_device_t*) d; /* * Event bookkeeping... */ if(!read_input) { read_input_event(); touch_device->current_event_ptr = NULL; read_input = 1; } /* * Event bookkeeping... */ event_count = touchpanel_event_list.input_filled / sizeof(input_event_t); event_iter = touchpanel_event_list.input_read / sizeof(input_event_t); if(event_iter == event_count) read_input = 0; if (touch_device->current_event_ptr == NULL) { /* * let's allocate new event and hold it here. */ touch_device->current_event_ptr = touch_event_create(); } touch_device->current_event_ptr->_parent.type = NYX_EVENT_TOUCHPANEL; for (; event_iter < event_count; event_iter++) { input_event_t* input_event_ptr; nyx_touchpanel_event_item_t* item_ptr; input_event_ptr = &touchpanel_event_list.input[event_iter]; touchpanel_event_list.input_read += sizeof(input_event_t); switch (input_event_ptr->type) { case EV_FINGERID: item_ptr = touch_event_get_next_item( touch_device->current_event_ptr); if (NULL == item_ptr) { p_generated = (nyx_event_t*) touch_device->current_event_ptr; touch_device->current_event_ptr = touch_event_create(); item_ptr = touch_event_get_next_item( touch_device->current_event_ptr); } if (NULL != item_ptr) { touch_item_reset(item_ptr); item_ptr->finger = input_event_ptr->value * 1000 + input_event_ptr->code; item_ptr->timestamp = get_ts_tval(&(input_event_ptr->time)); } break; case EV_ABS: item_ptr = touch_event_get_current_item( touch_device->current_event_ptr); if (NULL != item_ptr) { if (ABS_X == input_event_ptr->code) { item_ptr->x = input_event_ptr->value; } else if (ABS_Y == input_event_ptr->code) { item_ptr->y = input_event_ptr->value; } else { nyx_error("Unexpected code 0x%x\n",input_event_ptr->code); } } break; case EV_KEY: item_ptr = touch_event_get_current_item( touch_device->current_event_ptr); if (NULL != item_ptr) { if (BTN_TOUCH == input_event_ptr->code) { if (1 == input_event_ptr->value) { item_ptr->state = NYX_TOUCHPANEL_STATE_DOWN; } else { item_ptr->state = NYX_TOUCHPANEL_STATE_UP; } } } break; case EV_SYN: p_generated = (nyx_event_t*) touch_device->current_event_ptr; touch_device->current_event_ptr = NULL; break; default: nyx_warn("Invalid event type (0x%x)", input_event_ptr->type); break; } // Generated event, bail out and let the caller know. if (NULL != p_generated) { break; } } *e = p_generated; return NYX_ERROR_NONE; }