#include "ubl-settings-video.h"

config main_config;

void on_resolutions_unsupported_show(GtkToggleButton *, monitor_edit_window *window){
    on_port_chosen_changed(NULL,window);
}

void on_save_done(main_window *, config_str output, int size){
	char *final_output = yon_char_parsed_to_string(output,size,"");
	yon_debug_output("%s\n",final_output);
	free(final_output);
	yon_char_parsed_free(output,size);
    textdomain(template_ui_LocaleName);
	switch (main_config.save_config){
		case YON_CONFIG_GLOBAL:
			yon_ubl_status_box_render(GLOBAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case YON_CONFIG_LOCAL:
			yon_ubl_status_box_render(LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case YON_CONFIG_BOTH:
			yon_ubl_status_box_render(GLOBAL_LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
        case YON_CONFIG_CUSTOM:
			yon_ubl_status_box_render(SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
            break;
	}
    textdomain(LocaleName);
}

void yon_interface_save(){
    char *dpi = NULL;
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->AutoDriverCombo))){
        case 0: yon_config_remove_by_key(VGADRV_AUTO);
            break;
        case 1: yon_config_register(VGADRV_AUTO,VGADRV_AUTO_command,"no");
            break;
        case 2: yon_config_register(VGADRV_AUTO,VGADRV_AUTO_command,"yes");
            break;
    }
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->FreeDriverCombo))){
        case 0: yon_config_remove_by_key(VGADRV_NOFREE);
            break;
        case 1: yon_config_register(VGADRV_NOFREE,VGADRV_NOFREE_command,"yes");
            break;
        case 2: yon_config_register(VGADRV_NOFREE,VGADRV_NOFREE_command,"nouveau");
            break;
        case 3: yon_config_register(VGADRV_NOFREE,VGADRV_NOFREE_command,"radeon");
            break;
        case 4: yon_config_register(VGADRV_NOFREE,VGADRV_NOFREE_command,"no");
            break;
    }
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->FailsafeAMDCombo))){
        case 0: yon_config_remove_by_key(FAILSAFEATI);
            break;
        case 1: yon_config_register(FAILSAFEATI,FAILSAFEATI_command,"no");
            break;
        case 2: yon_config_register(FAILSAFEATI,FAILSAFEATI_command,"fbdev");
            break;
    }
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->FailsafeNvidiaCombo))){
        case 0: yon_config_remove_by_key(FAILSAFENVIDIA);
            break;
        case 1: yon_config_register(FAILSAFENVIDIA,FAILSAFENVIDIA_command,"no");
            break;
        case 2: yon_config_register(FAILSAFENVIDIA,FAILSAFENVIDIA_command,"fbdev");
            break;
    }
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->DontSwitchDisplaysOffCombo))){
        case 0: yon_config_remove_by_key(XORG_EXT);
            break;
        case 1: yon_config_register(XORG_EXT,XORG_EXT_command,"nodpms");
            break;
        case 2: yon_config_register(XORG_EXT,XORG_EXT_command,"dpms");
            break;
    }
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->DisplayScalingCombo))){
        case 0: yon_config_remove_by_key(XORG_DPI);
            break;
        case 1: yon_config_register(XORG_DPI,XORG_DPI_command,"auto");
            break;
        default: dpi = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(main_config.widgets->DisplayScalingCombo));
            yon_char_divide_search_self(dpi," ",-1);
            yon_config_register(XORG_DPI,XORG_DPI_command,dpi);
            break;
    }
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->OptirunCombo))){
        case 0: yon_config_remove_by_key(OPTIRUN);
            break;
        case 1: yon_config_register(OPTIRUN,OPTIRUN_command,"no");
            break;
        case 2: if (!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(main_config.widgets->OptirunEntry)))){
                yon_config_register(OPTIRUN,OPTIRUN_command,(char*)gtk_entry_get_text(GTK_ENTRY(main_config.widgets->OptirunEntry)));
            }
            break;
    }
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->PrimusrunCombo))){
        case 0: yon_config_remove_by_key(PRIMUSRUN);
            break;
        case 1: yon_config_register(PRIMUSRUN,PRIMUSRUN_command,"no");
            break;
        case 2: if (!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(main_config.widgets->PrimusrunEntry)))){
                yon_config_register(PRIMUSRUN,PRIMUSRUN_command,(char*)gtk_entry_get_text(GTK_ENTRY(main_config.widgets->PrimusrunEntry)));
            }
            break;
    }
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(main_config.widgets->DisplayDiscreteOnly))){
        case 0:
            yon_config_remove_by_key(INTEGRATEDVGA);
            break;
        case 1:yon_config_register(INTEGRATEDVGA,INTEGRATEDVGA_command,"no");
            break;
        case 2:yon_config_register(INTEGRATEDVGA,INTEGRATEDVGA_command,"yes");
            break;
    }
}

