From bcc4f43932bb79389eded0fd50aba3bb08a5b541 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 29 Jan 2015 19:37:22 +0100 Subject: [PATCH] Add SSH terminal colorization --- Makefile | 4 ++-- calmwm.h | 5 ++++ colorize.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ cwmrc.5 | 3 +++ kbfunc.c | 13 +++++++++-- parse.y | 9 +++++++- 6 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 colorize.c diff --git a/Makefile b/Makefile index 4c34ee4..020724f 100644 --- a/Makefile +++ b/Makefile @@ -7,12 +7,12 @@ PREFIX?= /usr/local SRCS= calmwm.c screen.c xmalloc.c client.c menu.c \ search.c util.c xutil.c conf.c xevents.c group.c \ - kbfunc.c mousefunc.c parse.y + kbfunc.c mousefunc.c parse.y colorize.c OBJS= calmwm.o screen.o xmalloc.o client.o menu.o \ search.o util.o xutil.o conf.o xevents.o group.o \ kbfunc.o mousefunc.o strlcpy.o strlcat.o y.tab.o \ - strtonum.o fgetln.o + strtonum.o fgetln.o colorize.o CPPFLAGS+= `pkg-config --cflags fontconfig x11 xft xinerama xrandr` diff --git a/calmwm.h b/calmwm.h index b56a9d7..40f1fb1 100644 --- a/calmwm.h +++ b/calmwm.h @@ -302,6 +302,7 @@ struct conf { struct ignore_q ignoreq; struct cmd_q cmdq; #define CONF_STICKY_GROUPS 0x0001 +#define CONF_COLORIZE_SSH 0x0002 int flags; #define CONF_BWIDTH 1 int bwidth; @@ -583,4 +584,8 @@ int xasprintf(char **, const char *, ...) __attribute__((__format__ (printf, 2, 3))) __attribute__((__nonnull__ (2))); +long crc24(char *); +long tint(long); +long shade(long); + #endif /* _CALMWM_H_ */ diff --git a/colorize.c b/colorize.c new file mode 100644 index 0000000..c60d75b --- /dev/null +++ b/colorize.c @@ -0,0 +1,67 @@ +/* $Id$ */ +/* + * Copyright (c) 2015 Dimitri Sokolyuk + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "calmwm.h" + +#define CRC24_INIT 0x0B704CEL +#define CRC24_POLY 0x1864CFBL + +long +crc24(char *s) +{ + long crc; + int i; + + for (crc = CRC24_INIT; *s; s++) { + crc ^= *s << 0x10; + for (i = 0; i < 8; i++) { + crc <<= 1; + if (crc & 0x1000000) + crc ^= CRC24_POLY; + } + } + + return crc; +} + +long +shade(long c) +{ + unsigned char r = c >> 0x10; + unsigned char g = c >> 0x08; + unsigned char b = c; + + r >>= 2; + g >>= 2; + b >>= 2; + + return (r << 0x10) | (g << 0x8) | b; +} + +long +tint(long c) +{ + unsigned char r = c >> 0x10; + unsigned char g = c >> 0x08; + unsigned char b = c; + + r += (UCHAR_MAX - r) >> 1; + g += (UCHAR_MAX - g) >> 1; + b += (UCHAR_MAX - b) >> 1; + + return (r << 0x10) | (g << 0x8) | b; +} diff --git a/cwmrc.5 b/cwmrc.5 index a0b39d3..b98d3fe 100644 --- a/cwmrc.5 +++ b/cwmrc.5 @@ -131,6 +131,9 @@ Set the color of the border of a window indicating urgency. .It Ic color ungroupborder Ar color Set the color of the border while ungrouping a window. .Pp +.It Ic colorize Ic yes Ns \&| Ns Ic no +Colorize SSH terminals with hashed colors. +.Pp .It Ic command Ar name path Every .Ar name diff --git a/kbfunc.c b/kbfunc.c index 8ad5b99..8af5758 100644 --- a/kbfunc.c +++ b/kbfunc.c @@ -326,6 +326,8 @@ kbfunc_ssh(struct client_ctx *cc, union arg *arg) char *buf, *lbuf, *p; char hostbuf[HOST_NAME_MAX+1]; char path[PATH_MAX]; + long color = 0; + char colorarg[32]; int l; size_t len; @@ -371,8 +373,15 @@ kbfunc_ssh(struct client_ctx *cc, union arg *arg) search_match_exec, NULL)) != NULL) { if (mi->text[0] == '\0') goto out; - l = snprintf(path, sizeof(path), "%s -T '[ssh] %s' -e ssh %s", - cmd->path, mi->text, mi->text); + if (Conf.flags & CONF_COLORIZE_SSH) { + color = crc24(mi->text); + l = snprintf(colorarg, sizeof(colorarg), + " -fg #%.6x -bg #%.6x", tint(color), shade(color)); + if (l == -1 || l >= sizeof(colorarg)) + goto out; + } + l = snprintf(path, sizeof(path), "%s -T '[ssh] %s'%s -e ssh %s", + cmd->path, mi->text, color ? colorarg : "", mi->text); if (l == -1 || l >= sizeof(path)) goto out; u_spawn(path); diff --git a/parse.y b/parse.y index eb8ed64..f19b3b2 100644 --- a/parse.y +++ b/parse.y @@ -70,7 +70,7 @@ typedef struct { %} -%token FONTNAME STICKY GAP MOUSEBIND +%token FONTNAME STICKY COLORIZE GAP MOUSEBIND %token AUTOGROUP BIND COMMAND IGNORE %token YES NO BORDERWIDTH MOVEAMOUNT %token COLOR SNAPDIST @@ -119,6 +119,12 @@ main : FONTNAME STRING { else conf->flags |= CONF_STICKY_GROUPS; } + | COLORIZE yesno { + if ($2 == 0) + conf->flags &= ~CONF_COLORIZE_SSH; + else + conf->flags |= CONF_COLORIZE_SSH; + } | BORDERWIDTH NUMBER { if ($2 < 0 || $2 > UINT_MAX) { yyerror("invalid borderwidth: %lld", $2); @@ -276,6 +282,7 @@ lookup(char *s) { "bind", BIND}, { "borderwidth", BORDERWIDTH}, { "color", COLOR}, + { "colorize", COLORIZE}, { "command", COMMAND}, { "font", FONTCOLOR}, { "fontname", FONTNAME},