diff --git a/src/apply/application.ts b/src/apply/application.ts index 8aba1ca..ebae5a4 100644 --- a/src/apply/application.ts +++ b/src/apply/application.ts @@ -2,6 +2,7 @@ import { type Colordx, extend } from "@colordx/core" import lab from "@colordx/core/plugins/lab" import type { ImageAnalysisData } from "../analyze/analysis.ts" import { chalkDebug } from "../utils.ts" +import * as path from "@std/path" extend([lab]) @@ -98,8 +99,79 @@ export function findMatchingImages(imagesData: ImageAnalysisData, targetColor: C return matchingImages } -export function setWindowsWallpaper() { - const user32 = Deno.dlopen("user32.dll", { - SystemParametersInfoA: { parameters: ["u16", "u16", "pointer", "u16"], result: "void" } - }) + +export async function setWindowsWallpaper(tempImagePath: string, ...imagePaths: string[]) { + interface Rect { + left: number + top: number + right: number + bottom: number + } + + interface Vector2 { + width: number + height: number + } + + const pathToUser32 = path.fromFileUrl( + "file:///C:/Windows/System32/user32.dll", + ); // jank + + const user32 = Deno.dlopen(pathToUser32, { + SystemParametersInfoA: { + parameters: ["u16", "u16", "pointer", "u16"], + result: "bool", + }, + + EnumDisplayMonitors: { + parameters: ["pointer", "pointer", "pointer", "i32"], + result: "bool", + }, + }); + + const monitors: Vector2[] = [] + + const monitorEnum = new Deno.UnsafeCallback({ + parameters: ["pointer", "pointer", "pointer", "i32"], + result: "bool", + }, (_hMon, _hdc, lprcMonitor, _pData) => { + // grab the buffer of our data... + const buffer = Deno.UnsafePointerView.getArrayBuffer(lprcMonitor!, 16) + // ..convert it to an Int32 array... + const array = Array.from(new Int32Array(buffer)) + // ..then populate: left - right top - bottom + const rect = { width: Math.abs(array[2] - array[0]), height: Math.abs(array[1] - array[3]) } + monitors.push(rect) + + return true + }); + + user32.symbols.EnumDisplayMonitors(null, null, monitorEnum.pointer, 0); + + // from this point on monitorsRect is populated + + console.log(monitors) + + for (let i = 0; i < monitors.length; i++) { + const monitor = monitors[i] + if (monitor.width >= monitor.height) { + await $`magick ${imagePaths[i]} -resize ${monitor.width}x -format "%m" ${path.join(tempImagePath, `monitorImage${i}.png`)}` + } else { + await $`magick ${imagePaths[i]} -resize x${monitor.height} -format "%m" ${path.join(tempImagePath, `monitorImage${i}.png`)}` + } + } + + // TODO: this isn't the right order of images + console.log(await $`magick ${path.join(tempImagePath, `monitorImage*.png`)} +append ${path.join(tempImagePath, "out.png")}`) + + const pointer = Deno.UnsafePointer.of( + Buffer.from( + "C:\\Users\\angelcube\\Pictures\\wallpaper\\vocaloid_morningglory_lobelia.jpg", + "ascii", + ), + ); + + user32.symbols.SystemParametersInfoA(20, 0, pointer, 3); + + user32.close(); } \ No newline at end of file