void yon_interface_update(){
    g_hash_table_remove_all(main_config.ports_table);
    yon_monitor_set_resolutions();
    g_hash_table_remove_all(main_config.resolutions);
    yon_all_resolutions_update();
    
    char *autodrivers = yon_config_get_by_key(VGADRV_AUTO);
    char *freedrivers = yon_config_get_by_key(VGADRV_NOFREE);
    char *dpms= yon_config_get_by_key(XORG_EXT);
    char *dpi = yon_config_get_by_key(XORG_DPI);
    char *failsafenvidia = yon_config_get_by_key(FAILSAFENVIDIA);
    char *failsafeati = yon_config_get_by_key(FAILSAFEATI);
    char *optirun = yon_config_get_by_key(OPTIRUN);
    char *primusrun = yon_config_get_by_key(PRIMUSRUN);
    char *discrete = yon_config_get_by_key(INTEGRATEDVGA);
    char *gapfix1 = config(GAPFIX1_parameter);
    char *gapfix2 = config(GAPFIX2_parameter);

    g_signal_handlers_block_by_func(G_OBJECT(main_config.widgets->FrameGapCombo),G_CALLBACK(on_gapfix_changed),main_config.widgets);
    if (!yon_char_is_empty(gapfix1)){
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FrameGapCombo),2);

    } else if (!yon_char_is_empty(gapfix2)){
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FrameGapCombo),3);

    } else {
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FrameGapCombo),0);
    }
    g_signal_handlers_unblock_by_func(G_OBJECT(main_config.widgets->FrameGapCombo),G_CALLBACK(on_gapfix_changed),main_config.widgets);

    if (!yon_char_is_empty(autodrivers)&&!strcmp(autodrivers,"yes"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->AutoDriverCombo),2);
    else  if (!yon_char_is_empty(autodrivers)&&!strcmp(autodrivers,"no"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->AutoDriverCombo),1);
    else
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->AutoDriverCombo)," ");

    if (!yon_char_is_empty(freedrivers)&&!strcmp(freedrivers,"yes"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FreeDriverCombo),1);
    else  if (!yon_char_is_empty(freedrivers)&&!strcmp(freedrivers,"nouveau"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FreeDriverCombo),2);
    else  if (!yon_char_is_empty(freedrivers)&&!strcmp(freedrivers,"radeon"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FreeDriverCombo),3);    
    else  if (!yon_char_is_empty(freedrivers)&&!strcmp(freedrivers,"no"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FreeDriverCombo),4);
    else
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->FreeDriverCombo)," ");

    if (!yon_char_is_empty(dpi)&&!strcmp(dpi,"auto"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->DisplayScalingCombo),1);
    else if (yon_char_is_empty(dpi))
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->DisplayScalingCombo)," ");
    else{
        if (!strcmp(dpi,"2K")||!strcmp(dpi,"4K")){
            gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets),dpi);
        } else {
            yon_gtk_combo_box_text_find(main_config.widgets->DisplayScalingCombo,dpi);
        }
    }

    if (!yon_char_is_empty(failsafenvidia)&&!strcmp(failsafenvidia,"fbdev"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FailsafeNvidiaCombo),2);
    else  if (!yon_char_is_empty(failsafenvidia)&&!strcmp(failsafenvidia,"no"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FailsafeNvidiaCombo),1);
    else
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->FailsafeNvidiaCombo)," ");

    if (!yon_char_is_empty(failsafeati)&&!strcmp(failsafeati,"fbdev"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FailsafeAMDCombo),2);
    else  if (!yon_char_is_empty(failsafeati)&&!strcmp(failsafeati,"no"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->FailsafeAMDCombo),1);
    else
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->FailsafeAMDCombo)," ");

    if (!yon_char_is_empty(optirun)){
        if (strcmp(optirun,"no")){
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->OptirunCombo),2);
        gtk_entry_set_text(GTK_ENTRY(main_config.widgets->OptirunEntry),optirun);
        } else {
            gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->OptirunCombo),1);
        }
    } else  if (yon_char_is_empty(optirun))
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->OptirunCombo)," ");
    else
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->OptirunCombo)," ");

    if (!yon_char_is_empty(primusrun)){
        if (strcmp(primusrun,"no")){
            gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->PrimusrunCombo),2);
            gtk_entry_set_text(GTK_ENTRY(main_config.widgets->PrimusrunEntry),primusrun);
        } else {
            gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->PrimusrunCombo),1);
        }
    } else  if (yon_char_is_empty(primusrun))
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->PrimusrunCombo)," ");
    else
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->PrimusrunCombo)," ");

    if (!yon_char_is_empty(dpms)&&!strcmp(dpms,"dpms"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->DontSwitchDisplaysOffCombo),2);
    else  if (!yon_char_is_empty(dpms)&&!strcmp(dpms,"nodpms"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->DontSwitchDisplaysOffCombo),1);
    else
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->DontSwitchDisplaysOffCombo)," ");

    if (!yon_char_is_empty(discrete)&&!strcmp(discrete,"yes"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->DisplayDiscreteOnly),2);
    else  if (!yon_char_is_empty(discrete)&&!strcmp(discrete,"no"))
        gtk_combo_box_set_active(GTK_COMBO_BOX(main_config.widgets->DisplayDiscreteOnly),1);
    else
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(main_config.widgets->DisplayDiscreteOnly)," ");

    yon_monitor_view_update();
}

void yon_load_proceed(YON_CONFIG_TYPE type){ 
	if (type!=YON_CONFIG_CUSTOM){
		yon_config_clean();
        if (!yon_char_is_empty(config_get_default_command))
            yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
	}
    if (type==YON_CONFIG_GLOBAL){
		yon_config_load_config(type,config_get_command("global"),NULL);
	} else if (type==YON_CONFIG_LOCAL){
		yon_config_load_config(type,config_get_command("system"),NULL);
    } else if (type==YON_CONFIG_CUSTOM){
		char *path="";
        textdomain(template_ui_LocaleName);
		GtkWidget *dialog = gtk_file_chooser_dialog_new(template_app_information.app_title,NULL,GTK_FILE_CHOOSER_ACTION_SAVE,CANCEL_LABEL,GTK_RESPONSE_CANCEL,OPEN_LABEL,GTK_RESPONSE_ACCEPT,NULL);
        textdomain(LocaleName);
        gtk_window_set_icon_name(GTK_WINDOW(dialog),"com.ublinux.ubl-settings-services");
        gtk_window_set_title(GTK_WINDOW(dialog),TITLE_LABEL);
        GtkFileFilter *filter = gtk_file_filter_new();
		gtk_file_filter_add_pattern(filter,"*.ini");
        gtk_file_filter_set_name(filter, "*.ini");
		gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
		gtk_widget_show(dialog);
		int response = gtk_dialog_run(GTK_DIALOG(dialog));
		if (response == GTK_RESPONSE_ACCEPT){
			char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
			if (!yon_char_is_empty(file)){
				path=file;
			}
            yon_config_clean();
            if (!yon_char_is_empty(config_get_default_command))
                yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
            char *command = config_get_command(path);
            yon_config_load_config(type,command,NULL);
			gtk_widget_destroy(dialog);
		} else {
			gtk_widget_destroy(dialog);
		}
	}
}

void on_config_local_load(GtkWidget *self,main_window *){
    if (self){};
        yon_load_proceed(YON_CONFIG_LOCAL);
        main_config.load_mode=1;
        yon_interface_update();
}

