From: Andre Noll Date: Sat, 18 May 2013 12:06:22 +0000 (+0200) Subject: Merge branch 't/sched_improvements' X-Git-Tag: v0.4.13~39 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=2f07d34b5d4c37606be5849b6ee51e0443707898;hp=3ad5b03e1afa554220a516f3941b2ef5348fcc19 Merge branch 't/sched_improvements' This topic has been cooking for a month. During this period, one problem on BSD has been found and fixed. 3ad5b0 Fix build on FreeBSD 484e75 sched: Rename new_post_select back to post_select. 74c880 sched: Kill old ->post_select variant. c77e19 grab client: Switch to the alternative post select method. 38aeac client supervisor: Switch to the alternative post select method. e009fa client exec: Switch to the alternative post select method. 742718 i9e: Switch to the alternative post select method. 2e6b8f vss: Switch to the alternative post select method. 6e83b4 stdout: Switch to the alternative post select method. 1995ce stdin: Switch to the alternative post select method. 88b0ec audioc: Switch to the alternative post select method. b210e8 afs command task: Switch to the alternative post select method. 12f683 afs signal task: Switch to the alternative post select method. ccef24 server signal task: Switch to the alternative post select method. a6dabd server command task: Switch to the alternative post select method. 855c53 write: Switch to the alternative post select method. 58b74b play: Switch to the alternative post select method. cf4982 afh_recv: Switch to the alternative post select method. 4dc05b client: Switch to the alternative post select method. 043fd6 audiod: Switch command_task to the alternative post select method. ce462a audiod: Switch the status task to the alternative post select method. a7f2d1 audiod: Switch signal task to the alternative post select method. ba2f65 osx writer: Switch to the alternative post select method. 3642d2 oss writer: Switch to the alternative post select method. c29db3 file writer: Switch to the alternative post select method. 36875c ao: Switch to the alternative post select method. 3b3049 alsa: Switch to the alternative post select method. 60b853 wmadec: Switch to the alternative post select method. 6c8719 wav filter: Switch to the alternative post select method. 03e915 spxdec: Switch to the alternative post select method. 24bbc5 resample: Switch to the alternative post select method. 4ca80f prebuffer: Switch to the alternative post select method. a55083 oggdec: Switch to the alternative post select method. 806d26 mp3dec: Switch to the alternative post select method. 7dcaf5 flacdec: Switch to the alternative post select method. 4dc9b9 fecdec: Switch to the alternative post select method. f6e2cb compress: Switch to the alternative post select method. ac3371 amp: Switch to the alternative post select method. 7c2c68 aacdec: Switch to the alternative post select method. b333e0 dccp_recv: Switch to the alternative post select method. 00e793 udp_recv: Switch to the alternative post select method. 9c00a7 sched: Provide alternative post_select variant. 24758c Replace gettimeofday() by clock_gettime(). 01f802 sched: Get rid of (pre)select shortcuts. 5bb44a audiod: Avoid delay when closing slot. Conflicts: string.c --- diff --git a/CREDITS b/CREDITS index 1d8ad0ce..ec4e5362 100644 --- a/CREDITS +++ b/CREDITS @@ -40,7 +40,7 @@ Loren Merritt (FFmpeg) Simon Morlat (ortp) -Christof Müller (bug reports) +Christof Müller (bug reports) M. Hari Nezumi (AudioCompress) @@ -50,7 +50,7 @@ Manuel Odendahl (poc) Guillaume Outters (mosx-mpg123) -Christian Reißmann (design) +Christian Reißmann (design) Gerrit Renker (dccp improvements, IPv6 support) diff --git a/Makefile.in b/Makefile.in index 7ccd970d..fa2e468f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -258,7 +258,7 @@ para_client: $(client_objs) para_gui: $(gui_objs) @[ -z "$(Q)" ] || echo 'LD $@' - $(Q) $(CC) $(LDFLAGS) -o $@ $(gui_objs) -lcurses + $(Q) $(CC) $(LDFLAGS) -o $@ $(gui_objs) @gui_ldflags@ para_audiod: $(audiod_objs) @[ -z "$(Q)" ] || echo 'LD $@' diff --git a/NEWS b/NEWS index f323df1d..56f84753 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,11 @@ 0.?.? (to be announced) "spectral gravity" ------------------------------------------ + - UTF8 support for para_gui and the mp3 audio format handler. + - Scheduler improvements and fixes. + - The obsolete gettimeofday() function has been replaced + by clock_gettime() on systems which support it. + ----------------------------------------- 0.4.12 (2012-12-20) "volatile relativity" ----------------------------------------- diff --git a/aacdec_filter.c b/aacdec_filter.c index 9eea045c..3ff90834 100644 --- a/aacdec_filter.c +++ b/aacdec_filter.c @@ -122,7 +122,7 @@ next_buffer: ret = -E_AACDEC_INIT; if (NeAACDecInit2(padd->handle, p, padd->decoder_length, &rate, - &channels) < 0) + &channels) != 0) goto out; } padd->sample_rate = rate; diff --git a/buffer_tree.c b/buffer_tree.c index bb854124..f0a7b224 100644 --- a/buffer_tree.c +++ b/buffer_tree.c @@ -386,7 +386,8 @@ void btr_add_output(char *buf, size_t size, struct btr_node *btrn) { struct btr_buffer *btrb; - assert(size != 0); + if (size == 0) + return; if (list_empty(&btrn->children)) { free(buf); return; @@ -415,7 +416,8 @@ void btr_add_output_dont_free(const char *buf, size_t size, struct btr_node *btr { struct btr_buffer *btrb; - assert(size != 0); + if (size == 0) + return; if (list_empty(&btrn->children)) return; btrb = new_btrb((char *)buf, size); @@ -442,7 +444,8 @@ void btr_add_output_pool(struct btr_pool *btrp, size_t size, char *buf; size_t avail; - assert(size != 0); + if (size == 0) + return; if (list_empty(&btrn->children)) return; avail = btr_pool_get_buffer(btrp, &buf); diff --git a/configure.ac b/configure.ac index 6f0fbc0c..135a0bd9 100644 --- a/configure.ac +++ b/configure.ac @@ -483,12 +483,17 @@ fi AC_CHECK_HEADER(curses.h, [], [ have_curses="no" ]) -AC_CHECK_LIB([curses], [initscr], [], [ - have_curses="no" -]) +gui_ldflags="$curses_libs" +AC_CHECK_LIB([ncursesw], [initscr], + [gui_ldflags="$curses_libs -lncursesw"], [ + AC_CHECK_LIB([curses], [initscr], + [gui_ldflags="$curses_libs -lcurses"], + [have_curses="no"] + ) + ] +) if test "$have_curses" = "yes"; then AC_SUBST(curses_cppflags) - AC_DEFINE(HAVE_NCURSES, 1, [define to 1 to turn on curses support]) extras="$extras gui" executables="$executables gui" else @@ -1255,6 +1260,7 @@ AC_DEFINE_UNQUOTED(INIT_AUDIOC_ERRLISTS, objlist_to_errlist($audioc_errlist_objs), errors used by para_audioc) AC_SUBST(gui_objs, add_dot_o($gui_objs)) +AC_SUBST(gui_ldflags, $gui_ldflags) AC_DEFINE_UNQUOTED(INIT_GUI_ERRLISTS, objlist_to_errlist($gui_errlist_objs), errors used by para_gui) @@ -1263,8 +1269,6 @@ AC_SUBST(play_ldflags, $play_ldflags) AC_DEFINE_UNQUOTED(INIT_PLAY_ERRLISTS, objlist_to_errlist($play_errlist_objs), errors used by para_play) -AC_MSG_NOTICE(play objs: $play_objs) - enum="$(for i in $filters; do printf "${i}_FILTER, " | tr '[a-z]' '[A-Z]'; done)" AC_DEFINE_UNQUOTED(FILTER_ENUM, $enum NUM_SUPPORTED_FILTERS, enum of supported filters) diff --git a/gui.c b/gui.c index a4ee7275..631e7cc5 100644 --- a/gui.c +++ b/gui.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "gui.cmdline.h" #include "para.h" @@ -305,11 +306,18 @@ static void add_spaces(WINDOW* win, unsigned int num) static int align_str(WINDOW* win, char *str, unsigned int len, unsigned int align) { - int i, num; /* of spaces */ + int ret, i, num; /* of spaces */ + size_t width; if (!win || !str) - return -1; - num = len - strlen(str); + return 0; + ret = strwidth(str, &width); + if (ret < 0) { + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); + width = 0; + str[0] = '\0'; + } + num = len - width; if (num < 0) { str[len] = '\0'; num = 0; @@ -391,11 +399,14 @@ static int first_visible_rbe(unsigned *lines) return RINGBUFFER_SIZE - 1; } +/* +returns number of first visible rbe, *lines is the number of lines drawn. + */ static int draw_top_rbe(unsigned *lines) { - unsigned len; - int offset, fvr = first_visible_rbe(lines); + int ret, fvr = first_visible_rbe(lines); struct rb_entry *rbe; + size_t bytes_to_skip, cells_to_skip, width; if (fvr < 0) return -1; @@ -403,16 +414,22 @@ static int draw_top_rbe(unsigned *lines) rbe = ringbuffer_get(bot_win_rb, fvr); if (!rbe) return -1; - len = strlen(rbe->msg); if (*lines > bot.lines) { - /* first rbe is only partially visible */ - offset = (*lines - bot.lines) * bot.cols; - assert(offset <= len); - } else - offset = 0; + /* rbe is partially visible multi-line */ + cells_to_skip = (*lines - bot.lines) * bot.cols; + ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip); + if (ret < 0) + return ret; + ret = strwidth(rbe->msg + bytes_to_skip, &width); + if (ret < 0) + return ret; + } else { + bytes_to_skip = 0; + width = rbe->len; + } wattron(bot.win, COLOR_PAIR(rbe->color)); - waddstr(bot.win, rbe->msg + offset); - *lines = NUM_LINES(len - offset); + waddstr(bot.win, rbe->msg + bytes_to_skip); + *lines = NUM_LINES(width); return fvr; } @@ -444,10 +461,15 @@ out: static void rb_add_entry(int color, char *msg) { - struct rb_entry *old, *new = para_malloc(sizeof(struct rb_entry)); + struct rb_entry *old, *new; int x, y; + size_t len; + + if (strwidth(msg, &len) < 0) + return; + new = para_malloc(sizeof(struct rb_entry)); new->color = color; - new->len = strlen(msg); + new->len = len; new->msg = msg; old = ringbuffer_add(bot_win_rb, new); // fprintf(stderr, "added: %s\n", new->msg); @@ -1162,7 +1184,7 @@ static void com_scroll_top(void) struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i); if (!rbe) break; - lines += NUM_LINES(strlen(rbe->msg)); + lines += NUM_LINES(rbe->len); } i++; if (lines > 0 && scroll_position != i) { @@ -1193,7 +1215,7 @@ static void com_page_down(void) struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i); if (!rbe) break; - lines += NUM_LINES(strlen(rbe->msg)); + lines += NUM_LINES(rbe->len); } if (lines) { scroll_position = i; @@ -1514,6 +1536,7 @@ int main(int argc, char *argv[]) top.lines = theme.top_lines_default; setup_signal_handling(); bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE); + setlocale(LC_CTYPE, ""); initscr(); /* needed only once, always successful */ init_curses(); print_welcome(); diff --git a/mp3_afh.c b/mp3_afh.c index 64709821..ece13e1f 100644 --- a/mp3_afh.c +++ b/mp3_afh.c @@ -72,11 +72,11 @@ static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mon #include -static char *get_latin1(id3_ucs4_t const *string) +static char *get_utf8(id3_ucs4_t const *string) { if (!string) return NULL; - return (char *)id3_ucs4_latin1duplicate(string); + return (char *)id3_ucs4_utf8duplicate(string); } static char *get_stringlist(union id3_field *field) @@ -85,7 +85,7 @@ static char *get_stringlist(union id3_field *field) char *result = NULL; for (k = 0; k < nstrings; k++) { - char *tmp = (char *)get_latin1(id3_field_getstrings(field, k)); + char *tmp = (char *)get_utf8(id3_field_getstrings(field, k)); if (result) { char *tmp2 = result; result = make_message("%s %s", tmp2, tmp); @@ -101,7 +101,7 @@ static char *get_string(union id3_field *field) { id3_ucs4_t const *string = id3_field_getfullstring(field); - return get_latin1(string); + return get_utf8(string); } #define FOR_EACH_FIELD(f, j, fr) for (j = 0; j < (fr)->nfields && \ diff --git a/string.c b/string.c index e5de147c..dfcfa2cd 100644 --- a/string.c +++ b/string.c @@ -6,11 +6,18 @@ /** \file string.c Memory allocation and string handling functions. */ +#define _GNU_SOURCE + #include #include /* uname() */ + #include #include +#include +#include +#include + #include "para.h" #include "string.h" #include "error.h" @@ -981,3 +988,145 @@ char *key_value_copy(const char *src, size_t len, const char *key) return NULL; return safe_strdup(src + keylen + 1, len - keylen - 1); } + +static bool utf8_mode(void) +{ + static bool initialized, have_utf8; + + if (!initialized) { + char *info = nl_langinfo(CODESET); + have_utf8 = (info && strcmp(info, "UTF-8") == 0); + initialized = true; + PARA_INFO_LOG("%susing UTF-8 character encoding\n", + have_utf8? "" : "not "); + } + return have_utf8; +} + +/* + * glibc's wcswidth returns -1 if the string contains a tab character, which + * makes the function next to useless. The two functions below are taken from + * mutt. + */ + +#define IsWPrint(wc) (iswprint(wc) || wc >= 0xa0) + +static int mutt_wcwidth(wchar_t wc, size_t pos) +{ + int n; + + if (wc == 0x09) /* tab */ + return (pos | 7) + 1 - pos; + n = wcwidth(wc); + if (IsWPrint(wc) && n > 0) + return n; + if (!(wc & ~0x7f)) + return 2; + if (!(wc & ~0xffff)) + return 6; + return 10; +} + +static size_t mutt_wcswidth(const wchar_t *s, size_t n) +{ + size_t w = 0; + + while (n--) + w += mutt_wcwidth(*s++, w); + return w; +} + +/** + * Skip a given number of cells at the beginning of a string. + * + * \param s The input string. + * \param cells_to_skip Desired number of cells that should be skipped. + * \param bytes_to_skip Result. + * + * This function computes how many input bytes must be skipped to advance a + * string by the given width. If the current character encoding is not UTF-8, + * this is simply the given number of cells, i.e. \a cells_to_skip. Otherwise, + * \a s is treated as a multibyte string and on successful return, \a s + + * bytes_to_skip points to the start of a multibyte string such that the total + * width of the multibyte characters that are skipped by advancing \a s that + * many bytes equals at least \a cells_to_skip. + * + * \return Standard. + */ +int skip_cells(const char *s, size_t cells_to_skip, size_t *bytes_to_skip) +{ + wchar_t wc; + mbstate_t ps; + size_t n, bytes_parsed, cells_skipped; + + *bytes_to_skip = 0; + if (cells_to_skip == 0) + return 0; + if (!utf8_mode()) { + *bytes_to_skip = cells_to_skip; + return 0; + } + bytes_parsed = cells_skipped = 0; + memset(&ps, 0, sizeof(ps)); + n = strlen(s); + while (cells_to_skip > cells_skipped) { + size_t mbret; + + mbret = mbrtowc(&wc, s + bytes_parsed, n - bytes_parsed, &ps); + assert(mbret != 0); + if (mbret == (size_t)-1 || mbret == (size_t)-2) + return -ERRNO_TO_PARA_ERROR(EILSEQ); + bytes_parsed += mbret; + cells_skipped += mutt_wcwidth(wc, cells_skipped); + } + *bytes_to_skip = bytes_parsed; + return 1; +} + +/** + * Compute the width of an UTF-8 string. + * + * \param s The string. + * \param result The width of \a s is returned here. + * + * If not in UTF8-mode. this function is just a wrapper for strlen(3). + * Otherwise \a s is treated as an UTF-8 string and its display width is + * computed. Note that this function may fail if the underlying call to + * mbsrtowcs(3) fails, so the caller must check the return value. + * + * \sa nl_langinfo(3), wcswidth(3). + * + * \return Standard. + */ +__must_check int strwidth(const char *s, size_t *result) +{ + const char *src = s; + mbstate_t state; + static wchar_t *dest; + size_t num_wchars; + + /* + * Never call any log function here. This may result in an endless loop + * as para_gui's para_log() calls this function. + */ + + if (!utf8_mode()) { + *result = strlen(s); + return 0; + } + memset(&state, 0, sizeof(state)); + *result = 0; + num_wchars = mbsrtowcs(NULL, &src, 0, &state); + if (num_wchars == (size_t)-1) + return -ERRNO_TO_PARA_ERROR(errno); + if (num_wchars == 0) + return 0; + dest = para_malloc(num_wchars * sizeof(*dest)); + src = s; + memset(&state, 0, sizeof(state)); + num_wchars = mbsrtowcs(dest, &src, num_wchars, &state); + assert(num_wchars > 0 && num_wchars != (size_t)-1); + *result = mutt_wcswidth(dest, num_wchars); + free(dest); + return 1; +} diff --git a/string.h b/string.h index d4627f33..d45c9199 100644 --- a/string.h +++ b/string.h @@ -91,3 +91,5 @@ void freep(void *arg); int compute_word_num(const char *buf, const char *delim, int offset); char *safe_strdup(const char *src, size_t len); char *key_value_copy(const char *src, size_t len, const char *key); +int skip_cells(const char *s, size_t cells_to_skip, size_t *result); +__must_check int strwidth(const char *s, size_t *result); diff --git a/web/header.html b/web/header.html index 464db042..f7cb2776 100644 --- a/web/header.html +++ b/web/header.html @@ -2,7 +2,7 @@ "http://www.w3.org/TR/html4/loose.dtd"> - + Paraslash diff --git a/web/header2.html b/web/header2.html index c8de7afd..94fbcad2 100644 --- a/web/header2.html +++ b/web/header2.html @@ -2,7 +2,7 @@ "http://www.w3.org/TR/html4/loose.dtd"> - + Paraslash diff --git a/wma.h b/wma.h index 33e34a35..f0ba7f63 100644 --- a/wma.h +++ b/wma.h @@ -25,7 +25,7 @@ struct asf_header_info { }; /* wma_common.c */ -int wma_log2(unsigned int v); +__a_const int wma_log2(unsigned int v); const char *search_pattern(const char *pattern, int pattern_len, const char *buf, int buf_size); int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi);