aboutsummaryrefslogtreecommitdiff
path: root/src/wine.c
blob: c6f445ef54ce35a053b3805ddf4efc1e3b6d5fab (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <json.h>
#include <libgen.h>
#include <unistd.h>
#include <linux/limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

#include "wine.h"
#include "net.h"
#include "tar.h"
#include "common.h"
#include "config.h"


const static struct Command wine_commands[] = {
    { .name = "download",       .func = wine_download,  .description = "download and extract a wine version from lutris" },
    { .name = "list",           .func = wine_list,      .description = "list installable wine versions" },
    { .name = "run",            .func = wine_run,       .description = "run a installed wine version" },
    { .name = "installed",      .func = wine_installed, .description = "list installed wine versions" },
};

int wine(int argc, char** argv)
{
    if (argc > 1)
    {
        for (int i = 0; i < ARRAY_LEN(wine_commands); ++i)
        {
            if (!strcmp(wine_commands[i].name, argv[1])) return wine_commands[i].func(argc-1, argv+1);
        }
    } 


    return wine_help(argc, argv);
}

int wine_download(int argc, char** argv)
{
    if (argc == 2)
    {
        struct json_object* runner = fetchJSON(WINE_API);

        if (runner)
        {
            struct json_object* versions;
            json_object_object_get_ex(runner, "versions", &versions);

            int choice = atoi(argv[1]);

            if (choice > json_object_array_length(versions) - 1 || choice < 0)
            {
                printf("`%i' is not a valid ID\n\nrun `polecat wine list' to get a valid ID\n", choice);
            }
            else
            {
                struct json_object* url;
                struct json_object* value = json_object_array_get_idx(versions, choice);

                json_object_object_get_ex(value, "url", &url);
                char* name = basename((char*)json_object_get_string(url));

                char datadir[PATH_MAX];
                char downloadpath[PATH_MAX];

                getDataDir(datadir);
                makeDir(datadir);

                strcpy(downloadpath, datadir);
                strcat(downloadpath, "/");
                strcat(downloadpath, name);
                
                printf("Downloading %s\n", name);
                downloadFile(json_object_get_string(url), downloadpath);
                printf("Extracting %s\n", name);
                extract(downloadpath, datadir);
                printf("Done\n");
            }

            json_object_put(runner);
        }
    }
    else
    {
        puts(USAGE_STR " wine download <ID>\n\nIDs are obtained via `" NAME " wine list' ");
    }
    return 0;
}

int wine_list(int argc, char** argv)
{
    struct json_object* runner = fetchJSON(WINE_API);

    if (runner)
    {
        struct json_object* versions, *value, *val;
        json_object_object_get_ex(runner, "versions", &versions);

        puts("Installable wine versions:");

        for (size_t i = 0; i < json_object_array_length(versions); ++i)
        {
            value = json_object_array_get_idx(versions, i);
            json_object_object_get_ex(value, "version", &val);
            printf(" [%zu]\t%s\n", i, json_object_get_string(val));
        }

        json_object_put(runner);
    }

    return 0;
}

int wine_run(int argc, char** argv)
{
    if (argc > 1)
    {
        char winepath[PATH_MAX];
        getDataDir(winepath);
        char* winever = argv[1];

        strcat(winepath, "/");
        strcat(winepath, winever);
        strcat(winepath, "/bin/wine");

        for (int i = 2; i < argc; ++i)
        {
            strcat(winepath, " ");
            strcat(winepath, argv[i]);
        }

        return system(winepath);
    }

    printf("Specify a what wine version to run.\nUse `" NAME " wine list-installed' to list available versions\n");
        
    return 0;
}

int wine_installed(int argc, char** argv)
{
    char datadir[PATH_MAX];
    getDataDir(datadir);

    DIR *dir;
    struct dirent *ent;

    printf("Installed wine versions:\n");
    if ((dir = opendir(datadir)) != NULL)
    {
        while ((ent = readdir (dir)) != NULL)
        {
            /*
             * WARNING: crusty
             * d_type is only specified on glibc (including musl) and BSD
             */
            if (ent->d_name[0] != '.' && ent->d_type == DT_DIR)
            {
                printf(" - %s\n", ent->d_name);
            }
        }
        closedir (dir);
    } 

    return 0;
}

int wine_help(int argc, char** argv)
{
    puts(USAGE_STR " wine <command>\n\nList of commands:");

    print_help(wine_commands, ARRAY_LEN(wine_commands));

    return 0;
}