205 lines
6.4 KiB
C++
205 lines
6.4 KiB
C++
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <regex>
|
|
#include <future>
|
|
//#define _WIN32_WINNT 0x0500
|
|
#include <windows.h>
|
|
#define APPWM_ICONNOTIFY (WM_APP + 1)
|
|
#include <conio.h>
|
|
#include "../wt95.rc"
|
|
#include <shellapi.h>
|
|
#include <commctrl.h>
|
|
#include <curl/curl.h>
|
|
#include "SimpleIni.h"
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
#include "stb_image.h"
|
|
#include "stb_image_write.h"
|
|
#define IDT_TIMER1 20
|
|
CURL *curl;
|
|
CURLcode res;
|
|
std::string old_response = "Dummy";
|
|
std::string walltaker_id = "";
|
|
|
|
int imgconvert(std::string filepath){
|
|
|
|
int width;
|
|
int height;
|
|
int channels;
|
|
stbi_uc *image = stbi_load(filepath.c_str(), &width, &height, &channels, 3);
|
|
if (image == NULL) {
|
|
printf("Failed to load image!\n");
|
|
return 1;
|
|
}
|
|
|
|
int bmpWidth = width;
|
|
int bmpHeight = height;
|
|
int bpp = 24; // 24-bit color depth
|
|
stbi_uc *bmpImage = (stbi_uc *) malloc(bmpWidth * bmpHeight * bpp);
|
|
|
|
stbi_write_bmp("output.bmp", bmpWidth, bmpHeight, 3, image);
|
|
|
|
std::remove(filepath.c_str()); // Delete original file once we're done with it to save space
|
|
|
|
free(bmpImage);
|
|
stbi_image_free(image);
|
|
return 0;
|
|
}
|
|
|
|
size_t getAnswerFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
|
|
data->append((char*)ptr, size * nmemb);
|
|
return size * nmemb;
|
|
}
|
|
|
|
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream){
|
|
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
|
|
return written;
|
|
}
|
|
|
|
int LoadConfig(){
|
|
CSimpleIniA ini;
|
|
ini.SetUnicode();
|
|
ini.LoadFile("wt95.ini");
|
|
const char* pVal = ini.GetValue("Walltaker95", "LinkId", NULL);
|
|
if (pVal == NULL){
|
|
printf("Error loading config file, using default link ID\n");
|
|
walltaker_id = "40573";
|
|
} else {
|
|
walltaker_id = pVal;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int UpdateWallpaper(){
|
|
|
|
std::string response_string;
|
|
FILE *img;
|
|
std::string myurl = "https://walltaker.joi.how/api/links/" + walltaker_id + ".json";
|
|
//std::cout << myurl;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, myurl.c_str());
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); // set HTTP method to GET
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/8.13.0");
|
|
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getAnswerFunction);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
|
|
res = curl_easy_perform(curl);
|
|
//std::cout << response_string << "\n";
|
|
|
|
// Who needs parsing when you have regex?
|
|
std::regex rgx("\"post_url\":[\"]((?=(\\\\?))\\2.*?)[\"]");
|
|
std::smatch match;
|
|
std::regex_search(response_string, match, rgx);
|
|
std::string esixurl = match[0];
|
|
esixurl.erase(0, 12);
|
|
esixurl.erase(esixurl.size()-1);
|
|
|
|
std::string fileext = esixurl;
|
|
fileext.erase(0, fileext.size()-4);
|
|
std::string fname = "tmp" + fileext;
|
|
|
|
if (!old_response.compare(esixurl)){
|
|
return 0; // We already got this wallpaper before, skip it.
|
|
}
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, esixurl.c_str());
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
|
img = fopen(fname.c_str(), "wb");
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, img);
|
|
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
|
curl_easy_perform(curl);
|
|
old_response = esixurl;
|
|
fclose(img);
|
|
|
|
imgconvert(fname);
|
|
|
|
// Open the registry key for writing
|
|
HKEY hKey = NULL;
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_SET_VALUE, &hKey) != ERROR_SUCCESS) {
|
|
printf("Error: Could not open registry key for writing.\n");
|
|
return -1;
|
|
}
|
|
|
|
char fullFilename[MAX_PATH];
|
|
GetFullPathName("output.bmp", MAX_PATH, fullFilename, nullptr);
|
|
|
|
if (RegSetValueEx(hKey, "Wallpaper", 0, REG_SZ, (const BYTE*)fullFilename, sizeof(fullFilename)) != ERROR_SUCCESS) {
|
|
printf("Error: Could not set registry value.\n");
|
|
return -1;
|
|
}
|
|
|
|
if (RegSetValueEx(hKey, "OriginalWallpaper", 0, REG_SZ, (const BYTE*)fullFilename, sizeof(fullFilename)) != ERROR_SUCCESS) {
|
|
printf("Error: Could not set registry value.\n");
|
|
return -1;
|
|
}
|
|
|
|
if (RegSetValueEx(hKey, "ConvertedWallpaper", 0, REG_SZ, (const BYTE*)fullFilename, sizeof(fullFilename)) != ERROR_SUCCESS) {
|
|
printf("Error: Could not set registry value.\n");
|
|
return -1;
|
|
}
|
|
|
|
// Close the registry key
|
|
RegCloseKey(hKey);
|
|
|
|
int isWallSet = SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, (void*)fullFilename, SPIF_UPDATEINIFILE);
|
|
//int result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID*)fullFilename, SPIF_SENDCHANGE);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
|
|
if (uMsg == WM_TIMER && idEvent == IDT_TIMER1) {
|
|
UpdateWallpaper();
|
|
}
|
|
}
|
|
|
|
int main (int argc, char *argv[]) {
|
|
|
|
LoadConfig();
|
|
HWND hwnd = CreateWindowW(L"static", L"Dummy Window", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
|
|
printf("Starting Walltaker95, please wait...\n");
|
|
NOTIFYICONDATAA nid;
|
|
nid.hWnd = hwnd; // handle to the window that will display the tray icon
|
|
nid.uID = 100; // unique identifier for the tray icon
|
|
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; // flags to indicate which fields are valid
|
|
nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
|
|
nid.uVersion = 0;
|
|
nid.uCallbackMessage = APPWM_ICONNOTIFY;
|
|
strcpy(nid.szTip,"Walltaker95");
|
|
//nid.szTip[]="Walltaker95";
|
|
curl = curl_easy_init(); // Initializing cURL
|
|
if(!curl) {
|
|
printf("Curl isn't working for some reason\n");
|
|
getchar();
|
|
return 0;
|
|
}
|
|
if (Shell_NotifyIcon(NIM_ADD, &nid)) {
|
|
// Tray icon created successfully
|
|
ShowWindow(hwnd, SW_HIDE);
|
|
SetTimer(hwnd, IDT_TIMER1, 10000, TimerProc);
|
|
MSG msg;
|
|
while (GetMessage(&msg, NULL, 0, 0)) {
|
|
TranslateMessage(&msg);
|
|
DispatchMessage(&msg);
|
|
}
|
|
|
|
} else {
|
|
// Error creating tray icon
|
|
printf("The tray icon fucked up somehow, oops...\n");
|
|
getchar();
|
|
}
|
|
|
|
KillTimer(hwnd, IDT_TIMER1);
|
|
DestroyWindow(hwnd);
|
|
curl_easy_cleanup(curl);
|
|
return 0;
|
|
|
|
}
|