Skip to content

Commit

Permalink
Merge pull request #96 from olin/brushmode-mouse-segfault
Browse files Browse the repository at this point in the history
Fix brush mode segfaults
  • Loading branch information
newsch authored Feb 29, 2020
2 parents 6b1b341 + 061551c commit a5d4f34
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
21 changes: 16 additions & 5 deletions src/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ Cursor *cursor_newyx(int y, int x) {
}

/* Make a copy of an existing cursor.
*
*/
Cursor *cursor_copy(Cursor *original) {
return cursor_newyx(original->y, original->x);
}

void cursor_free(Cursor *cursor) {
free(cursor);
}

/* Check if a cursor is contained in the view.
*/
bool cursor_isin_view(Cursor *cursor, View *view) {
return view_isin(view, cursor->y, cursor->x);
}

void cursor_move_up(Cursor *cursor, View *view) {
if (cursor->y == 0) {
view_move_up(view);
Expand Down Expand Up @@ -79,6 +88,12 @@ int cursor_y_to_canvas(Cursor *cursor) {
return cursor->y + 1;
}

/* Attempt to move the cursor given an arrow key input.
*
* If the move would push the cursor beyond the current view (the cursor is
* at an edge of the screen) the view will be shifted if there is more canvas,
* otherwise it is a no-op.
*/
void cursor_key_to_move(int arrow, Cursor *cursor, View *view) {
switch (arrow) {
case KEY_LEFT:
Expand Down Expand Up @@ -109,7 +124,3 @@ int cursor_opposite_dir(int arrow) {
}
return -1;
}

void cursor_free(Cursor *cursor) {
free(cursor);
}
2 changes: 2 additions & 0 deletions src/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Cursor *cursor_newyx(int y, int x);
Cursor *cursor_copy(Cursor *original);
void cursor_free(Cursor *cursor);

bool cursor_isin_view(Cursor *cursor, View *view);

void cursor_move_up(Cursor *cursor, View *view);
void cursor_move_down(Cursor *cursor, View *view);
void cursor_move_left(Cursor *cursor, View *view);
Expand Down
17 changes: 12 additions & 5 deletions src/fe_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ typedef struct {
} mode_brush_config_t;

mode_brush_config_t mode_brush_config = {
.pattern = 'B', .state = PAINT_OFF,
.pattern = 'B',
.state = PAINT_OFF,
};

typedef struct { Cursor *last_dir_change; } mode_insert_config_t;
typedef struct {
Cursor *last_dir_change;
} mode_insert_config_t;

mode_insert_config_t mode_insert_config = {NULL};

Expand Down Expand Up @@ -497,10 +500,14 @@ int mode_brush(reason_t reason, State *state) {
} else if (state->mevent_in->bstate & BUTTON1_RELEASED) {
mode_cfg->state = PAINT_OFF;
}
// only update cursor position on mouse move if we're painting
// only update cursor position on mouse move if we're painting and in view
if (mode_cfg->state == PAINT_ON) {
state->cursor->x = state->mevent_in->x - 1;
state->cursor->y = state->mevent_in->y - 1;
const int x = state->mevent_in->x - 1;
const int y = state->mevent_in->y - 1;
if (view_isin(state->view, y, x)) {
state->cursor->x = x;
state->cursor->y = y;
}
}
} else if (reason == NEW_KEY) {
if ((state->ch_in == KEY_LEFT) || (state->ch_in == KEY_RIGHT) ||
Expand Down
5 changes: 4 additions & 1 deletion src/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ int main(int argc, char *argv[]) {
mmask |= REPORT_MOUSE_POSITION;
#endif
mmask_t return_mask = mousemask(mmask, NULL);
logd("Returned mouse mask: %li\n", return_mask);
logd("Returned mouse mask: %i\n", return_mask);
// get mouse updates faster at the expense of not registering "clicks"
mouseinterval(0);
#ifdef ENABLE_MOUSE_MOVEMENT
Expand Down Expand Up @@ -487,6 +487,9 @@ void setup_colors() {
/* Update canvas with character at cursor current position.
*
* Changes the canvas and updates the ncurses `canvas_win` with the change.
*
* This will assert the cursor is within canvas bounds in DEBUG mode ONLY, so
* bounds checking should be done before calling this.
*/
void front_setcharcursor(char ch) {
canvas_scharyx(view->canvas, cursor->y + view->y, cursor->x + view->x, ch);
Expand Down
8 changes: 8 additions & 0 deletions src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ void view_pan_ch(int arrow, View *view) {
void view_free(View *view) {
free(view);
}

/* Check if a coordinate is inside of a view.
*/
bool view_isin(View *view, int y, int x) {
return ((x < view_max_x) && (x >= 0) && // horizontal
(y < view_max_y) && (y >= 0) // vertical
);
}
1 change: 1 addition & 0 deletions src/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ void view_move_right(View *view);

void view_pan_ch(int ch, View *view);

bool view_isin(View *view, int y, int x);
#endif

0 comments on commit a5d4f34

Please sign in to comment.