summaryrefslogtreecommitdiff
path: root/src/index.tsx
blob: 0c706d842e0a7e83c0488d900d01c4b410cf9aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import {
  ButtonItem,
  PanelSection,
  PanelSectionRow,
  staticClasses,
  ToggleField,
  SliderField
} from "@decky/ui";
import {
  callable,
  definePlugin,
  toaster,
} from "@decky/api"
import { useState, useEffect } from "react";
import { FaDownload, FaTrash } from "react-icons/fa";
import { GiPlasticDuck } from "react-icons/gi";

// Function to install lsfg-vk
const installLsfgVk = callable<[], { success: boolean; error?: string; message?: string }>("install_lsfg_vk");

// Function to uninstall lsfg-vk
const uninstallLsfgVk = callable<[], { success: boolean; error?: string; message?: string; removed_files?: string[] }>("uninstall_lsfg_vk");

// Function to check if lsfg-vk is installed
const checkLsfgVkInstalled = callable<[], { installed: boolean; lib_exists: boolean; json_exists: boolean; lib_path: string; json_path: string; error?: string }>("check_lsfg_vk_installed");

// Function to check if Lossless Scaling DLL is available
const checkLosslessScalingDll = callable<[], { detected: boolean; path?: string; source?: string; message?: string; error?: string }>("check_lossless_scaling_dll");

// Function to get lsfg configuration
const getLsfgConfig = callable<[], { success: boolean; config?: { enable_lsfg: boolean; multiplier: number; flow_scale: number; hdr: boolean; immediate_mode: boolean }; error?: string }>("get_lsfg_config");

// Function to update lsfg configuration
const updateLsfgConfig = callable<[boolean, number, number, boolean, boolean], { success: boolean; message?: string; error?: string }>("update_lsfg_config");

