接上一篇文章《xf86-video-intel源码分析4 —— intel_options.c和intel_options.h(1)》,链接为:
xf86-video-intel源码分析4 —— intel_options.c和intel_options.h(1)_蓝天居士的博客-CSDN博客
本节对intel_options.c中的函数进行分析。
源码如下:
OptionInfoPtr intel_options_get(ScrnInfoPtr scrn)
{OptionInfoPtr options;xf86CollectOptions(scrn, NULL);if (!(options = malloc(sizeof(intel_options))))return NULL;memcpy(options, intel_options, sizeof(intel_options));xf86ProcessOptions(scrn->scrnIndex, scrn->options, options);return options;
}
options的类型为OptionInfoPtr,即OptionInfoRec结构的指针。但是注意,这里给它分配的空间不仅仅包含一个实例即数组的一项,而是包含了整个intel_options数组,options指向的是数组intel_options数组拷贝到内存后的内存首地址。
xf86开头的函数并不在xf86-video-intel源码中,也是在xorf-server代码中。xf86CollectOptions在xorg-server的hw/xfree86/common/xf86Option.c中,源码如下:
/** xf86CollectOptions collects the options from each of the config file* sections used by the screen and puts the combined list in pScrn->options.* This function requires that the following have been initialised:** pScrn->confScreen* pScrn->Entities[i]->device* pScrn->display* pScrn->monitor** The extraOpts parameter may optionally contain a list of additional options* to include.** The order of precedence for options is:** extraOpts, display, confScreen, monitor, device, outputClassOptions*/void
xf86CollectOptions(ScrnInfoPtr pScrn, XF86OptionPtr extraOpts)
{XF86OptionPtr tmp;XF86OptionPtr extras = (XF86OptionPtr) extraOpts;GDevPtr device;int i;pScrn->options = NULL;for (i = pScrn->numEntities - 1; i >= 0; i--) {xf86MergeOutputClassOptions(pScrn->entityList[i], &pScrn->options);device = xf86GetDevFromEntity(pScrn->entityList[i],pScrn->entityInstanceList[i]);if (device && device->options) {tmp = xf86optionListDup(device->options);if (pScrn->options)pScrn->options = xf86optionListMerge(pScrn->options, tmp);elsepScrn->options = tmp;}}if (pScrn->monitor->options) {tmp = xf86optionListDup(pScrn->monitor->options);if (pScrn->options)pScrn->options = xf86optionListMerge(pScrn->options, tmp);elsepScrn->options = tmp;}if (pScrn->confScreen->options) {tmp = xf86optionListDup(pScrn->confScreen->options);if (pScrn->options)pScrn->options = xf86optionListMerge(pScrn->options, tmp);elsepScrn->options = tmp;}if (pScrn->display->options) {tmp = xf86optionListDup(pScrn->display->options);if (pScrn->options)pScrn->options = xf86optionListMerge(pScrn->options, tmp);elsepScrn->options = tmp;}if (extras) {tmp = xf86optionListDup(extras);if (pScrn->options)pScrn->options = xf86optionListMerge(pScrn->options, tmp);elsepScrn->options = tmp;}
}
根据函数说明,xf86CollectOptions收集屏幕使用的配置文件中每个段的选项,并且将这些选项的组合列表放置到pScrn->options中。
xf86ProcessOptions函数同样在xorg-server的xf86Option.c中实现,代码如下:
void
xf86ProcessOptions(int scrnIndex, XF86OptionPtr options, OptionInfoPtr optinfo)
{OptionInfoPtr p;for (p = optinfo; p->name != NULL; p++) {ParseOptionValue(scrnIndex, options, p, TRUE);}
}
static Bool
ParseOptionValue(int scrnIndex, XF86OptionPtr options, OptionInfoPtr p,Bool markUsed)
{const char *s;char *end;Bool wasUsed = FALSE;if ((s = xf86findOptionValue(options, p->name)) != NULL) {if (markUsed) {wasUsed = xf86CheckIfOptionUsedByName(options, p->name);xf86MarkOptionUsedByName(options, p->name);}switch (p->type) {case OPTV_INTEGER:if (*s == '\0') {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires an integer value\n",p->name);}p->found = FALSE;}else {p->value.num = strtoul(s, &end, 0);if (*end == '\0') {p->found = TRUE;}else {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires an integer value\n",p->name);}p->found = FALSE;}}break;case OPTV_STRING:if (*s == '\0') {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a string value\n",p->name);}p->found = FALSE;}else {p->value.str = s;p->found = TRUE;}break;case OPTV_ANYSTR:p->value.str = s;p->found = TRUE;break;case OPTV_REAL:if (*s == '\0') {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a floating point ""value\n", p->name);}p->found = FALSE;}else {p->value.realnum = strtod(s, &end);if (*end == '\0') {p->found = TRUE;}else {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a floating point ""value\n", p->name);}p->found = FALSE;}}break;case OPTV_BOOLEAN:if (GetBoolValue(p, s)) {p->found = TRUE;}else {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a boolean value\n",p->name);}p->found = FALSE;}break;case OPTV_PERCENT:{char tmp = 0;/* awkward match, but %% doesn't increase the match counter,* hence 100 looks the same as 100% to the caller of sccanf*/if (sscanf(s, "%lf%c", &p->value.realnum, &tmp) != 2 || tmp != '%') {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a percent value\n",p->name);}p->found = FALSE;}else {p->found = TRUE;}}break;case OPTV_FREQ:if (*s == '\0') {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a frequency value\n",p->name);}p->found = FALSE;}else {double freq = strtod(s, &end);int units = 0;if (end != s) {p->found = TRUE;if (!xf86NameCmp(end, "Hz"))units = 1;else if (!xf86NameCmp(end, "kHz") || !xf86NameCmp(end, "k"))units = 1000;else if (!xf86NameCmp(end, "MHz") || !xf86NameCmp(end, "M"))units = 1000000;else {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a frequency value\n",p->name);}p->found = FALSE;}if (p->found)freq *= (double) units;}else {if (markUsed) {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a frequency value\n",p->name);}p->found = FALSE;}if (p->found) {p->value.freq.freq = freq;p->value.freq.units = units;}}break;case OPTV_NONE:/* Should never get here */p->found = FALSE;break;}if (p->found && markUsed) {int verb = 2;if (wasUsed)verb = 4;xf86DrvMsgVerb(scrnIndex, X_CONFIG, verb, "Option \"%s\"", p->name);if (!(p->type == OPTV_BOOLEAN && *s == 0)) {xf86ErrorFVerb(verb, " \"%s\"", s);}xf86ErrorFVerb(verb, "\n");}}else if (p->type == OPTV_BOOLEAN) {/* Look for matches with options with or without a "No" prefix. */char *n, *newn;OptionInfoRec opt;n = xf86NormalizeName(p->name);if (!n) {p->found = FALSE;return FALSE;}if (strncmp(n, "no", 2) == 0) {newn = n + 2;}else {free(n);if (asprintf(&n, "No%s", p->name) == -1) {p->found = FALSE;return FALSE;}newn = n;}if ((s = xf86findOptionValue(options, newn)) != NULL) {if (markUsed)xf86MarkOptionUsedByName(options, newn);if (GetBoolValue(&opt, s)) {p->value.boolean = !opt.value.boolean;p->found = TRUE;}else {xf86DrvMsg(scrnIndex, X_WARNING,"Option \"%s\" requires a boolean value\n", newn);p->found = FALSE;}}else {p->found = FALSE;}if (p->found && markUsed) {xf86DrvMsgVerb(scrnIndex, X_CONFIG, 2, "Option \"%s\"", newn);if (*s != 0) {xf86ErrorFVerb(2, " \"%s\"", s);}xf86ErrorFVerb(2, "\n");}free(n);}else {p->found = FALSE;}return p->found;
}
Bool intel_option_cast_to_bool(OptionInfoPtr options, int id, Bool val)
{
#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,7,99,901,0)xf86getBoolValue(&val, xf86GetOptValString(options, id));
#endifreturn val;
}
如果xorg-server的版本比1.7.99.901新,则调用xf86getBoolValue函数,将options中的字符串转换为布尔值;否则不做处理,直接返回。
static int
namecmp(const char *s1, const char *s2)
{char c1, c2;if (!s1 || *s1 == 0) {if (!s2 || *s2 == 0)return 0;elsereturn 1;}while (*s1 == '_' || *s1 == ' ' || *s1 == '\t')s1++;while (*s2 == '_' || *s2 == ' ' || *s2 == '\t')s2++;c1 = isupper(*s1) ? tolower(*s1) : *s1;c2 = isupper(*s2) ? tolower(*s2) : *s2;while (c1 == c2) {if (c1 == '\0')return 0;s1++;while (*s1 == '_' || *s1 == ' ' || *s1 == '\t')s1++;s2++;while (*s2 == '_' || *s2 == ' ' || *s2 == '\t')s2++;c1 = isupper(*s1) ? tolower(*s1) : *s1;c2 = isupper(*s2) ? tolower(*s2) : *s2;}return c1 - c2;
}
这个函数顾名思义,进行名称比较,有些类似于strcmp函数。
unsigned intel_option_cast_to_unsigned(OptionInfoPtr options, int id, unsigned val)
{
#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,7,99,901,0)const char *str = xf86GetOptValString(options, id);
#elseconst char *str = NULL;
#endifunsigned v;if (str == NULL || *str == '\0')return val;if (namecmp(str, "on") == 0)return val;if (namecmp(str, "true") == 0)return val;if (namecmp(str, "yes") == 0)return val;if (namecmp(str, "0") == 0)return 0;if (namecmp(str, "off") == 0)return 0;if (namecmp(str, "false") == 0)return 0;if (namecmp(str, "no") == 0)return 0;v = atoi(str);if (v)return v;return val;
}
这个函数同样不难理解,调用了上边的namecmp函数。如果入参str中包含了"on"、"true"、"yes",则返回val;如果str中包含的是"0"、"off"、"false"或者"no",则直接返回0;如果str中没有这些关键字,则将字符串转换为整数并返回。
上一篇:一个完整的项目流程
下一篇:机器学习 --- kNN算法