//**************************************************************************
//*                     This file is part of the                           *
//*                MMC - Mpxplay Multimedia Commander                      *
//*                   The source code of MMC is                            *
//*        (C) copyright 1998-2018 by PDSoft (Attila Padar)                *
//*                http://mpxplay.sourceforge.net                          *
//*                  email: mpxplay@freemail.hu                            *
//**************************************************************************
//*  This program is distributed in the hope that it will be useful,       *
//*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
//*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
//*  Please contact with the author (with me) if you want to use           *
//*  or modify this source.                                                *
//**************************************************************************
//function: initialize, load, save gui config

//#define MPXPLAY_USE_DEBUGF 1
#define DISPQT_DEBUG_OUTPUT stdout

#include "mpxplay.h"
#include "control/control.h"
#include "control/cntfuncs.h"
#include "disp_qt.h"
#include "moc_config.h"
#include "moc_mainwindow.h"

extern "C" {
 extern unsigned long mpxplay_config_videoplayer_type;
 extern unsigned long mpxplay_config_video_audiovisualization_type;
 extern char mpxplay_extrafiletypes_config[MPXINI_MAX_CHARDATA_LEN];
#ifdef MPXPLAY_LINK_ORIGINAL_FFMPEG
 extern char mpxplay_config_video_preffered_audiotypes[MPXINI_MAX_CHARDATA_LEN];
 extern char mpxplay_config_video_preferred_language[MPXPLAY_STREAMTYPEINDEX_NUM][MPXINI_MAX_CHARDATA_LEN];
#endif
}

struct mmc_dispqt_config_s mmc_config;

static mpxini_var_s mpxpgui_ini[]={
{"MainWinControl", &mmc_config.mainwin_control,(ARG_HEX | ARG_SAVE) },
{"MainWinState",   &mmc_config.mainwin_state,  ARG_SAVE },
{"MainWinPosX",    &mmc_config.mainwin_pos_x,  (ARG_SIGNED|ARG_SAVE) },
{"MainWinPosY",    &mmc_config.mainwin_pos_y,  (ARG_SIGNED|ARG_SAVE) },
{"MainWinSizeX",   &mmc_config.mainwin_size_x, ARG_SAVE },
{"MainWinSizeY",   &mmc_config.mainwin_size_y, ARG_SAVE },
{"MainWinOpGen",   &mmc_config.mainwin_opacity_general, ARG_SAVE },
{"MainWinOpIna",   &mmc_config.mainwin_opacity_inactive, ARG_SAVE },
{"MainWinOpTra",   &mmc_config.mainwin_opacity_transparent, ARG_SAVE },
{"MainWinOpVSh",   &mmc_config.mainwin_opacity_onvideo_show, ARG_SAVE },
{"MainWinOpVHi",   &mmc_config.mainwin_opacity_onvideo_hide, ARG_SAVE },
{"MainWinOpVSKT",  &mmc_config.mainwin_opacity_showkeep_time, ARG_SAVE },
{"MainWinOpVFIT",  &mmc_config.mainwin_opacity_fadein_time, ARG_SAVE },
{"MainWinOpVFOT",  &mmc_config.mainwin_opacity_fadeout_time, ARG_SAVE },
{"EditorControl",  &mmc_config.editor_flags, (ARG_HEX | ARG_SAVE) },
{"EditorSizeY",    &mmc_config.editor_size_y, ARG_SAVE },
{"FontEditor",     &mmc_config.font_text_name[DISPQT_FONTTEXTID_EDITOR],  (ARG_PACHAR|ARG_SAVE) },
{"FontMainwin",    &mmc_config.font_text_name[DISPQT_FONTTEXTID_MAINWIN], (ARG_PACHAR|ARG_SAVE) },
{"FontDialog",     &mmc_config.font_text_name[DISPQT_FONTTEXTID_DIALOG],  (ARG_PACHAR|ARG_SAVE) },
{"FontSubtitle",   &mmc_config.font_text_name[DISPQT_FONTTEXTID_SUBTITLE],(ARG_PACHAR|ARG_SAVE) },
{"ColorEditFontbase",  &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_FONT],  (ARG_PACHAR|ARG_SAVE) },
{"ColorEditBackGr1",   &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_BACK1], (ARG_PACHAR|ARG_SAVE) },
{"ColorEditBackGr2",   &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_BACK2], (ARG_PACHAR|ARG_SAVE) },
{"ColorEditCursor",    &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_CURSOR], (ARG_PACHAR|ARG_SAVE) },
{"ColorEditMark",      &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_MARK],   (ARG_PACHAR|ARG_SAVE) },
{"ColorEditCurrSong",  &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_CURRSONG], (ARG_PACHAR|ARG_SAVE) },
{"ColorEditNextSong",  &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_NEXTSONG], (ARG_PACHAR|ARG_SAVE) },
{"ColorEditPlayed",    &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_PLAYED],   (ARG_PACHAR|ARG_SAVE) },
{"ColorEditNotPlayed", &mmc_config.color_text_name[DISPQT_COLORTEXTID_EDITOR_NOTPLAYED], (ARG_PACHAR|ARG_SAVE) },
{"TransparentControl", &mmc_config.transparent_control, (ARG_HEX | ARG_SAVE) },
{"VideoControl",       &mmc_config.video_control, (ARG_HEX | ARG_SAVE) },
{"VideoRendererType",  &mmc_config.video_renderer_type, ARG_SAVE },
{"VideoARFullscreen"  ,&mmc_config.video_fullscreen_ar_type, ARG_SAVE },
{"VideoARWindowed",    &mmc_config.video_windowed_ar_type, ARG_SAVE },
{"VideoARCustom",      &mmc_config.video_ar_custom, (ARG_CHAR|ARG_SAVE) },
{"VideoCropType",      &mmc_config.video_crop_type, ARG_SAVE },
{"VideoCropCustom",    &mmc_config.video_crop_custom, (ARG_CHAR|ARG_SAVE) },
{"VideoZoomType",      &mmc_config.video_zoom_type, ARG_SAVE },
{"VideoZoomCustom",    &mmc_config.video_zoom_custom, (ARG_CHAR|ARG_SAVE) },
{"VideoZoomValue",     &mmc_config.video_zoom_value, ARG_SAVE },
{"VideoDeinterlace",   &mmc_config.video_deinterlace_type, ARG_SAVE },
{"VideoRotation",      &mmc_config.video_rotation_type, ARG_SAVE },
{"VideoBrightness",    &mmc_config.video_mixer_values[DISPQT_VIDEO_VMIXERTYPE_BRIGHTNESS], ARG_SAVE },
{"VideoContrast",      &mmc_config.video_mixer_values[DISPQT_VIDEO_VMIXERTYPE_CONTRAST], ARG_SAVE },
{"VideoSaturation",    &mmc_config.video_mixer_values[DISPQT_VIDEO_VMIXERTYPE_SATURATION], ARG_SAVE },
{"VideoHue",           &mmc_config.video_mixer_values[DISPQT_VIDEO_VMIXERTYPE_HUE], ARG_SAVE },
{"AudioVisualization", &mpxplay_config_video_audiovisualization_type, ARG_SAVE },

