Rockbox Technical Forums

Support and General Use => Plugins/Viewers => Topic started by: Bilgus on August 15, 2018, 06:20:06 PM

Title: Lua using global_settings, global_status
Post by: Bilgus on August 15, 2018, 06:20:06 PM
This Patch adds access to rockbox global_settings and global_status
http://gerrit.rockbox.org/r/#/c/1901/

here is an example of how to use it
Code: [Select]
require("settings") --settings.lua

local var = {offset = 1, size = 2, type = 3}

local function bytesLE_n(str)
    str = str or ""
    local tbyte={str:byte(1, -1)}
    local bpos = 1
    local num  = 0
    for k = 1,#tbyte do -- (k = #t, 1, -1 for BE)
        num = num + tbyte[k] * bpos
        bpos = bpos * 256
    end
    return num
end

local function format_val(val, var_type)
    local ret = val or 0
    local s_bool = {"False", "True"}
    if var_type == "str" then
        ret = val
    elseif string.find(var_type, "^b") then       
        if(bytesLE_n(val) <= 0) then
            ret = s_bool[1]
        else
            ret = s_bool[2]
        end
    elseif string.find(var_type, "^u_i") then
        -- Lua integers are signed so we need to do a bit of extra processing
        ret = string.format("%u", bytesLE_n(val))
    elseif var_type ~= nil then
        ret = bytesLE_n(val)
    end

    return tostring(ret)
end

local function get_array_elems(st_type)
    --extract the number of elements
    local elems = string.match(st_type,".*%[(%d+)%]")
    return tonumber(elems) or 0
end

local function get_struct_name(st_type)
    --extract the name of a struct
    local name = string.match(st_type,"^s_([^%[%]%s]+)")
    return name
end

local function dump_struct(s_settings, struct_name, elems, k, v, t_output)
    --Internal function dumps structs
    local val, offset, fmtstr, s

    local s_t = rb.system[struct_name]
    if elems > 0 then
        fmtstr = "%s[%d].%s = %s"
        local elem_size = (v[var.size] / elems)

        for i = 0, elems - 1 do
            for k1, v1 in pairs(s_t) do
                offset = v[var.offset] + v1[var.offset] + (elem_size * i)
                val = rb[s_settings](offset, v1[var.size])
                s = string.format (fmtstr, k, i, k1, format_val(val, v1[var.type]))
                t_output[#t_output + 1] = s
            end
        end
    else
        fmtstr = "%s.%s = %s"
        for k1, v1 in pairs(s_t) do
            offset = v[var.offset] + v1[var.offset]
            val = rb[s_settings](offset, v1[var.size])
            s = string.format (fmtstr, k, k1, format_val(val, v1[var.type]))
            t_output[#t_output + 1] = s
        end
    end
end

local function dump_settings(s_settings)
    local t_output = {}
    local val, offset, fmtstr, s

    for k, v in pairs(rb.system[s_settings]) do
        local elems = get_array_elems(v[var.type])
        local struct_name = get_struct_name(v[var.type])

        if struct_name then   
            dump_struct(s_settings, struct_name, elems, k, v, t_output)
        else
            if elems > 0 then
                fmtstr = "%s[%d] = %s"
                local elem_size = (v[var.size] / elems)

                for i = 0, elems - 1 do
                    offset = v[var.offset] + (elem_size * i)
                    val = rb[s_settings](offset, elem_size)
                    s = string.format (fmtstr, k, i, format_val(val, v[var.type]))
                    t_output[#t_output + 1] = s
                end
            else
                fmtstr = "%s = %s"
                offset = v[var.offset]
                val = rb[s_settings](offset, v[var.size])
                s = string.format (fmtstr, k, format_val(val, v[var.type]))
                t_output[#t_output + 1] = s
            end
        end
    end
    return t_output
end


local filename = "/settings.txt"
local file = io.open(filename, "w+") -- overwrite
local t_settings

if not file then
    rb.splash(rb.HZ, "Error writing " .. filename)
    return
end


t_settings = dump_settings('global_status')
file:write("global_status:\n")
file:write(table.concat(t_settings, "\n"))
file:write("\n\n")

t_settings = dump_settings('global_settings')
file:write("global_settings:\n")
file:write(table.concat(t_settings, "\n"))
file:write("\n\n")

file:close()

-- this is how you would write a setting and save it
--local start_directory = rb.system.global_settings.start_directory
--rb.global_settings(start_directory[1], start_directory[2], "/.rockbox")
--rb.settings_save()
--endoffile

Sample output:
Code: [Select]
global_status:
last_screen = 0
viewer_icon_count = 0
resume_elapsed = 174217
resume_crc32 = 4294967295
runtime = 0
resume_index = 0
last_frequency = 0
font_id[0] = 0
topruntime = 379
last_volume_change = 0
resume_offset = 2963338

global_settings:
rec_stop_thres_db = -45
qs_items[0] = -1
qs_items[1] = 8
qs_items[2] = 13
qs_items[3] = -1
peak_meter_release = 8
browse_current = False
backlight_fade_out = True
root_menu_customized = False
scroll_step = 6
colors_file = -
eq_enabled = False
crossfeed_cross_gain = 4294967236
replaygain_settings.noclip = False
replaygain_settings.preamp = 0
replaygain_settings.type = 2
show_path_in_browser = 1
pbe_precut = -25
sbs_file = -
bg_color = 0
rec_start_duration = 0
timeformat = 0
show_filename_ext = 3
bl_filter_first_keypress = False
ff_rewind_min_step = 1
lang_file =
viewers_icon_file = tango_icons_viewers.16x16
alarm_wake_up_screen = 0
bass = 0
crossfade_fade_in_delay = 0
play_frequency = 0
rec_sizesplit = 0
peak_meter_dbfs = True
caption_backlight = False
invert = False
fm_region = 0
eq_band_settings[0].q = 7
eq_band_settings[0].cutoff = 32
eq_band_settings[0].gain = 0
eq_band_settings[1].q = 10
eq_band_settings[1].cutoff = 64
eq_band_settings[1].gain = 0
eq_band_settings[2].q = 10
eq_band_settings[2].cutoff = 125
eq_band_settings[2].gain = 0
eq_band_settings[3].q = 10
eq_band_settings[3].cutoff = 250
eq_band_settings[3].gain = 0
eq_band_settings[4].q = 10
eq_band_settings[4].cutoff = 500
eq_band_settings[4].gain = 0
eq_band_settings[5].q = 10
eq_band_settings[5].cutoff = 1000
eq_band_settings[5].gain = 0
eq_band_settings[6].q = 10
eq_band_settings[6].cutoff = 2000
eq_band_settings[6].gain = 0
eq_band_settings[7].q = 10
eq_band_settings[7].cutoff = 4000
eq_band_settings[7].gain = 0
eq_band_settings[8].q = 10
eq_band_settings[8].cutoff = 8000
eq_band_settings[8].gain = 0
eq_band_settings[9].q = 7
eq_band_settings[9].cutoff = 16000
eq_band_settings[9].gain = 0
bt_selective_softlock_actions = False
surround_balance = 35
fade_on_stop = True
channel_config = 0
autoresume_paths = /podcast:/podcasts
rec_split_method = 0
list_separator_height = 0
lse_color = 46176
cuesheet = False
next_folder = 0
touchpad_sensitivity = 13
crossfeed_direct_gain = 4294967281
fms_file = cabbiev2
hold_lr_for_scroll_in_list = True
scroll_speed = 9
rec_start_thres_linear = 5
lss_color = 65363
glyphs_to_cache = 250
crossfeed_hf_attenuation = 4294967136
cursor_style = 3
sort_dir = 0
list_accel_wait = 3
sleeptimer_on_startup = False
playlist_viewer_icons = True
timestretch_enabled = False
peak_meter_clip_hold = 16
playlist_viewer_track_display = 0
rec_timesplit = 0
volume = -25
dirfilter = 1
car_adapter_mode = False
constrain_next_folder = False
peak_meter_hold = 3
afr_enabled = 0
tagcache_autoupdate = False
battery_display = 0
kbd_file = -
crossfeed = 0
pitch_mode_timestretch = False
rec_stop_postrec = 0
keypress_restarts_sleeptimer = False
talk_dir = 0
peak_meter_max = 0
crossfade_fade_out_mixmode = 0
pitch_mode_semitone = False
autoupdatebookmark = False
sort_case = False
crossfade = 0
backlight_fade_in = True
max_files_in_playlist = 10000
mp3_enc_config.bitrate = 11
battery_capacity = 550
autoloadbookmark = 0
surround_mix = 50
surround_method2 = False
hotkey_wps = 1
rec_frequency = 4
autoresume_enable = False
rec_editable = False
surround_fx1 = 3400
rec_prerecord_time = 0
volume_limit = 12
scroll_delay = 1000
start_directory = /
depth_3d = 0
resume_rewind = 0
hotkey_tree = 0
fmr_file = -
show_icons = True
playlist_viewer_indices = True
usemrb = 0
beep = 0
bidir_limit = 50
playlist_catalog_dir = //Playlists
screen_scroll_step = 16
sleeptimer_duration = 30
volume_type = 0
compressor_settings.threshold = 0
compressor_settings.knee = 1
compressor_settings.makeup_gain = 1
compressor_settings.ratio = 1
compressor_settings.attack_time = 5
compressor_settings.release_time = 500
pause_rewind = 0
ui_vp_config = -
poweroff = 10
brightness = 32
playlist_shuffle = False
rec_source = 0
talk_battery_level = False
skip_length = 0
bl_selective_actions = False
recursive_dir_insert = 1
start_in_screen = 1
backlight_timeout_plugged = 10
rec_stop_gap = 1
backlight_timeout = 0
font_file = 15-Adobe-Helvetica
rec_mic_gain = 23
icon_file = tango_icons.16x16
warnon_erase_dynplaylist = True
flip_display = False
fm_force_mono = False
prevent_skip = False
eq_precut = 0
sort_file = 0
keyclick = 0
talk_filetype = False
list_separator_color = 33808
rec_trigger_mode = 0
keyclick_repeats = 0
balance = 0
talk_file_clip = False
talk_file = 0
wps_file = cabbiev2
bl_selective_actions_mask = 0
offset_out_of_view = False
peak_meter_clipcounter = False
talk_menu = True
party_mode = False
bt_selective_softlock_actions_mask = 0
lst_color = 0
rec_right_gain = 23
fg_color = 52857
backdrop_file = /.rockbox/backdrops/cabbiev2.bmp
stereo_width = 100
tagcache_scan_paths = /
autoresume_automatic = 0
runtimedb = False
tagcache_ram = False
crossfade_fade_out_delay = 0
rec_start_thres_db = -35
rec_format = 0
autocreatebookmark = 0
pbe = 0
shortcuts_replaces_qs = False
morse_input = False
scroll_paginated = False
rec_trigger_type = 0
scrollbar_width = 6
scrollbar = 1
statusbar = 1
max_files_in_dir = 1000
rec_mono_mode = 0
talk_dir_clip = False
peak_meter_min = 60
ff_rewind_accel = 2
dithering_enabled = False
repeat_mode = 0
crossfeed_hf_cutoff = 700
play_selected = True
default_codepage = 0
surround_enabled = 0
treble = 0
rec_stop_thres_linear = 10
cliplight = 0
rec_directory = /
list_accel_start_delay = 2
rec_split_type = 0
surround_fx2 = 320
rec_left_gain = 23
rec_channels = 0
interpret_numbers = 1
audioscrobbler = False
crossfade_fade_out_duration = 2
crossfade_fade_in_duration = 2


Here is a sample of the generated settings.lua file the patch creates...
Code: [Select]
-- Don't change this file!
-- It is automatically generated of settings.h $Revision$
-- member = {offset, size, "type"}

--const= 'const_', signed= '', struct= 's_', unsigned= 'u_',
--#string#= 'str', bool= 'b', char= 'c', double= 'd', int= 'i', long= 'l', uint= 'u_i',

rb.system = {
}

rb.system.global_status = {
resume_index = {0x0, 4, "i"},
resume_crc32 = {0x4, 4, "u_i"},
resume_elapsed = {0x8, 4, "u_i"},
resume_offset = {0xc, 4, "u_i"},
runtime = {0x10, 4, "i"},
topruntime = {0x14, 4, "i"},
last_frequency = {0x18, 4, "i"},
last_screen = {0x1c, 1, "c"},
viewer_icon_count = {0x20, 4, "i"},
last_volume_change = {0x24, 4, "i"},
font_id = {0x28, 4, "i[1]"},
}

rb.system.global_settings = {
volume = {0x0, 4, "i"},
balance = {0x4, 4, "i"},
bass = {0x8, 4, "i"},
treble = {0xc, 4, "i"},
channel_config = {0x10, 4, "i"},
stereo_width = {0x14, 4, "i"},
crossfade = {0x18, 4, "i"},
crossfade_fade_in_delay = {0x1c, 4, "i"},
crossfade_fade_out_delay = {0x20, 4, "i"},
crossfade_fade_in_duration = {0x24, 4, "i"},
crossfade_fade_out_duration = {0x28, 4, "i"},
crossfade_fade_out_mixmode = {0x2c, 4, "i"},
replaygain_settings = {0x30, 12, "s_replaygain_settings"},
crossfeed = {0x3c, 4, "i"},
crossfeed_direct_gain = {0x40, 4, "u_i"},
crossfeed_cross_gain = {0x44, 4, "u_i"},
crossfeed_hf_attenuation = {0x48, 4, "u_i"},
crossfeed_hf_cutoff = {0x4c, 4, "u_i"},
eq_enabled = {0x50, 1, "b"},
eq_precut = {0x54, 4, "u_i"},
eq_band_settings = {0x58, 120, "s_eq_band_setting[10]"},
beep = {0xd0, 4, "i"},
keyclick = {0xd4, 4, "i"},
keyclick_repeats = {0xd8, 4, "i"},
dithering_enabled = {0xdc, 1, "b"},
timestretch_enabled = {0xdd, 1, "b"},
rec_format = {0xe0, 4, "i"},
rec_mono_mode = {0xe4, 4, "i"},
mp3_enc_config = {0xe8, 4, "s_mp3_enc_config"},
rec_source = {0xec, 4, "i"},
rec_frequency = {0xf0, 4, "i"},
rec_channels = {0xf4, 4, "i"},
rec_mic_gain = {0xf8, 4, "i"},
rec_left_gain = {0xfc, 4, "i"},
rec_right_gain = {0x100, 4, "i"},
peak_meter_clipcounter = {0x104, 1, "b"},
rec_editable = {0x105, 1, "b"},
rec_timesplit = {0x108, 4, "i"},
rec_sizesplit = {0x10c, 4, "i"},
rec_split_type = {0x110, 4, "i"},
rec_split_method = {0x114, 4, "i"},
rec_prerecord_time = {0x118, 4, "i"},
rec_directory = {0x11c, 81, "str"},
cliplight = {0x170, 4, "i"},
rec_start_thres_db = {0x174, 4, "i"},
rec_start_thres_linear = {0x178, 4, "i"},
rec_start_duration = {0x17c, 4, "i"},
rec_stop_thres_db = {0x180, 4, "i"},
rec_stop_thres_linear = {0x184, 4, "i"},
rec_stop_postrec = {0x188, 4, "i"},
rec_stop_gap = {0x18c, 4, "i"},
rec_trigger_mode = {0x190, 4, "i"},
rec_trigger_type = {0x194, 4, "i"},
fm_region = {0x198, 4, "i"},
fm_force_mono = {0x19c, 1, "b"},
fmr_file = {0x19d, 33, "str"},
fms_file = {0x1be, 33, "str"},
list_accel_start_delay = {0x1e0, 4, "i"},
list_accel_wait = {0x1e4, 4, "i"},
touchpad_sensitivity = {0x1e8, 4, "i"},
pause_rewind = {0x1ec, 4, "i"},
qs_items = {0x1f0, 16, "i[4]"},
timeformat = {0x200, 4, "i"},
dirfilter = {0x204, 4, "i"},
show_filename_ext = {0x208, 4, "i"},
default_codepage = {0x20c, 4, "i"},
hold_lr_for_scroll_in_list = {0x210, 1, "b"},
play_selected = {0x211, 1, "b"},
party_mode = {0x212, 1, "b"},
audioscrobbler = {0x213, 1, "b"},
cuesheet = {0x214, 1, "b"},
car_adapter_mode = {0x215, 1, "b"},
start_in_screen = {0x218, 4, "i"},
alarm_wake_up_screen = {0x21c, 4, "i"},
ff_rewind_min_step = {0x220, 4, "i"},
ff_rewind_accel = {0x224, 4, "i"},
peak_meter_release = {0x228, 4, "i"},
peak_meter_hold = {0x22c, 4, "i"},
peak_meter_clip_hold = {0x230, 4, "i"},
peak_meter_dbfs = {0x234, 1, "b"},
peak_meter_min = {0x238, 4, "i"},
peak_meter_max = {0x23c, 4, "i"},
wps_file = {0x240, 33, "str"},
sbs_file = {0x261, 33, "str"},
lang_file = {0x282, 33, "str"},
playlist_catalog_dir = {0x2a3, 81, "str"},
skip_length = {0x2f4, 4, "i"},
max_files_in_dir = {0x2f8, 4, "i"},
max_files_in_playlist = {0x2fc, 4, "i"},
volume_type = {0x300, 4, "i"},
battery_display = {0x304, 4, "i"},
show_icons = {0x308, 1, "b"},
statusbar = {0x30c, 4, "i"},
scrollbar = {0x310, 4, "i"},
scrollbar_width = {0x314, 4, "i"},
list_separator_height = {0x318, 4, "i"},
list_separator_color = {0x31c, 4, "i"},
browse_current = {0x320, 1, "b"},
scroll_paginated = {0x321, 1, "b"},
scroll_speed = {0x324, 4, "i"},
bidir_limit = {0x328, 4, "i"},
scroll_delay = {0x32c, 4, "i"},
scroll_step = {0x330, 4, "i"},
autoloadbookmark = {0x334, 4, "i"},
autocreatebookmark = {0x338, 4, "i"},
autoupdatebookmark = {0x33c, 1, "b"},
usemrb = {0x340, 4, "i"},
tagcache_ram = {0x344, 1, "b"},
tagcache_autoupdate = {0x345, 1, "b"},
autoresume_enable = {0x346, 1, "b"},
autoresume_automatic = {0x348, 4, "i"},
autoresume_paths = {0x34c, 81, "str"},
runtimedb = {0x39d, 1, "b"},
tagcache_scan_paths = {0x39e, 81, "str"},
backdrop_file = {0x3ef, 81, "str"},
bg_color = {0x440, 4, "i"},
fg_color = {0x444, 4, "i"},
lss_color = {0x448, 4, "i"},
lse_color = {0x44c, 4, "i"},
lst_color = {0x450, 4, "i"},
colors_file = {0x454, 33, "str"},
repeat_mode = {0x478, 4, "i"},
next_folder = {0x47c, 4, "i"},
constrain_next_folder = {0x480, 1, "b"},
recursive_dir_insert = {0x484, 4, "i"},
fade_on_stop = {0x488, 1, "b"},
playlist_shuffle = {0x489, 1, "b"},
warnon_erase_dynplaylist = {0x48a, 1, "b"},
playlist_viewer_icons = {0x48b, 1, "b"},
playlist_viewer_indices = {0x48c, 1, "b"},
playlist_viewer_track_display = {0x490, 4, "i"},
talk_menu = {0x494, 1, "b"},
talk_dir = {0x498, 4, "i"},
talk_dir_clip = {0x49c, 1, "b"},
talk_file = {0x4a0, 4, "i"},
talk_file_clip = {0x4a4, 1, "b"},
talk_filetype = {0x4a5, 1, "b"},
talk_battery_level = {0x4a6, 1, "b"},
sort_case = {0x4a7, 1, "b"},
sort_dir = {0x4a8, 4, "i"},
sort_file = {0x4ac, 4, "i"},
interpret_numbers = {0x4b0, 4, "i"},
poweroff = {0x4b4, 4, "i"},
battery_capacity = {0x4b8, 4, "i"},
invert = {0x4bc, 1, "b"},
flip_display = {0x4bd, 1, "b"},
cursor_style = {0x4c0, 4, "i"},
screen_scroll_step = {0x4c4, 4, "i"},
show_path_in_browser = {0x4c8, 4, "i"},
offset_out_of_view = {0x4cc, 1, "b"},
icon_file = {0x4cd, 33, "str"},
viewers_icon_file = {0x4ee, 33, "str"},
font_file = {0x50f, 33, "str"},
glyphs_to_cache = {0x530, 4, "i"},
kbd_file = {0x534, 33, "str"},
backlight_timeout = {0x558, 4, "i"},
caption_backlight = {0x55c, 1, "b"},
bl_filter_first_keypress = {0x55d, 1, "b"},
backlight_timeout_plugged = {0x560, 4, "i"},
bt_selective_softlock_actions = {0x564, 1, "b"},
bt_selective_softlock_actions_mask = {0x568, 4, "i"},
bl_selective_actions = {0x56c, 1, "b"},
bl_selective_actions_mask = {0x570, 4, "i"},
backlight_fade_in = {0x574, 1, "b"},
backlight_fade_out = {0x575, 1, "b"},
brightness = {0x578, 4, "i"},
prevent_skip = {0x57c, 1, "b"},
pitch_mode_semitone = {0x57d, 1, "b"},
pitch_mode_timestretch = {0x57e, 1, "b"},
ui_vp_config = {0x57f, 64, "str"},
compressor_settings = {0x5c0, 24, "s_compressor_settings"},
sleeptimer_duration = {0x5d8, 4, "i"},
sleeptimer_on_startup = {0x5dc, 1, "b"},
keypress_restarts_sleeptimer = {0x5dd, 1, "b"},
morse_input = {0x5de, 1, "b"},
hotkey_wps = {0x5e0, 4, "i"},
hotkey_tree = {0x5e4, 4, "i"},
resume_rewind = {0x5e8, 4, "i"},
depth_3d = {0x5ec, 4, "i"},
start_directory = {0x5f0, 81, "str"},
root_menu_customized = {0x641, 1, "b"},
shortcuts_replaces_qs = {0x642, 1, "b"},
play_frequency = {0x644, 4, "i"},
volume_limit = {0x648, 4, "i"},
surround_enabled = {0x64c, 4, "i"},
surround_balance = {0x650, 4, "i"},
surround_fx1 = {0x654, 4, "i"},
surround_fx2 = {0x658, 4, "i"},
surround_method2 = {0x65c, 1, "b"},
surround_mix = {0x660, 4, "i"},
pbe = {0x664, 4, "i"},
pbe_precut = {0x668, 4, "i"},
afr_enabled = {0x66c, 4, "i"},
}

rb.system.replaygain_settings = {
noclip = {0x0, 1, "b"},
type = {0x4, 4, "i"},
preamp = {0x8, 4, "i"},
}

rb.system.eq_band_setting = {
cutoff = {0x0, 4, "i"},
q = {0x4, 4, "i"},
gain = {0x8, 4, "i"},
}

rb.system.compressor_settings = {
threshold = {0x0, 4, "i"},
makeup_gain = {0x4, 4, "i"},
ratio = {0x8, 4, "i"},
knee = {0xc, 4, "i"},
release_time = {0x10, 4, "i"},
attack_time = {0x14, 4, "i"},
}

rb.system.mp3_enc_config = {
bitrate = {0x0, 4, "u_l"},
}