summaryrefslogtreecommitdiff
path: root/Externals/discord-rpc/src/discord_register_linux.cpp
blob: dd92eea0d4a60f5ec58ae174c58c6f0757081941 (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
#include "discord_rpc.h"
#include "discord_register.h"
#include <stdio.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

static bool Mkdir(const char* path)
{
    int result = mkdir(path, 0755);
    if (result == 0) {
        return true;
    }
    if (errno == EEXIST) {
        return true;
    }
    return false;
}

// we want to register games so we can run them from Discord client as discord-<appid>://
extern "C" DISCORD_EXPORT void Discord_Register(const char* applicationId, const char* command)
{
    // Add a desktop file and update some mime handlers so that xdg-open does the right thing.

    const char* home = getenv("HOME");
    if (!home) {
        return;
    }

    char exePath[1024];
    if (!command || !command[0]) {
        ssize_t size = readlink("/proc/self/exe", exePath, sizeof(exePath));
        if (size <= 0 || size >= (ssize_t)sizeof(exePath)) {
            return;
        }
        exePath[size] = '\0';
        command = exePath;
    }

    const char* desktopFileFormat = "[Desktop Entry]\n"
                                   "Name=Game %s\n"
                                   "Exec=%s %%u\n" // note: it really wants that %u in there
                                   "Type=Application\n"
                                   "NoDisplay=true\n"
                                   "Categories=Discord;Games;\n"
                                   "MimeType=x-scheme-handler/discord-%s;\n";
    char desktopFile[2048];
    int fileLen = snprintf(
      desktopFile, sizeof(desktopFile), desktopFileFormat, applicationId, command, applicationId);
    if (fileLen <= 0) {
        return;
    }

    char desktopFilename[256];
    snprintf(desktopFilename, sizeof(desktopFilename), "/discord-%s.desktop", applicationId);

    char desktopFilePath[1024];
    snprintf(desktopFilePath, sizeof(desktopFilePath), "%s/.local", home);
    if (!Mkdir(desktopFilePath)) {
        return;
    }
    strcat(desktopFilePath, "/share");
    if (!Mkdir(desktopFilePath)) {
        return;
    }
    strcat(desktopFilePath, "/applications");
    if (!Mkdir(desktopFilePath)) {
        return;
    }
    strcat(desktopFilePath, desktopFilename);

    FILE* fp = fopen(desktopFilePath, "w");
    if (fp) {
        fwrite(desktopFile, 1, fileLen, fp);
        fclose(fp);
    }
    else {
        return;
    }

    char xdgMimeCommand[1024];
    snprintf(xdgMimeCommand,
             sizeof(xdgMimeCommand),
             "xdg-mime default discord-%s.desktop x-scheme-handler/discord-%s",
             applicationId,
             applicationId);
    if (system(xdgMimeCommand) < 0) {
        fprintf(stderr, "Failed to register mime handler\n");
    }
}

extern "C" DISCORD_EXPORT void Discord_RegisterSteamGame(const char* applicationId,
                                                         const char* steamId)
{
    char command[256];
    sprintf(command, "xdg-open steam://rungameid/%s", steamId);
    Discord_Register(applicationId, command);
}