// placed here, due to mpxplay.ini's limited string data length
{"FileTypesExt",       &mpxplay_extrafiletypes_config[0], (ARG_CHAR|ARG_SAVE) },
#ifdef MPXPLAY_LINK_ORIGINAL_FFMPEG
{"PreferAudioTypes",   &mpxplay_config_video_preffered_audiotypes[0], (ARG_CHAR|ARG_SAVE) },
{"AudioLanguage", &mpxplay_config_video_preferred_language[MPXPLAY_STREAMTYPEINDEX_AUDIO][0], (ARG_CHAR | ARG_CONFIG_SAVE)},
{"SubtitleLange", &mpxplay_config_video_preferred_language[MPXPLAY_STREAMTYPEINDEX_SUBTITLE][0], (ARG_CHAR | ARG_CONFIG_SAVE)},
{"VideoLanguage", &mpxplay_config_video_preferred_language[MPXPLAY_STREAMTYPEINDEX_VIDEO][0], (ARG_CHAR | ARG_CONFIG_SAVE)},
#endif
{NULL, NULL, 0}
};

#define MPXPGUI_INI_CONFIG_LINES (sizeof(mpxpgui_ini) / sizeof(mpxini_var_s))

static const char *config_editortable_colortext_default[DISPQT_COLORTEXTID_EDITOR_MAX] = {
 //"#000000", // DISPQT_COLORTEXTID_EDITOR_FONT : black
 //"#ffffff", // DISPQT_COLORTEXTID_EDITOR_BACK1: white
 //"#f0f0f0", // DISPQT_COLORTEXTID_EDITOR_BACK2: light-gray
 //"#55aaff", // DISPQT_COLORTEXTID_EDITOR_CURSOR: light-blue
 "#aaffff", // DISPQT_COLORTEXTID_EDITOR_FONT : light-cyan
 "#002d4b", // DISPQT_COLORTEXTID_EDITOR_BACK1: dark cyan-blue
 "#002d4b", // DISPQT_COLORTEXTID_EDITOR_BACK2: dark cyan-blue
 "#808080", // DISPQT_COLORTEXTID_EDITOR_CURSOR: gray
 "#ffff00", // DISPQT_COLORTEXTID_EDITOR_MARK:  yellow
 "#00ffff", // DISPQT_COLORTEXTID_EDITOR_CURRSONG: cyan
 "#00ff00", // DISPQT_COLORTEXTID_EDITOR_NEXTSONG: green
 "#c8c8c8", // DISPQT_COLORTEXTID_EDITOR_PLAYED: light-gray
 "#ffffff", // DISPQT_COLORTEXTID_EDITOR_NOTPLAYED: white
};