function Content() {
  const [isInstalled, setIsInstalled] = useState<boolean>(false);
  const [isInstalling, setIsInstalling] = useState<boolean>(false);
  const [isUninstalling, setIsUninstalling] = useState<boolean>(false);
  const [installationStatus, setInstallationStatus] = useState<string>("");
  const [dllDetected, setDllDetected] = useState<boolean>(false);
  const [dllDetectionStatus, setDllDetectionStatus] = useState<string>("");

  // LSFG configuration state
  const [enableLsfg, setEnableLsfg] = useState<boolean>(true);
  const [multiplier, setMultiplier] = useState<number>(2);
  const [flowScale, setFlowScale] = useState<number>(1.0);
  const [hdr, setHdr] = useState<boolean>(false);
  const [immediateMode, setImmediateMode] = useState<boolean>(false);

  // Check installation status on component mount
  useEffect(() => {
    const checkInstallation = async () => {
      try {
        const status = await checkLsfgVkInstalled();
        setIsInstalled(status.installed);
        if (status.installed) {
          setInstallationStatus("lsfg-vk is installed");
        } else {
          setInstallationStatus("lsfg-vk is not installed");
        }
      } catch (error) {
        setInstallationStatus("Error checking installation status");
      }
    };

    const checkDllDetection = async () => {
      try {
        const result = await checkLosslessScalingDll();
        setDllDetected(result.detected);
        if (result.detected) {
          setDllDetectionStatus(`Lossless Scaling App detected (${result.source})`);
        } else {
          setDllDetectionStatus(result.message || "Lossless Scaling App not detected");
        }
      } catch (error) {
        setDllDetectionStatus("Error checking Lossless Scaling App");
      }
    };

    const loadLsfgConfig = async () => {
      try {
        const result = await getLsfgConfig();
        if (result.success && result.config) {
          setEnableLsfg(result.config.enable_lsfg);
          setMultiplier(result.config.multiplier);
          setFlowScale(result.config.flow_scale);
          setHdr(result.config.hdr);
          setImmediateMode(result.config.immediate_mode);
        }
      } catch (error) {
        console.error("Error loading lsfg config:", error);
      }
    };

    checkInstallation();
    checkDllDetection();
    loadLsfgConfig();
  }, []);  const handleInstall = async () => {
    setIsInstalling(true);
    setInstallationStatus("Installing lsfg-vk...");
    
    try {
      const result = await installLsfgVk();
      if (result.success) {
        setIsInstalled(true);
        setInstallationStatus("lsfg-vk installed successfully!");
        toaster.toast({
          title: "Installation Complete",
          body: "lsfg-vk has been installed successfully"
        });
        
        // Reload lsfg config after installation
        try {
          const configResult = await getLsfgConfig();
          if (configResult.success && configResult.config) {
            setEnableLsfg(configResult.config.enable_lsfg);
            setMultiplier(configResult.config.multiplier);
            setFlowScale(configResult.config.flow_scale);
            setHdr(configResult.config.hdr);
            setImmediateMode(configResult.config.immediate_mode);
          }
        } catch (error) {
          console.error("Error reloading config after install:", error);
        }
      } else {
        setInstallationStatus(`Installation failed: ${result.error}`);
        toaster.toast({
          title: "Installation Failed",
          body: result.error || "Unknown error occurred"
        });
      }
    } catch (error) {
      setInstallationStatus(`Installation failed: ${error}`);
      toaster.toast({
        title: "Installation Failed",
        body: `Error: ${error}`
      });
    } finally {
      setIsInstalling(false);
    }
  };

  const handleUninstall = async () => {
    setIsUninstalling(true);
    setInstallationStatus("Uninstalling lsfg-vk...");

    try {
      const result = await uninstallLsfgVk();
      if (result.success) {
        setIsInstalled(false);
        setInstallationStatus("lsfg-vk uninstalled successfully!");
        toaster.toast({
          title: "Uninstallation Complete",
          body: result.message || "lsfg-vk has been uninstalled successfully"
        });
      } else {
        setInstallationStatus(`Uninstallation failed: ${result.error}`);
        toaster.toast({
          title: "Uninstallation Failed",
          body: result.error || "Unknown error occurred"
        });
      }
    } catch (error) {
      setInstallationStatus(`Uninstallation failed: ${error}`);
      toaster.toast({
        title: "Uninstallation Failed",
        body: `Error: ${error}`
      });
    } finally {
      setIsUninstalling(false);
    }
  };

  const updateConfig = async (newEnableLsfg: boolean, newMultiplier: number, newFlowScale: number, newHdr: boolean, newImmediateMode: boolean) => {
    try {
      const result = await updateLsfgConfig(newEnableLsfg, newMultiplier, newFlowScale, newHdr, newImmediateMode);
      if (!result.success) {
        toaster.toast({
          title: "Update Failed", 
          body: result.error || "Failed to update configuration"
        });
      }
      // Only show error notifications, not success notifications to avoid spam
    } catch (error) {
      toaster.toast({
        title: "Update Failed",
        body: `Error: ${error}`
      });
    }
  };

  const handleEnableLsfgChange = async (value: boolean) => {
    setEnableLsfg(value);
    await updateConfig(value, multiplier, flowScale, hdr, immediateMode);
  };

  const handleMultiplierChange = async (value: number) => {
    setMultiplier(value);
    await updateConfig(enableLsfg, value, flowScale, hdr, immediateMode);
  };

  const handleFlowScaleChange = async (value: number) => {
    setFlowScale(value);
    await updateConfig(enableLsfg, multiplier, value, hdr, immediateMode);
  };

  const handleHdrChange = async (value: boolean) => {
    setHdr(value);
    await updateConfig(enableLsfg, multiplier, flowScale, value, immediateMode);
  };

  const handleImmediateModeChange = async (value: boolean) => {
    setImmediateMode(value);
    await updateConfig(enableLsfg, multiplier, flowScale, hdr, value);
  };

  return (
    <PanelSection>
      <PanelSectionRow>
      <div style={{ marginBottom: "8px", fontSize: "14px" }}>
        <div style={{ 
        color: dllDetected ? "#4CAF50" : "#F44336", 
        fontWeight: "bold", 
        marginBottom: "4px" 
        }}>
        {dllDetectionStatus}
        </div>
        <div style={{ 
        color: isInstalled ? "#4CAF50" : "#FF9800" 
        }}>
        Status: {installationStatus}
        </div>
      </div>
      </PanelSectionRow>
      
      <PanelSectionRow>
      <ButtonItem
        layout="below"
        onClick={isInstalled ? handleUninstall : handleInstall}
        disabled={isInstalling || isUninstalling}
      >
        {isInstalling ? (
        <div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
          <div>Installing...</div>
        </div>
        ) : isUninstalling ? (
        <div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
          <div>Uninstalling...</div>
        </div>
        ) : isInstalled ? (
        <div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
          <FaTrash />
          <div>Uninstall lsfg-vk</div>
        </div>
        ) : (
        <div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
          <FaDownload />
          <div>Install lsfg-vk</div>
        </div>
        )}
      </ButtonItem>
      </PanelSectionRow>

      {/* Configuration Section - only show if installed */}
      {isInstalled && (
        <>
          <PanelSectionRow>
            <div style={{ 
              fontSize: "14px", 
              fontWeight: "bold", 
              marginTop: "16px", 
              marginBottom: "8px",
              borderBottom: "1px solid rgba(255, 255, 255, 0.2)",
              paddingBottom: "4px"
            }}>
              LSFG Configuration
            </div>
          </PanelSectionRow>

          <PanelSectionRow>
            <ToggleField
              label="Enable LSFG"
              description="Enables the frame generation layer"
              checked={enableLsfg}
              onChange={handleEnableLsfgChange}
            />
          </PanelSectionRow>

          <PanelSectionRow>
            <SliderField
              label="FPS Multiplier"
              description="Traditional FPS multiplier value (2-4)"
              value={multiplier}
              min={2}
              max={4}
              step={1}
              notchCount={3}
              notchLabels={[
                { notchIndex: 0, label: "2" },
                { notchIndex: 1, label: "3" }, 
                { notchIndex: 2, label: "4" }
              ]}
              onChange={handleMultiplierChange}
            />
          </PanelSectionRow>

          <PanelSectionRow>
            <SliderField
              label={`Flow Scale ${Math.round(flowScale * 100)}%`}
              description="Lowers the flow scale for performance (0.25-1.0)"
              value={flowScale}
              min={0.25}
              max={1.0}
              step={0.01}
              onChange={handleFlowScaleChange}
            />
          </PanelSectionRow>

          <PanelSectionRow>
            <ToggleField
              label="HDR Mode"
              description="Enable HDR mode (only if using HDR)"
              checked={hdr}
              onChange={handleHdrChange}
            />
          </PanelSectionRow>

          <PanelSectionRow>
            <ToggleField
              label="Immediate Mode"
              description="Disable vsync for reduced input lag"
              checked={immediateMode}
              onChange={handleImmediateModeChange}
            />
          </PanelSectionRow>
        </>
      )}
      
      <PanelSectionRow>
      <div style={{ 
        fontSize: "13px", 
        marginTop: "12px", 
        padding: "8px", 
        backgroundColor: "rgba(255, 255, 255, 0.05)", 
        borderRadius: "4px" 
      }}>
        <div style={{ fontWeight: "bold", marginBottom: "6px" }}>
        Usage Instructions:
        </div>
        <div style={{ marginBottom: "4px" }}>
        Option 1: Use the lsfg script (recommended):
        </div>
        <div style={{ 
        fontFamily: "monospace", 
        backgroundColor: "rgba(0, 0, 0, 0.3)", 
        padding: "4px", 
        borderRadius: "2px",
        fontSize: "12px",
        marginBottom: "6px"
        }}>
        ~/lsfg && %COMMAND%
        </div>
        <div style={{ marginBottom: "4px" }}>
        Option 2: Manual environment variables:
        </div>
        <div style={{ 
        fontFamily: "monospace", 
        backgroundColor: "rgba(0, 0, 0, 0.3)", 
        padding: "4px", 
        borderRadius: "2px",
        fontSize: "12px",
        marginBottom: "6px"
        }}>
        ENABLE_LSFG=1 LSFG_MULTIPLIER={multiplier} %COMMAND%
        </div>
        <div style={{ fontSize: "11px", opacity: 0.8 }}>
        The lsfg script uses your current configuration settings.
        <br />
        • ENABLE_LSFG=1 - Enables frame generation
        <br />
        • LSFG_MULTIPLIER=2-4 - FPS multiplier (start with 2)
        <br />
        • LSFG_FLOW_SCALE=0.25-1.0 - Flow scale (for performance)
        <br />
        • LSFG_HDR=1 - HDR mode (only if using HDR)
        <br />
        • MESA_VK_WSI_PRESENT_MODE=immediate - Disable vsync
        </div>
      </div>
      </PanelSectionRow>
    </PanelSection>
  );
};

export default definePlugin(() => {
  console.log("Lossless Scaling plugin initializing")

  return {
    // The name shown in various decky menus
    name: "Lossless Scaling",
    // The element displayed at the top of your plugin's menu
    titleView: <div className={staticClasses.Title}>Lossless Scaling</div>,
    // The content of your plugin's menu
    content: <Content />,
    // The icon displayed in the plugin list
    icon: <GiPlasticDuck />,
    // The function triggered when your plugin unloads
    onDismount() {
      console.log("Lossless Scaling unloading")
    },
  };
});