sway/common/util.c

78 lines
1.8 KiB
C
Raw Normal View History

2018-11-17 17:11:28 +01:00
#define _POSIX_C_SOURCE 200809L
2018-11-17 20:31:33 +01:00
#include <float.h>
2016-01-24 02:59:58 +01:00
#include <math.h>
#include <stdlib.h>
2016-09-01 14:18:37 +02:00
#include <string.h>
2017-03-11 05:41:24 +01:00
#include <strings.h>
#include <wayland-server-protocol.h>
2016-09-01 14:18:37 +02:00
#include "log.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
uint32_t parse_color(const char *color) {
if (color[0] == '#') {
++color;
}
int len = strlen(color);
if (len != 6 && len != 8) {
sway_log(SWAY_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color);
return 0xFFFFFFFF;
}
uint32_t res = (uint32_t)strtoul(color, NULL, 16);
if (strlen(color) == 6) {
res = (res << 8) | 0xFF;
}
return res;
}
2017-04-14 22:37:43 +02:00
2018-07-23 22:22:09 +02:00
bool parse_boolean(const char *boolean, bool current) {
if (strcasecmp(boolean, "1") == 0
|| strcasecmp(boolean, "yes") == 0
|| strcasecmp(boolean, "on") == 0
|| strcasecmp(boolean, "true") == 0
|| strcasecmp(boolean, "enable") == 0
|| strcasecmp(boolean, "enabled") == 0
|| strcasecmp(boolean, "active") == 0) {
return true;
} else if (strcasecmp(boolean, "toggle") == 0) {
return !current;
}
// All other values are false to match i3
return false;
}
2018-11-17 20:31:33 +01:00
float parse_float(const char *value) {
errno = 0;
char *end;
float flt = strtof(value, &end);
if (*end || errno) {
sway_log(SWAY_DEBUG, "Invalid float value '%s', defaulting to NAN", value);
2018-11-17 20:31:33 +01:00
return NAN;
}
return flt;
}
const char *sway_wl_output_subpixel_to_string(enum wl_output_subpixel subpixel) {
switch (subpixel) {
case WL_OUTPUT_SUBPIXEL_UNKNOWN:
return "unknown";
case WL_OUTPUT_SUBPIXEL_NONE:
return "none";
case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
return "rgb";
case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
return "bgr";
case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
return "vrgb";
case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
return "vbgr";
}
sway_assert(false, "Unknown value for wl_output_subpixel.");
return NULL;
}