void mpxplay_dispqt_configini_init(void)
{
	int i;
	struct mmc_dispqt_config_s *gcfg;
	char **color_text_name = &(mmc_config.color_text_name[0]);
	const char **default_colortext = &config_editortable_colortext_default[0];

	memset((void *)&mmc_config, 0, sizeof(mmc_config));

	for(i = 0; i < DISPQT_COLORTEXTID_EDITOR_MAX; i++, color_text_name++, default_colortext++){
		int len = pds_strlen((char *)*default_colortext);
		*color_text_name = new char[len + 4];
		memcpy(*color_text_name, *default_colortext, (len + 1));
	}

	gcfg = &mmc_config;

	gcfg->mainwin_control = DISPQT_CONFIG_MAINWINCTRLS_DEFAULT;
	gcfg->mainwin_pos_x = -1;
	gcfg->mainwin_pos_y = -1;
	gcfg->mainwin_size_x = DISPQT_MAINWIN_INITSIZE_X;
	gcfg->mainwin_size_y = -1;
	gcfg->mainwin_opacity_general = DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_DEFAULT;
	gcfg->mainwin_opacity_transparent = DISPQT_CONFIG_MAINWIN_OPACITY_TRANSPARENT_DEFAULT;
	gcfg->mainwin_opacity_inactive = DISPQT_CONFIG_MAINWIN_OPACITY_INACTIVE_DEFAULT;
	gcfg->mainwin_opacity_onvideo_show = DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_SHOW_DEF;
	gcfg->mainwin_opacity_onvideo_hide = DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_HIDE_DEF;
	gcfg->mainwin_opacity_showkeep_time = DISPQT_CONFIG_MAINWIN_OPACITY_SHOWKEEP_TIMEMS_DEFAULT;
	gcfg->mainwin_opacity_fadein_time = DISPQT_CONFIG_MAINWIN_OPACITY_FADEIN_TIMEMS_DEFAULT;
	gcfg->mainwin_opacity_fadeout_time = DISPQT_CONFIG_MAINWIN_OPACITY_FADEOUT_TIMEMS_DEFAULT;
	gcfg->editor_flags = DISPQT_CONFIG_EDITORFLAGS_DEFAULT;
	gcfg->editor_size_y  = 100;
	gcfg->transparent_control = DISPQT_CONFIG_TRANSCTRLS_DEFAULT;
	gcfg->video_control  = DISPQT_CONFIG_VIDEOCTRLS_DEFAULT;
	gcfg->video_renderer_type = DISPQT_CONFIG_VIDEORENDERTYPE_DEFAULT;
	gcfg->video_fullscreen_ar_type = DISPQT_CONFIG_VIDEOFULLSCREENARTYPE_DEFAULT;
	gcfg->video_windowed_ar_type = DISPQT_CONFIG_VIDEOWINDOWEDARTYPE_DEFAULT;
	gcfg->video_crop_type = DISPQT_CONFIG_VIDEOSRCCROPTYPE_DEFAULT;
	gcfg->video_zoom_type = DISPQT_CONFIG_VIDEOZOOMTYPE_DEFAULT;
	gcfg->video_zoom_value = DISPQT_CONFIG_VIDEOZOOM_CENTER;
	gcfg->video_deinterlace_type = DISPQT_VIDEO_DEINTERLACETYPE_AUTO;
	gcfg->video_rotation_type = DISPQT_VIDEO_ROTATIONTYPE_AUTO;

	for(i = 0; i < DISPQT_VIDEO_VMIXERTYPE_MAX; i++)
		gcfg->video_mixer_values[i] = DISPQT_VMIXER_VALUE_CENTER;

	pds_strcpy(gcfg->video_ar_custom, (char *)DISPQT_CONFIG_VIDEOARCUSTOM_DEFAULTSTR);
	pds_strcpy(gcfg->video_crop_custom, (char *)DISPQT_CONFIG_VIDEOCROPCUSTOM_DEFAULTSTR);
	pds_strcpy(gcfg->video_zoom_custom, (char *)DISPQT_CONFIG_VIDEOZOOMCUSTOM_DEFAULTSTR);
	mpxplay_config_video_audiovisualization_type = DISPQT_CONFIG_AUDIOVISUALIZATIONTYPE_DEFAULT;
}

