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>
34 extern char **environ;
36 #define WID_ARG_BUFSIZE 24
38 static void* start_player(void *data);
40 void PlayerOpenFile(MainWindow *win) {
42 if(pthread_create(&tid, NULL, start_player, win)) {
43 perror("pthread_create");
47 static void* start_player(void *data) {
48 MainWindow *win = data;
50 char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings
52 // -wid parameter value for embedding the player in the player_widget
53 Window wid = XtWindow(win->player_widget);
54 char wid_arg[WID_ARG_BUFSIZE];
55 if(snprintf(wid_arg, WID_ARG_BUFSIZE, "%lu", wid) >= WID_ARG_BUFSIZE) {
59 // create player arg list
67 // redirect stdin/stdout
79 posix_spawn_file_actions_t actions;
80 posix_spawn_file_actions_init(&actions);
82 posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO);
83 posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO);
87 if(posix_spawn(&player_pid, player_bin, &actions, NULL, args, environ)) {
88 perror("posix_spawn");
91 posix_spawn_file_actions_destroy(&actions);
93 Player *player = malloc(sizeof(Player));
94 memset(player, 0, sizeof(Player));
97 player->out = pout[0];
100 player->process = player_pid;
103 PlayerDestroy(win->player);
105 win->player = player;
110 void PlayerDestroy(Player *p) {