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 };
|
4
|
35 struct config config = {
|
0
|
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
|
4
|
74 char* details_message = 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);
|
4
|
77 assert(WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, title, -1, details_message, 256, NULL, NULL));
|
|
78 free(title);
|
1
|
79 }
|
4
|
80 strcpy(activity.details, details_message);
|
|
81 free(details_message);
|
0
|
82
|
|
83 app.activities->update_activity(app.activities, &activity, &app, update_activity_callback);
|
|
84 }
|
|
85
|
|
86 void report_idle_status(void)
|
|
87 {
|
|
88 activity.timestamps.start = 0;
|
|
89 strcpy(activity.state, "(Idle)");
|
|
90
|
|
91 app.activities->update_activity(app.activities, &activity, &app, update_activity_callback);
|
|
92 }
|
|
93
|
|
94 void update_rich_presence_details(void)
|
|
95 {
|
|
96 LONG isPlayingResult = SendMessageW(g_plugin.hwndParent, WM_WA_IPC, 0, IPC_ISPLAYING);
|
|
97
|
|
98 switch (isPlayingResult) {
|
|
99 case 0:
|
|
100 report_idle_status();
|
|
101 break;
|
4
|
102 case 1:
|
|
103 case 3:
|
|
104 report_current_song_status(isPlayingResult);
|
|
105 default:
|
|
106 break;
|
0
|
107 }
|
|
108 }
|
|
109
|
|
110 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|
111 {
|
|
112 if (message == WM_WA_IPC && lParam == IPC_CB_MISC && wParam == IPC_CB_MISC_STATUS)
|
|
113 {
|
|
114 update_rich_presence_details();
|
|
115 }
|
|
116
|
|
117 return CallWindowProc(g_lpWndProcOld, hwnd, message, wParam, lParam);
|
|
118 }
|
|
119
|
|
120 int init() {
|
|
121 memset(&app, 0, sizeof(app));
|
|
122 if (IsWindowUnicode(g_plugin.hwndParent)) {
|
|
123 g_lpWndProcOld = (WNDPROC)SetWindowLongW(g_plugin.hwndParent, -4, (LONG)WndProc);
|
1
|
124 } else {
|
0
|
125 g_lpWndProcOld = (WNDPROC)SetWindowLongA(g_plugin.hwndParent, -4, (LONG)WndProc);
|
|
126 }
|
|
127
|
|
128 memset(&activity, 0, sizeof(activity));
|
1
|
129 memset(&activities_events, 0, sizeof(activities_events));
|
0
|
130
|
|
131 struct DiscordCreateParams params;
|
|
132 DiscordCreateParamsSetDefault(¶ms);
|
|
133 params.client_id = CLIENT_ID;
|
|
134 params.flags = DiscordCreateFlags_Default;
|
|
135 params.event_data = &app;
|
|
136 params.activity_events = &activities_events;
|
|
137
|
|
138 DISCORD_REQUIRE(DiscordCreate(DISCORD_VERSION, ¶ms, &app.core));
|
|
139
|
|
140 app.activities = app.core->get_activity_manager(app.core);
|
|
141
|
|
142 timer_init(&timer_callbacks, g_plugin.hwndParent, TimerProc);
|
|
143 timer_set(&timer_callbacks, g_plugin.hwndParent);
|
|
144
|
|
145 cfg_load(&config);
|
|
146
|
|
147 strcpy(activity.assets.large_image, "winamp-logo");
|
|
148
|
|
149 update_rich_presence_details();
|
|
150
|
|
151 return 0;
|
|
152 }
|
|
153
|
|
154 void quit() {
|
|
155 assert(!cfg_save(config));
|
|
156 app.activities->clear_activity(app.activities, &app, update_activity_callback);
|
|
157 }
|
|
158
|
|
159 void CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD) {
|
|
160 DISCORD_REQUIRE(app.core->run_callbacks(app.core));
|
|
161 }
|
|
162
|
1
|
163 void conf() {
|
4
|
164 DialogBoxW(g_plugin.hDllInstance, (LPWSTR)DIALOG_CONFIG, g_plugin.hwndParent, (DLGPROC)cfg_win_proc);
|
1
|
165 }
|
|
166
|
0
|
167 __declspec(dllexport) winamp_general_purpose_plugin* winampGetGeneralPurposePlugin() {
|
|
168 return &g_plugin;
|
|
169 }
|