2 * Copyright 2022 Olaf Wintermann
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
28 #include <sys/fcntl.h>
35 extern char **environ;
37 #define WID_ARG_BUFSIZE 24
39 static void* start_player(void *data);
40 static void* player_io_thread(void *data);
42 void PlayerOpenFile(MainWindow *win) {
44 if(pthread_create(&tid, NULL, start_player, win)) {
45 perror("pthread_create");
49 static void* start_player(void *data) {
50 MainWindow *win = data;
52 char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings
54 // -wid parameter value for embedding the player in the player_widget
55 Window wid = XtWindow(win->player_widget);
56 char wid_arg[WID_ARG_BUFSIZE];
57 if(snprintf(wid_arg, WID_ARG_BUFSIZE, "%lu", wid) >= WID_ARG_BUFSIZE) {
61 // create player arg list
69 // redirect stdin/stdout
81 posix_spawn_file_actions_t actions;
82 posix_spawn_file_actions_init(&actions);
84 posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO);
85 posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO);
89 if(posix_spawn(&player_pid, player_bin, &actions, NULL, args, environ)) {
90 perror("posix_spawn");
93 posix_spawn_file_actions_destroy(&actions);
95 Player *player = malloc(sizeof(Player));
96 memset(player, 0, sizeof(Player));
99 player->out = pout[0];
102 player->process = player_pid;
103 player->isactive = TRUE;
106 PlayerDestroy(win->player);
108 win->player = player;
110 // start thread for mplayer IO
112 if(pthread_create(&tid, NULL, player_io_thread, player)) {
113 perror("pthread_create");
119 void PlayerDestroy(Player *p) {
123 kill(p->process, SIGTERM);
128 static void* player_io_thread(void *data) {
129 Player *player = data;