+static struct {
+ char text[16];
+ int idle;
+} user_msgs[16];
+
+static int handle_socket(int sock)
+{
+ char buf[256], *p;
+ int i, ret;
+
+ ret = recv(sock, buf, sizeof(buf) - 1, 0);
+ if (ret < 0) {
+ perror("recv");
+ return ret;
+ }
+ if (ret == 0)
+ return 0;
+
+ buf[ret] = 0;
+ p = strchr(buf, ':');
+ if (p != NULL) {
+ for (i = 0; i < ARRAY_SIZE(user_msgs); i++) {
+ if (user_msgs[i].text[0] == 0)
+ break;
+ if (!strncmp(user_msgs[i].text, buf, p - buf + 1))
+ break;
+ }
+ if (i == ARRAY_SIZE(user_msgs)) {
+ printf("out of user_msg slots\n");
+ return 0;
+ }
+ memcpy(user_msgs[i].text, buf, sizeof(user_msgs[i].text) - 1);
+ user_msgs[i].text[sizeof(user_msgs[i].text) - 1] = 0;
+ user_msgs[i].idle = 0;
+ }
+ else if (!strcmp(buf, "poke")) {
+ // if hidden, show up
+ // if visible, exit
+ if (g_hide) {
+ g_hide = 0;
+ return 1;
+ }
+ g_exit = 1;
+ return 1;
+ }
+ else if (!strcmp(buf, "hide")) {
+ g_hide = 1;
+ return 1;
+ }
+ else if (!strcmp(buf, "show")) {
+ g_hide = 0;
+ return 1;
+ }
+ else if (!strcmp(buf, "quit")) {
+ g_exit = 1;
+ return 1;
+ }
+ else {
+ printf("unknown command: '%s'\n", buf);
+ }
+
+ return 0;
+}
+
+static void default_config(void)
+{
+ g_layer.x = SCREEN_WIDTH - WIDTH;
+ g_layer.y = 0;
+ g_layer.w = WIDTH;
+ g_layer.h = HEIGHT;
+}
+
+static void handle_config(void)
+{
+ char buf[256], *p;
+ int x, y, v;
+ FILE *f;
+
+ default_config();
+
+ f = fopen("config.cfg", "r");
+ if (f == NULL)
+ return;
+
+ while ((p = fgets(buf, sizeof(buf), f))) {
+ while (isspace(*p))
+ p++;
+ if (*p == '#' || *p == ';' || *p == 0)
+ continue;
+
+ if (sscanf(p, "position = %d %d", &x, &y) == 2) {
+ g_layer.x = x;
+ g_layer.y = y;
+ }
+ else {
+ printf("unhandled config entry: '%s'\n", p);
+ }
+ }
+ fclose(f);
+
+ v = SCREEN_WIDTH - g_layer.x;
+ if (v <= 0) {
+ printf("bad x position in config: %d\n", g_layer.x);
+ default_config();
+ return;
+ }
+ if (v < WIDTH)
+ g_layer.w = v;
+
+ v = SCREEN_HEIGHT - g_layer.y;
+ if (v <= 0) {
+ printf("bad y position in config: %d\n", g_layer.y);
+ default_config();
+ return;
+ }
+ if (v < HEIGHT)
+ g_layer.h = v;
+}
+
+#define s_printf(x, y, fmt, ...) do { \
+ if ((y) <= g_layer.h - 8) \
+ basic_text_out16(g_screen, g_layer.w, x, y, fmt, ##__VA_ARGS__); \
+} while (0)