Exemple #1
0
static int new_pref(lua_State* L, pref_type_t type) {
    const gchar* label = luaL_optstring(L,1,NULL);
    const gchar* descr = luaL_optstring(L,3,"");

    Pref pref = (wslua_pref_t *)g_malloc(sizeof(wslua_pref_t));
    pref->name = NULL;
    pref->label = label ? g_strdup(label) : NULL;
    pref->desc = g_strdup(descr);
    pref->type = type;
    pref->next = NULL;
    pref->proto = NULL;

    switch(type) {
        case PREF_BOOL: {
            gboolean def = wslua_toboolean(L,2);
            pref->value.b = def;
            break;
        }
        case PREF_UINT: {
            guint32 def = wslua_optgint32(L,2,0);
            pref->value.u = def;
            break;
        }
        case PREF_STRING: {
            gchar* def = g_strdup(luaL_optstring(L,2,""));
            pref->value.s = def;
            break;
        }
        case PREF_ENUM: {
            guint32 def = wslua_optgint32(L,2,0);
            enum_val_t *enum_val = get_enum(L,4);
            gboolean radio = wslua_toboolean(L,5);
            pref->value.e = def;
            pref->info.enum_info.enumvals = enum_val;
            pref->info.enum_info.radio_buttons = radio;
            break;
        }
        case PREF_RANGE: {
            range_t *range = get_range(L,2,4);
            guint32 max = wslua_optgint32(L,4,0);
            pref->value.r = range;
            pref->info.max_value = max;
            break;
        }
        case PREF_STATIC_TEXT: {
            /* This is just a static text. */
            break;
        }
        default:
            g_assert_not_reached();
            break;

    }

    pushPref(L,pref);
    return 1;
}
Exemple #2
0
static int new_pref(lua_State* L, pref_type_t type) {
    const gchar* label = luaL_optstring(L,1,NULL);
    const gchar* descr = luaL_optstring(L,3,"");

    Pref pref = (wslua_pref_t *)g_malloc0(sizeof(wslua_pref_t));
    pref->label = g_strdup(label);
    pref->desc = g_strdup(descr);
    pref->type = type;

    switch(type) {
        case PREF_BOOL: {
            gboolean def = wslua_toboolean(L,2);
            pref->value.b = def;
            break;
        }
        case PREF_UINT: {
            guint32 def = wslua_optgint32(L,2,0);
            pref->value.u = def;
            break;
        }
        case PREF_STRING: {
            gchar* def = g_strdup(luaL_optstring(L,2,""));
            /*
             * prefs_register_string_preference() assumes that the
             * variable for the preference points to a static
             * string that is the initial (default) value of the
             * preference.  It makes a g_strdup()ed copy of that
             * string, and assigns a pointer to that string to
             * the variable.
             *
             * Our default string is *not* a static string, it's
             * a g_strdup()ed copy of a string from Lua, so it would
             * be leaked.
             *
             * We save it in info.default_s, as well as setting the
             * initial value of the preference from it, so that we
             * can free it after prefs_register_string_preference()
             * returns.
             *
             * (Would that we were programming in a language where
             * the details of memory management were handled by the
             * compiler and language support....)
             */
            pref->value.s = def;
            pref->info.default_s = def;
            break;
        }
        case PREF_ENUM: {
            guint32 def = wslua_optgint32(L,2,0);
            enum_val_t *enum_val = get_enum(L,4);
            gboolean radio = wslua_toboolean(L,5);
            pref->value.e = def;
            pref->info.enum_info.enumvals = enum_val;
            pref->info.enum_info.radio_buttons = radio;
            break;
        }
        case PREF_RANGE: {
            range_t *range = get_range(L,2,4);
            guint32 max = wslua_optgint32(L,4,0);
            pref->value.r = range;
            pref->info.max_value = max;
            break;
        }
        case PREF_STATIC_TEXT: {
            /* This is just a static text. */
            break;
        }
        default:
            g_assert_not_reached();
            break;

    }

    pushPref(L,pref);
    return 1;
}