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