[4932] | 1 | //rolestack.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2013 |
---|
[4932] | 5 | * |
---|
| 6 | * This file is part of roard a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
| 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "roard.h" |
---|
| 27 | |
---|
[4934] | 28 | #define STACKSIZE 16 |
---|
| 29 | |
---|
| 30 | static struct rolestack g_rolestack[STACKSIZE]; |
---|
| 31 | |
---|
| 32 | void rolestack_init(void) { |
---|
| 33 | int i; |
---|
| 34 | |
---|
| 35 | for (i = 0; i < STACKSIZE; i++) { |
---|
| 36 | g_rolestack[i].index = i; |
---|
| 37 | g_rolestack[i].role = ROAR_ROLE_UNKNOWN; |
---|
| 38 | g_rolestack[i].action = RS_MIX; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | g_rolestack[0].role = ROAR_ROLE_NONE; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void print_rolestack(void) { |
---|
| 45 | int i; |
---|
| 46 | |
---|
| 47 | printf("Index Role Action\n"); |
---|
| 48 | printf("-----------------------------\n"); |
---|
| 49 | for (i = 0; i < STACKSIZE && g_rolestack[i].role != ROAR_ROLE_UNKNOWN; i++) { |
---|
| 50 | printf("%-5i %-16s %s\n", g_rolestack[i].index, |
---|
| 51 | roar_role2str(g_rolestack[i].role), rolestack_action2str(g_rolestack[i].action)); |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | const struct rolestack * rolestack_get_role(int role) { |
---|
| 56 | int i; |
---|
| 57 | |
---|
| 58 | ROAR_DBG("rolestack_get_role(role=%i) = ?", role); |
---|
| 59 | |
---|
| 60 | if ( role == -1 ) |
---|
| 61 | return &(g_rolestack[0]); |
---|
| 62 | |
---|
| 63 | for (i = STACKSIZE - 1; i >= 0; i--) |
---|
| 64 | if ( g_rolestack[i].role == role ) |
---|
| 65 | return &(g_rolestack[i]); |
---|
| 66 | |
---|
| 67 | return &(g_rolestack[0]); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | int rolestack_push(const struct rolestack * role) { |
---|
| 71 | int i; |
---|
| 72 | |
---|
| 73 | for (i = 0; i < STACKSIZE; i++) { |
---|
| 74 | if ( g_rolestack[i].role == ROAR_ROLE_UNKNOWN ) { |
---|
| 75 | memcpy(&(g_rolestack[i]), role, sizeof(g_rolestack[i])); |
---|
| 76 | g_rolestack[i].index = i; |
---|
| 77 | return 0; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | roar_err_set(ROAR_ERROR_NOMEM); |
---|
| 82 | return -1; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | enum rs_action rolestack_str2action(const char * str) { |
---|
| 86 | if ( str == NULL ) { |
---|
| 87 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 88 | return -1; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if (!strcasecmp(str, "mix")) { |
---|
| 92 | return RS_MIX; |
---|
| 93 | } else if (!strcasecmp(str, "kick")) { |
---|
| 94 | return RS_KICK; |
---|
| 95 | } else if (!strcasecmp(str, "mute")) { |
---|
| 96 | return RS_MUTE; |
---|
| 97 | } else if (!strcasecmp(str, "pause")) { |
---|
| 98 | return RS_PAUSE; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | return RS_ERROR; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | const char * rolestack_action2str(enum rs_action action) { |
---|
| 105 | switch (action) { |
---|
| 106 | case RS_ERROR: return "(error)"; break; |
---|
| 107 | case RS_MIX: return "mix"; break; |
---|
| 108 | case RS_MUTE: return "mute"; break; |
---|
| 109 | case RS_PAUSE: return "pause"; break; |
---|
| 110 | case RS_KICK: return "kick"; break; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | return "(unknown)"; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | const struct rolestack * rolestack_parse(const char * str) { |
---|
| 117 | static struct rolestack ret; |
---|
| 118 | char * role, * action; |
---|
| 119 | |
---|
| 120 | if ( str == NULL ) { |
---|
| 121 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 122 | return NULL; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | role = roar_mm_strdup(str); |
---|
| 126 | if ( role == NULL ) |
---|
| 127 | return NULL; |
---|
| 128 | |
---|
| 129 | if ( (action = strstr(role, ":")) == NULL ) { |
---|
| 130 | roar_mm_free(role); |
---|
| 131 | roar_err_set(ROAR_ERROR_BADMSG); |
---|
| 132 | return NULL; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | *action = 0; |
---|
| 136 | action++; |
---|
| 137 | |
---|
| 138 | ret.index = -1; |
---|
| 139 | ret.role = roar_str2role(role); |
---|
| 140 | ret.action = rolestack_str2action(action); |
---|
| 141 | |
---|
| 142 | roar_mm_free(role); |
---|
| 143 | |
---|
| 144 | if ( ret.role == -1 || ret.action == RS_ERROR ) { |
---|
| 145 | roar_err_set(ROAR_ERROR_BADMSG); |
---|
| 146 | return NULL; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return &ret; |
---|
| 150 | } |
---|
| 151 | |
---|
[4932] | 152 | //ll |
---|