fix bug##227186 系统显示信息获取模块-亮度-命令执行获取亮度值与控制面板设置值不一致

This commit is contained in:
shaozhimin 2024-05-17 15:46:26 +08:00
parent 2a72d2311d
commit 23d56e9fb6
2 changed files with 122 additions and 102 deletions

View File

@ -1955,6 +1955,8 @@ BrightnessInfo* kdk_edid_get_max_brightness(char *name)
char bus_name[32] = {0};
char **bus = NULL;
char command[32] = {0};
int brightness = 0;
FILE *fp = popen("ddcutil detect", "r");
if(!fp)
@ -1982,36 +1984,33 @@ BrightnessInfo* kdk_edid_get_max_brightness(char *name)
}
pclose(fp);
if(strlen(bus_name) == 0)
if(strlen(bus_name) != 0)
{
return NULL;
}
char *p = NULL;
char *buff[3];
int i = 0;
p = strtok(bus_name, "-");
while(p)
{
buff[i] = p;
i++;
p = strtok(NULL,"");
}
char *p = NULL;
char *buff[3];
int i = 0;
p = strtok(bus_name, "-");
while(p)
{
buff[i] = p;
i++;
p = strtok(NULL,"");
sprintf(command, "ddcutil --bus %s getvcp 10", buff[1]);
fp = popen(command, "r");
if(!fp)
{
return NULL;
}
fgets(line, 256, fp);
pclose(fp);
char res[256] = {0};
sscanf(line, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %s", res);
strstrip(res, ',');
brightness = atoi(res);
}
char command[32] = {0};
int brightness = 0;
sprintf(command, "ddcutil --bus %s getvcp 10", buff[1]);
fp = popen(command, "r");
if(!fp)
{
return NULL;
}
fgets(line, 256, fp);
pclose(fp);
char res[256] = {0};
sscanf(line, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %s", res);
strstrip(res, ',');
brightness = atoi(res);
if(brightness == 0)
{
brightness = 100;
@ -2026,8 +2025,15 @@ BrightnessInfo* kdk_edid_get_max_brightness(char *name)
char path[256] = {0};
int brightness_value = 0;
direfd = readdir(dirfd);
sprintf(path, "/sys/class/backlight/%s/max_brightness", direfd->d_name);
while ((direfd = readdir(dirfd)) != NULL) // 循环读取/proc下的每一个文件/文件夹
{
// 如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过
if ((strcmp(direfd->d_name, ".") == 0) || (strcmp(direfd->d_name, "..") == 0))
continue;
sprintf(path, "/sys/class/backlight/%s/max_brightness", direfd->d_name);
}
closedir(dirfd);
fp = fopen(path, "r");
@ -2040,7 +2046,6 @@ BrightnessInfo* kdk_edid_get_max_brightness(char *name)
info = (BrightnessInfo *)calloc(1, sizeof(BrightnessInfo));
info->brightness_value = brightness_value;
info->brightness_percentage = brightness;
return info;
}
@ -2051,69 +2056,9 @@ BrightnessInfo* kdk_edid_get_current_brightness(char *name)
return NULL;
}
BrightnessInfo *info = NULL;
char *serial = kdk_edid_get_serialNumber(name);
char line[256] = {0};
char bus_name[32] = {0};
char **bus = NULL;
FILE *fp = popen("ddcutil detect", "r");
if(!fp)
{
return NULL;
}
while (fgets(line, 256, fp))
{
if(strlen(line) == 0)
break;
if(strstr(line, "I2C bus:"))
{
strstripspace(line);
strcpy(bus_name, line);
continue;
}
if(serial)
{
if(strstr(line, serial))
{
break;
}
}
}
pclose(fp);
if(strlen(bus_name) == 0)
{
return NULL;
}
char *p = NULL;
char *buff[3];
int i = 0;
p = strtok(bus_name, "-");
while(p)
{
buff[i] = p;
i++;
p = strtok(NULL,"");
}
char command[32] = {0};
FILE *fp = NULL;
int brightness = 0;
sprintf(command, "ddcutil --bus %s getvcp 10", buff[1]);
fp = popen(command, "r");
if(!fp)
{
return NULL;
}
fgets(line, 256, fp);
char res[256] = {0};
pclose(fp);
sscanf(line, "%*s %*s %*s %*s %*s %*s %*s %*s %s", res);
strstrip(res, ',');
brightness = atoi(res);
char line[256] = {0};
DIR *dirfd = opendir("/sys/class/backlight");
if (!dirfd)
@ -2123,8 +2068,13 @@ BrightnessInfo* kdk_edid_get_current_brightness(char *name)
struct dirent *direfd = NULL;
char path[512] = {0};
direfd = readdir(dirfd);
sprintf(path, "/sys/class/backlight/%s/brightness", direfd->d_name);
while ((direfd = readdir(dirfd)) != NULL) // 循环读取/proc下的每一个文件/文件夹
{
// 如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过
if ((strcmp(direfd->d_name, ".") == 0) || (strcmp(direfd->d_name, "..") == 0))
continue;
sprintf(path, "/sys/class/backlight/%s/brightness", direfd->d_name);
}
closedir(dirfd);
int brightness_value = 0;
@ -2138,12 +2088,78 @@ BrightnessInfo* kdk_edid_get_current_brightness(char *name)
info = (BrightnessInfo *)calloc(1, sizeof(BrightnessInfo));
info->brightness_value = brightness_value;
BrightnessInfo *max_info = kdk_edid_get_max_brightness(name);
if(max_info)
{
if(max_info->brightness_value != 0)
{
float value = ((float)brightness_value / (float)max_info->brightness_value) *100;
brightness = value;
}
}
if(brightness == 0)
{
BrightnessInfo *max_info = kdk_edid_get_max_brightness(name);
if(max_info->brightness_value != 0)
info->brightness_percentage = brightness_value / max_info->brightness_value;
char *serial = kdk_edid_get_serialNumber(name);
char bus_name[32] = {0};
char **bus = NULL;
fp = popen("ddcutil detect", "r");
if(!fp)
{
return NULL;
}
while (fgets(line, 256, fp))
{
if(strlen(line) == 0)
break;
if(strstr(line, "I2C bus:"))
{
strstripspace(line);
strcpy(bus_name, line);
continue;
}
if(serial)
{
if(strstr(line, serial))
{
break;
}
}
}
pclose(fp);
if(strlen(bus_name) != 0)
{
char *p = NULL;
char *buff[3];
int i = 0;
p = strtok(bus_name, "-");
while(p)
{
buff[i] = p;
i++;
p = strtok(NULL,"");
}
char command[32] = {0};
sprintf(command, "ddcutil --bus %s getvcp 10", buff[1]);
fp = popen(command, "r");
if(!fp)
{
return NULL;
}
fgets(line, 256, fp);
char res[256] = {0};
pclose(fp);
sscanf(line, "%*s %*s %*s %*s %*s %*s %*s %*s %s", res);
strstrip(res, ',');
brightness = atoi(res);
info->brightness_percentage = brightness;
}
}
else
{

View File

@ -93,21 +93,25 @@ int main()
if (cur_brightness)
{
printf("brightness = %d\n", cur_brightness->brightness_percentage);
printf("brightness_value = %d\n", max_info->brightness_value);
printf("brightness_value = %d\n", cur_brightness->brightness_value);
}
ChromaticityCoordinates *red = kdk_edid_get_red_primary(name[count]);
printf("redx = %s, redy = %s\n", red->xCoordinate, red->yCoordinate);
if(red)
printf("redx = %s, redy = %s\n", red->xCoordinate, red->yCoordinate);
ChromaticityCoordinates *green = kdk_edid_get_green_primary(name[count]);
printf("greenx = %s, greeny = %s\n", green->xCoordinate, green->yCoordinate);
if(green)
printf("greenx = %s, greeny = %s\n", green->xCoordinate, green->yCoordinate);
ChromaticityCoordinates *blue = kdk_edid_get_blue_primary(name[count]);
printf("bluex = %s, bluey = %s\n", blue->xCoordinate, blue->yCoordinate);
if(blue)
printf("bluex = %s, bluey = %s\n", blue->xCoordinate, blue->yCoordinate);
ChromaticityCoordinates *white = kdk_edid_get_white_primary(name[count]);
printf("whitex = %s, whitey = %s\n", white->xCoordinate, white->yCoordinate);
if(white)
printf("whitex = %s, whitey = %s\n", white->xCoordinate, white->yCoordinate);
float rawDpiX = kdk_edid_get_rawDpiX(name[count]);
printf("rawDpiX = %.2f\n", rawDpiX);