void on_config_global_load(GtkWidget *self,main_window *){
    if (self){}
        yon_load_proceed(YON_CONFIG_GLOBAL);
        main_config.load_mode=0;
    yon_interface_update();
}

void on_config_custom_load(GtkWidget *self,main_window *){
    if (self){}
	yon_load_proceed(YON_CONFIG_CUSTOM);
	main_config.load_mode=3;
	yon_interface_update();
}

void on_config_global_local_save(){
    yon_interface_save();
    main_config.save_config=YON_CONFIG_BOTH;
    yon_save_proceed(NULL,YON_CONFIG_BOTH,config_get_command("global"),NULL);
}

void on_config_local_save(){
    yon_interface_save();
    main_config.save_config=YON_CONFIG_LOCAL;
    yon_save_proceed("system",YON_CONFIG_LOCAL,config_get_command("system"),NULL);
}

void on_config_global_save(){
    yon_interface_save();
    main_config.save_config=YON_CONFIG_GLOBAL;
    yon_save_proceed("global",YON_CONFIG_GLOBAL,config_get_command("global"),NULL);
}

void on_config_custom_save(GtkWidget *self, main_window *widgets){
    yon_interface_save();
    if (self&&widgets){}
    main_config.save_config=YON_CONFIG_CUSTOM;
	template_saving_window *window = yon_save_proceed(NULL,YON_CONFIG_CUSTOM,config_get_command("system"),NULL);
	if (window){};
}

// functions


gboolean yon_proprietary_connect(main_window *widgets){
    gtk_spinner_stop(GTK_SPINNER(widgets->DriversLoadingSpinner));
    gtk_widget_hide(widgets->DriversLoadingSpinner);
    return G_SOURCE_REMOVE;
}

