Esempio n. 1
0
void writeInt(int fd, long long unsigned val){
  int nbDigits(long long unsigned int x){
    if ( x == 0){
      return 1;
    }else{
      return (unsigned int)log10(val)+1;
    }
  }	
  int n = nbDigits(val);
  char string[n];
  unsigned int i = n;
  

  string[0]='0';
  while ( i > 0 ) {
    i--;
    string[i] = '0' + (val % 10);
    val = val / 10;
  }

  write(fd,&string,n);
  return;
}
bool interface_keyPressed(unsigned char key, int x, int y)
{ // Returns true if the event was intercepted by the interface
	uint pos;
	uint len;

	if (!renameString)
		return false;

	if (!_curObject)
		return false;

	len = strlen(*renameString);
	pos = len;//renameStringCursor;

	if (key == 8)
	{ // Backspace
		if (len > 0)
		{
			(*renameString)[pos-1] = '\0';
			*renameString = (char*)mem_realloc(*renameString, sizeof(char)*(pos));
		}
	}
	else if (key == 13)
	{ // Enter
		if (len > 0)
		{
			if (_curObject->gen_component.type == COMP_TEXTFIELD)
			{
				textField_t *field = _curObject->gen_component.object.textField;
				updateFieldValue(field);
				_dropFocus();
				if (field->onDeselected) field->onDeselected(false);
			}
			else if (_curObject->gen_component.type == COMP_LIST)
			{
				list_t *list = _curObject->gen_component.object.list;
				int index = list->selectIndex;
				strcpy_safe(list->entries[index].dynValue ? *list->entries[index].dynValue : list->entries[index].value, *renameString);
				_dropFocus();
				list->onSelect(index); // Update it	
			}
		}
	}
	else if (key == 27)
	{ // Escape
		if (_curObject->gen_component.type == COMP_TEXTFIELD)
		{
			textField_t *field = _curObject->gen_component.object.textField;
			_dropFocus();
			if (field->onDeselected) field->onDeselected(true);
		}
		else
		{
			_dropFocus();
		}
	}
	else
	{ // Normal input
		if (_curObject->gen_component.type == COMP_TEXTFIELD && _curObject->gen_component.object.textField->type != FIELDTYPE_TEXT)
		{ // Apply rules to select what we can type
			textField_t *field = _curObject->gen_component.object.textField;
			if (field->type == FIELDTYPE_BINARY)
			{ // Only two choices here really
				if (key != '0' && key != '1')
				{
					return true;
				}
			}
			else
			{
				if (key < '0' || key > '9')
				{
					if (key == '.' && field->type != FIELDTYPE_FLOAT)
					{
						return true;
					}
				}

				if ((field->max > 0 && pos >= nbDigits(field->max)) || pos >= 8)
				{
					return true;
				}
			}
		}
		else
		{ // Take most things
			if ((key < 'a' || key > 'z') &&
				(key < 'A' || key > 'Z') &&
				(key < '0' || key > '9') &&
				!_isAcceptedSpecialChar(key) &&
				((key != ' ') || pos == 0)) // Can't start with a space
			{
				return true;
			}

			if (pos >= 15)
			{
				return true;
			}
		}
		
		*renameString = (char*)mem_realloc(*renameString, sizeof(char)*(pos+2));
		(*renameString)[pos] = key;
		(*renameString)[pos+1] = '\0';
	}

	return true;
}