2014-10-24 22:58:18 +02:00
|
|
|
/*
|
2021-11-16 11:50:48 +01:00
|
|
|
* neetdraw.c: terminal drawing application with multiplayer support
|
2014-10-24 22:58:18 +02:00
|
|
|
*
|
2023-05-22 05:47:03 +02:00
|
|
|
* Copyright (c) 2014 - 2023, Přemysl Eric Janouch <p@janouch.name>
|
2014-10-24 22:58:18 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2018-06-24 02:58:23 +02:00
|
|
|
* purpose with or without fee is hereby granted.
|
2014-10-24 22:58:18 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2015-02-28 21:33:08 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "liberty/liberty.c"
|
|
|
|
#include "termo.h"
|
2014-10-28 02:39:37 +01:00
|
|
|
|
2015-02-28 21:33:08 +01:00
|
|
|
#include <locale.h>
|
2014-10-28 02:39:37 +01:00
|
|
|
#include <termios.h>
|
|
|
|
#ifndef TIOCGWINSZ
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif // ! TIOCGWINSZ
|
|
|
|
|
|
|
|
#include <ev.h>
|
2014-10-22 23:58:25 +02:00
|
|
|
#include <curses.h>
|
|
|
|
|
2014-10-27 13:54:53 +01:00
|
|
|
#define PALETTE_WIDTH 9 ///< Width of the palette
|
|
|
|
#define TOP_BAR_CUTOFF 3 ///< Height of the top bar
|
2014-10-23 23:23:09 +02:00
|
|
|
|
2014-10-27 13:54:53 +01:00
|
|
|
#define BITMAP_BLOCK_SIZE 50 ///< Step for extending bitmap size
|
|
|
|
|
|
|
|
#define PROTOCOL_VERSION 1 ///< Network protocol version
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MESSAGE_HELLO, ///< Server/client hello
|
|
|
|
MESSAGE_GET_BITMAP, ///< Request bitmap data
|
|
|
|
MESSAGE_PUT_POINT, ///< Request to place a point
|
|
|
|
|
|
|
|
MESSAGE_COUNT ///< Total number of messages
|
|
|
|
};
|
2014-10-23 09:22:17 +02:00
|
|
|
|
2014-10-26 18:57:28 +01:00
|
|
|
enum network_mode
|
|
|
|
{
|
|
|
|
NETWORK_MODE_STANDALONE, ///< No networking taking place
|
|
|
|
NETWORK_MODE_SERVER, ///< We're the server
|
|
|
|
NETWORK_MODE_CLIENT ///< We're a client
|
|
|
|
};
|
|
|
|
|
|
|
|
struct client
|
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
LIST_HEADER (struct client)
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
int fd; ///< Client connection
|
2014-10-28 02:59:39 +01:00
|
|
|
ev_io read_watcher; ///< Client readability watcher
|
|
|
|
ev_io write_watcher; ///< Client writability watcher
|
2014-10-27 13:54:53 +01:00
|
|
|
struct msg_reader msg_reader; ///< Client message reader
|
2016-03-14 21:32:33 +01:00
|
|
|
struct write_queue write_queue; ///< Write queue
|
2014-10-26 18:57:28 +01:00
|
|
|
};
|
|
|
|
|
2014-10-27 16:48:06 +01:00
|
|
|
#define BITMAP_PIXEL(app, x, y) (app)->bitmap[(y) * (app)->bitmap_w + (x)]
|
|
|
|
|
2014-10-23 09:22:17 +02:00
|
|
|
struct app_context
|
2014-10-22 23:58:25 +02:00
|
|
|
{
|
2014-10-23 09:22:17 +02:00
|
|
|
termo_t *tk; ///< Termo instance
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_io tty_watcher; ///< TTY input watcher
|
|
|
|
ev_timer tty_timer; ///< TTY timeout timer
|
|
|
|
ev_signal winch_watcher; ///< SIGWINCH watcher
|
2014-10-24 22:59:24 +02:00
|
|
|
|
2017-06-07 21:42:28 +02:00
|
|
|
enum network_mode mode; ///< Networking mode
|
2014-10-26 18:57:28 +01:00
|
|
|
|
|
|
|
// Client:
|
2014-10-28 02:39:37 +01:00
|
|
|
int server_fd; ///< Server connection
|
2014-10-28 02:59:39 +01:00
|
|
|
ev_io server_read_watcher; ///< Server readability watcher
|
|
|
|
ev_io server_write_watcher; ///< Server writability watcher
|
2014-10-27 13:54:53 +01:00
|
|
|
struct msg_reader msg_reader; ///< Server message reader
|
2016-03-14 21:32:33 +01:00
|
|
|
struct write_queue write_queue; ///< Server write queue
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2015-02-08 02:33:02 +01:00
|
|
|
bool no_wait; ///< Don't wait for server confirmations
|
|
|
|
|
2014-10-26 18:57:28 +01:00
|
|
|
// Server:
|
2014-10-28 02:39:37 +01:00
|
|
|
int listen_fd; ///< Listening FD
|
|
|
|
ev_io listen_watcher; ///< Listening FD watcher
|
2017-06-07 21:42:28 +02:00
|
|
|
struct client *clients; ///< Client connections
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-23 09:22:17 +02:00
|
|
|
chtype palette[2 * 9]; ///< Attribute palette
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-23 23:23:09 +02:00
|
|
|
uint8_t *bitmap; ///< Canvas data for drawing
|
|
|
|
int bitmap_x; ///< X coord. of left top bitmap corner
|
|
|
|
int bitmap_y; ///< Y coord. of left top bitmap corner
|
|
|
|
size_t bitmap_w; ///< Canvas data width
|
|
|
|
size_t bitmap_h; ///< Canvas data height
|
|
|
|
|
|
|
|
int center_x; ///< X coordinate at center
|
|
|
|
int center_y; ///< Y coordinate at center
|
|
|
|
|
|
|
|
// These two are computed from `center_x' and `center_y':
|
|
|
|
|
|
|
|
int corner_x; ///< X coordinate of LT screen corner
|
|
|
|
int corner_y; ///< Y coordinate of LT screen corner
|
|
|
|
|
|
|
|
int move_saved_x; ///< Saved X coord. for moving
|
|
|
|
int move_saved_y; ///< Saved Y coord. for moving
|
|
|
|
|
2014-10-23 09:22:17 +02:00
|
|
|
uint8_t current_color_left; ///< Left mouse button color
|
|
|
|
uint8_t current_color_right; ///< Right mouse button color
|
2014-10-22 23:58:25 +02:00
|
|
|
};
|
|
|
|
|
2017-06-07 21:42:28 +02:00
|
|
|
static void remove_client (struct app_context *app, struct client *client);
|
|
|
|
static void on_server_disconnected (struct app_context *app);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-10-23 09:22:17 +02:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
app_init (struct app_context *self)
|
2014-10-23 09:22:17 +02:00
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
2014-10-28 02:59:39 +01:00
|
|
|
self->server_fd = -1;
|
|
|
|
self->listen_fd = -1;
|
2017-06-22 23:23:57 +02:00
|
|
|
self->msg_reader = msg_reader_make ();
|
|
|
|
self->write_queue = write_queue_make ();
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
|
|
|
|
2014-10-24 22:59:24 +02:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
app_free (struct app_context *self)
|
2014-10-24 22:59:24 +02:00
|
|
|
{
|
|
|
|
if (self->tk)
|
|
|
|
termo_destroy (self->tk);
|
2014-10-26 18:57:28 +01:00
|
|
|
while (self->clients)
|
2014-10-27 13:54:53 +01:00
|
|
|
// XXX: we probably shouldn't do this from here
|
2014-10-26 18:57:28 +01:00
|
|
|
remove_client (self, self->clients);
|
2014-10-24 22:59:24 +02:00
|
|
|
|
|
|
|
free (self->bitmap);
|
2014-10-27 13:54:53 +01:00
|
|
|
msg_reader_free (&self->msg_reader);
|
2014-10-28 02:39:37 +01:00
|
|
|
write_queue_free (&self->write_queue);
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Server-client messaging -------------------------------------------------
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
static bool
|
|
|
|
read_loop (EV_P_ ev_io *watcher,
|
|
|
|
bool (*cb) (EV_P_ ev_io *, const void *, ssize_t))
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2014-10-28 02:39:37 +01:00
|
|
|
char buf[8192];
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
ssize_t n_read = recv (watcher->fd, buf, sizeof buf, 0);
|
|
|
|
if (n_read < 0)
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (n_read <= 0 || !cb (EV_A_ watcher, buf, n_read))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
static bool
|
2016-03-14 21:32:33 +01:00
|
|
|
flush_queue (struct write_queue *queue, ev_io *watcher)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2014-10-28 02:39:37 +01:00
|
|
|
struct iovec vec[queue->len], *vec_iter = vec;
|
2016-03-14 21:32:33 +01:00
|
|
|
for (struct write_req *iter = queue->head; iter; iter = iter->next)
|
2014-10-28 02:39:37 +01:00
|
|
|
*vec_iter++ = iter->data;
|
|
|
|
|
|
|
|
ssize_t written;
|
|
|
|
again:
|
|
|
|
written = writev (watcher->fd, vec, N_ELEMENTS (vec));
|
|
|
|
if (written < 0)
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
goto skip;
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto again;
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
write_queue_processed (queue, written);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
skip:
|
2014-10-28 02:59:39 +01:00
|
|
|
if (write_queue_is_empty (queue))
|
|
|
|
ev_io_stop (EV_DEFAULT_ watcher);
|
|
|
|
else
|
|
|
|
ev_io_start (EV_DEFAULT_ watcher);
|
2014-10-28 02:39:37 +01:00
|
|
|
return true;
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 21:32:33 +01:00
|
|
|
static struct write_req *
|
2014-10-28 02:39:37 +01:00
|
|
|
flush_writer (struct msg_writer *writer)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2016-03-14 21:32:33 +01:00
|
|
|
struct write_req *req = xcalloc (1, sizeof *req);
|
2014-10-28 02:39:37 +01:00
|
|
|
req->data.iov_base = msg_writer_flush (writer, &req->data.iov_len);
|
|
|
|
return req;
|
2014-10-24 22:59:24 +02:00
|
|
|
}
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-27 13:54:53 +01:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
flush_writer_to_client (struct msg_writer *writer, struct client *client)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2014-10-28 02:39:37 +01:00
|
|
|
write_queue_add (&client->write_queue, flush_writer (writer));
|
2014-10-28 02:59:39 +01:00
|
|
|
ev_io_start (EV_DEFAULT_ &client->write_watcher);
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
flush_writer_to_server (struct msg_writer *writer, struct app_context *app)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2014-10-28 02:39:37 +01:00
|
|
|
write_queue_add (&app->write_queue, flush_writer (writer));
|
2014-10-28 02:59:39 +01:00
|
|
|
ev_io_start (EV_DEFAULT_ &app->server_write_watcher);
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
send_draw_point_response (struct client *client, int x, int y, uint8_t color)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2017-06-22 23:23:57 +02:00
|
|
|
struct msg_writer writer = msg_writer_make ();
|
2015-03-29 03:56:25 +02:00
|
|
|
str_pack_u8 (&writer.buf, MESSAGE_PUT_POINT);
|
|
|
|
str_pack_i32 (&writer.buf, x);
|
|
|
|
str_pack_i32 (&writer.buf, y);
|
|
|
|
str_pack_u8 (&writer.buf, color);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
flush_writer_to_client (&writer, client);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
send_draw_point_request (struct app_context *app, int x, int y, uint8_t color)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2017-06-22 23:23:57 +02:00
|
|
|
struct msg_writer writer = msg_writer_make ();
|
2015-03-29 03:56:25 +02:00
|
|
|
str_pack_u8 (&writer.buf, MESSAGE_PUT_POINT);
|
|
|
|
str_pack_i32 (&writer.buf, x);
|
|
|
|
str_pack_i32 (&writer.buf, y);
|
|
|
|
str_pack_u8 (&writer.buf, color);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
flush_writer_to_server (&writer, app);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
send_hello_request (struct app_context *app)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2017-06-22 23:23:57 +02:00
|
|
|
struct msg_writer writer = msg_writer_make ();
|
2015-03-29 03:56:25 +02:00
|
|
|
str_pack_u8 (&writer.buf, MESSAGE_HELLO);
|
|
|
|
str_pack_u8 (&writer.buf, PROTOCOL_VERSION);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
flush_writer_to_server (&writer, app);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
send_hello_response (struct client *client)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2017-06-22 23:23:57 +02:00
|
|
|
struct msg_writer writer = msg_writer_make ();
|
2015-03-29 03:56:25 +02:00
|
|
|
str_pack_u8 (&writer.buf, MESSAGE_HELLO);
|
|
|
|
str_pack_u8 (&writer.buf, PROTOCOL_VERSION);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
flush_writer_to_client (&writer, client);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
send_get_bitmap_request (struct app_context *app)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2017-06-22 23:23:57 +02:00
|
|
|
struct msg_writer writer = msg_writer_make ();
|
2015-03-29 03:56:25 +02:00
|
|
|
str_pack_u8 (&writer.buf, MESSAGE_GET_BITMAP);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
flush_writer_to_server (&writer, app);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
send_get_bitmap_response (struct client *client, struct app_context *app)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2017-06-22 23:23:57 +02:00
|
|
|
struct msg_writer writer = msg_writer_make ();
|
2015-03-29 03:56:25 +02:00
|
|
|
str_pack_u8 (&writer.buf, MESSAGE_GET_BITMAP);
|
|
|
|
str_pack_i32 (&writer.buf, app->bitmap_x);
|
|
|
|
str_pack_i32 (&writer.buf, app->bitmap_y);
|
|
|
|
str_pack_u64 (&writer.buf, app->bitmap_w);
|
|
|
|
str_pack_u64 (&writer.buf, app->bitmap_h);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-10-27 18:49:21 +01:00
|
|
|
// Simple RLE compression
|
|
|
|
size_t size = app->bitmap_w * app->bitmap_h;
|
|
|
|
uint8_t last_value = 0, count = 0;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
uint8_t value = app->bitmap[i];
|
|
|
|
if ((count && value != last_value) || count == 0xFF)
|
|
|
|
{
|
2015-03-29 03:56:25 +02:00
|
|
|
str_pack_u8 (&writer.buf, count);
|
|
|
|
str_pack_u8 (&writer.buf, last_value);
|
2014-10-27 18:49:21 +01:00
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
last_value = value;
|
|
|
|
}
|
|
|
|
if (count)
|
|
|
|
{
|
2015-03-29 03:56:25 +02:00
|
|
|
str_pack_u8 (&writer.buf, count);
|
|
|
|
str_pack_u8 (&writer.buf, last_value);
|
2014-10-27 18:49:21 +01:00
|
|
|
}
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
flush_writer_to_client (&writer, client);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Server-client messaging -------------------------------------------------
|
|
|
|
|
2014-10-22 23:58:25 +02:00
|
|
|
static void
|
|
|
|
display (const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
mvwhline (stdscr, 0, 0, A_REVERSE, COLS);
|
|
|
|
attron (A_REVERSE);
|
|
|
|
|
|
|
|
va_start (ap, format);
|
|
|
|
vw_printw (stdscr, format, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
attroff (A_REVERSE);
|
|
|
|
refresh ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
init_palette (struct app_context *app)
|
2014-10-22 23:58:25 +02:00
|
|
|
{
|
|
|
|
start_color ();
|
|
|
|
|
|
|
|
// Also does init_pair (0, -1, -1);
|
|
|
|
use_default_colors ();
|
|
|
|
// Duplicate it for convenience.
|
|
|
|
init_pair (9, -1, -1);
|
|
|
|
|
|
|
|
// Add the basic 8 colors to the default pair. Once normally, once
|
|
|
|
// inverted to workaround VTE's inability to set a bright background.
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
init_pair (1 + i, COLOR_WHITE, COLOR_BLACK + i);
|
|
|
|
init_pair (10 + i, COLOR_BLACK + i, COLOR_WHITE);
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:22:17 +02:00
|
|
|
// Initialize the palette of characters with attributes
|
|
|
|
for (int i = 0; i < PALETTE_WIDTH; i++)
|
|
|
|
{
|
|
|
|
app->palette[i] = ' ' | COLOR_PAIR (i);
|
|
|
|
app->palette[i + 9] = ' ' | COLOR_PAIR (i + 9) | A_REVERSE | A_BOLD;
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:58:25 +02:00
|
|
|
// This usually creates a solid black or white.
|
2014-10-23 09:22:17 +02:00
|
|
|
app->current_color_left = app->current_color_right = 9;
|
2014-10-22 23:58:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 23:23:09 +02:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
update_canvas_for_screen (struct app_context *app)
|
2014-10-23 23:23:09 +02:00
|
|
|
{
|
|
|
|
app->corner_x = app->center_x - COLS / 2;
|
|
|
|
app->corner_y = app->center_y - (LINES - TOP_BAR_CUTOFF) / 2;
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:58:25 +02:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
redraw (struct app_context *app)
|
2014-10-22 23:58:25 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mvwhline (stdscr, 1, 0, A_REVERSE, COLS);
|
|
|
|
mvwhline (stdscr, 2, 0, A_REVERSE, COLS);
|
|
|
|
|
|
|
|
for (i = 0; i < COLS; i++)
|
|
|
|
{
|
2014-10-23 09:22:17 +02:00
|
|
|
int pair = (float) i / COLS * PALETTE_WIDTH;
|
|
|
|
mvaddch (1, i, app->palette[pair]);
|
|
|
|
mvaddch (2, i, app->palette[pair + PALETTE_WIDTH]);
|
2014-10-22 23:58:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
display ("Choose a color from the palette and draw. "
|
|
|
|
"Press Escape or ^C to quit.");
|
|
|
|
refresh ();
|
|
|
|
}
|
|
|
|
|
2014-10-23 23:23:09 +02:00
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
is_in_bitmap_data (struct app_context *app, int x, int y)
|
2014-10-23 23:23:09 +02:00
|
|
|
{
|
|
|
|
return x >= app->bitmap_x
|
|
|
|
&& y >= app->bitmap_y
|
|
|
|
&& x < app->bitmap_x + (int) app->bitmap_w
|
|
|
|
&& y < app->bitmap_y + (int) app->bitmap_h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
redraw_canvas (struct app_context *app)
|
2014-10-23 23:23:09 +02:00
|
|
|
{
|
|
|
|
int y = app->corner_y;
|
|
|
|
for (int screen_y = TOP_BAR_CUTOFF; screen_y < LINES; screen_y++, y++)
|
|
|
|
{
|
|
|
|
move (screen_y, 0);
|
|
|
|
|
|
|
|
int x = app->corner_x;
|
|
|
|
for (int screen_x = 0; screen_x < COLS; screen_x++, x++)
|
|
|
|
{
|
2014-10-27 16:48:06 +01:00
|
|
|
uint8_t color = 0;
|
|
|
|
if (is_in_bitmap_data (app, x, y))
|
|
|
|
color = BITMAP_PIXEL (app,
|
|
|
|
x - app->bitmap_x, y - app->bitmap_y);
|
2014-10-23 23:23:09 +02:00
|
|
|
|
|
|
|
addch (app->palette[color]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refresh ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
is_visible (struct app_context *app, int x, int y)
|
2014-10-23 23:23:09 +02:00
|
|
|
{
|
|
|
|
return x >= app->corner_x
|
|
|
|
&& y >= app->corner_y
|
|
|
|
&& x < app->corner_x + COLS
|
|
|
|
&& y < app->corner_y + LINES - TOP_BAR_CUTOFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
make_place_for_point (struct app_context *app, int x, int y)
|
2014-10-23 23:23:09 +02:00
|
|
|
{
|
|
|
|
if (is_in_bitmap_data (app, x, y))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Make sure the point has some place to go
|
|
|
|
int new_bitmap_x = app->bitmap_x;
|
|
|
|
int new_bitmap_y = app->bitmap_y;
|
|
|
|
|
|
|
|
while (new_bitmap_x > x)
|
|
|
|
new_bitmap_x -= BITMAP_BLOCK_SIZE;
|
|
|
|
while (new_bitmap_y > y)
|
|
|
|
new_bitmap_y -= BITMAP_BLOCK_SIZE;
|
|
|
|
|
|
|
|
int new_bitmap_w = app->bitmap_w + (app->bitmap_x - new_bitmap_x);
|
|
|
|
int new_bitmap_h = app->bitmap_h + (app->bitmap_y - new_bitmap_y);
|
|
|
|
|
|
|
|
while (new_bitmap_x + new_bitmap_w <= x)
|
|
|
|
new_bitmap_w += BITMAP_BLOCK_SIZE;
|
|
|
|
while (new_bitmap_y + new_bitmap_h <= y)
|
|
|
|
new_bitmap_h += BITMAP_BLOCK_SIZE;
|
|
|
|
|
2014-10-25 00:46:26 +02:00
|
|
|
uint8_t *new_bitmap = xcalloc (new_bitmap_w * new_bitmap_h,
|
2014-10-23 23:23:09 +02:00
|
|
|
sizeof *new_bitmap);
|
|
|
|
if (app->bitmap)
|
|
|
|
{
|
|
|
|
// Copy data, assuming that the area can only get larger
|
|
|
|
for (size_t data_y = 0; data_y < app->bitmap_h; data_y++)
|
|
|
|
memcpy (new_bitmap
|
|
|
|
+ ((data_y + app->bitmap_y - new_bitmap_y) * new_bitmap_w)
|
|
|
|
+ (app->bitmap_x - new_bitmap_x),
|
|
|
|
app->bitmap + (data_y * app->bitmap_w),
|
|
|
|
app->bitmap_w * sizeof *new_bitmap);
|
|
|
|
|
|
|
|
free (app->bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the bitmap with the reallocated version
|
|
|
|
app->bitmap_x = new_bitmap_x;
|
|
|
|
app->bitmap_y = new_bitmap_y;
|
|
|
|
app->bitmap_w = new_bitmap_w;
|
|
|
|
app->bitmap_h = new_bitmap_h;
|
|
|
|
app->bitmap = new_bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
draw_point_internal (struct app_context *app, int x, int y, uint8_t color)
|
2014-10-23 23:23:09 +02:00
|
|
|
{
|
|
|
|
make_place_for_point (app, x, y);
|
2014-10-27 16:48:06 +01:00
|
|
|
BITMAP_PIXEL (app, x - app->bitmap_x, y - app->bitmap_y) = color;
|
2014-10-23 23:23:09 +02:00
|
|
|
|
|
|
|
if (is_visible (app, x, y))
|
|
|
|
{
|
|
|
|
int screen_x = x - app->corner_x;
|
|
|
|
int screen_y = y - app->corner_y + TOP_BAR_CUTOFF;
|
|
|
|
|
|
|
|
move (screen_y, screen_x);
|
|
|
|
addch (app->palette[color]);
|
|
|
|
refresh ();
|
|
|
|
}
|
2014-11-30 17:24:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
draw_point (struct app_context *app, int x, int y, uint8_t color)
|
2014-11-30 17:24:35 +01:00
|
|
|
{
|
|
|
|
if (app->mode == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
|
|
|
send_draw_point_request (app, x, y, color);
|
2015-02-08 02:33:02 +01:00
|
|
|
|
|
|
|
// We don't usually draw anything immediately in client mode,
|
|
|
|
// instead we wait for confirmation from the server
|
|
|
|
if (!app->no_wait)
|
|
|
|
return;
|
2014-11-30 17:24:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
draw_point_internal (app, x, y, color);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-11-30 17:24:35 +01:00
|
|
|
// Broadcast clients about the event
|
2014-10-27 13:54:53 +01:00
|
|
|
if (app->mode == NETWORK_MODE_SERVER)
|
2017-06-07 21:42:28 +02:00
|
|
|
for (struct client *iter = app->clients; iter; iter = iter->next)
|
2014-10-27 13:54:53 +01:00
|
|
|
send_draw_point_response (iter, x, y, color);
|
2014-10-23 23:23:09 +02:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:58:41 +01:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
draw_line (struct app_context *app, int x0, int x1, int y0, int y1,
|
|
|
|
uint8_t color)
|
2014-11-29 23:58:41 +01:00
|
|
|
{
|
|
|
|
// Integer version of Bresenham's line drawing algorithm,
|
|
|
|
// loosely based on code from libcaca because screw math
|
|
|
|
int dx = abs (x1 - x0);
|
|
|
|
int dy = abs (y1 - y0);
|
|
|
|
|
|
|
|
bool steep = dx < dy;
|
|
|
|
if (steep)
|
|
|
|
{
|
|
|
|
// Flip the coordinate system on input
|
|
|
|
int tmp;
|
|
|
|
tmp = x0; x0 = y0; y0 = tmp;
|
|
|
|
tmp = x1; x1 = y1; y1 = tmp;
|
|
|
|
tmp = dx; dx = dy; dy = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int step_x = x0 > x1 ? -1 : 1;
|
|
|
|
int step_y = y0 > y1 ? -1 : 1;
|
|
|
|
|
|
|
|
int dpr = dy * 2;
|
|
|
|
int delta = dpr - dx;
|
|
|
|
int dpru = delta - dx;
|
|
|
|
|
|
|
|
while (dx-- >= 0)
|
|
|
|
{
|
|
|
|
// Unflip the coordinate system on output
|
|
|
|
if (steep)
|
|
|
|
draw_point (app, y0, x0, color);
|
|
|
|
else
|
|
|
|
draw_point (app, x0, y0, color);
|
|
|
|
|
|
|
|
x0 += step_x;
|
|
|
|
if (delta > 0)
|
|
|
|
{
|
|
|
|
y0 += step_y;
|
|
|
|
delta += dpru;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
delta += dpr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 08:27:13 +02:00
|
|
|
// --- Exports -----------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
is_data_row_empty (struct app_context *app, int y)
|
2014-10-24 08:27:13 +02:00
|
|
|
{
|
|
|
|
for (size_t x = 0; x < app->bitmap_w; x++)
|
|
|
|
if (app->bitmap[y * app->bitmap_w + x])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
is_data_column_empty (struct app_context *app, int x)
|
2014-10-24 08:27:13 +02:00
|
|
|
{
|
|
|
|
for (size_t y = 0; y < app->bitmap_h; y++)
|
|
|
|
if (app->bitmap[y * app->bitmap_w + x])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
find_data_bounding_rect (struct app_context *app,
|
2014-10-24 08:27:13 +02:00
|
|
|
size_t *x, size_t *y, size_t *w, size_t *h)
|
|
|
|
{
|
|
|
|
size_t my_x = 0, my_y = 0;
|
|
|
|
size_t my_w = app->bitmap_w, my_h = app->bitmap_h;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i < app->bitmap_h && is_data_row_empty (app, i++))
|
|
|
|
my_y++;
|
|
|
|
|
|
|
|
// Special case: the whole canvas is empty
|
|
|
|
if (my_y == my_h)
|
|
|
|
{
|
|
|
|
my_x = my_w;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = app->bitmap_h;
|
|
|
|
while (i-- && is_data_row_empty (app, i))
|
|
|
|
my_h--;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i < app->bitmap_w && is_data_column_empty (app, i++))
|
|
|
|
my_x++;
|
|
|
|
|
|
|
|
i = app->bitmap_w;
|
|
|
|
while (i-- && is_data_column_empty (app, i))
|
|
|
|
my_w--;
|
|
|
|
|
|
|
|
end:
|
|
|
|
*x = my_x;
|
|
|
|
*y = my_y;
|
|
|
|
*w = my_w - my_x;
|
|
|
|
*h = my_h - my_y;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:03:16 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2018-06-24 02:45:41 +02:00
|
|
|
static const char *g_ansi_table[2 * PALETTE_WIDTH] =
|
|
|
|
{
|
|
|
|
"\033[0m",
|
|
|
|
"\033[0;40m",
|
|
|
|
"\033[0;41m",
|
|
|
|
"\033[0;42m",
|
|
|
|
"\033[0;43m",
|
|
|
|
"\033[0;44m",
|
|
|
|
"\033[0;45m",
|
|
|
|
"\033[0;46m",
|
|
|
|
"\033[0;47m",
|
|
|
|
"\033[0;1;7m",
|
|
|
|
"\033[0;1;7;30m",
|
|
|
|
"\033[0;1;7;31m",
|
|
|
|
"\033[0;1;7;32m",
|
|
|
|
"\033[0;1;7;33m",
|
|
|
|
"\033[0;1;7;34m",
|
|
|
|
"\033[0;1;7;35m",
|
|
|
|
"\033[0;1;7;36m",
|
|
|
|
"\033[0;1;7;37m",
|
|
|
|
};
|
|
|
|
|
2014-10-24 08:27:13 +02:00
|
|
|
static const char *
|
|
|
|
color_to_ansi (uint8_t color)
|
|
|
|
{
|
2018-06-24 02:45:41 +02:00
|
|
|
if (color < N_ELEMENTS (g_ansi_table))
|
|
|
|
return g_ansi_table[color];
|
|
|
|
return NULL;
|
2014-10-24 08:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
export_ansi (struct app_context *app)
|
2014-10-24 08:27:13 +02:00
|
|
|
{
|
|
|
|
FILE *fp = fopen ("export-ansi.asc", "wb");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
display ("Error opening file for writing.");
|
2014-10-27 18:03:16 +01:00
|
|
|
beep ();
|
2014-10-24 08:27:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t x, y, w, h;
|
|
|
|
find_data_bounding_rect (app, &x, &y, &w, &h);
|
|
|
|
|
|
|
|
for (size_t row = 0; row < h; row++)
|
|
|
|
{
|
|
|
|
const char *color = NULL;
|
|
|
|
for (size_t column = 0; column < w; column++)
|
|
|
|
{
|
2014-10-27 16:48:06 +01:00
|
|
|
const char *new_color = color_to_ansi
|
|
|
|
(BITMAP_PIXEL (app, x + column, y + row));
|
2014-10-24 08:27:13 +02:00
|
|
|
if (color != new_color)
|
|
|
|
fputs (new_color, fp);
|
|
|
|
color = new_color;
|
|
|
|
fputc (' ', fp);
|
|
|
|
}
|
2014-10-24 23:01:22 +02:00
|
|
|
|
|
|
|
// We need to reset the attributes
|
|
|
|
fputs (color_to_ansi (0), fp);
|
2014-10-24 08:27:13 +02:00
|
|
|
fputc ('\n', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:03:16 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2014-10-24 08:27:13 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MIRC_NONE = -1,
|
|
|
|
|
|
|
|
MIRC_WHITE = 0,
|
|
|
|
MIRC_BLACK = 1,
|
|
|
|
MIRC_BLUE = 2,
|
|
|
|
MIRC_GREEN = 3,
|
|
|
|
MIRC_L_RED = 4,
|
|
|
|
MIRC_RED = 5,
|
|
|
|
MIRC_PURPLE = 6,
|
|
|
|
MIRC_ORANGE = 7,
|
|
|
|
MIRC_YELLOW = 8,
|
|
|
|
MIRC_L_GREEN = 9,
|
|
|
|
MIRC_CYAN = 10,
|
|
|
|
MIRC_L_CYAN = 11,
|
|
|
|
MIRC_L_BLUE = 12,
|
|
|
|
MIRC_L_PURPLE = 13,
|
|
|
|
MIRC_GRAY = 14,
|
|
|
|
MIRC_L_GRAY = 15,
|
|
|
|
|
|
|
|
MIRC_TRANSPARENT = 99
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
color_to_mirc (uint8_t color)
|
|
|
|
{
|
|
|
|
static const int table[2 * PALETTE_WIDTH] =
|
|
|
|
{
|
|
|
|
// XXX: not sure what to map the default color pair to;
|
|
|
|
// the mIRC code for reverse colours seems to not be well supported
|
|
|
|
MIRC_TRANSPARENT, MIRC_BLACK, MIRC_RED, MIRC_GREEN, MIRC_YELLOW,
|
|
|
|
MIRC_BLUE, MIRC_PURPLE, MIRC_CYAN, MIRC_L_GRAY,
|
|
|
|
MIRC_BLACK, MIRC_GRAY, MIRC_L_RED, MIRC_L_GREEN, MIRC_YELLOW,
|
|
|
|
MIRC_L_BLUE, MIRC_L_PURPLE, MIRC_L_CYAN, MIRC_WHITE
|
|
|
|
};
|
|
|
|
|
2015-02-14 08:50:27 +01:00
|
|
|
if (color >= sizeof table / sizeof table[0])
|
2014-10-24 08:27:13 +02:00
|
|
|
return MIRC_NONE;
|
|
|
|
return table[color];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
export_irc (struct app_context *app)
|
2014-10-24 08:27:13 +02:00
|
|
|
{
|
|
|
|
FILE *fp = fopen ("export-irc.asc", "wb");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
display ("Error opening file for writing.");
|
2014-10-27 18:03:16 +01:00
|
|
|
beep ();
|
2014-10-24 08:27:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t x, y, w, h;
|
|
|
|
find_data_bounding_rect (app, &x, &y, &w, &h);
|
|
|
|
|
2014-11-30 17:25:34 +01:00
|
|
|
// This is tricky and needs to be tested with major IRC clients. Currently
|
|
|
|
// works with: weechat 1.0, xchat 2.8.8, freenode's qwebirc.
|
|
|
|
|
|
|
|
// We cannot use the same non-space character for transparent and opaque
|
|
|
|
// pixels because many clients don't understand the transparent 99 colour
|
|
|
|
// that is needed for the foreground of transparent pixels. Therefore it
|
|
|
|
// is not possible to display correctly using non-monospace fonts.
|
|
|
|
|
2014-10-24 08:27:13 +02:00
|
|
|
for (size_t row = 0; row < h; row++)
|
|
|
|
{
|
2014-11-30 17:25:34 +01:00
|
|
|
// qwebirc is retarded and in some cases it reduces spaces, misaligning
|
|
|
|
// the picture. Appending two spaces after the attribute reset and
|
|
|
|
// rendering opaque pixels as something different from a space seems
|
|
|
|
// to prevent that behaviour.
|
|
|
|
int color = MIRC_TRANSPARENT;
|
|
|
|
fprintf (fp, "\x0f ");
|
|
|
|
|
2014-10-24 08:27:13 +02:00
|
|
|
for (size_t column = 0; column < w; column++)
|
|
|
|
{
|
2014-10-27 16:48:06 +01:00
|
|
|
int new_color = color_to_mirc
|
|
|
|
(BITMAP_PIXEL (app, x + column, y + row));
|
2014-10-24 08:27:13 +02:00
|
|
|
if (color != new_color)
|
2014-11-30 17:25:34 +01:00
|
|
|
{
|
|
|
|
color = new_color;
|
|
|
|
if (color == MIRC_TRANSPARENT)
|
|
|
|
fprintf (fp, "\x0f");
|
|
|
|
else
|
|
|
|
fprintf (fp, "\x03%02d,%02d", color, color);
|
|
|
|
}
|
|
|
|
fputc ("# "[color == MIRC_TRANSPARENT], fp);
|
2014-10-24 08:27:13 +02:00
|
|
|
}
|
|
|
|
fputc ('\n', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:03:16 +01:00
|
|
|
// --- Loading, saving ---------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
2023-05-22 05:47:03 +02:00
|
|
|
load (struct app_context *app, const char *filename)
|
2014-10-27 18:03:16 +01:00
|
|
|
{
|
2015-02-14 08:48:56 +01:00
|
|
|
// Client cannot load at all, the server would have send the new bitmap out
|
|
|
|
if (app->mode != NETWORK_MODE_STANDALONE)
|
2014-10-27 18:03:16 +01:00
|
|
|
{
|
2015-02-14 08:48:56 +01:00
|
|
|
display ("Cannot load bitmaps in networked mode.");
|
2014-10-27 18:03:16 +01:00
|
|
|
beep ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-22 05:47:03 +02:00
|
|
|
FILE *fp = fopen (filename, "rb");
|
2015-02-14 08:48:56 +01:00
|
|
|
if (!fp)
|
2014-10-27 18:03:16 +01:00
|
|
|
{
|
2015-02-14 08:48:56 +01:00
|
|
|
display ("Error opening file for reading.");
|
2014-10-27 18:03:16 +01:00
|
|
|
beep ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some way of loading/saving is better than no way, let's just do our job.
|
|
|
|
// The format neither standardised nor effective but it works for us.
|
|
|
|
// We just eat everything and make sure to not crash.
|
|
|
|
|
|
|
|
int x, y;
|
|
|
|
size_t w, h;
|
|
|
|
if (fscanf (fp, "%d %d %zu %zu", &x, &y, &w, &h) != 4)
|
|
|
|
goto error;
|
|
|
|
|
2015-02-14 08:54:42 +01:00
|
|
|
if (w && h > SIZE_MAX / w)
|
|
|
|
goto error;
|
2014-10-27 18:03:16 +01:00
|
|
|
size_t size = w * h;
|
2015-02-14 08:54:42 +01:00
|
|
|
|
2014-10-27 18:03:16 +01:00
|
|
|
uint8_t *bitmap = calloc (size, sizeof *bitmap);
|
|
|
|
if (!bitmap)
|
|
|
|
goto error;
|
|
|
|
|
2015-02-14 08:55:43 +01:00
|
|
|
int c;
|
2014-10-27 18:03:16 +01:00
|
|
|
uint8_t pixel = 0;
|
|
|
|
bool have_nibble = false;
|
|
|
|
size_t loaded = 0;
|
|
|
|
|
|
|
|
while (loaded < size && (c = fgetc (fp)) != EOF)
|
|
|
|
{
|
|
|
|
static const char digits[] = "0123456789abcdef";
|
|
|
|
const char *value = strchr (digits, c);
|
|
|
|
if (value && c != '\0')
|
|
|
|
{
|
|
|
|
pixel = pixel << 4 | (value - digits);
|
|
|
|
if (have_nibble)
|
|
|
|
bitmap[loaded++] = pixel;
|
|
|
|
have_nibble = !have_nibble;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free (app->bitmap);
|
|
|
|
app->bitmap = bitmap;
|
|
|
|
app->bitmap_h = h; app->bitmap_x = x;
|
|
|
|
app->bitmap_w = w; app->bitmap_y = y;
|
|
|
|
redraw_canvas (app);
|
|
|
|
|
|
|
|
error:
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-05-22 05:47:03 +02:00
|
|
|
save (struct app_context *app, const char *filename)
|
2014-10-27 18:03:16 +01:00
|
|
|
{
|
2023-05-22 05:47:03 +02:00
|
|
|
FILE *fp = fopen (filename, "wb");
|
2014-10-27 18:03:16 +01:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
display ("Error opening file for writing.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = app->bitmap_x, y = app->bitmap_y;
|
|
|
|
size_t w = app->bitmap_w, h = app->bitmap_h;
|
|
|
|
fprintf (fp, "%d %d %zu %zu\n", x, y, w, h);
|
|
|
|
|
|
|
|
for (size_t row = 0; row < h; row++)
|
|
|
|
{
|
|
|
|
for (size_t column = 0; column < w; column++)
|
|
|
|
fprintf (fp, "%02x", BITMAP_PIXEL (app, column, row));
|
|
|
|
fputc ('\n', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
2014-10-26 18:57:28 +01:00
|
|
|
// --- Event handlers ----------------------------------------------------------
|
2014-10-24 08:27:13 +02:00
|
|
|
|
2014-10-27 17:20:46 +01:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
move_canvas (struct app_context *app, int x, int y)
|
2014-10-22 23:58:25 +02:00
|
|
|
{
|
2014-10-27 17:20:46 +01:00
|
|
|
app->corner_x += x;
|
|
|
|
app->corner_y += y;
|
2014-10-24 08:27:13 +02:00
|
|
|
|
2014-10-27 17:20:46 +01:00
|
|
|
app->center_x += x;
|
|
|
|
app->center_y += y;
|
2014-10-24 08:27:13 +02:00
|
|
|
|
2014-10-27 17:20:46 +01:00
|
|
|
redraw_canvas (app);
|
|
|
|
}
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-27 17:20:46 +01:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
on_mouse (struct app_context *app, termo_key_t *key)
|
2014-10-27 17:20:46 +01:00
|
|
|
{
|
2014-10-23 23:23:09 +02:00
|
|
|
int screen_y, screen_x, button;
|
2014-10-22 23:58:25 +02:00
|
|
|
termo_mouse_event_t event;
|
|
|
|
|
2014-10-23 23:23:09 +02:00
|
|
|
termo_interpret_mouse (app->tk, key, &event, &button, &screen_y, &screen_x);
|
2014-10-22 23:58:25 +02:00
|
|
|
if (event != TERMO_MOUSE_PRESS && event != TERMO_MOUSE_DRAG)
|
2014-10-27 17:20:46 +01:00
|
|
|
return;
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-27 13:54:53 +01:00
|
|
|
// Middle mouse button, or Ctrl + left mouse button, moves the canvas
|
|
|
|
if (button == 2 || (button == 1 && key->modifiers == TERMO_KEYMOD_CTRL))
|
2014-10-23 23:23:09 +02:00
|
|
|
{
|
|
|
|
if (event == TERMO_MOUSE_DRAG)
|
2014-10-27 17:20:46 +01:00
|
|
|
move_canvas (app,
|
|
|
|
app->move_saved_x - screen_x,
|
|
|
|
app->move_saved_y - screen_y);
|
2014-10-23 23:23:09 +02:00
|
|
|
|
|
|
|
app->move_saved_x = screen_x;
|
|
|
|
app->move_saved_y = screen_y;
|
2014-10-27 17:20:46 +01:00
|
|
|
return;
|
2014-10-23 23:23:09 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 09:22:17 +02:00
|
|
|
uint8_t *color;
|
2014-10-22 23:58:25 +02:00
|
|
|
if (button == 1)
|
2014-10-23 09:22:17 +02:00
|
|
|
color = &app->current_color_left;
|
2014-10-22 23:58:25 +02:00
|
|
|
else if (button == 3)
|
2014-10-23 09:22:17 +02:00
|
|
|
color = &app->current_color_right;
|
2014-10-22 23:58:25 +02:00
|
|
|
else
|
2014-10-27 17:20:46 +01:00
|
|
|
return;
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-23 23:23:09 +02:00
|
|
|
int canvas_x = app->corner_x + screen_x;
|
|
|
|
int canvas_y = app->corner_y + screen_y - TOP_BAR_CUTOFF;
|
|
|
|
|
|
|
|
if (screen_y >= TOP_BAR_CUTOFF)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2014-11-29 23:58:41 +01:00
|
|
|
if (event == TERMO_MOUSE_DRAG)
|
|
|
|
draw_line (app,
|
|
|
|
app->move_saved_x, canvas_x,
|
|
|
|
app->move_saved_y, canvas_y,
|
|
|
|
*color);
|
2014-10-27 13:54:53 +01:00
|
|
|
else
|
|
|
|
draw_point (app, canvas_x, canvas_y, *color);
|
2014-11-29 23:58:41 +01:00
|
|
|
|
|
|
|
app->move_saved_x = canvas_x;
|
|
|
|
app->move_saved_y = canvas_y;
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
2014-10-23 23:23:09 +02:00
|
|
|
else if (screen_y > 0 && event != TERMO_MOUSE_DRAG)
|
2014-10-23 09:22:17 +02:00
|
|
|
{
|
2014-10-23 23:23:09 +02:00
|
|
|
int pair = (float) screen_x / COLS * PALETTE_WIDTH;
|
|
|
|
*color = pair + (screen_y - 1) * PALETTE_WIDTH;
|
2014-10-23 09:22:17 +02:00
|
|
|
}
|
2014-10-27 17:20:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
on_key (struct app_context *app, termo_key_t *key)
|
2014-10-27 17:20:46 +01:00
|
|
|
{
|
|
|
|
if (key->type == TERMO_TYPE_KEYSYM)
|
|
|
|
{
|
|
|
|
if (key->code.sym == TERMO_SYM_ESCAPE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (key->modifiers)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (key->code.sym)
|
|
|
|
{
|
2014-10-27 18:03:16 +01:00
|
|
|
case TERMO_SYM_UP: move_canvas (app, 0, -1); break;
|
|
|
|
case TERMO_SYM_DOWN: move_canvas (app, 0, 1); break;
|
|
|
|
case TERMO_SYM_LEFT: move_canvas (app, -1, 0); break;
|
|
|
|
case TERMO_SYM_RIGHT: move_canvas (app, 1, 0); break;
|
|
|
|
default: break;
|
2014-10-27 17:20:46 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->type == TERMO_TYPE_KEY)
|
|
|
|
{
|
|
|
|
if ((key->modifiers & TERMO_KEYMOD_CTRL)
|
|
|
|
&& (key->code.codepoint == 'C' || key->code.codepoint == 'c'))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (key->modifiers)
|
|
|
|
return true;
|
|
|
|
|
2023-05-22 05:47:03 +02:00
|
|
|
if (key->code.codepoint == 'l') load (app, "drawing.bin");
|
|
|
|
if (key->code.codepoint == 's') save (app, "drawing.bin");
|
2014-10-27 17:20:46 +01:00
|
|
|
if (key->code.codepoint == 'e') export_ansi (app);
|
|
|
|
if (key->code.codepoint == 'E') export_irc (app);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->type == TERMO_TYPE_MOUSE)
|
|
|
|
on_mouse (app, key);
|
2014-10-22 23:58:25 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-10-28 02:39:37 +01:00
|
|
|
on_winch (EV_P_ ev_signal *handle, int revents)
|
2014-10-22 23:58:25 +02:00
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context *app = ev_userdata (loop);
|
2014-10-28 02:39:37 +01:00
|
|
|
(void) handle;
|
|
|
|
(void) revents;
|
2014-10-24 22:59:24 +02:00
|
|
|
|
2021-10-30 02:02:54 +02:00
|
|
|
#if defined HAVE_RESIZETERM && defined TIOCGWINSZ
|
2014-10-28 02:39:37 +01:00
|
|
|
struct winsize size;
|
|
|
|
if (!ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &size))
|
2014-10-24 22:59:24 +02:00
|
|
|
{
|
|
|
|
char *row = getenv ("LINES");
|
|
|
|
char *col = getenv ("COLUMNS");
|
|
|
|
unsigned long tmp;
|
|
|
|
resizeterm (
|
2014-10-28 02:39:37 +01:00
|
|
|
(row && xstrtoul (&tmp, row, 10)) ? (int) tmp : size.ws_row,
|
|
|
|
(col && xstrtoul (&tmp, col, 10)) ? (int) tmp : size.ws_col);
|
2014-10-24 22:59:24 +02:00
|
|
|
}
|
2015-02-08 02:31:13 +01:00
|
|
|
#else // ! HAVE_RESIZETERM || ! TIOCGWINSZ
|
2014-10-24 22:59:24 +02:00
|
|
|
endwin ();
|
|
|
|
refresh ();
|
2015-02-08 02:31:13 +01:00
|
|
|
#endif // ! HAVE_RESIZETERM || ! TIOCGWINSZ
|
2014-10-24 22:59:24 +02:00
|
|
|
|
|
|
|
update_canvas_for_screen (app);
|
|
|
|
redraw (app);
|
|
|
|
redraw_canvas (app);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-10-28 02:39:37 +01:00
|
|
|
on_key_timer (EV_P_ ev_timer *handle, int revents)
|
2014-10-24 22:59:24 +02:00
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context *app = ev_userdata (loop);
|
2014-10-28 02:39:37 +01:00
|
|
|
(void) handle;
|
|
|
|
(void) revents;
|
2014-10-24 22:59:24 +02:00
|
|
|
|
|
|
|
termo_key_t key;
|
|
|
|
if (termo_getkey_force (app->tk, &key) == TERMO_RES_KEY)
|
|
|
|
if (!on_key (app, &key))
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_break (EV_A_ EVBREAK_ONE);
|
2014-10-24 22:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-10-28 02:39:37 +01:00
|
|
|
on_tty_readable (EV_P_ ev_io *handle, int revents)
|
2014-10-24 22:59:24 +02:00
|
|
|
{
|
|
|
|
// Ignoring and hoping for the best
|
2014-10-28 02:39:37 +01:00
|
|
|
(void) handle;
|
|
|
|
(void) revents;
|
2014-10-24 22:59:24 +02:00
|
|
|
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context *app = ev_userdata (loop);
|
2014-10-24 22:59:24 +02:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_timer_stop (EV_A_ &app->tty_timer);
|
2014-10-24 22:59:24 +02:00
|
|
|
termo_advisereadable (app->tk);
|
|
|
|
|
|
|
|
termo_key_t key;
|
|
|
|
termo_result_t ret;
|
|
|
|
while ((ret = termo_getkey (app->tk, &key)) == TERMO_RES_KEY)
|
|
|
|
if (!on_key (app, &key))
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_break (EV_A_ EVBREAK_ONE);
|
2014-10-24 22:59:24 +02:00
|
|
|
|
|
|
|
if (ret == TERMO_RES_AGAIN)
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_timer_start (EV_A_ &app->tty_timer);
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
|
|
|
|
2014-10-27 13:54:53 +01:00
|
|
|
// --- Client-specific stuff ---------------------------------------------------
|
|
|
|
|
2017-06-07 21:42:28 +02:00
|
|
|
typedef bool (*server_handler_fn) (struct app_context *, struct msg_unpacker *);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-10-26 18:57:28 +01:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
on_server_disconnected (struct app_context *app)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2014-10-28 02:59:39 +01:00
|
|
|
write_queue_free (&app->write_queue);
|
2017-06-22 23:23:57 +02:00
|
|
|
app->write_queue = write_queue_make ();
|
2014-10-28 02:59:39 +01:00
|
|
|
|
|
|
|
ev_io_stop (EV_DEFAULT_ &app->server_read_watcher);
|
|
|
|
ev_io_stop (EV_DEFAULT_ &app->server_write_watcher);
|
2014-10-28 02:39:37 +01:00
|
|
|
xclose (app->server_fd);
|
2014-10-28 02:59:39 +01:00
|
|
|
app->server_fd = -1;
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
display ("Disconnected!");
|
|
|
|
beep (); // Beep beep! Made a boo-boo.
|
|
|
|
|
|
|
|
// Let the user save the picture at least.
|
|
|
|
// Also prevents us from trying to use the dead server handle.
|
|
|
|
app->mode = NETWORK_MODE_STANDALONE;
|
|
|
|
}
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
on_server_hello (struct app_context *app, struct msg_unpacker *unpacker)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
|
|
|
(void) app;
|
|
|
|
|
|
|
|
uint8_t version;
|
|
|
|
if (!msg_unpacker_u8 (unpacker, &version))
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Not enough data
|
2014-10-27 13:54:53 +01:00
|
|
|
if (version != PROTOCOL_VERSION)
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Incompatible version
|
|
|
|
return true;
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
on_server_get_bitmap (struct app_context *app, struct msg_unpacker *unpacker)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
|
|
|
int32_t x, y;
|
|
|
|
uint64_t w, h;
|
|
|
|
if (!msg_unpacker_i32 (unpacker, &x)
|
|
|
|
|| !msg_unpacker_i32 (unpacker, &y)
|
|
|
|
|| !msg_unpacker_u64 (unpacker, &w)
|
|
|
|
|| !msg_unpacker_u64 (unpacker, &h))
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Not enough data
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-10-27 18:49:21 +01:00
|
|
|
size_t size = w * h;
|
2014-10-27 13:54:53 +01:00
|
|
|
if ((h && w > SIZE_MAX / h) || w > SIZE_MAX || h > SIZE_MAX)
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // The server is flooding us
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-10-27 18:49:21 +01:00
|
|
|
uint8_t *bitmap = xcalloc (size, sizeof *app->bitmap);
|
|
|
|
|
|
|
|
// RLE decompression
|
|
|
|
size_t i = 0;
|
|
|
|
uint8_t len, value;
|
|
|
|
while (msg_unpacker_u8 (unpacker, &len)
|
|
|
|
&& msg_unpacker_u8 (unpacker, &value))
|
|
|
|
{
|
|
|
|
// Don't allow overflow
|
|
|
|
if (i + len > size || i + len < i)
|
|
|
|
break;
|
|
|
|
for (size_t x = 0; x < len; x++)
|
|
|
|
bitmap[i++] = value;
|
|
|
|
}
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
free (app->bitmap);
|
2014-10-27 18:49:21 +01:00
|
|
|
app->bitmap = bitmap;
|
2014-10-27 13:54:53 +01:00
|
|
|
app->bitmap_x = x;
|
|
|
|
app->bitmap_y = y;
|
|
|
|
app->bitmap_w = w;
|
|
|
|
app->bitmap_h = h;
|
|
|
|
|
|
|
|
redraw_canvas (app);
|
2014-10-28 02:39:37 +01:00
|
|
|
return true;
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
on_server_put_point (struct app_context *app, struct msg_unpacker *unpacker)
|
2014-10-26 18:57:28 +01:00
|
|
|
{
|
2014-10-27 13:54:53 +01:00
|
|
|
int32_t x, y;
|
|
|
|
uint8_t color;
|
|
|
|
|
|
|
|
if (!msg_unpacker_i32 (unpacker, &x)
|
|
|
|
|| !msg_unpacker_i32 (unpacker, &y)
|
|
|
|
|| !msg_unpacker_u8 (unpacker, &color))
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Not enough data
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-11-30 17:24:35 +01:00
|
|
|
// Either a confirmation of our own request, or an event notification;
|
|
|
|
// let's just put the pixel in place without further ado
|
|
|
|
draw_point_internal (app, x, y, color);
|
2014-10-28 02:39:37 +01:00
|
|
|
return true;
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
static bool
|
|
|
|
on_server_data (EV_P_ ev_io *watcher, const void *buf, ssize_t n_read)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context *app = ev_userdata (loop);
|
2014-10-28 02:39:37 +01:00
|
|
|
(void) watcher;
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
msg_reader_feed (&app->msg_reader, buf, n_read);
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-27 13:54:53 +01:00
|
|
|
static const server_handler_fn handlers[MESSAGE_COUNT] =
|
|
|
|
{
|
|
|
|
[MESSAGE_HELLO] = on_server_hello,
|
|
|
|
[MESSAGE_GET_BITMAP] = on_server_get_bitmap,
|
|
|
|
[MESSAGE_PUT_POINT] = on_server_put_point,
|
|
|
|
};
|
|
|
|
|
|
|
|
void *msg;
|
|
|
|
size_t len;
|
|
|
|
while ((msg = msg_reader_get (&app->msg_reader, &len)))
|
|
|
|
{
|
2017-06-22 23:23:57 +02:00
|
|
|
struct msg_unpacker unpacker = msg_unpacker_make (msg, len);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
uint8_t type;
|
|
|
|
if (!msg_unpacker_u8 (&unpacker, &type)
|
|
|
|
|| type >= MESSAGE_COUNT)
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Unknown message
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
server_handler_fn handler = handlers[type];
|
|
|
|
if (!handler)
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Unknown message
|
|
|
|
if (!handler (app, &unpacker))
|
|
|
|
return false; // Invalid message
|
2014-10-27 13:54:53 +01:00
|
|
|
if (msg_unpacker_get_available (&unpacker) > 0)
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Overlong message
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
2014-10-28 02:39:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_server_ready (EV_P_ ev_io *watcher, int revents)
|
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context *app = ev_userdata (loop);
|
2014-10-28 02:39:37 +01:00
|
|
|
|
|
|
|
if (revents & EV_READ)
|
|
|
|
if (!read_loop (EV_A_ watcher, on_server_data))
|
|
|
|
goto error;
|
|
|
|
if (revents & EV_WRITE)
|
|
|
|
if (!flush_queue (&app->write_queue, watcher))
|
|
|
|
goto error;
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
on_server_disconnected (app);
|
2014-10-27 13:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Server-specific stuff ---------------------------------------------------
|
|
|
|
|
|
|
|
typedef bool (*client_handler_fn)
|
2017-06-07 21:42:28 +02:00
|
|
|
(struct app_context *, struct client *, struct msg_unpacker *);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
remove_client (struct app_context *app, struct client *client)
|
2014-10-27 13:54:53 +01:00
|
|
|
{
|
2014-10-28 02:59:39 +01:00
|
|
|
ev_io_stop (EV_DEFAULT_ &client->read_watcher);
|
|
|
|
ev_io_stop (EV_DEFAULT_ &client->write_watcher);
|
2014-10-28 02:39:37 +01:00
|
|
|
xclose (client->fd);
|
2014-10-27 13:54:53 +01:00
|
|
|
msg_reader_free (&client->msg_reader);
|
2014-10-28 02:39:37 +01:00
|
|
|
write_queue_free (&client->write_queue);
|
2014-10-28 02:59:39 +01:00
|
|
|
LIST_UNLINK (app->clients, client);
|
2014-10-27 13:54:53 +01:00
|
|
|
free (client);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
on_client_hello (struct app_context *app, struct client *client,
|
2014-10-27 13:54:53 +01:00
|
|
|
struct msg_unpacker *unpacker)
|
|
|
|
{
|
|
|
|
(void) app;
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-27 13:54:53 +01:00
|
|
|
uint8_t version;
|
|
|
|
if (!msg_unpacker_u8 (unpacker, &version)
|
|
|
|
|| version != PROTOCOL_VERSION)
|
|
|
|
// Nope, I don't like you
|
|
|
|
return false;
|
|
|
|
|
|
|
|
send_hello_response (client);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
on_client_get_bitmap (struct app_context *app, struct client *client,
|
2014-10-27 13:54:53 +01:00
|
|
|
struct msg_unpacker *unpacker)
|
|
|
|
{
|
|
|
|
(void) unpacker;
|
|
|
|
|
|
|
|
send_get_bitmap_response (client, app);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-06-07 21:42:28 +02:00
|
|
|
on_client_put_point (struct app_context *app, struct client *client,
|
2014-10-27 13:54:53 +01:00
|
|
|
struct msg_unpacker *unpacker)
|
|
|
|
{
|
|
|
|
(void) client;
|
|
|
|
|
|
|
|
int32_t x, y;
|
|
|
|
uint8_t color;
|
|
|
|
if (!msg_unpacker_i32 (unpacker, &x)
|
|
|
|
|| !msg_unpacker_i32 (unpacker, &y)
|
|
|
|
|| !msg_unpacker_u8 (unpacker, &color))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The function takes care of broadcasting to all the other clients,
|
|
|
|
// as well as back to the original sender
|
|
|
|
draw_point (app, x, y, color);
|
|
|
|
return true;
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
static bool
|
|
|
|
on_client_data (EV_P_ ev_io *watcher, const void *buf, ssize_t n_read)
|
2014-10-26 18:57:28 +01:00
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context *app = ev_userdata (loop);
|
|
|
|
struct client *client = watcher->data;
|
2014-10-27 13:54:53 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
msg_reader_feed (&client->msg_reader, buf, n_read);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
static const client_handler_fn handlers[MESSAGE_COUNT] =
|
2014-10-26 18:57:28 +01:00
|
|
|
{
|
2014-10-27 13:54:53 +01:00
|
|
|
[MESSAGE_HELLO] = on_client_hello,
|
|
|
|
[MESSAGE_GET_BITMAP] = on_client_get_bitmap,
|
|
|
|
[MESSAGE_PUT_POINT] = on_client_put_point,
|
|
|
|
};
|
|
|
|
|
|
|
|
void *msg;
|
|
|
|
size_t len;
|
|
|
|
while ((msg = msg_reader_get (&client->msg_reader, &len)))
|
|
|
|
{
|
2017-06-22 23:23:57 +02:00
|
|
|
struct msg_unpacker unpacker = msg_unpacker_make (msg, len);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
uint8_t type;
|
|
|
|
if (!msg_unpacker_u8 (&unpacker, &type))
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Invalid message
|
2014-10-27 13:54:53 +01:00
|
|
|
if (type >= MESSAGE_COUNT)
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Unknown message
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
client_handler_fn handler = handlers[type];
|
|
|
|
if (!handler)
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Unknown message
|
2014-10-27 13:54:53 +01:00
|
|
|
if (!handler (app, client, &unpacker))
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Invalid message
|
2014-10-27 13:54:53 +01:00
|
|
|
if (msg_unpacker_get_available (&unpacker) > 0)
|
2014-10-28 02:39:37 +01:00
|
|
|
return false; // Overlong message data
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
2014-10-28 02:39:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_client_ready (EV_P_ ev_io *watcher, int revents)
|
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context *app = ev_userdata (loop);
|
|
|
|
struct client *client = watcher->data;
|
2014-10-28 02:39:37 +01:00
|
|
|
|
|
|
|
if (revents & EV_READ)
|
|
|
|
if (!read_loop (EV_A_ watcher, on_client_data))
|
|
|
|
goto error;
|
|
|
|
if (revents & EV_WRITE)
|
|
|
|
if (!flush_queue (&client->write_queue, watcher))
|
|
|
|
goto error;
|
2014-10-27 13:54:53 +01:00
|
|
|
return;
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
error:
|
2014-10-27 13:54:53 +01:00
|
|
|
remove_client (app, client);
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-10-28 02:39:37 +01:00
|
|
|
on_new_client (EV_P_ ev_io *watcher, int revents)
|
2014-10-26 18:57:28 +01:00
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context *app = ev_userdata (loop);
|
2014-10-28 02:39:37 +01:00
|
|
|
(void) revents;
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int sock_fd = accept (watcher->fd, NULL, NULL);
|
|
|
|
if (sock_fd == -1)
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
|
|
|
if (errno == EINTR
|
|
|
|
|| errno == ECONNABORTED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Stop accepting connections to prevent busy looping
|
|
|
|
// TODO: indicate the error to the user
|
|
|
|
ev_io_stop (EV_A_ watcher);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2017-06-07 21:42:28 +02:00
|
|
|
struct client *client = xcalloc (1, sizeof *client);
|
2014-10-28 02:39:37 +01:00
|
|
|
client->fd = sock_fd;
|
2017-06-22 23:23:57 +02:00
|
|
|
client->msg_reader = msg_reader_make ();
|
|
|
|
client->write_queue = write_queue_make ();
|
2014-10-28 02:39:37 +01:00
|
|
|
|
|
|
|
set_blocking (sock_fd, false);
|
2014-10-28 02:59:39 +01:00
|
|
|
ev_io_init (&client->read_watcher, on_client_ready, sock_fd, EV_READ);
|
|
|
|
ev_io_init (&client->write_watcher, on_client_ready, sock_fd, EV_WRITE);
|
|
|
|
client->read_watcher.data = client;
|
|
|
|
client->write_watcher.data = client;
|
|
|
|
|
|
|
|
// We're only interested in reading as the write queue is empty now
|
|
|
|
ev_io_start (EV_A_ &client->read_watcher);
|
2014-10-28 02:39:37 +01:00
|
|
|
|
|
|
|
LIST_PREPEND (app->clients, client);
|
|
|
|
}
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Program startup ---------------------------------------------------------
|
|
|
|
|
|
|
|
struct app_options
|
|
|
|
{
|
|
|
|
struct addrinfo *client_address; ///< Address to connect to
|
|
|
|
struct addrinfo *server_address; ///< Address to listen at
|
2015-02-08 02:33:02 +01:00
|
|
|
bool no_wait; ///< Don't wait for server confirmations
|
2023-05-22 05:47:03 +02:00
|
|
|
const char *filename; ///< A filename to preload
|
2014-10-26 18:57:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
app_options_init (struct app_options *self)
|
2014-10-25 00:46:26 +02:00
|
|
|
{
|
2014-10-26 18:57:28 +01:00
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
}
|
2014-10-25 00:46:26 +02:00
|
|
|
|
2014-10-26 18:57:28 +01:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
app_options_free (struct app_options *self)
|
2014-10-26 18:57:28 +01:00
|
|
|
{
|
|
|
|
if (self->client_address) freeaddrinfo (self->client_address);
|
|
|
|
if (self->server_address) freeaddrinfo (self->server_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct addrinfo *
|
|
|
|
parse_address (const char *address, int flags)
|
|
|
|
{
|
|
|
|
char address_copy[strlen (address) + 1];
|
|
|
|
strcpy (address_copy, address);
|
|
|
|
|
|
|
|
char *colon = strrchr (address_copy, ':');
|
|
|
|
if (!colon)
|
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
print_error ("no port number specified in `%s'", address);
|
2014-10-26 18:57:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *host = address_copy, *service = colon + 1;
|
|
|
|
|
|
|
|
if (host == colon)
|
|
|
|
host = NULL;
|
|
|
|
else if (host < colon && *host == '[' && colon[-1] == ']')
|
|
|
|
{
|
|
|
|
// Remove IPv6 RFC 2732-style [] brackets from the host, if present.
|
|
|
|
// This also makes it possible to take the usage string literally. :))
|
|
|
|
host++;
|
|
|
|
colon[-1] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*colon = '\0';
|
|
|
|
|
|
|
|
struct addrinfo *result, hints =
|
|
|
|
{
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_protocol = IPPROTO_TCP,
|
|
|
|
.ai_flags = flags,
|
|
|
|
};
|
|
|
|
int err = getaddrinfo (host, service, &hints, &result);
|
|
|
|
if (err)
|
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
print_error ("cannot resolve `%s', port `%s': %s",
|
2014-10-26 18:57:28 +01:00
|
|
|
host, service, gai_strerror (err));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
parse_program_arguments (struct app_options *options, int argc, char **argv)
|
2014-10-26 18:57:28 +01:00
|
|
|
{
|
2014-10-25 00:46:26 +02:00
|
|
|
static const struct opt opts[] =
|
|
|
|
{
|
|
|
|
{ 'h', "help", NULL, 0, "display this help and exit" },
|
|
|
|
{ 'V', "version", NULL, 0, "output version information and exit" },
|
2014-10-26 18:57:28 +01:00
|
|
|
{ 's', "server", "[ADDRESS]:PORT", 0, "start a server" },
|
|
|
|
{ 'c', "client", "[ADDRESS]:PORT", 0, "connect to a server" },
|
2015-02-08 02:33:02 +01:00
|
|
|
{ 'n', "no-wait", NULL, OPT_LONG_ONLY,
|
|
|
|
"don't wait for server confirmations" },
|
2014-10-25 00:46:26 +02:00
|
|
|
{ 0, NULL, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
2023-05-22 05:47:03 +02:00
|
|
|
struct opt_handler oh = opt_handler_make (argc, argv, opts, "[drawing.bin]",
|
|
|
|
"Terminal drawing application with multiplayer support");
|
2014-10-25 00:46:26 +02:00
|
|
|
|
|
|
|
int c;
|
|
|
|
while ((c = opt_handler_get (&oh)) != -1)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
opt_handler_usage (&oh, stdout);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'V':
|
2015-02-28 21:33:08 +01:00
|
|
|
printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
|
2014-10-25 00:46:26 +02:00
|
|
|
exit (EXIT_SUCCESS);
|
2014-10-26 18:57:28 +01:00
|
|
|
case 's':
|
|
|
|
if (options->server_address)
|
2017-06-07 21:42:28 +02:00
|
|
|
exit_fatal ("cannot specify multiple listening addresses");
|
2014-10-26 18:57:28 +01:00
|
|
|
if (!(options->server_address = parse_address (optarg, AI_PASSIVE)))
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (options->client_address)
|
2017-06-07 21:42:28 +02:00
|
|
|
exit_fatal ("cannot specify multiple addresses to connect to");
|
2014-10-26 18:57:28 +01:00
|
|
|
if (!(options->client_address = parse_address (optarg, 0)))
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
break;
|
2015-02-08 02:33:02 +01:00
|
|
|
case 'n':
|
|
|
|
options->no_wait = true;
|
|
|
|
break;
|
2014-10-25 00:46:26 +02:00
|
|
|
default:
|
2017-06-07 21:42:28 +02:00
|
|
|
print_error ("wrong options");
|
2014-10-25 00:46:26 +02:00
|
|
|
opt_handler_usage (&oh, stderr);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-10-26 18:57:28 +01:00
|
|
|
if (options->client_address && options->server_address)
|
2017-06-07 21:42:28 +02:00
|
|
|
exit_fatal ("cannot be both a server and a client");
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-25 00:46:26 +02:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2023-05-22 05:47:03 +02:00
|
|
|
options->filename = argv[0];
|
|
|
|
if (argc > 1)
|
2014-10-25 00:46:26 +02:00
|
|
|
{
|
|
|
|
opt_handler_usage (&oh, stderr);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
opt_handler_free (&oh);
|
|
|
|
}
|
|
|
|
|
2014-10-26 18:57:28 +01:00
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
initialize_client (struct app_context *app, struct addrinfo *address)
|
2014-10-26 18:57:28 +01:00
|
|
|
{
|
|
|
|
app->mode = NETWORK_MODE_CLIENT;
|
|
|
|
|
|
|
|
int sock_fd, err;
|
|
|
|
for (; address; address = address->ai_next)
|
|
|
|
{
|
|
|
|
sock_fd = socket (address->ai_family,
|
|
|
|
address->ai_socktype, address->ai_protocol);
|
|
|
|
if (sock_fd == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char host_buf[NI_MAXHOST], serv_buf[NI_MAXSERV];
|
|
|
|
err = getnameinfo (address->ai_addr, address->ai_addrlen,
|
|
|
|
host_buf, sizeof host_buf, serv_buf, sizeof serv_buf,
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
if (err)
|
|
|
|
{
|
2017-06-07 21:42:28 +02:00
|
|
|
print_error ("%s: %s", "getnameinfo", gai_strerror (err));
|
|
|
|
print_status ("connecting...");
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *x = format_host_port_pair (host_buf, serv_buf);
|
2017-06-07 21:42:28 +02:00
|
|
|
print_status ("connecting to %s...", x);
|
2014-10-26 18:57:28 +01:00
|
|
|
free (x);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!connect (sock_fd, address->ai_addr, address->ai_addrlen))
|
|
|
|
break;
|
|
|
|
|
|
|
|
xclose (sock_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!address)
|
2017-06-07 21:42:28 +02:00
|
|
|
exit_fatal ("connection failed");
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
int yes = 1;
|
|
|
|
(void) setsockopt (sock_fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof yes);
|
|
|
|
|
2014-10-26 18:57:28 +01:00
|
|
|
set_blocking (sock_fd, false);
|
2014-10-28 02:39:37 +01:00
|
|
|
app->server_fd = sock_fd;
|
2014-10-28 02:59:39 +01:00
|
|
|
ev_io_init (&app->server_read_watcher, on_server_ready, sock_fd, EV_READ);
|
|
|
|
ev_io_init (&app->server_write_watcher, on_server_ready, sock_fd, EV_WRITE);
|
|
|
|
|
|
|
|
// We're only interested in reading as the write queue is empty now
|
|
|
|
ev_io_start (EV_DEFAULT_ &app->server_read_watcher);
|
2014-10-27 13:54:53 +01:00
|
|
|
|
|
|
|
send_hello_request (app);
|
|
|
|
send_get_bitmap_request (app);
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-07 21:42:28 +02:00
|
|
|
initialize_server (struct app_context *app, struct addrinfo *address)
|
2014-10-26 18:57:28 +01:00
|
|
|
{
|
|
|
|
app->mode = NETWORK_MODE_SERVER;
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
int sock_fd = socket (address->ai_family,
|
|
|
|
address->ai_socktype, address->ai_protocol);
|
|
|
|
if (sock_fd == -1)
|
2014-11-19 03:23:05 +01:00
|
|
|
goto fail_socket;
|
2014-10-28 02:39:37 +01:00
|
|
|
|
|
|
|
if (bind (sock_fd, address->ai_addr, address->ai_addrlen)
|
|
|
|
|| listen (sock_fd, 10))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
int yes = 1;
|
|
|
|
(void) setsockopt (sock_fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof yes);
|
|
|
|
(void) setsockopt (sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
|
|
|
|
|
|
|
|
set_blocking (sock_fd, false);
|
|
|
|
app->listen_fd = sock_fd;
|
|
|
|
ev_io_init (&app->listen_watcher, on_new_client, sock_fd, EV_READ);
|
|
|
|
ev_io_start (EV_DEFAULT_ &app->listen_watcher);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2014-11-19 03:23:05 +01:00
|
|
|
xclose (sock_fd);
|
|
|
|
fail_socket:
|
2017-06-07 21:42:28 +02:00
|
|
|
exit_fatal ("%s: %s", "initialization failed", strerror (errno));
|
2014-10-26 18:57:28 +01:00
|
|
|
}
|
|
|
|
|
2014-10-22 23:58:25 +02:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
TERMO_CHECK_VERSION;
|
|
|
|
setlocale (LC_CTYPE, "");
|
|
|
|
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_context app;
|
2014-10-25 00:46:26 +02:00
|
|
|
app_init (&app);
|
2014-10-26 18:57:28 +01:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
struct ev_loop *loop = EV_DEFAULT;
|
|
|
|
if (!loop)
|
2017-06-07 21:42:28 +02:00
|
|
|
exit_fatal ("cannot initialize libev");
|
2014-10-28 02:39:37 +01:00
|
|
|
|
2017-06-07 21:42:28 +02:00
|
|
|
struct app_options options;
|
2014-10-26 18:57:28 +01:00
|
|
|
app_options_init (&options);
|
|
|
|
parse_program_arguments (&options, argc, argv);
|
|
|
|
|
|
|
|
if (options.client_address)
|
|
|
|
initialize_client (&app, options.client_address);
|
|
|
|
else if (options.server_address)
|
|
|
|
initialize_server (&app, options.server_address);
|
|
|
|
else
|
|
|
|
app.mode = NETWORK_MODE_STANDALONE;
|
|
|
|
|
2015-02-08 02:33:02 +01:00
|
|
|
app.no_wait = options.no_wait;
|
2014-10-26 18:57:28 +01:00
|
|
|
app_options_free (&options);
|
2014-10-25 00:46:26 +02:00
|
|
|
|
2014-10-22 23:58:25 +02:00
|
|
|
termo_t *tk = termo_new (STDIN_FILENO, NULL, 0);
|
|
|
|
if (!tk)
|
2017-06-07 21:42:28 +02:00
|
|
|
exit_fatal ("cannot allocate termo instance");
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-25 00:46:26 +02:00
|
|
|
app.tk = tk;
|
2014-10-22 23:58:25 +02:00
|
|
|
termo_set_mouse_tracking_mode (tk, TERMO_MOUSE_TRACKING_DRAG);
|
|
|
|
|
|
|
|
// Set up curses for our drawing needs
|
|
|
|
if (!initscr () || nonl () == ERR || curs_set (0) == ERR)
|
2017-06-07 21:42:28 +02:00
|
|
|
exit_fatal ("cannot initialize curses");
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_set_userdata (loop, &app);
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_signal_init (&app.winch_watcher, on_winch, SIGWINCH);
|
|
|
|
ev_signal_start (EV_DEFAULT_ &app.winch_watcher);
|
|
|
|
ev_io_init (&app.tty_watcher, on_tty_readable, STDIN_FILENO, EV_READ);
|
|
|
|
ev_io_start (EV_DEFAULT_ &app.tty_watcher);
|
|
|
|
ev_timer_init (&app.tty_timer, on_key_timer,
|
|
|
|
termo_get_waittime (app.tk) / 1000., 0);
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2014-10-24 22:59:24 +02:00
|
|
|
init_palette (&app);
|
|
|
|
update_canvas_for_screen (&app);
|
|
|
|
redraw (&app);
|
|
|
|
redraw_canvas (&app);
|
2014-10-22 23:58:25 +02:00
|
|
|
|
2023-05-22 05:47:03 +02:00
|
|
|
if (options.filename)
|
|
|
|
load (&app, options.filename);
|
|
|
|
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_run (loop, 0);
|
2014-10-22 23:58:25 +02:00
|
|
|
endwin ();
|
2014-10-24 22:59:24 +02:00
|
|
|
|
|
|
|
app_free (&app);
|
2014-10-28 02:39:37 +01:00
|
|
|
ev_loop_destroy (loop);
|
2014-10-24 22:59:24 +02:00
|
|
|
return 0;
|
2014-10-22 23:58:25 +02:00
|
|
|
}
|