aboutsummaryrefslogtreecommitdiffstats
path: root/src/ytcpp.cpp
blob: 346f94e83a4ea701557b8f9c84b031926aa08e8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <stdlib.h>
#include <cstring>
#ifdef _WIN32
#include <windows.h>
#elif (__unix || __posix)
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
#endif

using namespace std;

void spawn(const char* url, bool wait) {

    #ifdef _WIN32

        start mpv
        bool CreateProcessA()
    #elif (__unix || __posix)

        extern char **environ;
        pid_t pid{0};
        char *const mpvArgs[] = {strdup("mpv"), strdup(url), nullptr};

        int result = posix_spawnp(&pid, mpvArgs[0], NULL, NULL, mpvArgs, environ);
        if (result != 0) { 

            cout << "ERROR RUNNING " << mpvArgs[0] << "! Error code " << result << ": " << strerror(result) << endl;
        } else {

            cout << "pid: " << pid << endl;
            if (wait) {

                do {
                    if (waitpid(pid, &result, 0) != -1) {

                        cout << "Child status: " << WEXITSTATUS(result) << endl;
                    } else {

                        perror("waitpid");
                        exit(1);
                    }
                } while (!WIFEXITED(result) && !WIFSIGNALED(result));
            }
        }
    #endif
}