void yon_proprietary_local_get(main_window *widgets){
    g_idle_add((GSourceFunc)yon_proprietary_clear,widgets);
    int kernels_size;
    config_str kernels_installed=yon_config_load(yon_get_kernels_installed,&kernels_size);
    if (!kernels_size){
        yon_ubl_status_box_render_thread(KERNELS_SUPPORTED_UNFOUND_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
    }

    for (int i=0;i<kernels_size;i++){
        yon_char_remove_last_symbol(kernels_installed[i],'\n');
        struct proprietary_kernel_append_struct *cur = malloc(sizeof(struct proprietary_kernel_append_struct));
        cur->widgets=widgets;
        cur->kernel=yon_char_new(kernels_installed[i]);
        yon_proprietary_append_kernels(cur);
    }

    int size;
    config_str rtn=NULL;
    rtn = yon_resource_open_file(get_proprietary_drivers_info_path,&size);
    for (int i=1;i<size;i++){
        yon_char_remove_last_symbol(rtn[i],'\n');
    }
    for (int dr_desc=1;dr_desc<size;dr_desc++){
        while(gtk_events_pending()) gtk_main_iteration();
        int current_size;
        config_str current = yon_char_parse(rtn[dr_desc],&current_size,";");
        if ((!strcmp(current[0],"")||
            yon_char_parsed_check_exist(kernels_installed,kernels_size,current[0])>-1)){
            if ((!yon_char_is_empty(current[1])&&
                yon_packages_check_exist(current[1]))||
                (!yon_char_is_empty(current[2])&&
                yon_packages_check_exist(current[2]))){
                driver_struct *target = yon_driver_struct_new();
                target->name = yon_char_new(current[3]);
                target->module = current[1];
                target->package = current[2];
                target->kernel = current[0];
                target->devices = current_size>5?current[5]:NULL;
                target->description = yon_char_new(_(current[4]));
                target->module_installed = yon_packages_check_installed(current[1]);
                target->package_installed = yon_packages_check_installed(current[2]);
                target->module_exist = yon_packages_check_exist(current[1]);
                target->package_exist = yon_packages_check_exist(current[2]);
                GtkWidget *list_row = gtk_list_box_row_new();
                gtk_widget_show(list_row);
                g_signal_connect(G_OBJECT(target->ModuleInstalledCheck),"clicked", G_CALLBACK(on_install_activate),widgets);
                g_signal_connect(G_OBJECT(target->PackageInstalledCheck),"clicked", G_CALLBACK(on_install_activate),widgets);
            
                gtk_container_add(GTK_CONTAINER(list_row),target->MainBox);
                g_object_set_data(G_OBJECT(list_row),"driver_struct",target);
                gtk_list_box_insert(GTK_LIST_BOX(widgets->DriversList),list_row,-1);
                yon_driver_struct_update(widgets,target);
                free(current);
            }
        }
        if (dr_desc==size-1) {
            g_idle_add((GSourceFunc)yon_proprietary_connect,widgets);
        }
    }

    if (!size){
        g_idle_add((GSourceFunc)yon_proprietary_connect,widgets);
    }
    char *status_text = yon_char_unite(yon_char_get_localised_from_lib(SUCCESS_LABEL),". ", !getuid()? main_config.load_mode==1?yon_char_get_localised_from_lib(LOCAL_LOAD_SUCCESS_LABEL):yon_char_get_localised_from_lib(GLOBAL_LOAD_SUCCESS_LABEL):yon_char_get_localised_from_lib(ROOT_WARNING_LABEL),NULL);
    yon_ubl_status_box_render_thread(status_text,!getuid()?BACKGROUND_IMAGE_SUCCESS_TYPE:BACKGROUND_IMAGE_FAIL_TYPE);
    free(status_text);
    yon_proprietary_set(widgets);
    yon_drivers_resize(widgets);
}

void yon_proprietary_get_thread(GtkWidget *,main_window *widgets){
    yon_packages_update();
    yon_ubl_status_box_render(PROPRIETARY_LOADING_LABEL,BACKGROUND_IMAGE_INFO_TYPE);
    yon_proprietary_local_get(widgets);
}

void yon_all_resolutions_update(){
    main_config.resolutions_sorted = yon_resource_open_file(get_resolutions_supportable_command,&main_config.resolutions_size);
    char *table_str = yon_char_parsed_to_string(main_config.resolutions_sorted,main_config.resolutions_size,"\n");
    int format_size;
    config_str resolutions_formatted = yon_config_load(yon_debug_output("%s\n",table_format_command(table_str)),&format_size);
    for (int i=0;i<format_size;i++){
        yon_char_remove_last_symbol(resolutions_formatted[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(main_config.resolutions_sorted[i+1],&parsed_size,";");
        g_hash_table_insert(main_config.resolutions,yon_char_new(parsed[0]),yon_char_new(resolutions_formatted[i]));
        yon_char_parsed_free(parsed,parsed_size);
    }
        yon_char_parsed_free(resolutions_formatted,format_size);
}

void yon_monitor_set_resolutions(){
    int size;
    config_str ports = yon_config_load(get_resolutions_and_ports_command,&size);
    for(int i=0;i<size;i++){
        int parsed_size;
        config_str parsed = yon_char_parse(ports[i],&parsed_size,":");
        if (parsed_size>1){
            g_hash_table_insert(main_config.ports_table,parsed[0],parsed[1]);
        }
    }
}

void on_app_chooser_open(GtkWidget *self, main_window *widgets){
    yon_app_chooser_window *window = yon_app_chooser_window_new(1);
    const char *prev_selection = gtk_entry_get_text(GTK_ENTRY(self==widgets->OptirunButton?widgets->OptirunEntry:widgets->PrimusrunEntry));
    if (!yon_char_is_empty(prev_selection)){
        int size;
        config_str parsed = yon_char_parse((char*)prev_selection,&size,",");
        yon_app_chooser_window_select(window,parsed,size);
        yon_char_parsed_free(parsed,size);
    }
    int size;
    config_str parsed = yon_app_chooser_window_run(window,&size);

    if (parsed&&size){
        char *string = yon_char_parsed_to_string(parsed,size,",");
        if (self==widgets->OptirunButton){
            gtk_entry_set_text(GTK_ENTRY(widgets->OptirunEntry),string);
        } else if (self == widgets->PrimusrunButton){
            gtk_entry_set_text(GTK_ENTRY(widgets->PrimusrunEntry),string);
        }
    }
}

void yon_adapter_update(struct adapter_struct *adapter){
    if (!adapter->vendor){
        gtk_widget_hide(adapter->widgets->InformationOverlay2);
        return;
    }
    const char *icon_ = NULL;
    GtkWidget *cur_label = NULL;
    GtkWidget *cur_image = NULL;
    gtk_image_get_icon_name(GTK_IMAGE(adapter->widgets->InformationCompanyLogoImage),&icon_,NULL);
    if (yon_char_is_empty(icon_)){
        cur_image = adapter->widgets->InformationCompanyLogoImage;
        cur_label = adapter->widgets->InformationLabel;
    } else {
        cur_image = adapter->widgets->InformationCompanyLogoImage2;
        cur_label = adapter->widgets->InformationLabel2;
    }
    
        if (strstr(adapter->vendor,"Intel")){
            gtk_image_set_from_icon_name(GTK_IMAGE(cur_image),"com.ublinux.ubl-settings-video.intel-logo",GTK_ICON_SIZE_BUTTON);
            gtk_image_set_pixel_size(GTK_IMAGE(cur_image),64);
            gtk_label_set_markup(GTK_LABEL(cur_label),adapter->adapter_text);
        }
        else if (strstr(adapter->vendor,"NVIDIA")){
            gtk_image_set_from_icon_name(GTK_IMAGE(cur_image),"com.ublinux.ubl-settings-video.nvidia-logo",GTK_ICON_SIZE_BUTTON);
            gtk_image_set_pixel_size(GTK_IMAGE(cur_image),64);
            gtk_label_set_markup(GTK_LABEL(cur_label),adapter->adapter_text);
        }
        else if (strstr(adapter->vendor,"AMD")||strstr(adapter->vendor,"Advanced Micro Devices")){
            gtk_image_set_from_icon_name(GTK_IMAGE(cur_image),"com.ublinux.ubl-settings-video.amd-logo",GTK_ICON_SIZE_BUTTON);
            gtk_image_set_pixel_size(GTK_IMAGE(cur_image),64);
            gtk_label_set_markup(GTK_LABEL(cur_label),adapter->adapter_text);
        }
        else if (strstr(adapter->vendor,"VMware")){
            gtk_image_set_from_icon_name(GTK_IMAGE(cur_image),"com.ublinux.ubl-settings-video.vmware-logo",GTK_ICON_SIZE_BUTTON);
            gtk_image_set_pixel_size(GTK_IMAGE(cur_image),64);
            gtk_label_set_markup(GTK_LABEL(cur_label),adapter->adapter_text);
        }
        else {
            gtk_widget_destroy(cur_image);
        }
}

void *yon_load_adapter(main_window *widgets){
    int size=0;
    config_str rtn = yon_config_load(get_adapter_info_json,&size);
    struct json_object *jsobject = json_tokener_parse(yon_char_parsed_to_string(rtn,size,""));
    {
        struct json_object *item_obj = json_object_array_get_idx(jsobject, 0);
        struct json_object *unit_obj;
        const char *desc="";
        if (json_object_object_get_ex(item_obj, "description", &unit_obj)) {
            desc = json_object_get_string(unit_obj);
        }
        const char *product ="";
        if (json_object_object_get_ex(item_obj, "product", &unit_obj)) {
            product = json_object_get_string(unit_obj);
        }
        const char *vendor="";
        if (json_object_object_get_ex(item_obj, "vendor", &unit_obj)) {
            vendor = json_object_get_string(unit_obj);
        }
        const char *driver_use="";
        if (json_object_object_get_ex(item_obj, "configuration", &unit_obj)) {
            if (json_object_object_get_ex(unit_obj, "driver", &unit_obj))
                driver_use = json_object_get_string(unit_obj);
        }
        const char *driver_all="";
        int loaded_size;
        config_str loaded = yon_config_load(get_drivers_for_card_command,&loaded_size);
        for (int i=0;i<loaded_size;i++){
            free(yon_char_divide_search(loaded[i],"=",-1));
            yon_char_remove_last_symbol(loaded[i],'\n');
        }
        int pos = yon_char_parsed_check_exist(loaded,loaded_size,(char*)driver_use);
        if (pos!=-1){
            driver_all=yon_char_new(loaded[pos]);
        }
        yon_char_parsed_free(loaded,loaded_size);
        char *final_text = yon_char_unite("<span size=\"12pt\"><span weight='bold' color='#1a5fb4'>",DESCRIPTION_LABEL,":</span> ",_(desc),
        "\n<span weight='bold' color='#1a5fb4'>",VENDOR_LABEL,":</span> ",vendor,
        "\n<span weight='bold' color='#1a5fb4'>",MODEL_LABEL,":</span> ",product,
        "\n<span weight='bold' color='#1a5fb4'>",USED_KERNEL_DRIVER_LABEL,":</span> ",driver_use,
        "\n<span weight='bold' color='#1a5fb4'>",AVALIABLE_KERNEL_DRIVERS_LABEL,":</span> ",driver_all,"</span>",NULL);
        struct adapter_struct *adapter_info = malloc(sizeof(struct adapter_struct));
        memset(adapter_info,0,sizeof(struct adapter_struct));
        adapter_info->widgets = widgets;
        adapter_info->vendor=yon_char_new(vendor);
        adapter_info->adapter_text = yon_char_new(final_text);
        g_idle_add_once((GSourceOnceFunc)yon_adapter_update,adapter_info);
    }
    {
        struct json_object *item_obj = json_object_array_get_idx(jsobject, 1);
        if (item_obj){
            struct json_object *unit_obj;
            const char *desc="";
            if (json_object_object_get_ex(item_obj, "description", &unit_obj)) {
                desc = json_object_get_string(unit_obj);
            }
            const char *product ="";
            if (json_object_object_get_ex(item_obj, "product", &unit_obj)) {
                product = json_object_get_string(unit_obj);
            }
            const char *vendor="";
            if (json_object_object_get_ex(item_obj, "vendor", &unit_obj)) {
                vendor = json_object_get_string(unit_obj);
            }
            const char *driver_use="";
            if (json_object_object_get_ex(item_obj, "configuration", &unit_obj)) {
                if (json_object_object_get_ex(unit_obj, "driver", &unit_obj))
                    driver_use = json_object_get_string(unit_obj);
            }
            const char *driver_all="";
            int loaded_size;
            config_str loaded = yon_config_load(get_drivers_for_card_command,&loaded_size);
            for (int i=0;i<loaded_size;i++){
                free(yon_char_divide_search(loaded[i],"=",-1));
                yon_char_remove_last_symbol(loaded[i],'\n');
            }
            int pos = yon_char_parsed_check_exist(loaded,loaded_size,(char*)driver_use);
            if (pos!=-1){
                driver_all=yon_char_new(loaded[pos]);
            }
            yon_char_parsed_free(loaded,loaded_size);
            char *final_text = yon_char_unite("<span size=\"12pt\"><span weight='bold' color='#1a5fb4'>",DESCRIPTION_LABEL,":</span> ",_(desc),
            "\n<span weight='bold' color='#1a5fb4'>",VENDOR_LABEL,":</span> ",vendor,
            "\n<span weight='bold' color='#1a5fb4'>",MODEL_LABEL,":</span> ",product,
            "\n<span weight='bold' color='#1a5fb4'>",USED_KERNEL_DRIVER_LABEL,":</span> ",driver_use,
            "\n<span weight='bold' color='#1a5fb4'>",AVALIABLE_KERNEL_DRIVERS_LABEL,":</span> ",driver_all,"</span>",NULL);
            struct adapter_struct *adapter_info = malloc(sizeof(struct adapter_struct));
            memset(adapter_info,0,sizeof(struct adapter_struct));
            adapter_info->widgets = widgets;
            adapter_info->vendor=yon_char_new(vendor);
            adapter_info->adapter_text = yon_char_new(final_text);
            g_idle_add_once((GSourceOnceFunc)yon_adapter_update,adapter_info);
        } else {
            struct adapter_struct *adapter_info = malloc(sizeof(struct adapter_struct));
            memset(adapter_info,0,sizeof(struct adapter_struct));
            adapter_info->widgets = widgets;
            
            g_idle_add_once((GSourceOnceFunc)yon_adapter_update,adapter_info);
        }
    }
    g_thread_exit(NULL);
}

void yon_adapter_window_setup(main_window *widgets){
    g_thread_new("adapter_thread",(GThreadFunc)yon_load_adapter,widgets);
}

void yon_set_sensitive_from_combo_box(GtkComboBox *toggle, GtkWidget *target){
    int active = gtk_combo_box_get_active(toggle);
    gtk_widget_set_sensitive(target,active>1 ? 1 : 0);
}

gboolean on_db_update_done(GtkWidget *,gint ,main_window *widgets){
    yon_packages_update();
    yon_proprietary_get_thread(NULL,(main_window*)widgets);
    g_signal_handlers_disconnect_by_func(G_OBJECT(widgets->InstallTerminal),G_CALLBACK(on_db_update_done),widgets);
    return 0;
}

void on_database_update(void *,main_window *widgets){
    yon_terminal_integrated_start_shell(widgets->InstallTerminal,yon_debug_output("%s\n",pacman_update_command),NULL,NULL);
    g_signal_connect(G_OBJECT(widgets->InstallTerminal),"child-exited",G_CALLBACK(on_db_update_done),widgets);
    gtk_button_clicked(GTK_BUTTON(widgets->MoreButton));
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->MenusTab),1);
}

void on_gapfix_changed(GtkWidget*, main_window *widgets){
    int active = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->FrameGapCombo));
    switch(active){
        case 0: {
            yon_config_remove_by_key(GAPFIX1_parameter);
            yon_config_remove_by_key(GAPFIX2_parameter);
        } break;
        case 1: {
            yon_config_remove_by_key(GAPFIX2_parameter);
            yon_config_remove_by_key(GAPFIX2_parameter);
        } break;
        case 2:{

            yon_config_register(GAPFIX1_parameter,GAPFIX1_command,"USLEEP");
            yon_config_remove_by_key(GAPFIX2_parameter);
        } break;
        case 3:{
            yon_config_register(GAPFIX2_parameter,GAPFIX2_command,"1");
            yon_config_remove_by_key(GAPFIX1_parameter);

        } break;
    }
}

