/* ss-core/ssgui.h * * Interface to ss-gui. * * Copyright (C) 2003, 2004 * Andy Goth * * This code is available under the GNU General Public License; see COPYING. */ #include #include #include #include #include "evlist/evlist.h" #include "prjlibs/vec.h" #include "ss-core/core.h" #include "ss-core/ssgui.h" typedef struct ssgui { core_t* core; } ssgui_t; static int ssgui_cleanup (device_t* dev); static int ssgui_incoming(device_t* dev); int ssgui_init(device_t* dev, core_t* core) { ssgui_t* ssgui = malloc(sizeof(ssgui_t)); if (ssgui == NULL) return -1; ssgui->core = core; dev->sw_state = ssgui ; dev->sw_cleanup = ssgui_cleanup ; dev->sw_incoming = ssgui_incoming; return 0; } int ssgui_send_tag_add(device_t* dev, uint64_t tag, uint32_t type) { return device_sendf(dev, "TAG-ADD %016llx %08x\n", tag, type); } int ssgui_send_tag_del(device_t* dev, uint64_t tag) { return device_sendf(dev, "TAG-DEL %016llx\n", tag); } int ssgui_send_pressure(device_t* dev, int a, int b, int c, int d) { return device_sendf(dev, "PRESSURE %d %d %d %d\n", a, b, c, d); } static int ssgui_cleanup(device_t* dev) { free(dev->sw_state); dev->sw_state = NULL; dev->sw_cleanup = NULL; dev->sw_incoming = NULL; return 0; } static int ssgui_incoming(device_t* dev) { core_t* core = dev->sw_state; char* buf = (char*)VEC_ELTS(byte_t, &dev->buf_in); char* data, *next; again: /* See if we have a complete packet. */ while (dev->buf_cursor < VEC_LEN(byte_t, &dev->buf_in)) { if (buf[dev->buf_cursor] == '\n') goto found; ++dev->buf_cursor; } /* Nope, no more data. */ return 0; found: /* Yes, complete packet. */ data = buf; while (*data != ' ' && *data != '\n') ++data; /* Find space. */ while (*data == ' ' ) ++data; /* Skip spaces. */ /* What kind? */ #if 0 if (strncasecmp(buf, "DOORS", 5) == 0) { int a = -1, b = -1; sscanf(data, "%d%d", &a, &b); if ((a != 0 && a != 1) || (b != 0 && b != 1)) goto bad_packet; core_doors(core, a, b); } else if (strncasecmp(buf, "PRESSURE", 8) == 0) { int a = -1, b = -1, c = -1, d = -1; sscanf(data, "%d%d%d%d", &a, &b, &c, &d); if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255) goto bad_packet; core_pressure(core, a, b, c, d); } else #endif { bad_packet: fprintf(stderr, "Invalid packet received from %s (%s): `%.*s'\n", dev->name, dev->path, dev->buf_cursor, buf); /* Fall through. */ } /* Discard consumed data. */ VEC_SHIFTN(byte_t, &dev->buf_in, dev->buf_cursor + 1); dev->buf_cursor = 0; /* Maybe we have another packet...? */ goto again; } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */