From a6160d52c6fc63cab8a8f7b7c5e8563713ac0964 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 14 Jan 2015 23:54:46 +0000 Subject: [PATCH 1/4] Pass value rather than address of ch during main loop --- src/profanity.c | 2 +- src/ui/core.c | 42 +++++++++++++++++++++--------------------- src/ui/ui.h | 2 +- tests/ui/stub_ui.c | 2 +- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/profanity.c b/src/profanity.c index b28eae20..3b43f797 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -118,7 +118,7 @@ prof_run(const int disable_tls, char *log_level, char *account_name) ch = ui_get_char(inp, &size, &result); - ui_handle_special_keys(&ch, result); + ui_handle_special_keys(ch, result); #ifdef HAVE_LIBOTR otr_poll(); #endif diff --git a/src/ui/core.c b/src/ui/core.c index e0f46f6e..15dddbb7 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -80,8 +80,8 @@ static Display *display; static GTimer *ui_idle_time; -static void _win_handle_switch(const wint_t * const ch); -static void _win_handle_page(const wint_t * const ch, const int result); +static void _win_handle_switch(const wint_t ch); +static void _win_handle_page(const wint_t ch, const int result); static void _win_show_history(int win_index, const char * const contact); static void _ui_draw_term_title(void); @@ -702,11 +702,11 @@ ui_disconnected(void) } void -ui_handle_special_keys(const wint_t * const ch, const int result) +ui_handle_special_keys(const wint_t ch, const int result) { _win_handle_switch(ch); _win_handle_page(ch, result); - if (*ch == KEY_RESIZE) { + if (ch == KEY_RESIZE) { ui_resize(); } } @@ -2935,33 +2935,33 @@ ui_hide_roster(void) } static void -_win_handle_switch(const wint_t * const ch) +_win_handle_switch(const wint_t ch) { - if (*ch == KEY_F(1)) { + if (ch == KEY_F(1)) { ui_switch_win(1); - } else if (*ch == KEY_F(2)) { + } else if (ch == KEY_F(2)) { ui_switch_win(2); - } else if (*ch == KEY_F(3)) { + } else if (ch == KEY_F(3)) { ui_switch_win(3); - } else if (*ch == KEY_F(4)) { + } else if (ch == KEY_F(4)) { ui_switch_win(4); - } else if (*ch == KEY_F(5)) { + } else if (ch == KEY_F(5)) { ui_switch_win(5); - } else if (*ch == KEY_F(6)) { + } else if (ch == KEY_F(6)) { ui_switch_win(6); - } else if (*ch == KEY_F(7)) { + } else if (ch == KEY_F(7)) { ui_switch_win(7); - } else if (*ch == KEY_F(8)) { + } else if (ch == KEY_F(8)) { ui_switch_win(8); - } else if (*ch == KEY_F(9)) { + } else if (ch == KEY_F(9)) { ui_switch_win(9); - } else if (*ch == KEY_F(10)) { + } else if (ch == KEY_F(10)) { ui_switch_win(0); } } static void -_win_handle_page(const wint_t * const ch, const int result) +_win_handle_page(const wint_t ch, const int result) { ProfWin *current = wins_get_current(); int rows = getmaxy(stdscr); @@ -2973,7 +2973,7 @@ _win_handle_page(const wint_t * const ch, const int result) if (prefs_get_boolean(PREF_MOUSE)) { MEVENT mouse_event; - if (*ch == KEY_MOUSE) { + if (ch == KEY_MOUSE) { if (getmouse(&mouse_event) == OK) { #ifdef PLATFORM_CYGWIN @@ -3008,7 +3008,7 @@ _win_handle_page(const wint_t * const ch, const int result) } // page up - if (*ch == KEY_PPAGE) { + if (ch == KEY_PPAGE) { *page_start -= page_space; // went past beginning, show first page @@ -3019,7 +3019,7 @@ _win_handle_page(const wint_t * const ch, const int result) win_update_virtual(current); // page down - } else if (*ch == KEY_NPAGE) { + } else if (ch == KEY_NPAGE) { *page_start += page_space; // only got half a screen, show full screen @@ -3045,7 +3045,7 @@ _win_handle_page(const wint_t * const ch, const int result) int *sub_y_pos = &(split_layout->sub_y_pos); // alt up arrow - if ((result == KEY_CODE_YES) && ((*ch == 565) || (*ch == 337))) { + if ((result == KEY_CODE_YES) && ((ch == 565) || (ch == 337))) { *sub_y_pos -= page_space; // went past beginning, show first page @@ -3055,7 +3055,7 @@ _win_handle_page(const wint_t * const ch, const int result) win_update_virtual(current); // alt down arrow - } else if ((result == KEY_CODE_YES) && ((*ch == 524) || (*ch == 336))) { + } else if ((result == KEY_CODE_YES) && ((ch == 524) || (ch == 336))) { *sub_y_pos += page_space; // only got half a screen, show full screen diff --git a/src/ui/ui.h b/src/ui/ui.h index e28914ff..58ce8fb5 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -61,7 +61,7 @@ void ui_close(void); void ui_redraw(void); void ui_resize(void); GSList* ui_get_chat_recipients(void); -void ui_handle_special_keys(const wint_t * const ch, const int result); +void ui_handle_special_keys(const wint_t ch, const int result); gboolean ui_switch_win(const int i); void ui_next_win(void); void ui_previous_win(void); diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c index 76b71265..da75c3dd 100644 --- a/tests/ui/stub_ui.c +++ b/tests/ui/stub_ui.c @@ -64,7 +64,7 @@ GSList* ui_get_chat_recipients(void) return NULL; } -void ui_handle_special_keys(const wint_t * const ch, const int result) {} +void ui_handle_special_keys(const wint_t ch, const int result) {} gboolean ui_switch_win(const int i) { From 3984c660b6c46533de5b03fd4ae583b68778bff7 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 14 Jan 2015 23:57:45 +0000 Subject: [PATCH 2/4] Removed result from main loop --- src/profanity.c | 5 +---- src/ui/core.c | 9 ++++++--- src/ui/ui.h | 2 +- tests/ui/stub_ui.c | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/profanity.c b/src/profanity.c index 3b43f797..9924fb91 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -99,7 +99,6 @@ prof_run(const int disable_tls, char *log_level, char *account_name) while(cmd_result == TRUE) { wint_t ch = ERR; - int result; size = 0; while(ch != '\n') { @@ -116,9 +115,7 @@ prof_run(const int disable_tls, char *log_level, char *account_name) g_timer_start(timer); } - ch = ui_get_char(inp, &size, &result); - - ui_handle_special_keys(ch, result); + ch = ui_get_char(inp, &size); #ifdef HAVE_LIBOTR otr_poll(); #endif diff --git a/src/ui/core.c b/src/ui/core.c index 15dddbb7..855c600c 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -175,16 +175,19 @@ ui_close(void) } wint_t -ui_get_char(char *input, int *size, int *result) +ui_get_char(char *input, int *size) { - wint_t ch = inp_get_char(input, size, result); - if (ch != ERR && *result != ERR) { + int result = 0; + wint_t ch = inp_get_char(input, size, &result); + if (ch != ERR && result != ERR) { ui_reset_idle_time(); ui_input_nonblocking(TRUE); } else { ui_input_nonblocking(FALSE); } + ui_handle_special_keys(ch, result); + return ch; } diff --git a/src/ui/ui.h b/src/ui/ui.h index 58ce8fb5..600f7fe0 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -230,7 +230,7 @@ void ui_update_presence(const resource_presence_t resource_presence, void ui_about(void); void ui_statusbar_new(const int win); -wint_t ui_get_char(char *input, int *size, int *result); +wint_t ui_get_char(char *input, int *size); void ui_input_clear(void); void ui_input_nonblocking(gboolean); void ui_replace_input(char *input, const char * const new_input, int *size); diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c index da75c3dd..a4130279 100644 --- a/tests/ui/stub_ui.c +++ b/tests/ui/stub_ui.c @@ -325,7 +325,7 @@ void ui_update_presence(const resource_presence_t resource_presence, void ui_about(void) {} void ui_statusbar_new(const int win) {} -wint_t ui_get_char(char *input, int *size, int *result) +wint_t ui_get_char(char *input, int *size) { return 0; } From db9a2cf0ab40912d1d342b8ad3dfacbf8776411a Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 15 Jan 2015 00:02:42 +0000 Subject: [PATCH 3/4] Inlined ui_handle_special_keys --- src/ui/core.c | 18 ++++++------------ src/ui/ui.h | 1 - tests/ui/stub_ui.c | 2 -- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 855c600c..5b621ba1 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -179,6 +179,12 @@ ui_get_char(char *input, int *size) { int result = 0; wint_t ch = inp_get_char(input, size, &result); + _win_handle_switch(ch); + _win_handle_page(ch, result); + if (ch == KEY_RESIZE) { + ui_resize(); + } + if (ch != ERR && result != ERR) { ui_reset_idle_time(); ui_input_nonblocking(TRUE); @@ -186,8 +192,6 @@ ui_get_char(char *input, int *size) ui_input_nonblocking(FALSE); } - ui_handle_special_keys(ch, result); - return ch; } @@ -704,16 +708,6 @@ ui_disconnected(void) ui_hide_roster(); } -void -ui_handle_special_keys(const wint_t ch, const int result) -{ - _win_handle_switch(ch); - _win_handle_page(ch, result); - if (ch == KEY_RESIZE) { - ui_resize(); - } -} - void ui_close_connected_win(int index) { diff --git a/src/ui/ui.h b/src/ui/ui.h index 600f7fe0..8c9690d6 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -61,7 +61,6 @@ void ui_close(void); void ui_redraw(void); void ui_resize(void); GSList* ui_get_chat_recipients(void); -void ui_handle_special_keys(const wint_t ch, const int result); gboolean ui_switch_win(const int i); void ui_next_win(void); void ui_previous_win(void); diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c index a4130279..cee10aa9 100644 --- a/tests/ui/stub_ui.c +++ b/tests/ui/stub_ui.c @@ -64,8 +64,6 @@ GSList* ui_get_chat_recipients(void) return NULL; } -void ui_handle_special_keys(const wint_t ch, const int result) {} - gboolean ui_switch_win(const int i) { check_expected(i); From 97aebb6113a7d9683a9d3258f2ac5b1e7b98cc61 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 15 Jan 2015 00:14:12 +0000 Subject: [PATCH 4/4] Moved win_handle_page to window module --- src/ui/core.c | 115 +----------------------------------------------- src/ui/window.c | 110 +++++++++++++++++++++++++++++++++++++++++++++ src/ui/window.h | 3 ++ 3 files changed, 115 insertions(+), 113 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 5b621ba1..2873b693 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -81,7 +81,6 @@ static Display *display; static GTimer *ui_idle_time; static void _win_handle_switch(const wint_t ch); -static void _win_handle_page(const wint_t ch, const int result); static void _win_show_history(int win_index, const char * const contact); static void _ui_draw_term_title(void); @@ -180,7 +179,8 @@ ui_get_char(char *input, int *size) int result = 0; wint_t ch = inp_get_char(input, size, &result); _win_handle_switch(ch); - _win_handle_page(ch, result); + ProfWin *current = wins_get_current(); + win_handle_page(current, ch, result); if (ch == KEY_RESIZE) { ui_resize(); } @@ -2957,117 +2957,6 @@ _win_handle_switch(const wint_t ch) } } -static void -_win_handle_page(const wint_t ch, const int result) -{ - ProfWin *current = wins_get_current(); - int rows = getmaxy(stdscr); - int y = getcury(current->layout->win); - - int page_space = rows - 4; - int *page_start = &(current->layout->y_pos); - - if (prefs_get_boolean(PREF_MOUSE)) { - MEVENT mouse_event; - - if (ch == KEY_MOUSE) { - if (getmouse(&mouse_event) == OK) { - -#ifdef PLATFORM_CYGWIN - if (mouse_event.bstate & BUTTON5_PRESSED) { // mouse wheel down -#else - if (mouse_event.bstate & BUTTON2_PRESSED) { // mouse wheel down -#endif - *page_start += 4; - - // only got half a screen, show full screen - if ((y - (*page_start)) < page_space) - *page_start = y - page_space; - - // went past end, show full screen - else if (*page_start >= y) - *page_start = y - page_space; - - current->layout->paged = 1; - win_update_virtual(current); - } else if (mouse_event.bstate & BUTTON4_PRESSED) { // mouse wheel up - *page_start -= 4; - - // went past beginning, show first page - if (*page_start < 0) - *page_start = 0; - - current->layout->paged = 1; - win_update_virtual(current); - } - } - } - } - - // page up - if (ch == KEY_PPAGE) { - *page_start -= page_space; - - // went past beginning, show first page - if (*page_start < 0) - *page_start = 0; - - current->layout->paged = 1; - win_update_virtual(current); - - // page down - } else if (ch == KEY_NPAGE) { - *page_start += page_space; - - // only got half a screen, show full screen - if ((y - (*page_start)) < page_space) - *page_start = y - page_space; - - // went past end, show full screen - else if (*page_start >= y) - *page_start = y - page_space - 1; - - current->layout->paged = 1; - win_update_virtual(current); - } - - // switch off page if last line and space line visible - if ((y) - *page_start == page_space) { - current->layout->paged = 0; - } - - if (current->layout->type == LAYOUT_SPLIT) { - ProfLayoutSplit *split_layout = (ProfLayoutSplit*)current->layout; - int sub_y = getcury(split_layout->subwin); - int *sub_y_pos = &(split_layout->sub_y_pos); - - // alt up arrow - if ((result == KEY_CODE_YES) && ((ch == 565) || (ch == 337))) { - *sub_y_pos -= page_space; - - // went past beginning, show first page - if (*sub_y_pos < 0) - *sub_y_pos = 0; - - win_update_virtual(current); - - // alt down arrow - } else if ((result == KEY_CODE_YES) && ((ch == 524) || (ch == 336))) { - *sub_y_pos += page_space; - - // only got half a screen, show full screen - if ((sub_y- (*sub_y_pos)) < page_space) - *sub_y_pos = sub_y - page_space; - - // went past end, show full screen - else if (*sub_y_pos >= sub_y) - *sub_y_pos = sub_y - page_space - 1; - - win_update_virtual(current); - } - } -} - static void _win_show_history(int win_index, const char * const contact) { diff --git a/src/ui/window.c b/src/ui/window.c index 3a45ab01..dfec5aab 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -357,6 +357,116 @@ win_free(ProfWin* window) free(window); } +void +win_handle_page(ProfWin *window, const wint_t ch, const int result) +{ + int rows = getmaxy(stdscr); + int y = getcury(window->layout->win); + + int page_space = rows - 4; + int *page_start = &(window->layout->y_pos); + + if (prefs_get_boolean(PREF_MOUSE)) { + MEVENT mouse_event; + + if (ch == KEY_MOUSE) { + if (getmouse(&mouse_event) == OK) { + +#ifdef PLATFORM_CYGWIN + if (mouse_event.bstate & BUTTON5_PRESSED) { // mouse wheel down +#else + if (mouse_event.bstate & BUTTON2_PRESSED) { // mouse wheel down +#endif + *page_start += 4; + + // only got half a screen, show full screen + if ((y - (*page_start)) < page_space) + *page_start = y - page_space; + + // went past end, show full screen + else if (*page_start >= y) + *page_start = y - page_space; + + window->layout->paged = 1; + win_update_virtual(window); + } else if (mouse_event.bstate & BUTTON4_PRESSED) { // mouse wheel up + *page_start -= 4; + + // went past beginning, show first page + if (*page_start < 0) + *page_start = 0; + + window->layout->paged = 1; + win_update_virtual(window); + } + } + } + } + + // page up + if (ch == KEY_PPAGE) { + *page_start -= page_space; + + // went past beginning, show first page + if (*page_start < 0) + *page_start = 0; + + window->layout->paged = 1; + win_update_virtual(window); + + // page down + } else if (ch == KEY_NPAGE) { + *page_start += page_space; + + // only got half a screen, show full screen + if ((y - (*page_start)) < page_space) + *page_start = y - page_space; + + // went past end, show full screen + else if (*page_start >= y) + *page_start = y - page_space - 1; + + window->layout->paged = 1; + win_update_virtual(window); + } + + // switch off page if last line and space line visible + if ((y) - *page_start == page_space) { + window->layout->paged = 0; + } + + if (window->layout->type == LAYOUT_SPLIT) { + ProfLayoutSplit *split_layout = (ProfLayoutSplit*)window->layout; + int sub_y = getcury(split_layout->subwin); + int *sub_y_pos = &(split_layout->sub_y_pos); + + // alt up arrow + if ((result == KEY_CODE_YES) && ((ch == 565) || (ch == 337))) { + *sub_y_pos -= page_space; + + // went past beginning, show first page + if (*sub_y_pos < 0) + *sub_y_pos = 0; + + win_update_virtual(window); + + // alt down arrow + } else if ((result == KEY_CODE_YES) && ((ch == 524) || (ch == 336))) { + *sub_y_pos += page_space; + + // only got half a screen, show full screen + if ((sub_y- (*sub_y_pos)) < page_space) + *sub_y_pos = sub_y - page_space; + + // went past end, show full screen + else if (*sub_y_pos >= sub_y) + *sub_y_pos = sub_y - page_space - 1; + + win_update_virtual(window); + } + } +} + void win_update_virtual(ProfWin *window) { diff --git a/src/ui/window.h b/src/ui/window.h index b6bd0298..fd10a1d7 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -37,6 +37,8 @@ #include "config.h" +#include + #ifdef HAVE_NCURSESW_NCURSES_H #include #elif HAVE_NCURSES_H @@ -176,6 +178,7 @@ void win_show_subwin(ProfWin *window); int win_roster_cols(void); int win_occpuants_cols(void); void win_printline_nowrap(WINDOW *win, char *msg); +void win_handle_page(ProfWin *current, const wint_t ch, const int result); int win_unread(ProfWin *window); gboolean win_has_active_subwin(ProfWin *window);