int check_rmb_menu(void *data){
    main_window *widgets = (main_window*)data;
    GtkListBoxRow *selection = gtk_list_box_get_selected_row(GTK_LIST_BOX(widgets->DriversList));
    if (selection){
        driver_struct *driver = g_object_get_data(G_OBJECT(selection),"driver_struct");
        if (driver->supported_devices_avaliable){
            gtk_widget_set_sensitive(GTK_WIDGET(widgets->menu->buttons->first->data),1);
        } else {
            gtk_widget_set_sensitive(GTK_WIDGET(widgets->menu->buttons->first->data),0);
        }
        return 1;
    }

    return 0;
}

char *yon_auto_driver_default(char *target){
    char *final = NULL;
    if (!yon_char_is_empty(target)){
        if (!strcmp(target,"yes")){
            final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(ON_LABEL));
        } else if (!strcmp(target,"no")){
            final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(OFF_LABEL));
        }

    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

char *yon_free_driver_default(char *target){
    char *final = NULL;
    if (!yon_char_is_empty(target)){
        if (!strcmp(target,"yes")){
            final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(FREE_DRIVERS_ON_LABEL));
        } else if (!strcmp(target,"no")){
            final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(NOUVEAU_RADEON_OFF_LABEL));
        } else if (!strcmp(target,"nouveau")){
            final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(RADEON_OFF_LABEL));
        } else if (!strcmp(target,"radeon")){
            final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(NOUVEAU_OFF_LABEL));
        }
    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

