tools: pretty colors for the demos

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2020-08-20 12:52:22 +10:00
parent 355093cc1b
commit ae5ef9ce2f
2 changed files with 61 additions and 19 deletions

View file

@ -29,15 +29,37 @@
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdarg.h>
#include "libei.h"
#include "src/util-macros.h"
#include "src/util-mem.h"
#include "src/util-color.h"
#include "src/util-strings.h"
DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei *, ei_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_device *, ei_device_unref);
static inline void
_printf_(1, 2)
colorprint(const char *format, ...)
{
static const char *color = ANSI_BG_RGB(230, 0, 230);
static const char *reset = ansi_colorcode[RESET];
run_only_once {
if (!isatty(STDOUT_FILENO))
color = reset = "";
}
printf("%sEI socket client:%s ", color, reset);
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int main(int argc, char **argv)
{
const char SOCKETNAME[] = "eis-0";
@ -55,7 +77,7 @@ int main(int argc, char **argv)
return 1;
}
printf("client: connected to %s\n", SOCKETNAME);
colorprint("connected to %s\n", SOCKETNAME);
struct pollfd fds = {
.fd = ei_get_fd(ei),
@ -82,28 +104,28 @@ int main(int argc, char **argv)
switch(ei_event_get_type(e)) {
case EI_EVENT_CONNECT:
printf("client: connected\n");
colorprint("connected\n");
ei_device_add(ptr);
ei_device_add(kbd);
break;
case EI_EVENT_DISCONNECT:
{
printf("client: disconnected us\n");
colorprint("disconnected us\n");
stop = true;
break;
}
case EI_EVENT_DEVICE_ADDED:
printf("client: our device was accepted, waiting for resume\n");
colorprint("our device was accepted, waiting for resume\n");
break;
case EI_EVENT_DEVICE_RESUMED:
printf("client: our device was resumed\n");
colorprint("our device was resumed\n");
if (ei_event_get_device(e) == ptr)
have_ptr = true;
if (ei_event_get_device(e) == kbd)
have_kbd = true;
break;
case EI_EVENT_DEVICE_SUSPENDED:
printf("client: our device was suspended\n");
colorprint("our device was suspended\n");
if (ei_event_get_device(e) == ptr)
have_ptr = false;
if (ei_event_get_device(e) == kbd)
@ -111,7 +133,7 @@ int main(int argc, char **argv)
break;
case EI_EVENT_DEVICE_REMOVED:
{
printf("client: our device was removed\n");
colorprint("our device was removed\n");
if (ei_event_get_device(e) == ptr)
have_ptr = true;
if (ei_event_get_device(e) == kbd)
@ -125,16 +147,16 @@ int main(int argc, char **argv)
}
if (have_ptr) {
printf("client: sending motion event\n");
colorprint("sending motion event\n");
ei_device_pointer_motion(ptr, -1, 1);
/* BTN_LEFT */
printf("client: sending button event\n");
colorprint("sending button event\n");
ei_device_pointer_button(ptr, 0x110, true);
ei_device_pointer_button(ptr, 0x110, false);
}
if (have_kbd) {
printf("client: sending key event\n");
colorprint("sending key event\n");
ei_device_keyboard_key(kbd, 57, true); /* KEY_SPACE */
ei_device_keyboard_key(kbd, 57, false); /* KEY_SPACE */
}

View file

@ -32,6 +32,7 @@
#include "libeis.h"
#include "src/util-color.h"
#include "src/util-mem.h"
#include "src/util-strings.h"
@ -51,6 +52,25 @@ static void unlink_free(char **path) {
}
#define _cleanup_unlink_free_ _cleanup_(unlink_free)
static inline void
_printf_(1, 2)
colorprint(const char *format, ...)
{
static const char *color = ANSI_BG_RGB(255, 127, 0);
static const char *reset = ansi_colorcode[RESET];
run_only_once {
if (!isatty(STDOUT_FILENO))
color = reset = "";
}
printf("%sEIS socket server:%s ", color, reset);
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int main(int argc, char **argv)
{
_cleanup_(eis_unrefp) struct eis *eis = eis_new(NULL);
@ -76,7 +96,7 @@ int main(int argc, char **argv)
return 1;
}
printf("server: waiting on %s\n", socketpath);
colorprint("waiting on %s\n", socketpath);
struct pollfd fds = {
.fd = eis_get_fd(eis),
@ -96,23 +116,23 @@ int main(int argc, char **argv)
case EIS_EVENT_CLIENT_CONNECT:
{
struct eis_client *client = eis_event_get_client(e);
printf("server: new client: %s\n", eis_client_get_name(client));
colorprint("new client: %s\n", eis_client_get_name(client));
/* insert sophisticated authentication here */
eis_client_connect(client);
printf("server: accepting client\n");
colorprint("accepting client\n");
break;
}
case EIS_EVENT_CLIENT_DISCONNECT:
{
struct eis_client *client = eis_event_get_client(e);
printf("server: client %s disconnected\n", eis_client_get_name(client));
colorprint("client %s disconnected\n", eis_client_get_name(client));
eis_client_disconnect(client);
break;
}
case EIS_EVENT_DEVICE_ADDED:
{
struct eis_device *device = eis_event_get_device(e);
printf("server: new device, caps:%s%s%s%s\n",
colorprint("new device, caps:%s%s%s%s\n",
eis_device_has_capability(device, EIS_DEVICE_CAP_POINTER) ? " ptr" : "",
eis_device_has_capability(device, EIS_DEVICE_CAP_KEYBOARD) ? " kbd" : "",
eis_device_has_capability(device, EIS_DEVICE_CAP_POINTER_ABSOLUTE) ? " abs" : "",
@ -124,26 +144,26 @@ int main(int argc, char **argv)
}
case EIS_EVENT_DEVICE_REMOVED:
{
printf("server: device removed\n");
colorprint("device removed\n");
break;
}
case EIS_EVENT_POINTER_MOTION:
{
printf("server: motion by %.2f/%.2f\n",
colorprint("motion by %.2f/%.2f\n",
eis_event_pointer_get_dx(e),
eis_event_pointer_get_dy(e));
}
break;
case EIS_EVENT_POINTER_BUTTON:
{
printf("server: button %d (%s)\n",
colorprint("button %d (%s)\n",
eis_event_pointer_get_button(e),
eis_event_pointer_get_button_is_press(e) ? "press" : "release");
}
break;
case EIS_EVENT_KEYBOARD_KEY:
{
printf("server: key %d (%s)\n",
colorprint("key %d (%s)\n",
eis_event_keyboard_get_key(e),
eis_event_keyboard_get_key_is_press(e) ? "press" : "release");
}