Refactoring.

This commit is contained in:
Přemysl Eric Janouch 2013-05-15 17:26:39 +02:00
parent 776cebe535
commit 421d87b7c0
1 changed files with 273 additions and 254 deletions

View File

@ -74,42 +74,12 @@ struct curses_event
MEVENT mouse; MEVENT mouse;
}; };
// --- SIGWINCH ----------------------------------------------------------------
static int g_winch_pipe[2]; /**< SIGWINCH signalling pipe. */
static void (*g_old_winch_handler) (int);
static void
winch_handler (int signum)
{
/* Call the ncurses handler. */
if (g_old_winch_handler)
g_old_winch_handler (signum);
/* And wake up the poll() call. */
write (g_winch_pipe[1], "x", 1);
}
static void
install_winch_handler (void)
{
struct sigaction act, oldact;
act.sa_handler = winch_handler;
act.sa_flags = SA_RESTART;
sigemptyset (&act.sa_mask);
sigaction (SIGWINCH, &act, &oldact);
/* Save the ncurses handler. */
if (oldact.sa_handler != SIG_DFL
&& oldact.sa_handler != SIG_IGN)
g_old_winch_handler = oldact.sa_handler;
}
// --- Application ------------------------------------------------------------- // --- Application -------------------------------------------------------------
/** Data relating to one entry within the dictionary. */ /** Data relating to one entry within the dictionary. */
typedef struct view_entry ViewEntry; typedef struct view_entry ViewEntry;
/** Encloses application data. */
typedef struct application Application;
struct view_entry struct view_entry
{ {
@ -118,18 +88,21 @@ struct view_entry
gsize definitions_length; //!< Length of the @a definitions array gsize definitions_length; //!< Length of the @a definitions array
}; };
GIConv g_utf8_to_wchar; //!< utf-8 -> wchar_t conversion struct application
GIConv g_wchar_to_utf8; //!< wchar_t -> utf-8 conversion {
GIConv utf8_to_wchar; //!< utf-8 -> wchar_t conversion
GIConv wchar_to_utf8; //!< wchar_t -> utf-8 conversion
StardictDict *g_dict; //!< The current dictionary StardictDict *dict; //!< The current dictionary
guint32 g_top_position; //!< Index of the topmost view entry guint32 top_position; //!< Index of the topmost dict. entry
guint g_top_offset; //!< Offset into the top entry guint top_offset; //!< Offset into the top entry
guint g_selected; //!< Offset to the selected definition guint selected; //!< Offset to the selected definition
GPtrArray *g_entries; //!< ViewEntry's within the view GPtrArray *entries; //!< ViewEntry's within the view
GString *g_input; //!< The current search input GString *input; //!< The current search input
guint g_input_pos; //!< Cursor position within input guint input_pos; //!< Cursor position within input
};
/** Decomposes a dictionary entry into the format we want. */ /** Decomposes a dictionary entry into the format we want. */
@ -189,19 +162,20 @@ view_entry_free (ViewEntry *ve)
/** Reload view items. */ /** Reload view items. */
static void static void
app_reload_view (void) app_reload_view (Application *self)
{ {
if (g_entries->len != 0) if (self->entries->len != 0)
g_ptr_array_remove_range (g_entries, 0, g_entries->len); g_ptr_array_remove_range (self->entries, 0, self->entries->len);
g_selected = 0; self->selected = 0;
gint remains = LINES - 1; gint remains = LINES - 1;
StardictIterator *iterator = stardict_iterator_new (g_dict, g_top_position); StardictIterator *iterator =
stardict_iterator_new (self->dict, self->top_position);
while (remains > 0 && stardict_iterator_is_valid (iterator)) while (remains > 0 && stardict_iterator_is_valid (iterator))
{ {
ViewEntry *entry = view_entry_new (iterator); ViewEntry *entry = view_entry_new (iterator);
remains -= entry->definitions_length; remains -= entry->definitions_length;
g_ptr_array_add (g_entries, entry); g_ptr_array_add (self->entries, entry);
stardict_iterator_next (iterator); stardict_iterator_next (iterator);
} }
g_object_unref (iterator); g_object_unref (iterator);
@ -209,46 +183,46 @@ app_reload_view (void)
/** Initialize the application core. */ /** Initialize the application core. */
static void static void
app_init (const gchar *filename) app_init (Application *self, const gchar *filename)
{ {
GError *error; GError *error;
g_dict = stardict_dict_new (filename, &error); self->dict = stardict_dict_new (filename, &error);
if (!g_dict) if (!self->dict)
{ {
g_printerr ("Error loading dictionary: %s\n", error->message); g_printerr ("Error loading dictionary: %s\n", error->message);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
g_top_position = 0; self->top_position = 0;
g_top_offset = 0; self->top_offset = 0;
g_selected = 0; self->selected = 0;
g_entries = g_ptr_array_new_with_free_func self->entries = g_ptr_array_new_with_free_func
((GDestroyNotify) view_entry_free); ((GDestroyNotify) view_entry_free);
g_input = g_string_new (NULL); self->input = g_string_new (NULL);
g_input_pos = 0; self->input_pos = 0;
g_wchar_to_utf8 = g_iconv_open ("utf-8//translit", "wchar_t"); self->wchar_to_utf8 = g_iconv_open ("utf-8//translit", "wchar_t");
g_utf8_to_wchar = g_iconv_open ("wchar_t//translit", "utf-8"); self->utf8_to_wchar = g_iconv_open ("wchar_t//translit", "utf-8");
app_reload_view (); app_reload_view (self);
} }
/** Free any resources used by the application. */ /** Free any resources used by the application. */
static void static void
app_destroy (void) app_destroy (Application *self)
{ {
g_object_unref (g_dict); g_object_unref (self->dict);
g_ptr_array_free (g_entries, TRUE); g_ptr_array_free (self->entries, TRUE);
g_string_free (g_input, TRUE); g_string_free (self->input, TRUE);
g_iconv_close (g_wchar_to_utf8); g_iconv_close (self->wchar_to_utf8);
g_iconv_close (g_utf8_to_wchar); g_iconv_close (self->utf8_to_wchar);
} }
/** Render the top bar. */ /** Render the top bar. */
static void static void
app_redraw_top (void) app_redraw_top (Application *self)
{ {
mvwhline (stdscr, 0, 0, A_UNDERLINE, COLS); mvwhline (stdscr, 0, 0, A_UNDERLINE, COLS);
attrset (A_UNDERLINE); attrset (A_UNDERLINE);
@ -257,22 +231,22 @@ app_redraw_top (void)
int y, x; int y, x;
getyx (stdscr, y, x); getyx (stdscr, y, x);
gchar *input = g_locale_from_utf8 (g_input->str, -1, NULL, NULL, NULL); gchar *input = g_locale_from_utf8 (self->input->str, -1, NULL, NULL, NULL);
g_return_if_fail (input != NULL); g_return_if_fail (input != NULL);
addstr (input); addstr (input);
g_free (input); g_free (input);
move (y, x + g_input_pos); move (y, x + self->input_pos);
refresh (); refresh ();
} }
/** Write the given utf-8 string padded with spaces, max. @a n characters. */ /** Write the given utf-8 string padded with spaces, max. @a n characters. */
static void static void
add_padded_string (const gchar *str, int n) add_padded_string (Application *self, const gchar *str, int n)
{ {
wchar_t *wide_str = (wchar_t *) g_convert_with_iconv wchar_t *wide_str = (wchar_t *) g_convert_with_iconv
(str, -1, g_utf8_to_wchar, NULL, NULL, NULL); (str, -1, self->utf8_to_wchar, NULL, NULL, NULL);
g_return_if_fail (wide_str != NULL); g_return_if_fail (wide_str != NULL);
ssize_t wide_len = wcslen (wide_str); ssize_t wide_len = wcslen (wide_str);
@ -292,24 +266,24 @@ add_padded_string (const gchar *str, int n)
/** Redraw the dictionary view. */ /** Redraw the dictionary view. */
static void static void
app_redraw_view (void) app_redraw_view (Application *self)
{ {
move (1, 0); move (1, 0);
guint i, k = g_top_offset, shown = 0; guint i, k = self->top_offset, shown = 0;
for (i = 0; i < g_entries->len; i++) for (i = 0; i < self->entries->len; i++)
{ {
ViewEntry *ve = g_ptr_array_index (g_entries, i); ViewEntry *ve = g_ptr_array_index (self->entries, i);
for (; k < ve->definitions_length; k++) for (; k < ve->definitions_length; k++)
{ {
int attrs = 0; int attrs = 0;
if (shown == g_selected) attrs |= A_REVERSE; if (shown == self->selected) attrs |= A_REVERSE;
if (k + 1 == ve->definitions_length) attrs |= A_UNDERLINE; if (k + 1 == ve->definitions_length) attrs |= A_UNDERLINE;
attrset (attrs); attrset (attrs);
add_padded_string (ve->word, COLS / 2); add_padded_string (self, ve->word, COLS / 2);
addwstr (L" "); addwstr (L" ");
add_padded_string (ve->definitions[k], COLS - COLS / 2 - 1); add_padded_string (self, ve->definitions[k], COLS - COLS / 2 - 1);
if ((gint) ++shown == LINES - 1) if ((gint) ++shown == LINES - 1)
goto done; goto done;
@ -326,29 +300,29 @@ done:
/** Just prepends a new view entry into the entries array. */ /** Just prepends a new view entry into the entries array. */
static ViewEntry * static ViewEntry *
prepend_entry (guint32 position) prepend_entry (Application *self, guint32 position)
{ {
StardictIterator *iterator = stardict_iterator_new (g_dict, position); StardictIterator *iterator = stardict_iterator_new (self->dict, position);
ViewEntry *ve = view_entry_new (iterator); ViewEntry *ve = view_entry_new (iterator);
g_object_unref (iterator); g_object_unref (iterator);
g_ptr_array_add (g_entries, NULL); g_ptr_array_add (self->entries, NULL);
memmove (g_entries->pdata + 1, g_entries->pdata, memmove (self->entries->pdata + 1, self->entries->pdata,
sizeof ve * (g_entries->len - 1)); sizeof ve * (self->entries->len - 1));
return g_ptr_array_index (g_entries, 0) = ve; return g_ptr_array_index (self->entries, 0) = ve;
} }
/** Just appends a new view entry to the entries array. */ /** Just appends a new view entry to the entries array. */
static ViewEntry * static ViewEntry *
append_entry (guint32 position) append_entry (Application *self, guint32 position)
{ {
ViewEntry *ve = NULL; ViewEntry *ve = NULL;
StardictIterator *iterator = stardict_iterator_new StardictIterator *iterator = stardict_iterator_new
(g_dict, position); (self->dict, position);
if (stardict_iterator_is_valid (iterator)) if (stardict_iterator_is_valid (iterator))
{ {
ve = view_entry_new (iterator); ve = view_entry_new (iterator);
g_ptr_array_add (g_entries, ve); g_ptr_array_add (self->entries, ve);
} }
g_object_unref (iterator); g_object_unref (iterator);
return ve; return ve;
@ -356,12 +330,12 @@ append_entry (guint32 position)
/** Counts the number of definitions available for seeing. */ /** Counts the number of definitions available for seeing. */
static guint static guint
count_view_items (void) count_view_items (Application *self)
{ {
guint i, n_definitions = 0; guint i, n_definitions = 0;
for (i = 0; i < g_entries->len; i++) for (i = 0; i < self->entries->len; i++)
{ {
ViewEntry *entry = g_ptr_array_index (g_entries, i); ViewEntry *entry = g_ptr_array_index (self->entries, i);
n_definitions += entry->definitions_length; n_definitions += entry->definitions_length;
} }
return n_definitions; return n_definitions;
@ -369,279 +343,326 @@ count_view_items (void)
/** Scroll up @a n entries. */ /** Scroll up @a n entries. */
static gboolean static gboolean
app_scroll_up (guint n) app_scroll_up (Application *self, guint n)
{ {
gboolean success = TRUE; gboolean success = TRUE;
guint n_definitions = count_view_items (); guint n_definitions = count_view_items (self);
while (n--) while (n--)
{ {
if (g_top_offset > 0) if (self->top_offset > 0)
{ {
g_top_offset--; self->top_offset--;
continue; continue;
} }
/* We've reached the top */ /* We've reached the top */
if (g_top_position == 0) if (self->top_position == 0)
{ {
success = FALSE; success = FALSE;
break; break;
} }
ViewEntry *ve = prepend_entry (--g_top_position); ViewEntry *ve = prepend_entry (self, --self->top_position);
g_top_offset = ve->definitions_length - 1; self->top_offset = ve->definitions_length - 1;
n_definitions += ve->definitions_length; n_definitions += ve->definitions_length;
/* Remove the last entry if not shown */ /* Remove the last entry if not shown */
ViewEntry *last_entry = ViewEntry *last_entry =
g_ptr_array_index (g_entries, g_entries->len - 1); g_ptr_array_index (self->entries, self->entries->len - 1);
if ((gint) (n_definitions - g_top_offset if ((gint) (n_definitions - self->top_offset
- last_entry->definitions_length) >= LINES - 1) - last_entry->definitions_length) >= LINES - 1)
{ {
n_definitions -= last_entry->definitions_length; n_definitions -= last_entry->definitions_length;
g_ptr_array_remove_index_fast g_ptr_array_remove_index_fast
(g_entries, g_entries->len - 1); (self->entries, self->entries->len - 1);
} }
} }
app_redraw_view (); app_redraw_view (self);
return success; return success;
} }
/** Scroll down @a n entries. */ /** Scroll down @a n entries. */
static gboolean static gboolean
app_scroll_down (guint n) app_scroll_down (Application *self, guint n)
{ {
gboolean success = TRUE; gboolean success = TRUE;
guint n_definitions = count_view_items (); guint n_definitions = count_view_items (self);
while (n--) while (n--)
{ {
if (g_entries->len == 0) if (self->entries->len == 0)
{ {
success = FALSE; success = FALSE;
break; break;
} }
ViewEntry *first_entry = g_ptr_array_index (g_entries, 0); ViewEntry *first_entry = g_ptr_array_index (self->entries, 0);
if (g_top_offset < first_entry->definitions_length - 1) if (self->top_offset < first_entry->definitions_length - 1)
g_top_offset++; self->top_offset++;
else else
{ {
n_definitions -= first_entry->definitions_length; n_definitions -= first_entry->definitions_length;
g_ptr_array_remove_index (g_entries, 0); g_ptr_array_remove_index (self->entries, 0);
g_top_position++; self->top_position++;
g_top_offset = 0; self->top_offset = 0;
} }
if ((gint) (n_definitions - g_top_offset) < LINES - 1) if ((gint) (n_definitions - self->top_offset) < LINES - 1)
{ {
ViewEntry *ve = append_entry (g_top_position + g_entries->len); ViewEntry *ve = append_entry (self,
self->top_position + self->entries->len);
if (ve != NULL) if (ve != NULL)
n_definitions += ve->definitions_length; n_definitions += ve->definitions_length;
} }
} }
/* Fix cursor to not point below the view items */ /* Fix cursor to not point below the view items */
if (g_selected >= n_definitions - g_top_offset) if (self->selected >= n_definitions - self->top_offset)
g_selected = n_definitions - g_top_offset - 1; self->selected = n_definitions - self->top_offset - 1;
app_redraw_view (); app_redraw_view (self);
return success; return success;
} }
/** Redraw everything. */ /** Redraw everything. */
static void static void
app_redraw (void) app_redraw (Application *self)
{ {
app_redraw_view (); app_redraw_view (self);
app_redraw_top (); app_redraw_top (self);
} }
/** Search for the current entry. */ /** Search for the current entry. */
static void static void
app_search_for_entry (void) app_search_for_entry (Application *self)
{ {
StardictIterator *iterator = stardict_dict_search StardictIterator *iterator = stardict_dict_search
(g_dict, g_input->str, NULL); (self->dict, self->input->str, NULL);
g_top_position = stardict_iterator_get_offset (iterator); self->top_position = stardict_iterator_get_offset (iterator);
g_top_offset = 0; self->top_offset = 0;
g_object_unref (iterator); g_object_unref (iterator);
app_reload_view (); app_reload_view (self);
app_redraw_view (); app_redraw_view (self);
} }
/** Process input that's not a character. */
static gboolean static gboolean
app_process_curses_event (CursesEvent *event) app_process_nonchar_code (Application *self, CursesEvent *event)
{ {
/* g_utf8_offset_to_pointer() is too dumb to detect this */
g_assert (g_utf8_strlen (g_input->str, -1) >= g_input_pos);
if (!event->is_char)
{
switch (event->code) switch (event->code)
{ {
case KEY_RESIZE: case KEY_RESIZE:
// TODO adapt to the new window size, COLS, LINES // TODO adapt to the new window size, COLS, LINES
// mind the position of the selection cursor // mind the position of the selection cursor
app_reload_view (); app_reload_view (self);
app_redraw (); app_redraw (self);
break; break;
case KEY_MOUSE: case KEY_MOUSE:
// TODO move the input entry cursor // TODO move the input entry cursor
if ((event->mouse.bstate & BUTTON1_PRESSED) && if ((event->mouse.bstate & BUTTON1_PRESSED) && event->mouse.y > 0 &&
event->mouse.y > 0 && event->mouse.y <= (int)
event->mouse.y <= (int) (count_view_items () - g_top_offset)) (count_view_items (self) - self->top_offset))
{ {
g_selected = event->mouse.y - 1; self->selected = event->mouse.y - 1;
app_redraw_view (); app_redraw_view (self);
app_redraw_top (); // FIXME just focus app_redraw_top (self); // FIXME just focus
} }
break; break;
case KEY_UP: case KEY_UP:
if (g_selected > 0) if (self->selected > 0)
{ {
g_selected--; self->selected--;
app_redraw_view (); app_redraw_view (self);
} }
else else
app_scroll_up (1); app_scroll_up (self, 1);
app_redraw_top (); // FIXME just focus app_redraw_top (self); // FIXME just focus
break; break;
case KEY_DOWN: case KEY_DOWN:
if ((gint) g_selected < LINES - 2 && if ((gint) self->selected < LINES - 2 &&
g_selected < count_view_items () - g_top_offset - 1) self->selected < count_view_items (self) - self->top_offset - 1)
{ {
g_selected++; self->selected++;
app_redraw_view (); app_redraw_view (self);
} }
else else
app_scroll_down (1); app_scroll_down (self, 1);
app_redraw_top (); // FIXME just focus app_redraw_top (self); // FIXME just focus
break; break;
case KEY_PPAGE: case KEY_PPAGE:
app_scroll_up (LINES - 1); app_scroll_up (self, LINES - 1);
app_redraw_top (); // FIXME just focus, selection app_redraw_top (self); // FIXME just focus, selection
break; break;
case KEY_NPAGE: case KEY_NPAGE:
app_scroll_down (LINES - 1); app_scroll_down (self, LINES - 1);
app_redraw_top (); // FIXME just focus, selection app_redraw_top (self); // FIXME just focus, selection
break; break;
case KEY_HOME: case KEY_HOME:
g_input_pos = 0; self->input_pos = 0;
app_redraw_top (); app_redraw_top (self);
break; break;
case KEY_END: case KEY_END:
g_input_pos = g_utf8_strlen (g_input->str, -1); self->input_pos = g_utf8_strlen (self->input->str, -1);
app_redraw_top (); app_redraw_top (self);
break; break;
case KEY_LEFT: case KEY_LEFT:
if (g_input_pos > 0) if (self->input_pos > 0)
{ {
g_input_pos--; self->input_pos--;
app_redraw_top (); app_redraw_top (self);
} }
break; break;
case KEY_RIGHT: case KEY_RIGHT:
if (g_input_pos < g_utf8_strlen (g_input->str, -1)) if (self->input_pos < g_utf8_strlen (self->input->str, -1))
{ {
g_input_pos++; self->input_pos++;
app_redraw_top (); app_redraw_top (self);
} }
break; break;
case KEY_BACKSPACE: case KEY_BACKSPACE:
if (g_input_pos > 0) if (self->input_pos > 0)
{ {
gchar *current = g_utf8_offset_to_pointer gchar *current = g_utf8_offset_to_pointer
(g_input->str, g_input_pos); (self->input->str, self->input_pos);
gchar *prev = g_utf8_prev_char (current); gchar *prev = g_utf8_prev_char (current);
g_string_erase (g_input, prev - g_input->str, current - prev); g_string_erase (self->input,
g_input_pos--; prev - self->input->str, current - prev);
app_search_for_entry (); self->input_pos--;
app_redraw_top (); app_search_for_entry (self);
app_redraw_top (self);
} }
break; break;
case KEY_DC: case KEY_DC:
if (g_input_pos < g_utf8_strlen (g_input->str, -1)) if (self->input_pos < g_utf8_strlen (self->input->str, -1))
{ {
gchar *current = g_utf8_offset_to_pointer gchar *current = g_utf8_offset_to_pointer
(g_input->str, g_input_pos); (self->input->str, self->input_pos);
g_string_erase (g_input, current - g_input->str, g_string_erase (self->input, current - self->input->str,
g_utf8_next_char (current) - current); g_utf8_next_char (current) - current);
app_search_for_entry (); app_search_for_entry (self);
app_redraw_top (); app_redraw_top (self);
} }
break; break;
} }
return TRUE; return TRUE;
} }
/** Process input events from ncurses. */
static gboolean
app_process_curses_event (Application *self, CursesEvent *event)
{
/* g_utf8_offset_to_pointer() is too dumb to detect this */
g_assert (g_utf8_strlen (self->input->str, -1) >= self->input_pos);
if (!event->is_char)
return app_process_nonchar_code (self, event);
switch (event->code) switch (event->code)
{ {
case KEY_ESCAPE: case KEY_ESCAPE:
return FALSE; return FALSE;
case KEY_VT: // Ctrl-K -- delete until the end of line case KEY_VT: // Ctrl-K -- delete until the end of line
g_string_erase (g_input, utf8_offset (g_input->str, g_input_pos), -1); g_string_erase (self->input,
utf8_offset (self->input->str, self->input_pos), -1);
app_search_for_entry (); app_search_for_entry (self);
app_redraw_top (); app_redraw_top (self);
return TRUE; return TRUE;
case KEY_ETB: // Ctrl-W -- delete word before cursor case KEY_ETB: // Ctrl-W -- delete word before cursor
{ {
if (!g_input_pos) if (!self->input_pos)
return TRUE; return TRUE;
gchar *current = g_utf8_offset_to_pointer (g_input->str, g_input_pos); gchar *current = g_utf8_offset_to_pointer
gchar *space = g_utf8_strrchr (g_input->str, (self->input->str, self->input_pos);
g_utf8_prev_char (current) - g_input->str, ' '); gchar *space = g_utf8_strrchr (self->input->str,
g_utf8_prev_char (current) - self->input->str, ' ');
if (space) if (space)
{ {
space = g_utf8_next_char (space); space = g_utf8_next_char (space);
g_string_erase (g_input, space - g_input->str, current - space); g_string_erase (self->input,
g_input_pos = g_utf8_pointer_to_offset (g_input->str, space); space - self->input->str, current - space);
self->input_pos = g_utf8_pointer_to_offset
(self->input->str, space);
} }
else else
{ {
g_string_erase (g_input, 0, current - g_input->str); g_string_erase (self->input, 0, current - self->input->str);
g_input_pos = 0; self->input_pos = 0;
} }
app_search_for_entry (); app_search_for_entry (self);
app_redraw_top (); app_redraw_top (self);
return TRUE; return TRUE;
} }
case KEY_NAK: // Ctrl-U -- delete everything before the cursor case KEY_NAK: // Ctrl-U -- delete everything before the cursor
g_string_erase (g_input, 0, utf8_offset (g_input->str, g_input_pos)); g_string_erase (self->input, 0,
g_input_pos = 0; utf8_offset (self->input->str, self->input_pos));
self->input_pos = 0;
app_search_for_entry (); app_search_for_entry (self);
app_redraw_top (); app_redraw_top (self);
return TRUE; return TRUE;
} }
wchar_t code = event->code; wchar_t code = event->code;
gchar *letter = g_convert_with_iconv ((gchar *) &code, sizeof code, gchar *letter = g_convert_with_iconv ((gchar *) &code, sizeof code,
g_wchar_to_utf8, NULL, NULL, NULL); self->wchar_to_utf8, NULL, NULL, NULL);
g_return_val_if_fail (letter != NULL, FALSE); g_return_val_if_fail (letter != NULL, FALSE);
if (g_unichar_isprint (g_utf8_get_char (letter))) if (g_unichar_isprint (g_utf8_get_char (letter)))
{ {
g_string_insert (g_input, g_string_insert (self->input,
utf8_offset (g_input->str, g_input_pos), letter); utf8_offset (self->input->str, self->input_pos), letter);
g_input_pos += g_utf8_strlen (letter, -1); self->input_pos += g_utf8_strlen (letter, -1);
app_search_for_entry (); app_search_for_entry (self);
app_redraw_top (); app_redraw_top (self);
} }
g_free (letter); g_free (letter);
return TRUE; return TRUE;
} }
// --- Event handlers ---------------------------------------------------------- // --- SIGWINCH ----------------------------------------------------------------
static int g_winch_pipe[2]; /**< SIGWINCH signalling pipe. */
static void (*g_old_winch_handler) (int);
static void
winch_handler (int signum)
{
/* Call the ncurses handler. */
if (g_old_winch_handler)
g_old_winch_handler (signum);
/* And wake up the poll() call. */
write (g_winch_pipe[1], "x", 1);
}
static void
install_winch_handler (void)
{
struct sigaction act, oldact;
act.sa_handler = winch_handler;
act.sa_flags = SA_RESTART;
sigemptyset (&act.sa_mask);
sigaction (SIGWINCH, &act, &oldact);
/* Save the ncurses handler. */
if (oldact.sa_handler != SIG_DFL
&& oldact.sa_handler != SIG_IGN)
g_old_winch_handler = oldact.sa_handler;
}
// --- Initialisation, event handling ------------------------------------------
Application g_application;
static gboolean static gboolean
process_stdin_input (void) process_stdin_input (void)
@ -655,7 +676,7 @@ process_stdin_input (void)
if (sta == KEY_CODE_YES && event.code == KEY_MOUSE if (sta == KEY_CODE_YES && event.code == KEY_MOUSE
&& getmouse (&event.mouse) == ERR) && getmouse (&event.mouse) == ERR)
abort (); abort ();
if (!app_process_curses_event (&event)) if (!app_process_curses_event (&g_application, &event))
return FALSE; return FALSE;
} }
@ -671,8 +692,6 @@ process_winch_input (int fd)
return process_stdin_input (); return process_stdin_input ();
} }
// --- Main --------------------------------------------------------------------
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -710,7 +729,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
g_option_context_free (ctx); g_option_context_free (ctx);
app_init (argv[1]); app_init (&g_application, argv[1]);
if (!initscr () if (!initscr ()
|| cbreak () == ERR || cbreak () == ERR
@ -728,7 +747,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
abort (); abort ();
install_winch_handler (); install_winch_handler ();
app_redraw (); app_redraw (&g_application);
/* Message loop. */ /* Message loop. */
struct pollfd pollfd[2]; struct pollfd pollfd[2];
@ -752,7 +771,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
} }
endwin (); endwin ();
app_destroy (); app_destroy (&g_application);
if (close (g_winch_pipe[0]) == -1 if (close (g_winch_pipe[0]) == -1
|| close (g_winch_pipe[1]) == -1) || close (g_winch_pipe[1]) == -1)