7
|
1 /**
|
|
2 * wgsdk - Winamp plugin for Discord's GameSDK
|
|
3 **/
|
0
|
4 #include "timer.h"
|
|
5 #include "config.h"
|
1
|
6 #include "resource.h"
|
7
|
7 #include "utils.h"
|
10
|
8
|
|
9 #include <Winamp/wa_ipc.h>
|
|
10 #include "discord_game_sdk.h"
|
|
11
|
|
12 #include <assert.h>
|
|
13 #include <stdio.h>
|
|
14 #include <stdlib.h>
|
0
|
15
|
|
16 #define CLIENT_ID 969367220599803955
|
|
17 #define DISCORD_REQUIRE(x) \
|
10
|
18 assert((x) == DiscordResult_Ok)
|
0
|
19
|
|
20 #define GPPHDR_VER 0x10
|
|
21
|
7
|
22 int init();
|
|
23 void conf();
|
|
24 void quit();
|
|
25
|
|
26 /* Winamp-specific stuff */
|
|
27 struct winamp_gpp {
|
|
28 int version; // version of the plugin structure
|
|
29 char *description; // name/title of the plugin
|
|
30 int(*init)(); // function which will be executed on init event
|
|
31 void(*conf)(); // function which will be executed on config event
|
|
32 void(*quit)(); // function which will be executed on quit event
|
|
33 HWND hwndParent; // hwnd of the Winamp client main window (stored by Winamp when dll is loaded)
|
|
34 HINSTANCE hDllInstance; // hinstance of this plugin DLL. (stored by Winamp when dll is loaded)
|
|
35 };
|
|
36
|
|
37 struct winamp_gpp g_plugin = {
|
1
|
38 GPPHDR_VER, // version of the plugin, defined in "gen_myplugin.h"
|
|
39 "Discord GameSDK", // name/title of the plugin, defined in "gen_myplugin.h"
|
|
40 init, // function name which will be executed on init event
|
|
41 conf, // function name which will be executed on config event
|
|
42 quit, // function name which will be executed on quit event
|
|
43 0, // handle to Winamp main window, loaded by winamp when this dll is loaded
|
|
44 0 // hinstance to this dll, loaded by winamp when this dll is loaded
|
0
|
45 };
|
|
46
|
7
|
47 /* Discord stuff */
|
0
|
48
|
|
49 struct DiscordActivity activity = {
|
|
50 .application_id = CLIENT_ID,
|
|
51 .name = "Winamp",
|
|
52 .instance = 0
|
|
53 };
|
|
54
|
|
55 struct IDiscordActivityEvents activities_events;
|
|
56
|
10
|
57 struct {
|
7
|
58 struct IDiscordCore* core;
|
|
59 struct IDiscordUsers* users;
|
|
60 struct IDiscordActivityManager* activities;
|
|
61 } app;
|
|
62
|
|
63 /* Now we get to our built-in stuff */
|
|
64
|
10
|
65 struct timer timer_callbacks = {0};
|
0
|
66
|
7
|
67 struct config config = {
|
|
68 .display_title = 1,
|
|
69 .show_elapsed_time = 1
|
|
70 };
|
|
71
|
|
72 /* CallWindowProc is the *only* function that ever needs this. */
|
|
73 WNDPROC _old_wnd_proc = 0;
|
|
74
|
|
75 /* ------------------------------------ */
|
|
76
|
|
77 void DISCORD_CALLBACK update_activity_callback(void* data, enum EDiscordResult result) {
|
1
|
78 DISCORD_REQUIRE(result);
|
0
|
79 }
|
|
80
|
7
|
81 void report_current_song_status(int playback_state) {
|
|
82 assert(playback_state != 0);
|
|
83 strcpy(activity.state, playback_state == 1 ? "(Playing)" : "(Paused)");
|
0
|
84
|
7
|
85 activity.timestamps.start = (playback_state == 1 && config.show_elapsed_time)
|
|
86 ? get_system_time_in_milliseconds() - SendMessage(g_plugin.hwndParent, WM_WA_IPC, 0, IPC_GETOUTPUTTIME) / 1000
|
|
87 : 0;
|
1
|
88
|
|
89 if (config.display_title) {
|
0
|
90 wchar_t* title = (wchar_t*)SendMessageW(g_plugin.hwndParent, WM_WA_IPC, 0, IPC_GET_PLAYING_TITLE);
|
7
|
91 assert(WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, title, -1, activity.details, 256, NULL, NULL));
|
1
|
92 }
|
0
|
93
|
|
94 app.activities->update_activity(app.activities, &activity, &app, update_activity_callback);
|
|
95 }
|
|
96
|
|
97 void report_idle_status(void)
|
|
98 {
|
|
99 activity.timestamps.start = 0;
|
|
100 strcpy(activity.state, "(Idle)");
|
|
101
|
|
102 app.activities->update_activity(app.activities, &activity, &app, update_activity_callback);
|
|
103 }
|
|
104
|
|
105 void update_rich_presence_details(void)
|
|
106 {
|
7
|
107 LONG is_playing = SendMessageW(g_plugin.hwndParent, WM_WA_IPC, 0, IPC_ISPLAYING);
|
0
|
108
|
7
|
109 switch (is_playing) {
|
0
|
110 case 0:
|
|
111 report_idle_status();
|
|
112 break;
|
4
|
113 case 1:
|
|
114 case 3:
|
7
|
115 report_current_song_status(is_playing);
|
10
|
116 break;
|
4
|
117 default:
|
|
118 break;
|
0
|
119 }
|
|
120 }
|
|
121
|
7
|
122 void CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD) {
|
|
123 DISCORD_REQUIRE(app.core->run_callbacks(app.core));
|
|
124 }
|
|
125
|
|
126 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
|
0
|
127 if (message == WM_WA_IPC && lParam == IPC_CB_MISC && wParam == IPC_CB_MISC_STATUS)
|
|
128 update_rich_presence_details();
|
|
129
|
7
|
130 return CallWindowProc(_old_wnd_proc, hwnd, message, wParam, lParam);
|
0
|
131 }
|
|
132
|
7
|
133 #define set_wnd_long(x) \
|
|
134 (WNDPROC)SetWindowLong##x(g_plugin.hwndParent, GWLP_WNDPROC, (LONG)WndProc)
|
0
|
135 int init() {
|
|
136 memset(&app, 0, sizeof(app));
|
|
137 memset(&activity, 0, sizeof(activity));
|
1
|
138 memset(&activities_events, 0, sizeof(activities_events));
|
0
|
139
|
7
|
140 if (IsWindowUnicode(g_plugin.hwndParent))
|
|
141 _old_wnd_proc = set_wnd_long(W);
|
|
142 else
|
|
143 _old_wnd_proc = set_wnd_long(A);
|
|
144
|
0
|
145 struct DiscordCreateParams params;
|
|
146 DiscordCreateParamsSetDefault(¶ms);
|
|
147 params.client_id = CLIENT_ID;
|
|
148 params.flags = DiscordCreateFlags_Default;
|
|
149 params.event_data = &app;
|
|
150 params.activity_events = &activities_events;
|
|
151
|
|
152 DISCORD_REQUIRE(DiscordCreate(DISCORD_VERSION, ¶ms, &app.core));
|
|
153
|
|
154 app.activities = app.core->get_activity_manager(app.core);
|
|
155
|
10
|
156 timer_init(&timer_callbacks, 16, TimerProc);
|
|
157 timer_set(&timer_callbacks);
|
0
|
158
|
|
159 cfg_load(&config);
|
|
160
|
|
161 strcpy(activity.assets.large_image, "winamp-logo");
|
|
162
|
|
163 update_rich_presence_details();
|
|
164
|
|
165 return 0;
|
|
166 }
|
7
|
167 #undef set_wnd_long
|
0
|
168
|
|
169 void quit() {
|
|
170 assert(!cfg_save(config));
|
|
171 app.activities->clear_activity(app.activities, &app, update_activity_callback);
|
|
172 }
|
|
173
|
1
|
174 void conf() {
|
4
|
175 DialogBoxW(g_plugin.hDllInstance, (LPWSTR)DIALOG_CONFIG, g_plugin.hwndParent, (DLGPROC)cfg_win_proc);
|
1
|
176 }
|
|
177
|
7
|
178 __declspec(dllexport) struct winamp_gpp* winampGetGeneralPurposePlugin() {
|
0
|
179 return &g_plugin;
|
|
180 }
|