/* * Copyright 2022 Olaf Wintermann * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include "player.h" #include #include #include #include #include #include #include extern char **environ; #define WID_ARG_BUFSIZE 24 static void* start_player(void *data); void PlayerOpenFile(MainWindow *win) { pthread_t tid; if(pthread_create(&tid, NULL, start_player, win)) { perror("pthread_create"); } } static void* start_player(void *data) { MainWindow *win = data; char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings // -wid parameter value for embedding the player in the player_widget Window wid = XtWindow(win->player_widget); char wid_arg[WID_ARG_BUFSIZE]; if(snprintf(wid_arg, WID_ARG_BUFSIZE, "%lu", wid) >= WID_ARG_BUFSIZE) { return NULL; } // create player arg list char *args[32]; args[0] = player_bin; args[1] = "-wid"; args[2] = wid_arg; args[3] = win->file; args[4] = NULL; // redirect stdin/stdout int pout[2]; int pin[2]; if(pipe(pout)) { perror("pipe"); return NULL; } if(pipe(pin)) { perror("pipe"); return NULL; } posix_spawn_file_actions_t actions; posix_spawn_file_actions_init(&actions); posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO); posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO); // start player pid_t player_pid; if(posix_spawn(&player_pid, player_bin, &actions, NULL, args, environ)) { perror("posix_spawn"); return NULL; } posix_spawn_file_actions_destroy(&actions); Player *player = malloc(sizeof(Player)); memset(player, 0, sizeof(Player)); player->in = pin[1]; player->out = pout[0]; close(pin[0]); close(pout[1]); player->process = player_pid; if(win->player) { PlayerDestroy(win->player); } win->player = player; return NULL; } void PlayerDestroy(Player *p) { free(p); }