int nv_write_config_file(const char *filename, CtrlHandles *h, ParsedAttribute *p, ConfigProperties *conf) { int screen, ret, entry, bit, val; FILE *stream; time_t now; AttributeTableEntry *a; ReturnStatus status; NVCTRLAttributeValidValuesRec valid; uint32 mask; CtrlHandleTarget *t; char *tmp_d_str, *prefix, scratch[4]; const char *tmp; char *locale = "C"; if (!filename) { nv_error_msg("Unable to open configuration file for writing."); return NV_FALSE; } stream = fopen(filename, "w"); if (!stream) { nv_error_msg("Unable to open file '%s' for writing.", filename); return NV_FALSE; } /* write header */ now = time(NULL); fprintf(stream, "#\n"); fprintf(stream, "# %s\n", filename); fprintf(stream, "#\n"); fprintf(stream, "# Configuration file for nvidia-settings - the NVIDIA " "X Server Settings utility\n"); /* NOTE: ctime(3) generates a new line */ fprintf(stream, "# Generated on %s", ctime(&now)); fprintf(stream, "#\n"); /* * set the locale to "C" before writing the configuration file to * reduce the risk of locale related parsing problems. Restore * the original locale before exiting this function. */ if (setlocale(LC_NUMERIC, "C") == NULL) { nv_warning_msg("Error writing configuration file '%s': could " "not set the locale 'C'.", filename); locale = conf->locale; } /* write the values in ConfigProperties */ write_config_properties(stream, conf, locale); /* for each screen, query each attribute in the table */ fprintf(stream, "\n"); fprintf(stream, "# Attributes:\n"); fprintf(stream, "\n"); /* * Note: we only save writable attributes addressable by X screen * (i.e., we don't look at other target types, yet). */ for (screen = 0; screen < h->targets[X_SCREEN_TARGET].n; screen++) { t = &h->targets[X_SCREEN_TARGET].t[screen]; /* skip it if we don't have a handle for this screen */ if (!t->h) continue; /* * construct the prefix that will be printed in the config * file infront of each attribute on this screen; this will * either be "[screen]" or "[displayname]". */ if (conf->booleans & CONFIG_PROPERTIES_INCLUDE_DISPLAY_NAME_IN_CONFIG_FILE) { prefix = t->name; } else { snprintf(scratch, 4, "%d", screen); prefix = scratch; } /* loop over all the entries in the table */ for (entry = 0; attributeTable[entry].name; entry++) { a = &attributeTable[entry]; /* * skip all attributes that are not supposed to be written * to the config file */ if (a->flags & NV_PARSER_TYPE_NO_CONFIG_WRITE) continue; /* * special case the color attributes because we want to * print floats */ if (a->flags & NV_PARSER_TYPE_COLOR_ATTRIBUTE) { float c[3], b[3], g[3]; status = NvCtrlGetColorAttributes(t->h, c, b, g); if (status != NvCtrlSuccess) continue; fprintf(stream, "%s%c%s=%f\n", prefix, DISPLAY_NAME_SEPARATOR, a->name, get_color_value(a->attr, c, b, g)); continue; } for (bit = 0; bit < 24; bit++) { mask = 1 << bit; /* * if this bit is not present in the screens's enabled * display device mask (and the X screen has enabled * display devices), skip to the next bit */ if (((t->d & mask) == 0x0) && (t->d)) continue; status = NvCtrlGetValidDisplayAttributeValues (t->h, mask, a->attr, &valid); if (status != NvCtrlSuccess) goto exit_bit_loop; if ((valid.permissions & ATTRIBUTE_TYPE_WRITE) == 0x0) goto exit_bit_loop; status = NvCtrlGetDisplayAttribute(t->h, mask, a->attr, &val); if (status != NvCtrlSuccess) goto exit_bit_loop; if (valid.permissions & ATTRIBUTE_TYPE_DISPLAY) { tmp_d_str = display_device_mask_to_display_device_name(mask); fprintf(stream, "%s%c%s[%s]=%d\n", prefix, DISPLAY_NAME_SEPARATOR, a->name, tmp_d_str, val); free(tmp_d_str); continue; } else { fprintf(stream, "%s%c%s=%d\n", prefix, DISPLAY_NAME_SEPARATOR, a->name, val); /* fall through to exit_bit_loop */ } exit_bit_loop: bit = 25; /* XXX force us out of the display device loop */ } /* bit */ } /* entry */ } /* screen */ /* * loop the ParsedAttribute list, writing the attributes to file. * note that we ignore conf->include_display_name_in_config_file * when writing these parsed attributes; this is because parsed * attributes (like the framelock properties) require a display * name be specified (since there are multiple X servers * involved). */ while (p) { char target_str[64]; if (!p->next) { p = p->next; continue; } tmp = nv_get_attribute_name(p->attr, NV_PARSER_TYPE_STRING_ATTRIBUTE, p->flags); if (!tmp) { nv_error_msg("Failure to save unknown attribute %d.", p->attr); p = p->next; continue; } /* * if the parsed attribute has a target specification, and a * target type other than an X screen, include a target * specification in what we write to the .rc file. */ target_str[0] = '\0'; if ((p->flags & NV_PARSER_HAS_TARGET) && (p->target_type != NV_CTRL_TARGET_TYPE_X_SCREEN)) { int j; /* Find the target name of the target type */ for (j = 0; targetTypeTable[j].name; j++) { if (targetTypeTable[j].nvctrl == p->target_type) { snprintf(target_str, 64, "[%s:%d]", targetTypeTable[j].parsed_name, p->target_id); break; } } } if (p->display_device_mask) { tmp_d_str = display_device_mask_to_display_device_name (p->display_device_mask); fprintf(stream, "%s%s%c%s[%s]=%d\n", p->display, target_str, DISPLAY_NAME_SEPARATOR, tmp, tmp_d_str, p->val); free(tmp_d_str); } else { fprintf(stream, "%s%s%c%s=%d\n", p->display, target_str, DISPLAY_NAME_SEPARATOR, tmp, p->val); } p = p->next; } setlocale(LC_NUMERIC, conf->locale); /* close the configuration file */ ret = fclose(stream); if (ret != 0) { nv_error_msg("Failure while closing file '%s'.", filename); return NV_FALSE; } return NV_TRUE; } /* nv_write_config_file() */
int nv_write_config_file(const char *filename, const CtrlSystem *system, const ParsedAttribute *p, const ConfigProperties *conf) { int ret, entry, val, randr_gamma_available; FILE *stream; time_t now; ReturnStatus status; CtrlAttributePerms perms; CtrlTargetNode *node; CtrlTarget *t; char *prefix, scratch[4]; char *locale = "C"; if (!filename) { nv_error_msg("Unable to open configuration file for writing."); return NV_FALSE; } stream = fopen(filename, "w"); if (!stream) { nv_error_msg("Unable to open file '%s' for writing.", filename); return NV_FALSE; } /* write header */ now = time(NULL); fprintf(stream, "#\n"); fprintf(stream, "# %s\n", filename); fprintf(stream, "#\n"); fprintf(stream, "# Configuration file for nvidia-settings - the NVIDIA " "X Server Settings utility\n"); /* NOTE: ctime(3) generates a new line */ fprintf(stream, "# Generated on %s", ctime(&now)); fprintf(stream, "#\n"); /* * set the locale to "C" before writing the configuration file to * reduce the risk of locale related parsing problems. Restore * the original locale before exiting this function. */ if (setlocale(LC_NUMERIC, "C") == NULL) { nv_warning_msg("Error writing configuration file '%s': could " "not set the locale 'C'.", filename); locale = conf->locale; } /* write the values in ConfigProperties */ write_config_properties(stream, conf, locale); /* for each screen, query each attribute in the table */ fprintf(stream, "\n"); fprintf(stream, "# Attributes:\n"); fprintf(stream, "\n"); /* * Note: we only save writable attributes addressable by X screen here * followed by attributes for display target types. */ for (node = system->targets[X_SCREEN_TARGET]; node; node = node->next) { t = node->t; /* skip it if we don't have a handle for this screen */ if (!t->h) continue; /* * construct the prefix that will be printed in the config * file in front of each attribute on this screen; this will * either be "[screen]" or "[displayname]". */ if (conf->booleans & CONFIG_PROPERTIES_INCLUDE_DISPLAY_NAME_IN_CONFIG_FILE) { prefix = t->name; } else { snprintf(scratch, 4, "%d", NvCtrlGetTargetId(t)); prefix = scratch; } /* loop over all the entries in the table */ for (entry = 0; entry < attributeTableLen; entry++) { const AttributeTableEntry *a = &attributeTable[entry]; /* * skip all attributes that are not supposed to be written * to the config file */ if (a->flags.no_config_write) { continue; } /* * special case the color attributes because we want to * print floats */ if (a->type == CTRL_ATTRIBUTE_TYPE_COLOR) { float c[3], b[3], g[3]; /* * if we are using RandR gamma, skip saving the color info */ status = NvCtrlGetAttribute(t, NV_CTRL_ATTR_RANDR_GAMMA_AVAILABLE, &val); if (status == NvCtrlSuccess && val) continue; status = NvCtrlGetColorAttributes(t, c, b, g); if (status != NvCtrlSuccess) continue; fprintf(stream, "%s%c%s=%f\n", prefix, DISPLAY_NAME_SEPARATOR, a->name, get_color_value(a->attr, c, b, g)); continue; } /* Only write out integer attributes, string and SDI CSC attributes * aren't written here. */ if (a->type != CTRL_ATTRIBUTE_TYPE_INTEGER) { continue; } /* * Ignore display attributes (they are written later on) and only * write attributes that can be written for an X screen target */ status = NvCtrlGetAttributePerms(t, a->type, a->attr, &perms); if (status != NvCtrlSuccess || !(perms.write) || !(perms.valid_targets & CTRL_TARGET_PERM_BIT(X_SCREEN_TARGET)) || (perms.valid_targets & CTRL_TARGET_PERM_BIT(DISPLAY_TARGET))) { continue; } status = NvCtrlGetAttribute(t, a->attr, &val); if (status != NvCtrlSuccess) { continue; } if (a->f.int_flags.is_display_id) { const char *name = NvCtrlGetDisplayConfigName(system, val); if (name) { fprintf(stream, "%s%c%s=%s\n", prefix, DISPLAY_NAME_SEPARATOR, a->name, name); } continue; } fprintf(stream, "%s%c%s=%d\n", prefix, DISPLAY_NAME_SEPARATOR, a->name, val); } /* entry */ } /* screen */ /* * Write attributes addressable to display targets */ for (node = system->targets[DISPLAY_TARGET]; node; node = node->next) { t = node->t; /* skip it if we don't have a handle for this display */ if (!t->h) continue; /* * check to see if we have RANDR gamma available. We may * skip writing attributes if it is missing. */ status = NvCtrlGetAttribute(t, NV_CTRL_ATTR_RANDR_GAMMA_AVAILABLE, &randr_gamma_available); if (status != NvCtrlSuccess) { randr_gamma_available = 0; } /* Get the prefix we want to use for the display device target */ prefix = create_display_device_target_string(t, conf); /* loop over all the entries in the table */ for (entry = 0; entry < attributeTableLen; entry++) { const AttributeTableEntry *a = &attributeTable[entry]; /* * skip all attributes that are not supposed to be written * to the config file */ if (a->flags.no_config_write) { continue; } /* * for the display target we only write color attributes for now */ if (a->type == CTRL_ATTRIBUTE_TYPE_COLOR) { float c[3], b[3], g[3]; if (!randr_gamma_available) continue; status = NvCtrlGetColorAttributes(t, c, b, g); if (status != NvCtrlSuccess) continue; fprintf(stream, "%s%c%s=%f\n", prefix, DISPLAY_NAME_SEPARATOR, a->name, get_color_value(a->attr, c, b, g)); continue; } /* Only write out integer attributes, string and SDI CSC attributes * aren't written here. */ if (a->type != CTRL_ATTRIBUTE_TYPE_INTEGER) { continue; } /* Make sure this is a display and writable attribute */ status = NvCtrlGetAttributePerms(t, a->type, a->attr, &perms); if (status != NvCtrlSuccess || !(perms.write) || !(perms.valid_targets & CTRL_TARGET_PERM_BIT(DISPLAY_TARGET))) { continue; } status = NvCtrlGetAttribute(t, a->attr, &val); if (status == NvCtrlSuccess) { fprintf(stream, "%s%c%s=%d\n", prefix, DISPLAY_NAME_SEPARATOR, a->name, val); } } free(prefix); } /* * loop the ParsedAttribute list, writing the attributes to file. * note that we ignore conf->include_display_name_in_config_file * when writing these parsed attributes; this is because parsed * attributes (like the framelock properties) require a display * name be specified (since there are multiple X servers * involved). */ while (p) { char target_str[64]; const AttributeTableEntry *a = p->attr_entry; if (!p->next) { p = p->next; continue; } /* * if the parsed attribute has a target specification, and a * target type other than an X screen, include a target * specification in what we write to the .rc file. */ target_str[0] = '\0'; if (p->parser_flags.has_target && (p->target_type != X_SCREEN_TARGET)) { const CtrlTargetTypeInfo *targetTypeInfo; /* Find the target name of the target type */ targetTypeInfo = NvCtrlGetTargetTypeInfo(p->target_type); if (targetTypeInfo) { snprintf(target_str, 64, "[%s:%d]", targetTypeInfo->parsed_name, p->target_id); } } if (a->flags.hijack_display_device) { fprintf(stream, "%s%s%c%s[0x%08x]=%d\n", p->display, target_str, DISPLAY_NAME_SEPARATOR, a->name, p->display_device_mask, p->val.i); } else { fprintf(stream, "%s%s%c%s=%d\n", p->display, target_str, DISPLAY_NAME_SEPARATOR, a->name, p->val.i); } p = p->next; } setlocale(LC_NUMERIC, conf->locale); /* close the configuration file */ ret = fclose(stream); if (ret != 0) { nv_error_msg("Failure while closing file '%s'.", filename); return NV_FALSE; } return NV_TRUE; } /* nv_write_config_file() */