bspwm/helpers.c

63 lines
1.5 KiB
C
Raw Normal View History

2012-07-30 12:21:22 +02:00
#include <stdlib.h>
#include <string.h>
2012-08-19 21:45:49 +02:00
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
2012-08-20 22:38:29 +02:00
#include "bspwm.h"
2012-10-03 20:06:10 +03:00
#include "helpers.h"
2012-07-30 12:21:22 +02:00
void warn(char *fmt, ...)
{
2012-10-03 20:06:10 +03:00
va_list ap;
va_start(ap, fmt);
2012-10-06 11:55:22 +02:00
vfprintf(stderr, fmt, ap);
2012-10-03 20:06:10 +03:00
va_end(ap);
}
__attribute__((noreturn))
2012-10-06 11:55:22 +02:00
void err(char *fmt, ...)
{
2012-07-30 12:21:22 +02:00
va_list ap;
2012-10-03 20:06:10 +03:00
va_start(ap, fmt);
2012-10-06 11:55:22 +02:00
vfprintf(stderr, fmt, ap);
2012-07-30 12:21:22 +02:00
va_end(ap);
exit(EXIT_FAILURE);
}
2012-08-19 21:45:49 +02:00
2012-08-20 22:38:29 +02:00
uint32_t get_color(char *col)
{
xcb_colormap_t map = screen->default_colormap;
2012-09-14 11:30:41 +02:00
uint32_t pxl = 0;
2012-08-20 22:38:29 +02:00
if (col[0] == '#') {
2012-09-14 11:30:41 +02:00
unsigned int red, green, blue;
if (sscanf(col + 1, "%02x%02x%02x", &red, &green, &blue) == 3) {
/* 2**16 - 1 == 0xffff and 0x101 * 0xij == 0xijij */
red *= 0x101;
green *= 0x101;
blue *= 0x101;
xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(dpy, xcb_alloc_color(dpy, map, red, green, blue), NULL);
if (reply != NULL) {
pxl = reply->pixel;
free(reply);
}
}
} else {
2012-09-14 11:30:41 +02:00
xcb_alloc_named_color_reply_t *reply = xcb_alloc_named_color_reply(dpy, xcb_alloc_named_color(dpy, map, strlen(col), col), NULL);
if (reply != NULL) {
pxl = reply->pixel;
free(reply);
}
}
2012-08-20 22:38:29 +02:00
return pxl;
}
2012-10-23 15:30:30 +02:00
void get_pointer_position(xcb_point_t *pos)
{
xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, screen->root), NULL);
if (qpr != NULL) {
*pos = (xcb_point_t) {qpr->root_x, qpr->root_y};
free(qpr);
}
}