void mpxplay_dispqt_config_check(void)
{
	struct mmc_dispqt_config_s *gcfg = &mmc_config;
	int i;
#ifdef MPXPLAY_WIN32
	if(!gcfg->font_text_name[DISPQT_FONTTEXTID_EDITOR]){
		char *fonttext_editor = (char *)"MS Sans Serif,8,-1,5,75,0,0,0,0,0";
		unsigned int len = pds_strlen(fonttext_editor) + 1;
		gcfg->font_text_name[DISPQT_FONTTEXTID_EDITOR] = (char *)pds_malloc(len);
		if(gcfg->font_text_name[DISPQT_FONTTEXTID_EDITOR])
			pds_memcpy(gcfg->font_text_name[DISPQT_FONTTEXTID_EDITOR], fonttext_editor, len);
	}
#endif
	if(!gcfg->font_text_name[DISPQT_FONTTEXTID_SUBTITLE]){
		char *fonttext_subtitle = (char *)"Arial,36,-1,5,75,0,0,0,0,0";
		unsigned int len = pds_strlen(fonttext_subtitle) + 1;
		gcfg->font_text_name[DISPQT_FONTTEXTID_SUBTITLE] = (char *)pds_malloc(len);
		if(gcfg->font_text_name[DISPQT_FONTTEXTID_SUBTITLE])
			pds_memcpy(gcfg->font_text_name[DISPQT_FONTTEXTID_SUBTITLE], fonttext_subtitle, len);
	}
	if(gcfg->mainwin_opacity_general < DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_MIN)
		gcfg->mainwin_opacity_general = DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_MIN;
	else if(gcfg->mainwin_opacity_general > DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_MAX)
		gcfg->mainwin_opacity_general = DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_MAX;
	if(gcfg->mainwin_opacity_inactive < DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_MIN)
		gcfg->mainwin_opacity_inactive = DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_MIN;
	else if(gcfg->mainwin_opacity_inactive > DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_MAX)
		gcfg->mainwin_opacity_inactive = DISPQT_CONFIG_MAINWIN_OPACITY_GENERAL_MAX;
	if(gcfg->mainwin_opacity_transparent < DISPQT_CONFIG_MAINWIN_OPACITY_TRANSPARENT_MIN)
		gcfg->mainwin_opacity_transparent = DISPQT_CONFIG_MAINWIN_OPACITY_TRANSPARENT_MIN;
	else if(gcfg->mainwin_opacity_transparent > DISPQT_CONFIG_MAINWIN_OPACITY_TRANSPARENT_MAX)
		gcfg->mainwin_opacity_transparent = DISPQT_CONFIG_MAINWIN_OPACITY_TRANSPARENT_MAX;
	if(gcfg->mainwin_opacity_onvideo_show < DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_SHOW_MIN)
		gcfg->mainwin_opacity_onvideo_show = DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_SHOW_MIN;
	else if(gcfg->mainwin_opacity_onvideo_show > DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_SHOW_MAX)
		gcfg->mainwin_opacity_onvideo_show = DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_SHOW_MAX;
	if(gcfg->mainwin_opacity_onvideo_hide < DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_HIDE_MIN)
		gcfg->mainwin_opacity_onvideo_hide = DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_HIDE_MIN;
	else if(gcfg->mainwin_opacity_onvideo_hide > DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_HIDE_MAX)
		gcfg->mainwin_opacity_onvideo_hide = DISPQT_CONFIG_MAINWIN_OPACITY_ONVIDEO_HIDE_MAX;
	if(gcfg->mainwin_opacity_showkeep_time < DISPQT_CONFIG_MAINWIN_OPACITY_SHOWKEEP_TIMEMS_MIN)
		gcfg->mainwin_opacity_showkeep_time = DISPQT_CONFIG_MAINWIN_OPACITY_SHOWKEEP_TIMEMS_MIN;
	else if(gcfg->mainwin_opacity_showkeep_time > DISPQT_CONFIG_MAINWIN_OPACITY_SHOWKEEP_TIMEMS_MAX)
		gcfg->mainwin_opacity_showkeep_time = DISPQT_CONFIG_MAINWIN_OPACITY_SHOWKEEP_TIMEMS_MAX;
	if(gcfg->mainwin_opacity_fadein_time < DISPQT_CONFIG_MAINWIN_OPACITY_FADEIN_TIMEMS_MIN)
		gcfg->mainwin_opacity_fadein_time = DISPQT_CONFIG_MAINWIN_OPACITY_FADEIN_TIMEMS_MIN;
	else if(gcfg->mainwin_opacity_fadein_time > DISPQT_CONFIG_MAINWIN_OPACITY_FADEIN_TIMEMS_MAX)
		gcfg->mainwin_opacity_fadein_time = DISPQT_CONFIG_MAINWIN_OPACITY_FADEIN_TIMEMS_MAX;
	if(gcfg->mainwin_opacity_fadein_time < DISPQT_CONFIG_MAINWIN_OPACITY_FADEOUT_TIMEMS_MIN)
		gcfg->mainwin_opacity_fadein_time = DISPQT_CONFIG_MAINWIN_OPACITY_FADEOUT_TIMEMS_MIN;
	else if(gcfg->mainwin_opacity_fadein_time > DISPQT_CONFIG_MAINWIN_OPACITY_FADEOUT_TIMEMS_MAX)
		gcfg->mainwin_opacity_fadein_time = DISPQT_CONFIG_MAINWIN_OPACITY_FADEOUT_TIMEMS_MAX;
	if(!(gcfg->transparent_control & DISPQT_CONFIG_TRANSCTRL_DESKTOPTRANS_RESTORE))
		funcbit_disable(gcfg->transparent_control, DISPQT_CONFIG_TRANSCTRL_DESKTOPTRANS_ENABLED);

	if( (gcfg->video_renderer_type > DISPQT_VIDEORENDERTYPE_MAX)
#ifndef MPXPLAY_WIN32
	 || (gcfg->video_render_type == DISPQT_VIDEORENDERTYPE_EVR)
#endif
	){
		gcfg->video_renderer_type = DISPQT_CONFIG_VIDEORENDERTYPE_DEFAULT;
	}

	if(gcfg->video_fullscreen_ar_type >= DISPQT_VIDEOFULLSCREENARTYPE_MAX)
		gcfg->video_fullscreen_ar_type = DISPQT_CONFIG_VIDEOFULLSCREENARTYPE_DEFAULT;
	if(gcfg->video_windowed_ar_type >= DISPQT_VIDEOWINDOWEDARTYPE_MAX)
		gcfg->video_windowed_ar_type = DISPQT_CONFIG_VIDEOWINDOWEDARTYPE_DEFAULT;
	if(gcfg->video_crop_type >= DISPQT_VIDEOSCRCROPTYPE_MAX)
		gcfg->video_crop_type = DISPQT_CONFIG_VIDEOSRCCROPTYPE_DEFAULT;
	if(gcfg->video_zoom_type >= DISPQT_VIDEOFULLSCREENZOOMTYPE_MAX)
		gcfg->video_zoom_type = DISPQT_CONFIG_VIDEOZOOMTYPE_DEFAULT;
	if(gcfg->video_zoom_value < DISPQT_CONFIG_VIDEOZOOM_MIN)
		gcfg->video_zoom_value = DISPQT_CONFIG_VIDEOZOOM_MIN;
	else if(gcfg->video_zoom_value > DISPQT_CONFIG_VIDEOZOOM_MAX)
		gcfg->video_zoom_value = DISPQT_CONFIG_VIDEOZOOM_MAX;
	if(gcfg->video_deinterlace_type >= DISPQT_VIDEO_DEINTERLACETYPE_MAX)
		gcfg->video_deinterlace_type = DISPQT_VIDEO_DEINTERLACETYPE_AUTO;
	if(gcfg->video_rotation_type >= DISPQT_VIDEO_ROTATIONTYPE_MAX)
		gcfg->video_rotation_type = DISPQT_VIDEO_ROTATIONTYPE_AUTO;

	for(i = 0; i < DISPQT_VIDEO_VMIXERTYPE_MAX; i++)
	{
		if(gcfg->video_mixer_values[i] < DISPQT_VMIXER_VALUE_MIN)
			gcfg->video_mixer_values[i] = DISPQT_VMIXER_VALUE_MIN;
		else if(gcfg->video_mixer_values[i] > DISPQT_VMIXER_VALUE_MAX)
			gcfg->video_mixer_values[i] = DISPQT_VMIXER_VALUE_MAX;
	}

	if(mpxplay_config_video_audiovisualization_type >= DISPQT_AUDIOVISUALIZATIONTYPE_MAX)
		mpxplay_config_video_audiovisualization_type = DISPQT_CONFIG_AUDIOVISUALIZATIONTYPE_DEFAULT;

	gcfg->selected_videoplayer_type = mpxplay_config_videoplayer_type;
	if(gcfg->selected_videoplayer_type == MPXPLAY_VIDEOPLAYERTYPE_NONE)
		funcbit_disable(gcfg->video_control, DISPQT_CONFIG_VIDEOCTRL_ENABLE_VIDEO);
	else
		funcbit_enable(gcfg->video_control, DISPQT_CONFIG_VIDEOCTRL_ENABLE_VIDEO);

	mpxplay_control_extrafiletypes_slice();
}

