make UI tips context-dependent
[cycon.git] / launch.c
blob0c9cc4dc618cf392f07f78c11f63db8618db7c19
1 /*
2 * Copyright (c) 2019, De Rais <derais@cock.li>
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all
7 * copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <spawn.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
29 #include "cycon.h"
30 #include "macros.h"
32 extern char **environ;
34 void
35 launch_downloader(const char *uri)
37 pid_t dummy = 0;
38 posix_spawn_file_actions_t fa;
39 int kill_fa = 0;
40 posix_spawnattr_t sa;
41 int kill_sa = 0;
42 char *uridup = strdup(uri);
43 char *prog = strdup("youtube-dl");
44 char *quiet = strdup("--quiet");
45 char *no_warnings = strdup("--no-warnings");
46 char * const argv[5] = { prog, quiet, no_warnings, uridup, 0 };
48 if (!uridup ||
49 !prog ||
50 !quiet) {
51 goto done;
54 if (posix_spawn_file_actions_init(&fa)) {
55 PERROR_MESSAGE("posix_spawn_file_actions_init");
56 goto done;
59 kill_fa = 1;
61 if (posix_spawnattr_init(&sa)) {
62 PERROR_MESSAGE("posix_spawnattr_init");
63 goto done;
66 kill_sa = 1;
68 if (posix_spawn_file_actions_addclose(&fa, 0)) {
69 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
70 goto done;
73 if (posix_spawn_file_actions_addclose(&fa, 1)) {
74 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
75 goto done;
78 if (posix_spawn_file_actions_addclose(&fa, 2)) {
79 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
80 goto done;
83 if (posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSIGDEF)) {
84 PERROR_MESSAGE("posix_spawnattr_setflags");
85 goto done;
88 if (posix_spawn_file_actions_addopen(&fa, 0, "/dev/null", O_RDONLY,
89 0)) {
90 PERROR_MESSAGE("posix_spawn_file_actions_addopen");
91 goto done;
94 if (posix_spawn_file_actions_addopen(&fa, 1, "/dev/null", O_WRONLY,
95 0)) {
96 PERROR_MESSAGE("posix_spawn_file_actions_addopen");
97 goto done;
100 if (posix_spawn_file_actions_adddup2(&fa, 1, 2)) {
101 PERROR_MESSAGE("posix_spawn_file_actions_adddup2");
102 goto done;
105 posix_spawnp(&dummy, "youtube-dl", &fa, &sa, argv, environ);
106 done:
108 if (kill_fa) {
109 posix_spawn_file_actions_destroy(&fa);
112 if (kill_sa) {
113 posix_spawnattr_destroy(&sa);
116 free(uridup);
117 free(prog);
118 free(quiet);
119 free(no_warnings);
122 void
123 launch_player(const char *uri)
125 pid_t dummy = 0;
126 posix_spawn_file_actions_t fa;
127 int kill_fa = 0;
128 posix_spawnattr_t sa;
129 int kill_sa = 0;
130 char *uridup = strdup(uri);
131 char *prog = strdup("mpv");
132 char *quiet = strdup("--quiet");
133 char * const argv[4] = { prog, quiet, uridup, 0 };
135 if (!uridup ||
136 !prog ||
137 !quiet) {
138 goto done;
141 if (posix_spawn_file_actions_init(&fa)) {
142 PERROR_MESSAGE("posix_spawn_file_actions_init");
143 goto done;
146 kill_fa = 1;
148 if (posix_spawnattr_init(&sa)) {
149 PERROR_MESSAGE("posix_spawnattr_init");
150 goto done;
153 kill_sa = 1;
155 if (posix_spawn_file_actions_addclose(&fa, 0)) {
156 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
157 goto done;
160 if (posix_spawn_file_actions_addclose(&fa, 1)) {
161 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
162 goto done;
165 if (posix_spawn_file_actions_addclose(&fa, 2)) {
166 PERROR_MESSAGE("posix_spawn_file_actions_addclose");
167 goto done;
170 if (posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETSIGDEF)) {
171 PERROR_MESSAGE("posix_spawnattr_setflags");
172 goto done;
175 if (posix_spawn_file_actions_addopen(&fa, 0, "/dev/null", O_RDONLY,
176 0)) {
177 PERROR_MESSAGE("posix_spawn_file_actions_addopen");
178 goto done;
181 if (posix_spawn_file_actions_addopen(&fa, 1, "/dev/null", O_WRONLY,
182 0)) {
183 PERROR_MESSAGE("posix_spawn_file_actions_addopen");
184 goto done;
187 if (posix_spawn_file_actions_adddup2(&fa, 1, 2)) {
188 PERROR_MESSAGE("posix_spawn_file_actions_adddup2");
189 goto done;
192 posix_spawnp(&dummy, "mpv-with-load-screen", &fa, &sa, argv, environ);
193 done:
195 if (kill_fa) {
196 posix_spawn_file_actions_destroy(&fa);
199 if (kill_sa) {
200 posix_spawnattr_destroy(&sa);
203 free(uridup);
204 free(prog);
205 free(quiet);