char *yon_failsafe_driver_default(char *target){
    char *final=NULL;
    if (!yon_char_is_empty(target)){
        final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(target));
    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

char *yon_ati_driver_default(char *target){
    char *final=NULL;
    if (!yon_char_is_empty(target)){
        final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(target));

    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

char *yon_switch_display_off_default(char *target){
    char *final=NULL;
    if (!yon_char_is_empty(target)){
        if (!strcmp(target,"dpms")){
            final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(ON_LABEL));
        } else if (!strcmp(target,"nodpms")){
            final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(OFF_LABEL));
        }

    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

char *yon_scaling_default(char *target){
    char *final=NULL;
    if (!yon_char_is_empty(target)){
        final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(target));

    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

char *yon_discrete_default(char *target){
    char *final=NULL;
    if (!yon_char_is_empty(target)){
        final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(target));

    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

char *yon_optirun_default(char *target){
    char *final=NULL;
    if (!yon_char_is_empty(target)){
        final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(target));

    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

char *yon_primusrun_default(char *target){
    char *final=NULL;
    if (!yon_char_is_empty(target)){
        final = yon_char_get_localised_from_lib(DEFAULT_CUSTOM_LABEL(target));

    } else {
        final = yon_char_get_localised_from_lib(DEFAULT_LABEL);
    }
    return final;
}

void on_paned_position_changed(GtkWidget *, GdkEvent*,main_window *widgets){
    int size = (int)(gtk_widget_get_allocated_height(widgets->InterfaceBox)*0.4);
    if (gtk_paned_get_position(GTK_PANED(widgets->TerminalPaned))>size-40)
        gtk_paned_set_position(GTK_PANED(widgets->TerminalPaned),size-40);
}

/** void yon_setup_widgets(main_window *widgets)
 * [EN]
 * Sets up every element on main window
 * [RU]
 * 
 * */
template_main_window *yon_main_window_complete(main_window *widgets){
    widgets = yon_remalloc(widgets,sizeof(main_window));
    main_config.widgets=widgets;
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path);
    widgets->MainBox = yon_gtk_builder_get_widget(builder,"BoxMain");
    gtk_box_pack_start(GTK_BOX(widgets->InterfaceBox),widgets->MainBox,1,1,0);
    // Custom widgets configuration
    {
    widgets->MainOverlay = yon_gtk_builder_get_widget(builder,"MainOverlay");
    widgets->MainSpinner = yon_gtk_builder_get_widget(builder,"MainSpinner");

    widgets->AutoDriverCombo = yon_gtk_builder_get_widget(builder,"AutoDriverCombo");
    widgets->FreeDriverCombo = yon_gtk_builder_get_widget(builder,"FreeDriverCombo");
    widgets->FailsafeNvidiaCombo = yon_gtk_builder_get_widget(builder,"FailsafeNvidiaCombo");
    widgets->FailsafeAMDCombo = yon_gtk_builder_get_widget(builder,"FailsafeAMDCombo");
    widgets->MonitorsBox = yon_gtk_builder_get_widget(builder,"MonitorsBox");
    widgets->DontSwitchDisplaysOffCombo = yon_gtk_builder_get_widget(builder,"DontSwitchDisplaysOffCombo");
    widgets->DisplayScalingCombo = yon_gtk_builder_get_widget(builder,"DisplayScalingCombo");
    widgets->DisplayDiscreteOnly = yon_gtk_builder_get_widget(builder,"DisplayDiscreteOnly");
    widgets->OptirunCombo = yon_gtk_builder_get_widget(builder,"OptirunCombo");
    widgets->OptirunEntry = yon_gtk_builder_get_widget(builder,"OptirunEntry");
    widgets->OptirunButton = yon_gtk_builder_get_widget(builder,"OptirunButton");
    widgets->PrimusrunCombo = yon_gtk_builder_get_widget(builder,"PrimusrunCombo");
    widgets->PrimusrunEntry = yon_gtk_builder_get_widget(builder,"PrimusrunEntry");
    widgets->PrimusrunButton = yon_gtk_builder_get_widget(builder,"PrimusrunButton");
    widgets->FrameGapCombo = yon_gtk_builder_get_widget(builder,"FrameGapCombo");

    widgets->GSWarningLabel = yon_gtk_builder_get_widget(builder,"GSWarningLabel");

    widgets->InfoButton = yon_gtk_builder_get_widget(builder, "InfoButton");
    widgets->TerminalOverlay = yon_gtk_builder_get_widget(builder, "TerminalOverlay");
    widgets->MenusTab = yon_gtk_builder_get_widget(builder, "MenusTab");
    widgets->LoadDriversButton = yon_gtk_builder_get_widget(builder,"LoadDriversButton");
    widgets->SupportedDevicesButton = yon_gtk_builder_get_widget(builder,"SupportedDevicesButton");
    widgets->AboutModuleButton = yon_gtk_builder_get_widget(builder,"AboutModuleButton");
    widgets->AboutPackageButton = yon_gtk_builder_get_widget(builder,"AboutPackageButton");

    widgets->KernelsCombo = yon_gtk_builder_get_widget(builder,"KernelsCombo");

    widgets->InstalledBox = yon_gtk_builder_get_widget(builder,"InstalledBox");
    widgets->InstalledModuleLabel = yon_gtk_builder_get_widget(builder,"InstalledModuleLabel");
    widgets->InstalledPackageLabel = yon_gtk_builder_get_widget(builder,"InstalledPackageLabel");
    widgets->KernelLabel = yon_gtk_builder_get_widget(builder,"KernelLabel");
    widgets->DriverLabel = yon_gtk_builder_get_widget(builder,"DriverLabel");
    widgets->DescriptionLabel = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
    widgets->DevicesLabel = yon_gtk_builder_get_widget(builder,"DevicesLabel");

    widgets->MoreButton = yon_gtk_builder_get_widget(builder,"TerminalMoreButton");
    widgets->UnlockPacmanButton = yon_gtk_builder_get_widget(builder,"UnlockPacmanButton");
    widgets->TerminalPaned = yon_gtk_builder_get_widget(builder,"TerminalPaned");
    widgets->TerminalMoreRevieler = yon_gtk_builder_get_widget(builder,"TerminalMoreRevieler");
    widgets->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    widgets->TerminalBox = yon_gtk_builder_get_widget(builder,"TerminalBox");
    widgets->InstallTerminal = yon_gtk_builder_get_widget(builder,"TerminalTerminal");
    widgets->TerminalRevealer = yon_gtk_builder_get_widget(builder,"TerminalMoreRevieler");

    widgets->DriverModulesTab = yon_gtk_builder_get_widget(builder, "DriverModulesTab");
    widgets->DriversTab = yon_gtk_builder_get_widget(builder, "DriversTab");
    widgets->InformationFrameLabel = yon_gtk_builder_get_widget(builder, "InformationFrameLabel");
    widgets->InformationOverlay = yon_gtk_builder_get_widget(builder, "InformationOverlay");
    widgets->InformationCompanyLogoImage = yon_gtk_builder_get_widget(builder, "InformationCompanyLogoImage");
    widgets->InformationOverlay2 = yon_gtk_builder_get_widget(builder, "InformationOverlay2");
    widgets->InformationCompanyLogoImage2 = yon_gtk_builder_get_widget(builder, "InformationCompanyLogoImage2");
    widgets->InformationLabel = yon_gtk_builder_get_widget(builder, "InformationLabel");
    widgets->InformationLabel2 = yon_gtk_builder_get_widget(builder, "InformationLabel2");
    widgets->DriversLoadingOverlay = yon_gtk_builder_get_widget(builder, "DriversLoadingOverlay");
    widgets->DriversLoadingSpinner = yon_gtk_builder_get_widget(builder, "DriversLoadingSpinner");

    widgets->DriversList = yon_gtk_builder_get_widget(builder, "DriversList");
    widgets->TerminalCancelButton = yon_gtk_builder_get_widget(builder, "TerminalCancelButton");
    widgets->TerminalInfoLabel = yon_gtk_builder_get_widget(builder, "TerminalInfoLabel");
    widgets->TerminalMoveBox = yon_gtk_builder_get_widget(builder, "TerminalMoveBox");
    widgets->TerminalMoveTargetBox = yon_gtk_builder_get_widget(builder, "TerminalMoveTargetBox");
    }
    gtk_overlay_add_overlay(GTK_OVERLAY(widgets->DriversLoadingOverlay),widgets->DriversLoadingSpinner);

    g_signal_connect(G_OBJECT(widgets->LoadDriversButton), "clicked", G_CALLBACK(yon_proprietary_get_thread), widgets);
    g_signal_connect(G_OBJECT(widgets->SupportedDevicesButton), "clicked", G_CALLBACK(on_devices_button_clicked), widgets);
    g_signal_connect(G_OBJECT(widgets->AboutModuleButton), "clicked", G_CALLBACK(on_driver_module_info), widgets);
    g_signal_connect(G_OBJECT(widgets->AboutPackageButton), "clicked", G_CALLBACK(on_driver_pack_info), widgets);

    g_signal_connect(G_OBJECT(widgets->DriversList), "row-selected", G_CALLBACK(on_driver_pack_selection_change), widgets);

    g_signal_connect(G_OBJECT(widgets->InstallTerminal),"child-exited",G_CALLBACK(on_command_execute_success),widgets);

    g_signal_connect(G_OBJECT(widgets->LoadLocalMenuItem),"activate",G_CALLBACK(on_config_local_load),widgets);
    g_signal_connect(G_OBJECT(widgets->LoadGlobalMenuItem),"activate",G_CALLBACK(on_config_global_load),widgets);
    g_signal_connect(G_OBJECT(widgets->LoadCustomMenuItem),"activate",G_CALLBACK(on_config_custom_load),widgets);

    g_signal_connect(G_OBJECT(widgets->SaveMenuItem),"activate",G_CALLBACK(on_config_global_local_save),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveLocalMenuItem),"activate",G_CALLBACK(on_config_local_save),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveGlobalMenuItem),"activate",G_CALLBACK(on_config_global_save),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveCustomMenuItem),"activate",G_CALLBACK(on_config_custom_save),NULL);
    g_signal_connect(G_OBJECT(widgets->OptirunCombo),"changed",G_CALLBACK(yon_set_sensitive_from_combo_box),widgets->OptirunEntry);
    g_signal_connect(G_OBJECT(widgets->OptirunCombo),"changed",G_CALLBACK(yon_set_sensitive_from_combo_box),widgets->OptirunButton);
    g_signal_connect(G_OBJECT(widgets->PrimusrunCombo),"changed",G_CALLBACK(yon_set_sensitive_from_combo_box),widgets->PrimusrunEntry);
    g_signal_connect(G_OBJECT(widgets->PrimusrunCombo),"changed",G_CALLBACK(yon_set_sensitive_from_combo_box),widgets->PrimusrunButton);
    g_signal_connect(G_OBJECT(widgets->KernelsCombo),"changed",G_CALLBACK(on_kernel_filter_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->FrameGapCombo),"changed",G_CALLBACK(on_gapfix_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->OptirunButton),"clicked",G_CALLBACK(on_app_chooser_open),widgets);
    g_signal_connect(G_OBJECT(widgets->PrimusrunButton),"clicked",G_CALLBACK(on_app_chooser_open),widgets);
    g_signal_connect(G_OBJECT(widgets->MoreButton),"clicked",G_CALLBACK(on_terminal_revealer_open),widgets);
    g_signal_connect(G_OBJECT(widgets->TerminalCancelButton),"clicked",G_CALLBACK(on_command_cancel),widgets);
    g_signal_connect(G_OBJECT(widgets->UnlockPacmanButton),"clicked",G_CALLBACK(on_pacman_db_unlock),NULL);
    g_signal_connect(G_OBJECT(widgets->TerminalPaned),"notify::position",G_CALLBACK(on_paned_position_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->TerminalPaned),"notify::max-position",G_CALLBACK(on_paned_position_changed),widgets);
    // g_signal_connect(G_OBJECT(widgets->InstallTerminal),"child-ready",G_CALLBACK(on_driver_child_ready),NULL);

        char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL);
        yon_window_config_load(path);
        while(gtk_events_pending()) gtk_main_iteration();
    g_object_set_data(G_OBJECT(widgets->InstallTerminal),"widgets",widgets);
    {
        gtk_paned_add2(GTK_PANED(widgets->TerminalPaned),widgets->TerminalRevealer);
        vte_terminal_set_scrollback_lines(VTE_TERMINAL(widgets->InstallTerminal), 100);
        vte_terminal_set_scroll_on_output(VTE_TERMINAL(widgets->InstallTerminal), TRUE);
        vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(widgets->InstallTerminal), TRUE);
        vte_terminal_set_input_enabled(VTE_TERMINAL(widgets->InstallTerminal),1);
        gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(widgets->TerminalInfoLabel),0.1);
    }
    widgets->menu = yon_rmb_menu_setup(widgets->DriversList,check_rmb_menu,widgets,
    SUPPORTED_LABEL,icon_devices_path,G_CALLBACK(on_devices_button_clicked),widgets,
    MODULE_INFO_LABEL,icon_module_path,G_CALLBACK(on_driver_module_info),widgets,
    PACKAGE_INFO_LABEL,icon_package_path,G_CALLBACK(on_driver_pack_info),widgets,
    NULL);
    while(gtk_events_pending()) gtk_main_iteration();
        yon_adapter_window_setup(widgets);

    yon_save_window_set_postsave_function(on_save_done,widgets);
    on_config_local_load(NULL,widgets);

    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->FailsafeAMDCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->KernelsCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->OptirunCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->FrameGapCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->PrimusrunCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->AutoDriverCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->FreeDriverCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->DisplayScalingCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->FailsafeNvidiaCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->DontSwitchDisplaysOffCombo));
    yon_gtk_combo_box_block_scroll(GTK_COMBO_BOX(widgets->DisplayDiscreteOnly));

    char *cur_env = getenv("XDG_SESSION_TYPE");
    if (yon_char_is_empty(cur_env)){
        cur_env = UNDEFINED_LABEL;
    }
    gtk_label_set_markup(GTK_LABEL(widgets->GSWarningLabel),XORG_ONLY_LABEL(cur_env));

    gtk_spinner_stop(GTK_SPINNER(widgets->MainSpinner));
    gtk_widget_hide(widgets->MainSpinner);
    return (template_main_window*)widgets;
}