struct mmc_dispqt_config_s *mpxplay_dispqt_configini_get(void)
{
	return &mmc_config;
}

void mpxplay_dispqt_configini_load_from_file(void)
{
	FILE *filehand = NULL;
	char *memfield = NULL;
	int linecount;
	mpxini_part_t pt;
	mpxini_line_t mpxgui_config_lines[MPXPGUI_INI_CONFIG_LINES + 8];

	filehand = mpxplay_control_configfile_open((char *)"MPXP_GUI.INI", "r", NULL, NULL);
	if(!filehand)
		goto err_out_load;
	memset(mpxgui_config_lines, 0, sizeof(mpxgui_config_lines));
	linecount = mpxplay_control_configfile_load(filehand, &memfield, mpxgui_config_lines, (MPXPGUI_INI_CONFIG_LINES + 6), 0);
	if(linecount <= 0)
		goto err_out_load;
	pt.filepos = 0;
	pt.partbegin_linenum = 0;
	pt.partlinenum = linecount;
	mpxplay_control_general_loadini(mpxgui_config_lines, &pt, mpxpgui_ini);

err_out_load:
	mpxplay_dispqt_config_check();
	if(filehand)
		fclose(filehand);
	if(memfield)
		free(memfield);
}

static void dispqt_configini_save_correct_values(void)
{
	struct mmc_dispqt_config_s *gcfg = &mmc_config;
	mpxplay_config_videoplayer_type = gcfg->selected_videoplayer_type;
	if(gcfg->selected_videoplayer_type == MPXPLAY_VIDEOPLAYERTYPE_NONE)
		funcbit_disable(gcfg->video_control, DISPQT_CONFIG_VIDEOCTRL_ENABLE_VIDEO);
	else
		funcbit_enable(gcfg->video_control, DISPQT_CONFIG_VIDEOCTRL_ENABLE_VIDEO);
}

