[1255] | 1 | //vio_cmd.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroar 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 | * libroar 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, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "libroar.h" |
---|
| 36 | |
---|
| 37 | int roar_vio_open_cmd(struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
| 38 | int flags, char * reader, char * writer, int options) { |
---|
| 39 | struct roar_vio_cmd_state * state; |
---|
| 40 | |
---|
| 41 | if ( calls == NULL || dst == NULL ) |
---|
| 42 | return -1; |
---|
| 43 | |
---|
| 44 | if ( flags == 0 ) |
---|
| 45 | return -1; |
---|
| 46 | |
---|
| 47 | if ( reader == NULL && writer == NULL ) |
---|
| 48 | return -1; |
---|
| 49 | |
---|
| 50 | if ( (state = malloc(sizeof(struct roar_vio_cmd_state))) == NULL ) |
---|
| 51 | return -1; |
---|
| 52 | |
---|
| 53 | // clear all |
---|
| 54 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 55 | memset(state, 0, sizeof(struct roar_vio_cmd_state)); |
---|
| 56 | |
---|
| 57 | // init reader and writer: |
---|
| 58 | state->reader.pid = -1; |
---|
| 59 | state->reader.in = -1; |
---|
| 60 | state->reader.out = -1; |
---|
| 61 | |
---|
| 62 | if ( reader != NULL ) |
---|
| 63 | state->reader.cmd = strdup(reader); |
---|
| 64 | |
---|
| 65 | state->writer.pid = -1; |
---|
| 66 | state->writer.in = -1; |
---|
| 67 | state->writer.out = -1; |
---|
| 68 | |
---|
| 69 | if ( writer != NULL ) |
---|
| 70 | state->writer.cmd = strdup(writer); |
---|
| 71 | |
---|
| 72 | // init state |
---|
| 73 | state->next = dst; |
---|
| 74 | state->flags = flags; |
---|
| 75 | state->options = options; |
---|
| 76 | |
---|
| 77 | // init calls |
---|
| 78 | calls->inst = (void*) state; |
---|
| 79 | |
---|
| 80 | if ( reader != NULL ) |
---|
| 81 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
| 82 | return roar_vio_cmd_close(calls); |
---|
| 83 | |
---|
| 84 | if ( writer != NULL ) |
---|
| 85 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
| 86 | return roar_vio_cmd_close(calls); |
---|
| 87 | |
---|
| 88 | return 0; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | int roar_vio_cmd_close(struct roar_vio_calls * vio) { |
---|
| 92 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 93 | |
---|
| 94 | if ( state->reader.opened ) |
---|
| 95 | roar_vio_cmd_wait(&(state->reader)); |
---|
| 96 | |
---|
| 97 | if ( state->writer.opened ) |
---|
| 98 | roar_vio_cmd_wait(&(state->writer)); |
---|
| 99 | |
---|
| 100 | if ( state->reader.cmd != NULL ) |
---|
| 101 | free(state->reader.cmd); |
---|
| 102 | |
---|
| 103 | if ( state->writer.cmd != NULL ) |
---|
| 104 | free(state->writer.cmd); |
---|
| 105 | |
---|
| 106 | roar_vio_close(state->next); |
---|
| 107 | |
---|
| 108 | free(state); |
---|
| 109 | |
---|
| 110 | return 0; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | int roar_vio_cmd_fork(struct roar_vio_cmd_child * child) { |
---|
| 114 | return -1; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | int roar_vio_cmd_wait(struct roar_vio_cmd_child * child) { |
---|
| 118 | int status; |
---|
| 119 | |
---|
| 120 | if ( child == NULL ) |
---|
| 121 | return -1; |
---|
| 122 | |
---|
| 123 | if ( !child->opened ) |
---|
| 124 | return 0; |
---|
| 125 | |
---|
| 126 | if ( child->out != -1 ) |
---|
| 127 | close(child->out); |
---|
| 128 | |
---|
| 129 | if ( child->in != -1 ) |
---|
| 130 | close(child->in); |
---|
| 131 | |
---|
| 132 | waitpid(child->pid, &status, 0); |
---|
| 133 | |
---|
| 134 | return 0; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | //ll |
---|