/**config_init()
 * [EN]
 *
 * [RU]
 * Функция инициализации всех параметров конфигурации
*/
void config_init(){
    main_config.always_open_documentation=0;
    main_config.socket_id=-1;
    main_config.save_socket_id=-1;
    main_config.load_socket_id=-1;
    main_config.lock_help=0;
    main_config.lock_help=0;
    main_config.lock_load_global=0;
    main_config.lock_save_global=0;
    main_config.lock_save_local=0;
    main_config.load_mode=1;
    main_config.progress_active=0;
    main_config.ports_table = g_hash_table_new_full(g_str_hash,g_str_equal,free,free);
    main_config.resolutions = g_hash_table_new_full(g_str_hash,g_str_equal,free,free);
    yon_monitor_set_resolutions();
    yon_all_resolutions_update();
}

int main(int argc, char *argv[]){
    setlocale(LC_ALL, "");
    textdomain (LocaleName);

    yon_ubl_connect_config((_template_config*)&main_config);
    yon_ubl_window_init(TITLE_LABEL,TITLE_INFO_LABEL,LocaleName,CssPath,LocaleName,version_application,WIKI_LINK);
	config_str unfound = NULL;
	int size=0;
	yon_ubl_setup_arguments(argc,argv,&unfound,&size,NULL);
    gtk_init(&argc, &argv);
    template_main_window *widgets = yon_ubl_window_setup();
    config_init();
    widgets = yon_main_window_complete((main_window*)widgets);

    yon_root_button_setup(widgets,argv,argc);
        yon_packages_init();

        if (system(yon_check_database_command)){
            dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
            data->action_text=DATABASE_UNACCESSIBLE_LABEL;
            GtkResponseType resp = yon_confirmation_dialog_call(widgets->HeadOverlay,data);
            if (resp){
                on_database_update(NULL,(main_window*)widgets);
            } else {
                yon_proprietary_get_thread(NULL,(main_window*)widgets);
            }
        } else {
            yon_proprietary_get_thread(NULL,(main_window*)widgets);
        }
        gtk_main();
        yon_packages_finish();
        return 0;
}