Пример #1
0
static Bool
kconfigStringToValue (CompObject      *object,
		      QString	      str,
		      CompOptionType  type,
		      CompOptionValue *value)
{
    switch (type) {
    case CompOptionTypeBool:
	value->b = str.toInt () ? TRUE : FALSE;
	break;
    case CompOptionTypeFloat:
	value->f = str.toFloat ();
	break;
    case CompOptionTypeString:
	value->s = strdup (str.ascii ());
	if (!value->s)
	    return FALSE;
	break;
    case CompOptionTypeColor:
	if (!stringToColor (str.ascii (), value->c))
	    return FALSE;
	break;
    case CompOptionTypeKey:
	while (object && object->type != COMP_OBJECT_TYPE_DISPLAY)
	    object = object->parent;

	if (!object)
	    return FALSE;

	stringToKeyAction (GET_CORE_DISPLAY (object), str.ascii (),
			   &value->action);
	break;
    case CompOptionTypeButton:
	while (object && object->type != COMP_OBJECT_TYPE_DISPLAY)
	    object = object->parent;

	if (!object)
	    return FALSE;

	stringToButtonAction (GET_CORE_DISPLAY (object), str.ascii (),
			      &value->action);
	break;
    case CompOptionTypeEdge:
	value->action.edgeMask = stringToEdgeMask (str.ascii ());
	break;
    case CompOptionTypeBell:
	value->action.bell = str.toInt () ? TRUE : FALSE;
	break;
    case CompOptionTypeMatch:
	matchInit (&value->match);
	matchAddFromString (&value->match, str.ascii ());
	break;
    default:
	return FALSE;
    }

    return TRUE;
}
Пример #2
0
static QString
kconfigValueToString (CompObject      *object,
		      CompOptionType  type,
		      CompOptionValue *value)
{
    QString str;

    switch (type) {
    case CompOptionTypeBool:
	str = QString::number (value->b ? TRUE : FALSE);
	break;
    case CompOptionTypeFloat:
	str = QString::number (value->f);
	break;
    case CompOptionTypeString:
	str = QString (value->s);
	break;
    case CompOptionTypeColor: {
	char *color;

	color = colorToString (value->c);
	if (color)
	{
	    str = QString (color);
	    free (color);
	}
    } break;
    case CompOptionTypeKey: {
	char *action = NULL;

	while (object && object->type != COMP_OBJECT_TYPE_DISPLAY)
	    object = object->parent;

	if (object)
	    action = keyActionToString (GET_CORE_DISPLAY (object),
					&value->action);
	if (action)
	{
	    str = QString (action);
	    free (action);
	}
    } break;
    case CompOptionTypeButton: {
	char *action = NULL;

	while (object && object->type != COMP_OBJECT_TYPE_DISPLAY)
	    object = object->parent;

	if (object)
	    action = buttonActionToString (GET_CORE_DISPLAY (object),
					   &value->action);
	if (action)
	{
	    str = QString (action);
	    free (action);
	}
    } break;
    case CompOptionTypeEdge: {
	char *edge;

	edge = edgeMaskToString (value->action.edgeMask);
	if (edge)
	{
	    str = QString (edge);
	    free (edge);
	}
    } break;
    case CompOptionTypeBell:
	str = QString::number (value->action.bell ? TRUE : FALSE);
	break;
    case CompOptionTypeMatch: {
	char *match;

	match = matchToString (&value->match);
	if (match)
	{
	    str = QString (match);
	    free (match);
	}
    }
    default:
	break;
    }

    return str;
}
static void
ccpSetValueToValue (CompObject      *object,
		    CCSSettingValue *sv,
		    CompOptionValue *v,
		    CCSSettingType  type)
{
    switch (type)
    {
    case TypeInt:
	v->i = sv->value.asInt;
	break;
    case TypeFloat:
	v->f = sv->value.asFloat;
	break;
    case TypeBool:
	v->b = sv->value.asBool;
	break;
    case TypeColor:
	{
	    int i;

	    for (i = 0; i < 4; i++)
		v->c[i] = sv->value.asColor.array.array[i];
	}
	break;
    case TypeString:
	v->s = strdup (sv->value.asString);
	break;
    case TypeMatch:
	matchInit (&v->match);
	matchAddFromString (&v->match, sv->value.asMatch);
	break;
    case TypeKey:
	{
	    CompDisplay *d;

	    while (object && object->type != COMP_OBJECT_TYPE_DISPLAY)
		object = object->parent;

	    if (!object)
		return;

	    d = GET_CORE_DISPLAY (object);

	    v->action.key.keycode =
		(sv->value.asKey.keysym != NoSymbol) ?
		XKeysymToKeycode (d->display, sv->value.asKey.keysym) : 0;

	    v->action.key.modifiers = sv->value.asKey.keyModMask;

	    if (v->action.key.keycode || v->action.key.modifiers)
		v->action.type = CompBindingTypeKey;
	    else
		v->action.type = CompBindingTypeNone;
	}
	break;
    case TypeButton:
	{
	    v->action.button.button = sv->value.asButton.button;
	    v->action.button.modifiers = sv->value.asButton.buttonModMask;
	    v->action.edgeMask = sv->value.asButton.edgeMask;

	    if (v->action.button.button || v->action.button.modifiers)
	    {
		if (sv->value.asButton.edgeMask)
		    v->action.type = CompBindingTypeEdgeButton;
		else
		    v->action.type = CompBindingTypeButton;
	    }
	    else
		v->action.type = CompBindingTypeNone;
	}
	break;
    case TypeEdge:
	{
	    v->action.edgeMask = sv->value.asEdge;
	}
	break;
    case TypeBell:
	{
	    v->action.bell = sv->value.asBell;
	}
	break;
    default:
	break;
    }
}
static void
ccpInitValue (CompObject      *object,
	      CCSSettingValue *value,
	      CompOptionValue *from,
	      CCSSettingType  type)
{
    switch (type)
    {
    case TypeInt:
	value->value.asInt = from->i;
	break;
    case TypeFloat:
	value->value.asFloat = from->f;
	break;
    case TypeBool:
	value->value.asBool = from->b;
	break;
    case TypeColor:
	{
	    int i;

	    for (i = 0; i < 4; i++)
		value->value.asColor.array.array[i] = from->c[i];
	}
	break;
    case TypeString:
	value->value.asString = strdup (from->s);
	break;
    case TypeMatch:
	value->value.asMatch = matchToString (&from->match);
	break;
    case TypeKey:
	if (from->action.type & CompBindingTypeKey)
	{
	    CompDisplay *d;

	    while (object && object->type != COMP_OBJECT_TYPE_DISPLAY)
		object = object->parent;

	    if (!object)
		return;

	    d = GET_CORE_DISPLAY (object);

	    value->value.asKey.keysym =
		XKeycodeToKeysym (d->display, from->action.key.keycode, 0);
	    value->value.asKey.keyModMask = from->action.key.modifiers;
	}
	else
	{
	    value->value.asKey.keysym = 0;
	    value->value.asKey.keyModMask = 0;
	}
    case TypeButton:
	if (from->action.type & CompBindingTypeButton)
	{
	    value->value.asButton.button = from->action.button.button;
	    value->value.asButton.buttonModMask =
		from->action.button.modifiers;
	    value->value.asButton.edgeMask = 0;
	}
	else if (from->action.type & CompBindingTypeEdgeButton)
	{
	    value->value.asButton.button = from->action.button.button;
	    value->value.asButton.buttonModMask =
		from->action.button.modifiers;
	    value->value.asButton.edgeMask = from->action.edgeMask;
	}
	else
	{
	    value->value.asButton.button = 0;
	    value->value.asButton.buttonModMask = 0;
	    value->value.asButton.edgeMask = 0;
	}
	break;
    case TypeEdge:
	value->value.asEdge = from->action.edgeMask;
	break;
    case TypeBell:
	value->value.asBell = from->action.bell;
	break;
    default:
	break;
    }
}
Пример #5
0
static Bool
iniLoadOptions (CompObject *object,
		const char *plugin)
{
    char         *filename, *directory, *fullPath;
    FILE         *optionFile;
    Bool         loadRes, reSave = FALSE;
    IniFileData *fileData;

    filename = directory = fullPath = NULL;
    optionFile = NULL;
    fileData = NULL;

    if (!iniGetFilename (object, plugin, &filename))
	return FALSE;

    fileData = iniGetFileDataFromFilename (filename);
    if (!fileData || (fileData && fileData->blockReads))
    {
	free(filename);
	return FALSE;
    }

    if (!iniGetHomeDir (&directory))
    {
	free (filename);
	return FALSE;
    }

    fullPath = malloc (sizeof (char) * (strlen (filename) + strlen (directory) + 2));
    if (!fullPath)
    {
	free (filename);
	free (directory);
	return FALSE;
    }

    sprintf(fullPath, "%s/%s", directory, filename);

    optionFile = fopen (fullPath, "r");

    if (!optionFile && iniMakeDirectories ())
	optionFile = fopen (fullPath, "r");

    if (!optionFile)
    {
	if (!plugin && object->type == COMP_OBJECT_TYPE_DISPLAY)
	{
	    CompOptionValue value;
	    value.list.value = malloc (NUM_DEFAULT_PLUGINS * sizeof (CompListValue));
	    if (!value.list.value)
	    {
		free (filename);
		free (directory);
		free (fullPath);
		return FALSE;
	    }

	    if (!csvToList (GET_CORE_DISPLAY (object), DEFAULT_PLUGINS,
		            &value.list,
		            CompOptionTypeString))
	    {
		free (filename);
		free (directory);
		free (fullPath);
		return FALSE;
	    }

	    value.list.type = CompOptionTypeString;

	    compLogMessage ("ini", CompLogLevelWarn,
			    "Could not open main display config file %s",
			    fullPath);
	    compLogMessage ("ini", CompLogLevelWarn,
			    "Loading default plugins (%s)", DEFAULT_PLUGINS);

	    (*core.setOptionForPlugin) (object,
					"core", "active_plugins",
					&value);

	    free (value.list.value);

	    fileData->blockWrites = FALSE;

	    iniSaveOptions (object, plugin);

	    fileData->blockWrites = TRUE;

	    optionFile = fopen (fullPath, "r");

	    if (!optionFile)
	    {
		free (filename);
		free (directory);
		free (fullPath);
		return FALSE;
	    }
	}
	else
	{
	    compLogMessage ("ini", CompLogLevelWarn,
			    "Could not open config file %s - "
			    "using defaults for %s",
			    fullPath, plugin ? plugin : "core");

	    fileData->blockWrites = FALSE;

	    iniSaveOptions (object, plugin);

	    fileData->blockWrites = TRUE;

	    optionFile = fopen (fullPath, "r");
	    if (!optionFile)
	    {
		free (filename);
		free (directory);
		free (fullPath);
		return FALSE;
	    }
	}
    }

    fileData->blockWrites = TRUE;

    loadRes = iniLoadOptionsFromFile (optionFile, object, plugin, &reSave);

    fileData->blockWrites = FALSE;

    fclose (optionFile);

    if (loadRes && reSave)
    {
	fileData->blockReads = TRUE;
	iniSaveOptions (object, plugin);
	fileData->blockReads = FALSE;
    }

    free (filename);
    free (directory);
    free (fullPath);

    return TRUE;
}
Пример #6
0
static Bool
iniSaveOptions (CompObject *object,
		const char *plugin)
{
    CompOption *option = NULL;
    int	       nOption = 0;
    char       *filename, *directory, *fullPath, *strVal = NULL;

    if (plugin)
    {
	CompPlugin *p;
	p = findActivePlugin (plugin);
	if (!p)
	    return FALSE;

	option = (*p->vTable->getObjectOptions) (p, object, &nOption);
    }
    else
    {
	return FALSE;
    }

    if (!option)
	return FALSE;

    if (!iniGetFilename (object, plugin, &filename))
	return FALSE;

    IniFileData *fileData;

    fileData = iniGetFileDataFromFilename (filename);
    if (!fileData || (fileData && fileData->blockWrites))
    {
	free (filename);
	return FALSE;
    }

    if (!iniGetHomeDir (&directory))
	return FALSE;

    fullPath = malloc (sizeof (char) * (strlen (filename) + strlen (directory) + 2));
    if (!fullPath)
    {
	free (filename);
	free (directory);
	return FALSE;
    }

    sprintf (fullPath, "%s/%s", directory, filename);

    FILE *optionFile = fopen (fullPath, "w");

    if (!optionFile && iniMakeDirectories ())
	optionFile = fopen (fullPath, "w");

    if (!optionFile)
    {
	compLogMessage ("ini", CompLogLevelError,
			"Failed to write to %s, check you " \
			"have the correct permissions", fullPath);
	free (filename);
	free (directory);
	free (fullPath);
	return FALSE;
    }

    fileData->blockReads = TRUE;

    Bool status, firstInList;
    while (nOption--)
    {
	status = FALSE;
	int i;

	switch (option->type)
	{
	case CompOptionTypeBool:
	case CompOptionTypeInt:
	case CompOptionTypeFloat:
	case CompOptionTypeString:
	case CompOptionTypeColor:
	case CompOptionTypeKey:
	case CompOptionTypeButton:
	case CompOptionTypeEdge:
	case CompOptionTypeBell:
	case CompOptionTypeMatch:
	    strVal = iniOptionValueToString (GET_CORE_DISPLAY (object),
					     &option->value, option->type);
		if (strVal)
		{
		    fprintf (optionFile, "%s=%s\n", option->name, strVal);
		    free (strVal);
		}
		else
		    fprintf (optionFile, "%s=\n", option->name);
		break;
	case CompOptionTypeList:
	    firstInList = TRUE;
	    switch (option->value.list.type)
	    {
	    case CompOptionTypeBool:
	    case CompOptionTypeInt:
	    case CompOptionTypeFloat:
	    case CompOptionTypeString:
	    case CompOptionTypeColor:
	    case CompOptionTypeMatch:
	    {
		int stringLen = MAX_OPTION_LENGTH * option->value.list.nValue;
		char *itemVal;

		strVal = malloc (sizeof(char) * stringLen);
		if (!strVal) {
		    fclose(optionFile);
		    free(fullPath);
		    return FALSE;
		}
		strcpy (strVal, "");
		firstInList = TRUE;

		for (i = 0; i < option->value.list.nValue; i++)
		{
		    itemVal =
			iniOptionValueToString (GET_CORE_DISPLAY (object),
						&option->value.list.value[i],
						option->value.list.type);
		    if (!firstInList)
		        strncat (strVal, ",", stringLen);
		    firstInList = FALSE;
			
		    if (itemVal)
		    {
			strncat (strVal, itemVal, stringLen);
			free (itemVal);
		    }
		}

		fprintf (optionFile, "%s=%s\n", option->name, strVal);
		free (strVal);
		break;
	    }
	    default:
		compLogMessage ("ini", CompLogLevelWarn,
				"Unknown list option type %d, %s\n",
				option->value.list.type,
				optionTypeToString (option->value.list.type));
		break;
	    }
		break;
	default:
		break;
	}

	option++;
    }

    fileData->blockReads = FALSE;

    fclose (optionFile);

    free (filename);
    free (directory);
    free (fullPath);

    return TRUE;
}
Пример #7
0
static Bool
iniLoadOptionsFromFile (FILE       *optionFile,
			CompObject *object,
			const char *plugin,
			Bool       *reSave)
{
    CompOption      *option = NULL, *o;
    CompPlugin      *p = NULL;
    CompOptionValue value;
    char            *optionName = NULL, *optionValue = NULL;
    char            tmp[MAX_OPTION_LENGTH];
    int             nOption, nOptionRead = 0;
    Bool            status = FALSE, hasValue = FALSE;

    if (plugin)
    {
	p = findActivePlugin (plugin);
	if (!p)
	{
	    compLogMessage ("ini", CompLogLevelWarn,
			    "Could not find running plugin " \
			    "%s (iniLoadOptionsFromFile)", plugin);
	    return FALSE;
	}
    }
    else
    {
	return FALSE;
    }

    if (p->vTable->getObjectOptions)
	option = (*p->vTable->getObjectOptions) (p, object, &nOption);

    while (fgets (tmp, MAX_OPTION_LENGTH, optionFile) != NULL)
    {
	status = FALSE;

	if (!iniParseLine (tmp, &optionName, &optionValue))
	{
	    compLogMessage ("ini", CompLogLevelWarn,
			    "Ignoring line '%s' in %s", tmp, plugin);
	    continue;
	}

	if (option)
	{
	    o = compFindOption (option, nOption, optionName, 0);
	    if (o)
	    {
		value = o->value;

		switch (o->type)
		{
		case CompOptionTypeBool:
		    hasValue = TRUE;
		    value.b = (Bool) atoi (optionValue);
			break;
		case CompOptionTypeInt:
		    hasValue = TRUE;
		    value.i = atoi (optionValue);
			break;
		case CompOptionTypeFloat:
		    hasValue = TRUE;
		    value.f = atof (optionValue);
			break;
		case CompOptionTypeString:
		    hasValue = TRUE;
		    value.s = strdup (optionValue);
			break;
		case CompOptionTypeColor:
		    hasValue = stringToColor (optionValue, value.c);
			break;
		case CompOptionTypeKey:
		    hasValue = TRUE;
		    stringToKeyAction (GET_CORE_DISPLAY (object),
				       optionValue, &value.action);
		    break;
		case CompOptionTypeButton:
		    hasValue = TRUE;
		    stringToButtonAction (GET_CORE_DISPLAY (object),
					  optionValue, &value.action);
		    break;
		case CompOptionTypeEdge:
		    hasValue = TRUE;
		    value.action.edgeMask = stringToEdgeMask (optionValue);
		    break;
		case CompOptionTypeBell:
		    hasValue = TRUE;
		    value.action.bell = (Bool) atoi (optionValue);
		    break;
		case CompOptionTypeList:
		    hasValue = csvToList (GET_CORE_DISPLAY (object),
					  optionValue,
					  &value.list, value.list.type);
			break;
		case CompOptionTypeMatch:
		    hasValue = TRUE;
		    matchInit (&value.match);
		    matchAddFromString (&value.match, optionValue);
			break;
		default:
			break;
		}

		if (hasValue)
		{
		    status = (*core.setOptionForPlugin) (object,
							 plugin,
							 optionName,
							 &value);

		    if (o->type == CompOptionTypeMatch)
		    {
			matchFini (&value.match);
		    }
		}

		nOptionRead++;
	    }
	}

	/* clear up */
	if (optionName)
	    free (optionName);
	if (optionValue)
	    free (optionValue);
    }

    if (nOption != nOptionRead)
    {
	*reSave = TRUE;
    }

    return TRUE;
}
Пример #8
0
static Bool
fuseInitValueFromString(CompObject *object,
                        CompOptionValue *value,
                        CompOptionType type,
                        char *str)
{
   switch (type)
     {
      case CompOptionTypeBool:
        value->b = strcmp(str, "true") ? FALSE : TRUE;
        break;

      case CompOptionTypeInt:
        value->i = atoi(str);
        break;

      case CompOptionTypeFloat:
        value->f = strtod(str, NULL);
        break;

      case CompOptionTypeString:
        value->s = strdup(str);
        break;

      case CompOptionTypeColor:
        if (!stringToColor(str, value->c))
          return FALSE;
        break;

      case CompOptionTypeKey:
        while (object && object->type != COMP_OBJECT_TYPE_DISPLAY)
          object = object->parent;

        if (!object)
          return FALSE;

        stringToKeyAction(GET_CORE_DISPLAY(object), str, &value->action);
        break;

      case CompOptionTypeButton:
        while (object && object->type != COMP_OBJECT_TYPE_DISPLAY)
          object = object->parent;

        if (!object)
          return FALSE;

        stringToButtonAction(GET_CORE_DISPLAY(object), str, &value->action);
        break;

      case CompOptionTypeEdge:
        value->action.edgeMask = stringToEdgeMask(str);
        break;

      case CompOptionTypeBell:
        value->action.bell = strcmp(str, "true") ? FALSE : TRUE;
        break;

      case CompOptionTypeMatch:
        matchInit(&value->match);
        matchAddFromString(&value->match, str);
        break;

      default:
        return FALSE;
     }

   return TRUE;
}