2017-06-05 13:44:29 +02:00
|
|
|
#define _XOPEN_SOURCE 700
|
2017-04-14 23:37:43 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
2016-01-24 02:59:58 +01:00
|
|
|
#include <math.h>
|
2016-09-01 08:18:37 -04:00
|
|
|
#include <stdint.h>
|
2016-06-10 06:08:59 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2016-09-01 08:18:37 -04:00
|
|
|
#include <string.h>
|
2017-03-10 23:41:24 -05:00
|
|
|
#include <strings.h>
|
2016-09-01 08:18:37 -04:00
|
|
|
#include <xkbcommon/xkbcommon-names.h>
|
|
|
|
|
#include "log.h"
|
2016-06-10 06:08:59 -05:00
|
|
|
#include "readline.h"
|
2015-08-25 19:53:59 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
2015-08-25 15:17:18 +02:00
|
|
|
int wrap(int i, int max) {
|
|
|
|
|
return ((i % max) + max) % max;
|
|
|
|
|
}
|
2015-12-14 17:07:31 +01:00
|
|
|
|
2016-01-23 16:35:32 -05:00
|
|
|
int numlen(int n) {
|
2016-01-24 02:59:58 +01:00
|
|
|
if (n == 0) {
|
2016-01-24 03:02:51 +01:00
|
|
|
return 1;
|
2016-01-24 02:59:58 +01:00
|
|
|
}
|
|
|
|
|
return log10(n) + 1;
|
2016-01-23 16:35:32 -05:00
|
|
|
}
|
|
|
|
|
|
2016-06-10 06:08:59 -05:00
|
|
|
pid_t get_parent_pid(pid_t child) {
|
2016-06-11 15:29:04 -05:00
|
|
|
pid_t parent = -1;
|
2016-06-10 06:08:59 -05:00
|
|
|
char file_name[100];
|
|
|
|
|
char *buffer = NULL;
|
|
|
|
|
char *token = NULL;
|
2016-06-11 12:43:34 -05:00
|
|
|
const char *sep = " ";
|
2016-06-10 06:08:59 -05:00
|
|
|
FILE *stat = NULL;
|
|
|
|
|
|
|
|
|
|
sprintf(file_name, "/proc/%d/stat", child);
|
|
|
|
|
|
2016-06-11 15:29:04 -05:00
|
|
|
if ((stat = fopen(file_name, "r"))) {
|
|
|
|
|
if ((buffer = read_line(stat))) {
|
|
|
|
|
token = strtok(buffer, sep); // pid
|
|
|
|
|
token = strtok(NULL, sep); // executable name
|
|
|
|
|
token = strtok(NULL, sep); // state
|
|
|
|
|
token = strtok(NULL, sep); // parent pid
|
|
|
|
|
parent = strtol(token, NULL, 10);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-11 12:43:34 -05:00
|
|
|
fclose(stat);
|
2016-06-11 15:29:04 -05:00
|
|
|
}
|
2016-06-11 12:43:34 -05:00
|
|
|
|
2016-06-11 15:29:04 -05:00
|
|
|
if (parent) {
|
2016-06-11 12:43:34 -05:00
|
|
|
return (parent == child) ? -1 : parent;
|
2016-06-10 06:08:59 -05:00
|
|
|
}
|
|
|
|
|
|
2016-06-11 12:43:34 -05:00
|
|
|
return -1;
|
2016-06-10 06:08:59 -05:00
|
|
|
}
|
2016-07-30 18:50:13 -05:00
|
|
|
|
|
|
|
|
uint32_t parse_color(const char *color) {
|
2017-02-21 12:49:22 -07:00
|
|
|
if (color[0] == '#') {
|
|
|
|
|
++color;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-30 18:50:13 -05:00
|
|
|
int len = strlen(color);
|
2017-02-21 12:49:22 -07:00
|
|
|
if (len != 6 && len != 8) {
|
2018-05-24 21:45:22 +02:00
|
|
|
sway_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color);
|
2016-07-30 18:50:13 -05:00
|
|
|
return 0xFFFFFFFF;
|
|
|
|
|
}
|
2017-02-21 12:49:22 -07:00
|
|
|
uint32_t res = (uint32_t)strtoul(color, NULL, 16);
|
|
|
|
|
if (strlen(color) == 6) {
|
2016-07-30 18:50:13 -05:00
|
|
|
res = (res << 8) | 0xFF;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2017-04-14 23:37:43 +03:00
|
|
|
|
|
|
|
|
char* resolve_path(const char* path) {
|
2017-05-11 19:38:32 +03:00
|
|
|
struct stat sb;
|
|
|
|
|
ssize_t r;
|
|
|
|
|
int i;
|
|
|
|
|
char *current = NULL;
|
|
|
|
|
char *resolved = NULL;
|
2017-04-14 23:37:43 +03:00
|
|
|
|
2017-05-11 19:38:32 +03:00
|
|
|
if(!(current = strdup(path))) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < 16; ++i) {
|
|
|
|
|
if (lstat(current, &sb) == -1) {
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
if((sb.st_mode & S_IFMT) != S_IFLNK) {
|
|
|
|
|
return current;
|
|
|
|
|
}
|
|
|
|
|
if (!(resolved = malloc(sb.st_size + 1))) {
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
r = readlink(current, resolved, sb.st_size);
|
|
|
|
|
if (r == -1 || r > sb.st_size) {
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
resolved[r] = '\0';
|
|
|
|
|
free(current);
|
|
|
|
|
current = strdup(resolved);
|
|
|
|
|
free(resolved);
|
|
|
|
|
resolved = NULL;
|
|
|
|
|
}
|
2017-04-14 23:37:43 +03:00
|
|
|
|
|
|
|
|
failed:
|
2017-05-11 19:38:32 +03:00
|
|
|
free(resolved);
|
|
|
|
|
free(current);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|