Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ frontend.out: LDLIBS +=-lncurses -lm
frontend.out: cursor.o fe_modes.o canvas.o view.o network.o lib/argtable3.o

server.out: LDLIBS +=-lpthread
server.out: canvas.o
server.out: canvas.o network.o

## PATTERNS

Expand Down
70 changes: 59 additions & 11 deletions src/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ FILE *logfile = NULL;
#define VERSION "unknown"
#endif

// colors of collaborator cursors, drawn in order based on uid
// see `setup_colors` and `draw_collab_cursors`
const short cursor_colors[] = {
COLOR_CYAN, COLOR_YELLOW, COLOR_MAGENTA, COLOR_GREEN, COLOR_RED, COLOR_BLUE,
};

// cli pieces
const char *program_name = "collascii";
const char *program_version = VERSION;
Expand Down Expand Up @@ -307,6 +313,7 @@ void init_state(State *state, const arguments_t *const arguments) {
.view = view,
.last_cursor = cursor_newyx(arguments->y, arguments->x),
.filepath = arguments->filename,
.collab_list = collab_list_create(NUM_COLLAB),
};
*state = new_state;
}
Expand Down Expand Up @@ -442,15 +449,18 @@ int main(int argc, char *argv[]) {
fd == net_cfg->sockfd) { // Accept data from open socket
logd("recv network\n");
// If server disconnects
if (net_handler(view) != 0) {
if (net_handler(state) != 0) {
networked = false;
print_msg_win("Server Disconnect!");
};
redraw_canvas_win(); // TODO: draw single char update
draw_collab_cursors(state->collab_list);
refresh_screen();
} else if (fd == 0) { // process keyboard activity
master_handler(state, canvas_win, status_interface->info_win);
draw_collab_cursors(state->collab_list);
refresh_screen();
net_update_pos(state);
}
}
}
Expand All @@ -470,18 +480,22 @@ int main(int argc, char *argv[]) {
finish(0);
}

/* Initialize ncurses color configurations
*
* Before using color features elsewhere make sure to check has_colors() first.
*/
void setup_colors() {
start_color();

// TODO: Use #define to get colors for standard uses
// Assign color codes
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
init_pair(6, COLOR_YELLOW, COLOR_BLACK);
init_pair(7, COLOR_BLACK, COLOR_WHITE);
// Use the terminal's prefered color scheme if it supports it
// -1 can be used to refer to the prefered background/foreground
use_default_colors();

// initialize cursor colors - prefered foreground with colored background
// start at index 1 (0 is already default colors)
const int start = 1;
for (int i = 0; i < sizeof(cursor_colors) / sizeof(short); i++) {
init_pair(i + start, -1, cursor_colors[i]);
}
}

/* Update canvas with character at cursor current position.
Expand Down Expand Up @@ -531,6 +545,40 @@ void redraw_canvas_win() {
}
}

/* Draw all visible collaborator cursors on the canvas.
*
* Collaborator cursor colors are from `cursors_colors` and set by uid.
*/
void draw_collab_cursors(collab_list_t *collab_list) {
collab_t *c = NULL;
// calculate visible bounds (in canvas coordinates)
const int min_x = view->x;
const int min_y = view->y;
const int max_x = min(view->canvas->num_cols, view->x + view_max_x) - 1;
const int max_y = min(view->canvas->num_rows, view->y + view_max_y) - 1;
for (int i = 0; i < collab_list->len; i++) {
c = collab_list->list[i];
// only draw cursors that exist and are visible on the screen
if (c != NULL && (c->x >= min_x && c->x <= max_x) &&
(c->y >= min_y && c->y <= max_y)) {
logd("Drawing collab %i\n", c->uid);

// pick color based on uid, which starts at 1
// if color isn't supported, use normal terminal colors and reverse video
const int uid_start = 0;
const int color_start = 1;
const size_t colors_len = sizeof(cursor_colors) / sizeof(short);
const int color =
has_colors() ? ((c->uid - uid_start) % colors_len) + color_start : 0;
const int attr = has_colors() ? 0 : A_REVERSE;
// TODO: blink cursor with A_BLINK attribute (needs to pause between
// updates/only move on changes?)
mvwchgat(canvas_win, c->y - view->y + 1, c->x - view->x + 1, 1, attr,
color, NULL);
}
}
}

void refresh_screen() {
update_screen_size();
wmove(canvas_win, cursor_y_to_canvas(cursor), cursor_x_to_canvas(cursor));
Expand Down
2 changes: 2 additions & 0 deletions src/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ncurses.h>
#include "cursor.h"
#include "mode_id.h"
#include "state.h"
#include "view.h"

#define KEY_TAB '\t'
Expand Down Expand Up @@ -34,4 +35,5 @@ void highlight_mode_text(int x, int num_ch);
int print_mode_win(char *format, ...);
void update_info_win(const Mode_ID current_mode, const int x, const int y,
const int w, const int h);
void draw_collab_cursors(collab_list_t *collab_list);
#endif
Loading