2023-10-08 10:09:22 +03:00
|
|
|
#include "wdisplays.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wayland-client-protocol.h>
|
2024-05-31 21:45:27 +03:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
2023-10-08 10:09:22 +03:00
|
|
|
#define MAX_NAME_LENGTH 256
|
|
|
|
#define MAX_MONITORS_NUM 10
|
|
|
|
struct wd_head_config;
|
|
|
|
struct profile_line {
|
|
|
|
int start;
|
|
|
|
int end;
|
|
|
|
};
|
|
|
|
char *get_config_file_path() {
|
2024-05-31 21:45:27 +03:00
|
|
|
char defaultPath[PATH_MAX]; // platform based marco PATH_MAX
|
|
|
|
char wdisplaysPath[PATH_MAX];
|
|
|
|
// if $XDG_CONFIG_HOME is set, use it
|
|
|
|
{
|
|
|
|
const char *configDir = getenv("XDG_CONFIG_HOME");
|
|
|
|
char defaultConfigDir[PATH_MAX];
|
|
|
|
if (configDir == NULL) {
|
|
|
|
const char *homeDir = getenv("HOME");
|
|
|
|
if (homeDir == NULL) {
|
|
|
|
perror("Cannot find home directory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
snprintf(defaultConfigDir, sizeof(defaultConfigDir), "%s/.config", homeDir);
|
|
|
|
} else {
|
|
|
|
snprintf(defaultConfigDir, sizeof(defaultConfigDir), "%s", configDir);
|
|
|
|
}
|
|
|
|
snprintf(defaultPath, sizeof(defaultPath), "%s/kanshi/config", defaultConfigDir);
|
|
|
|
snprintf(wdisplaysPath, sizeof(wdisplaysPath), "%s/wdisplays/config", defaultConfigDir);
|
2023-10-08 10:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *wdisplaysFile = fopen(wdisplaysPath, "r");
|
|
|
|
if (wdisplaysFile != NULL) {
|
2024-05-31 21:45:27 +03:00
|
|
|
char line[LINE_MAX]; // LINE_MAX is a platform based marco
|
2023-10-08 10:09:22 +03:00
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
// try to match "store_path" term
|
2023-10-08 10:09:22 +03:00
|
|
|
while (fgets(line, sizeof(line), wdisplaysFile) != NULL) {
|
|
|
|
if (strstr(line, "store_path") != NULL) {
|
2024-05-31 21:45:27 +03:00
|
|
|
// if found, extract path
|
2023-10-08 10:09:22 +03:00
|
|
|
char *pathStart = strchr(line, '=');
|
|
|
|
if (pathStart != NULL) {
|
2024-05-31 21:45:27 +03:00
|
|
|
pathStart++; // skip '='
|
2023-10-08 10:09:22 +03:00
|
|
|
char *pathEnd = strchr(pathStart, '\n');
|
|
|
|
if (pathEnd != NULL) {
|
2024-05-31 21:45:27 +03:00
|
|
|
*pathEnd = '\0'; // replace '\n' with '\0'
|
2023-10-08 10:09:22 +03:00
|
|
|
fclose(wdisplaysFile);
|
2024-05-31 21:45:27 +03:00
|
|
|
return strdup(pathStart); // return path
|
2023-10-08 10:09:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(wdisplaysFile);
|
|
|
|
}
|
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
// if store_path is not found in wdisplays config file, return default path
|
2023-10-08 10:09:22 +03:00
|
|
|
return strdup(defaultPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct profile_line match(char **descriptions, int num, char *filename) {
|
|
|
|
struct profile_line matched_profile;
|
|
|
|
matched_profile.start = -1;
|
|
|
|
matched_profile.end = -1;
|
2024-05-31 21:45:27 +03:00
|
|
|
// -1 means not found
|
2023-10-08 10:09:22 +03:00
|
|
|
FILE *configFile = fopen(filename, "r");
|
|
|
|
if (configFile == NULL) {
|
|
|
|
perror("File open failed.");
|
|
|
|
return matched_profile;
|
|
|
|
}
|
2024-05-31 21:45:27 +03:00
|
|
|
// buffer to store each line
|
|
|
|
char buffer[LINE_MAX];
|
2023-10-08 10:09:22 +03:00
|
|
|
char profileName[MAX_NAME_LENGTH];
|
2024-05-31 21:45:27 +03:00
|
|
|
int profileStartLine = 0; // mark the start line of matched profile
|
|
|
|
int profileEndLine = 0; // mark the end line of matched profile
|
2023-10-08 10:09:22 +03:00
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
int lineCount = 0; // current line number
|
2023-10-08 10:09:22 +03:00
|
|
|
|
|
|
|
while (fgets(buffer, sizeof(buffer), configFile) != NULL) {
|
2024-05-31 21:45:27 +03:00
|
|
|
lineCount++;
|
2023-10-08 10:09:22 +03:00
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
// check if "profile" keyword is in the line
|
2023-10-08 10:09:22 +03:00
|
|
|
if (strstr(buffer, "profile") != NULL) {
|
2024-05-31 21:45:27 +03:00
|
|
|
// extract profile name
|
2023-10-08 10:09:22 +03:00
|
|
|
sscanf(buffer, "profile %s {", profileName);
|
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
// the number of matched outputs
|
|
|
|
uint32_t profileMatchedNum = 0;
|
2023-10-08 10:09:22 +03:00
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
// record the start line of the profile
|
2023-10-08 10:09:22 +03:00
|
|
|
profileStartLine = lineCount;
|
|
|
|
|
|
|
|
while (fgets(buffer, sizeof(buffer), configFile) != NULL) {
|
2024-05-31 21:45:27 +03:00
|
|
|
lineCount++;
|
2023-10-08 10:09:22 +03:00
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
// check if the profile ends
|
2023-10-08 10:09:22 +03:00
|
|
|
if (buffer[0] == '}') {
|
|
|
|
profileEndLine = lineCount;
|
2024-05-31 21:45:27 +03:00
|
|
|
break;
|
2023-10-08 10:09:22 +03:00
|
|
|
}
|
|
|
|
char outputName[MAX_NAME_LENGTH];
|
|
|
|
// 从当前行提取输出名称
|
|
|
|
char *trimmedBuffer = buffer;
|
|
|
|
while (isspace(*trimmedBuffer)) {
|
2024-05-31 21:45:27 +03:00
|
|
|
trimmedBuffer++; // skip leading spaces
|
2023-10-08 10:09:22 +03:00
|
|
|
}
|
2024-05-31 21:45:27 +03:00
|
|
|
sscanf(trimmedBuffer, "output \"%99[^\"]\"", outputName); // extract output name
|
2023-10-08 10:09:22 +03:00
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
// check if the output name is in the descriptions
|
|
|
|
bool matched = false;
|
2023-10-08 10:09:22 +03:00
|
|
|
for (int i = 0; descriptions[i] != NULL; i++) {
|
|
|
|
if (strcmp(outputName, descriptions[i]) == 0) {
|
2024-05-31 21:45:27 +03:00
|
|
|
matched = true;
|
|
|
|
profileMatchedNum++;
|
2023-10-08 10:09:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matched) {
|
2024-05-31 21:45:27 +03:00
|
|
|
// if any output is not matched, break
|
|
|
|
profileMatchedNum = 0;
|
2023-10-08 10:09:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 21:45:27 +03:00
|
|
|
if (profileMatchedNum == num) {
|
|
|
|
printf("Matched profile:%s\n", profileName);
|
|
|
|
printf("Start line:%d\n", profileStartLine);
|
2023-10-08 10:09:22 +03:00
|
|
|
matched_profile.start = profileStartLine;
|
2024-05-31 21:45:27 +03:00
|
|
|
printf("End line:%d\n", profileEndLine);
|
2023-10-08 10:09:22 +03:00
|
|
|
matched_profile.end = profileEndLine;
|
|
|
|
|
|
|
|
fclose(configFile);
|
|
|
|
return matched_profile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(configFile);
|
2024-05-31 21:45:27 +03:00
|
|
|
printf("Cannot find existing profile to match\n");
|
2023-10-08 10:09:22 +03:00
|
|
|
return matched_profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
int store_config(struct wl_list *outputs) {
|
|
|
|
char *file_name = get_config_file_path();
|
2024-05-31 21:45:27 +03:00
|
|
|
char tmp_file_name[PATH_MAX];
|
2023-10-08 10:09:22 +03:00
|
|
|
sprintf(tmp_file_name,"%s.tmp",file_name);
|
|
|
|
|
|
|
|
char *descriptions[MAX_MONITORS_NUM];
|
|
|
|
for (int i = 0; i < MAX_MONITORS_NUM; i++) {
|
|
|
|
descriptions[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *outputConfigs[MAX_MONITORS_NUM];
|
|
|
|
for (int i = 0; i < MAX_MONITORS_NUM; i++) {
|
|
|
|
outputConfigs[i] = (char *)malloc(MAX_NAME_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wd_head_config *output;
|
|
|
|
int description_index = 0;
|
|
|
|
wl_list_for_each(output, outputs, link) {
|
|
|
|
struct wd_head *head = output->head;
|
|
|
|
|
|
|
|
// for transform
|
|
|
|
char *trans_str = (char *)malloc(15 * sizeof(char));
|
|
|
|
switch (output->transform) {
|
|
|
|
case WL_OUTPUT_TRANSFORM_NORMAL:
|
|
|
|
strcpy(trans_str, "normal");
|
|
|
|
break;
|
|
|
|
case WL_OUTPUT_TRANSFORM_90:
|
|
|
|
strcpy(trans_str, "90");
|
|
|
|
break;
|
|
|
|
case WL_OUTPUT_TRANSFORM_180:
|
|
|
|
strcpy(trans_str, "180");
|
|
|
|
break;
|
|
|
|
case WL_OUTPUT_TRANSFORM_270:
|
|
|
|
strcpy(trans_str, "270");
|
|
|
|
break;
|
|
|
|
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
|
|
|
|
strcpy(trans_str, "flipped-90");
|
|
|
|
break;
|
|
|
|
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
|
|
|
|
strcpy(trans_str, "flipped-180");
|
|
|
|
break;
|
|
|
|
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
|
|
|
|
strcpy(trans_str, "flipped-270");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strcpy(trans_str, "normal");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (description_index < MAX_MONITORS_NUM) {
|
|
|
|
descriptions[description_index] = strdup(head->description);
|
2024-05-31 21:45:27 +03:00
|
|
|
// write output config in given format
|
2023-10-08 10:09:22 +03:00
|
|
|
sprintf(
|
|
|
|
outputConfigs[description_index],
|
|
|
|
"output \"%s\" position %d,%d mode %dx%d@%.4f scale %.2f transform %s",
|
|
|
|
head->description, output->x, output->y, output->width,
|
|
|
|
output->height, output->refresh / 1.0e3, output->scale, trans_str);
|
|
|
|
description_index++;
|
|
|
|
} else {
|
2024-05-31 21:45:27 +03:00
|
|
|
free(trans_str);
|
|
|
|
printf("Too many monitor! 10 is the");
|
2023-10-08 10:09:22 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(trans_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
int num_of_monitors = description_index;
|
|
|
|
|
|
|
|
struct profile_line matched_profile;
|
|
|
|
matched_profile = match(descriptions, num_of_monitors, file_name);
|
|
|
|
|
|
|
|
if (matched_profile.start == -1) {
|
2024-05-31 21:45:27 +03:00
|
|
|
// append new profile
|
2023-10-08 10:09:22 +03:00
|
|
|
FILE *file = fopen(file_name, "a");
|
|
|
|
if (file == NULL) {
|
|
|
|
perror("File open failed.");
|
2024-05-31 21:45:27 +03:00
|
|
|
free(file_name);
|
2023-10-08 10:09:22 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fprintf(file, "\nprofile {\n");
|
|
|
|
for (int i = 0; i<num_of_monitors; i++) {
|
|
|
|
fprintf(file, " %s\n", outputConfigs[i]);
|
|
|
|
free(outputConfigs[i]);
|
|
|
|
}
|
|
|
|
fprintf(file, "}");
|
|
|
|
fclose(file);
|
|
|
|
} else if (matched_profile.start < matched_profile.end) {
|
2024-05-31 21:45:27 +03:00
|
|
|
// rewrite correspondence lines
|
2023-10-08 10:09:22 +03:00
|
|
|
FILE *file = fopen(file_name, "r");
|
|
|
|
if (file == NULL) {
|
|
|
|
perror("File open failed.");
|
2024-05-31 21:45:27 +03:00
|
|
|
free(file_name);
|
2023-10-08 10:09:22 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
FILE *tmp = fopen(tmp_file_name, "w");
|
|
|
|
if (tmp == NULL) {
|
|
|
|
perror("Tmp file cannot be created.");
|
|
|
|
fclose(file);
|
2024-05-31 21:45:27 +03:00
|
|
|
free(file_name);
|
2023-10-08 10:09:22 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2024-05-31 21:45:27 +03:00
|
|
|
char _buffer[LINE_MAX];
|
2023-10-08 10:09:22 +03:00
|
|
|
int _line = 0;
|
|
|
|
int _i_output = 0;
|
|
|
|
while (fgets(_buffer, sizeof(_buffer), file) != NULL) {
|
|
|
|
if (_line >= matched_profile.start && _line < matched_profile.end - 1) {
|
|
|
|
if(_i_output>=num_of_monitors){
|
|
|
|
perror("Null pointer");
|
|
|
|
fclose(tmp);
|
|
|
|
fclose(file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fprintf(tmp," %s\n",outputConfigs[_i_output]);
|
|
|
|
free(outputConfigs[_i_output]);
|
|
|
|
|
|
|
|
_i_output++;
|
|
|
|
} else{
|
|
|
|
fprintf(tmp,"%s",_buffer);
|
|
|
|
}
|
|
|
|
_line++;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
fclose(tmp);
|
|
|
|
|
|
|
|
remove(file_name);
|
|
|
|
rename(tmp_file_name, file_name);
|
2024-05-31 21:45:27 +03:00
|
|
|
free(file_name);
|
2023-10-08 10:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|