fix github issue 361 to make user_sounds useful even if MSGTYPE is hidden
fixes #361 Also, experminental introduction of vt_sounddata to enable tty to pass a sound file index to the terminal side of things where perhaps someone can add code to something like hterm to take the information relayed by NetHack to trigger user_sounds locally even if playing on a server. Compile time option TTY_SOUND_ESCCODES required to build that support in. It should be independent of TTY_TILE_ESCCODES.
This commit is contained in:
10
src/pline.c
10
src/pline.c
@@ -15,7 +15,9 @@ static char *FDECL(You_buf, (int));
|
||||
#if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__))
|
||||
static void FDECL(execplinehandler, (const char *));
|
||||
#endif
|
||||
|
||||
#ifdef USER_SOUNDS
|
||||
extern void FDECL(maybe_play_sound, (const char *));
|
||||
#endif
|
||||
#ifdef DUMPLOG
|
||||
|
||||
/* keep the most recent DUMPLOG_MSG_COUNT messages */
|
||||
@@ -163,6 +165,7 @@ VA_DECL(const char *, line)
|
||||
pbuf[BUFSZ - 1] = '\0';
|
||||
line = pbuf;
|
||||
}
|
||||
msgtyp = MSGTYP_NORMAL;
|
||||
|
||||
#ifdef DUMPLOG
|
||||
/* We hook here early to have options-agnostic output.
|
||||
@@ -182,10 +185,13 @@ VA_DECL(const char *, line)
|
||||
goto pline_done;
|
||||
}
|
||||
|
||||
msgtyp = MSGTYP_NORMAL;
|
||||
no_repeat = (g.pline_flags & PLINE_NOREPEAT) ? TRUE : FALSE;
|
||||
if ((g.pline_flags & OVERRIDE_MSGTYPE) == 0) {
|
||||
msgtyp = msgtype_type(line, no_repeat);
|
||||
#ifdef USER_SOUNDS
|
||||
if (msgtyp == MSGTYP_NORMAL || msgtyp == MSGTYP_NOSHOW)
|
||||
maybe_play_sound(line);
|
||||
#endif
|
||||
if ((g.pline_flags & URGENT_MESSAGE) == 0
|
||||
&& (msgtyp == MSGTYP_NOSHOW
|
||||
|| (msgtyp == MSGTYP_NOREP && !strcmp(line, g.prevmsg))))
|
||||
|
||||
72
src/sounds.c
72
src/sounds.c
@@ -1258,16 +1258,23 @@ tiphat()
|
||||
|
||||
#ifdef USER_SOUNDS
|
||||
|
||||
#if defined(WIN32) || defined(QT_GRAPHICS)
|
||||
extern void FDECL(play_usersound, (const char *, int));
|
||||
#endif
|
||||
#if defined(TTY_SOUND_ESCCODES)
|
||||
extern void FDECL(play_usersound_via_idx, (int, int));
|
||||
#endif
|
||||
|
||||
typedef struct audio_mapping_rec {
|
||||
struct nhregex *regex;
|
||||
char *filename;
|
||||
int volume;
|
||||
int idx;
|
||||
struct audio_mapping_rec *next;
|
||||
} audio_mapping;
|
||||
|
||||
static audio_mapping *soundmap = 0;
|
||||
static audio_mapping *FDECL(sound_matches_message, (const char *));
|
||||
|
||||
char *sounddir = 0; /* set in files.c */
|
||||
|
||||
@@ -1279,26 +1286,29 @@ const char *mapping;
|
||||
char text[256];
|
||||
char filename[256];
|
||||
char filespec[256];
|
||||
int volume;
|
||||
int volume, idx = -1;
|
||||
boolean toolong = FALSE;
|
||||
|
||||
if (sscanf(mapping, "MESG \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d", text,
|
||||
if (sscanf(mapping, "MESG \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d %d", text,
|
||||
filename, &volume, &idx) == 4
|
||||
|| sscanf(mapping, "MESG \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d", text,
|
||||
filename, &volume) == 3) {
|
||||
audio_mapping *new_map;
|
||||
|
||||
if (!sounddir)
|
||||
sounddir = dupstr(".");
|
||||
|
||||
if (strlen(sounddir) + 1 + strlen(filename) >= sizeof filespec) {
|
||||
raw_print("sound file name too long");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Sprintf(filespec, "%s/%s", sounddir, filename);
|
||||
|
||||
if (can_read_file(filespec)) {
|
||||
if (idx >= 0 || can_read_file(filespec)) {
|
||||
new_map = (audio_mapping *) alloc(sizeof *new_map);
|
||||
new_map->regex = regex_init();
|
||||
new_map->filename = dupstr(filespec);
|
||||
new_map->volume = volume;
|
||||
new_map->idx = idx;
|
||||
new_map->next = soundmap;
|
||||
|
||||
if (!regex_compile(text, new_map->regex)) {
|
||||
@@ -1325,18 +1335,56 @@ const char *mapping;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static audio_mapping *
|
||||
sound_matches_message(msg)
|
||||
const char *msg;
|
||||
{
|
||||
audio_mapping *snd = soundmap;
|
||||
|
||||
while (snd) {
|
||||
if (regex_match(msg, snd->regex))
|
||||
return snd;
|
||||
snd = snd->next;
|
||||
}
|
||||
return (audio_mapping *) 0;
|
||||
}
|
||||
|
||||
void
|
||||
play_sound_for_message(msg)
|
||||
const char *msg;
|
||||
{
|
||||
audio_mapping *cursor = soundmap;
|
||||
audio_mapping *snd = sound_matches_message(msg);
|
||||
|
||||
while (cursor) {
|
||||
if (regex_match(msg, cursor->regex)) {
|
||||
play_usersound(cursor->filename, cursor->volume);
|
||||
}
|
||||
cursor = cursor->next;
|
||||
}
|
||||
if (snd)
|
||||
play_usersound(snd->filename, snd->volume);
|
||||
}
|
||||
|
||||
void
|
||||
maybe_play_sound(msg)
|
||||
const char *msg;
|
||||
{
|
||||
#if defined(WIN32) || defined(QT_GRAPHICS) || defined(TTY_SOUND_ESCCODES)
|
||||
audio_mapping *snd = sound_matches_message(msg);
|
||||
|
||||
if (snd
|
||||
#if defined(WIN32) || defined(QT_GRAPHICS)
|
||||
#ifdef TTY_SOUND_ESCCODES
|
||||
&& !iflags.vt_sounddata
|
||||
#endif
|
||||
#if defined(QT_GRAPHICS)
|
||||
&& WINDOWPORT("Qt")
|
||||
#endif
|
||||
#if defined(WIN32)
|
||||
&& (WINDOWPORT("tty") || WINDOWPORT("mswin") || WINDOWPORT("curses"))
|
||||
#endif
|
||||
#endif /* WIN32 || QT_GRAPHICS */
|
||||
)
|
||||
play_usersound(snd->filename, snd->volume);
|
||||
#if defined(TTY_GRAPHICS) && defined(TTY_SOUND_ESCCODES)
|
||||
else if (snd && iflags.vt_sounddata && snd->idx >= 0 && WINDOWPORT("tty"))
|
||||
play_usersound_via_idx(snd->idx, snd->volume);
|
||||
#endif /* TTY_GRAPHICS && TTY_SOUND_ESCCODES */
|
||||
#endif /* WIN32 || QT_GRAPHICS || TTY_SOUND_ESCCODES */
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user