void mpxplay_dispqt_configini_save_to_file(void)
{
	FILE *filehand;
	mpxini_part_t pt;
	mpxini_line_t mpxgui_config_lines[MPXPGUI_INI_CONFIG_LINES + 4];

	dispqt_configini_save_correct_values();

	filehand = mpxplay_control_configfile_open((char *)"MPXP_GUI.INI", "w", NULL, NULL);
	if(!filehand) {
		mpxplay_debugf(DISPQT_DEBUG_OUTPUT, "configsave file open failed!");
		return;
	}
	memset(mpxgui_config_lines, 0, sizeof(mpxgui_config_lines));
	mpxini_var_s *varp = &mpxpgui_ini[0];
	mpxini_line_t *cfg = &mpxgui_config_lines[0];
	for(int i = 0; i < MPXPGUI_INI_CONFIG_LINES; i++, varp++, cfg++){
		cfg->storepoint = varp;
		cfg->varnamep = (char *)varp->name;
		cfg->valuep = (char *)varp->c;
	}
	pt.filepos = 0;
	pt.partbegin_linenum = 0;
	pt.partlinenum = MPXPGUI_INI_CONFIG_LINES;
	mpxplay_control_general_saveini(mpxgui_config_lines, &pt, filehand);
	fclose(filehand);
}
