queue.c (1885B)
1 /* 2 * Copyright 2021-2023 Jacob R. Edwards 3 * 4 * ap -- audio player 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <https://www.gnu.org/licenses/>. 18 */ 19 20 #include <sys/socket.h> 21 22 #include <limits.h> 23 #include <poll.h> 24 #include <stddef.h> 25 26 #include "aps.h" 27 #include "log.h" 28 29 void 30 queue_set(struct aps *aps, struct item *item) 31 { 32 aps->queue = item; 33 aps_log(aps, 0, INFO, logsubjects[LogQueue], "set '%s'", item ? item->path : ""); 34 35 if (aps->player->state != OFF && aps->player->state != STOPPED) { 36 pstop(aps->player); 37 aps->player->state = OFF; 38 } 39 } 40 41 struct item * 42 queue_add(struct aps *aps, struct item *at, char *s) 43 { 44 struct item *item; 45 46 aps_log(aps, 0, INFO, logsubjects[LogQueue], "add '%s'", s); 47 48 item = item_new(s); 49 if (item == NULL) { 50 aps_log(aps, 0, ERROR, logsubjects[LogQueue], "unable to create queue item"); 51 return NULL; 52 } 53 54 if (aps->queue == NULL) 55 queue_set(aps, item); 56 else 57 item_append(at ? at : aps->queue, item); 58 return item; 59 } 60 61 struct item * 62 queue_remove(struct aps *aps, struct item *item) 63 { 64 struct item *next; 65 66 if (item == NULL) 67 return NULL; 68 69 aps_log(aps, 0, INFO, logsubjects[LogQueue], "remove '%s'", item->path); 70 71 next = item->next; 72 if (next == item) 73 next = NULL; 74 if (aps->queue == item) 75 queue_set(aps, next); 76 77 item_unlink(item); 78 item_free(item); 79 return next; 80 }