2024-11-12 14:15:00 -05:00
|
|
|
import { execAsync, subprocess } from 'astal';
|
|
|
|
import GObject, { register } from 'astal/gobject';
|
|
|
|
|
|
|
|
/* Types */
|
|
|
|
interface NotifyAction {
|
|
|
|
id: string
|
|
|
|
label: string
|
|
|
|
callback: () => void
|
|
|
|
}
|
|
|
|
interface NotifySendProps {
|
|
|
|
actions?: NotifyAction[]
|
|
|
|
appName?: string
|
|
|
|
body?: string
|
|
|
|
category?: string
|
|
|
|
hint?: string
|
|
|
|
iconName: string
|
|
|
|
replaceId?: number
|
|
|
|
title: string
|
|
|
|
urgency?: 'low' | 'normal' | 'critical'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const APP_NAME = 'gpu-screen-recorder';
|
|
|
|
const ICON_NAME = 'nvidia';
|
|
|
|
|
|
|
|
const escapeShellArg = (arg: string): string => `'${arg.replace(/'/g, '\'\\\'\'')}'`;
|
|
|
|
|
|
|
|
const notifySend = ({
|
|
|
|
actions = [],
|
|
|
|
appName,
|
|
|
|
body,
|
|
|
|
category,
|
|
|
|
hint,
|
|
|
|
iconName,
|
|
|
|
replaceId,
|
|
|
|
title,
|
|
|
|
urgency = 'normal',
|
|
|
|
}: NotifySendProps) => new Promise<number>((resolve) => {
|
|
|
|
let printedId = false;
|
|
|
|
|
|
|
|
const cmd = [
|
|
|
|
'notify-send',
|
|
|
|
'--print-id',
|
|
|
|
`--icon=${escapeShellArg(iconName)}`,
|
|
|
|
escapeShellArg(title),
|
|
|
|
escapeShellArg(body ?? ''),
|
|
|
|
// Optional params
|
|
|
|
appName ? `--app-name=${escapeShellArg(appName)}` : '',
|
|
|
|
category ? `--category=${escapeShellArg(category)}` : '',
|
|
|
|
hint ? `--hint=${escapeShellArg(hint)}` : '',
|
|
|
|
replaceId ? `--replace-id=${replaceId.toString()}` : '',
|
|
|
|
`--urgency=${urgency}`,
|
|
|
|
].concat(
|
|
|
|
actions.map(({ id, label }) => `--action=${escapeShellArg(id)}=${escapeShellArg(label)}`),
|
|
|
|
).join(' ');
|
|
|
|
|
|
|
|
subprocess(
|
|
|
|
cmd,
|
|
|
|
(out) => {
|
|
|
|
if (!printedId) {
|
|
|
|
resolve(parseInt(out));
|
|
|
|
printedId = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
actions.find((action) => action.id === out)?.callback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
console.error(`[Notify] ${err}`);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
@register()
|
2024-11-25 12:18:46 -05:00
|
|
|
export default class GpuScreenRecorder extends GObject.Object {
|
2024-11-12 14:15:00 -05:00
|
|
|
private _lastNotifID: number | undefined;
|
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
public constructor() {
|
|
|
|
super();
|
|
|
|
|
2024-11-19 23:55:17 -05:00
|
|
|
try {
|
|
|
|
subprocess(
|
|
|
|
['gsr-start'],
|
|
|
|
(path) => {
|
|
|
|
if (!this._lastNotifID) {
|
2024-11-25 12:18:46 -05:00
|
|
|
console.error('[GpuScreenRecorder] ID of warning notif not found');
|
2024-11-12 14:15:00 -05:00
|
|
|
|
2024-11-19 23:55:17 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
this._onSaved(path);
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
else {
|
2024-11-12 14:15:00 -05:00
|
|
|
this._onSaved(path);
|
2024-11-19 23:55:17 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
() => { /**/ },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch (_e) {
|
|
|
|
console.error('Missing dependency for gpu-screen-recorder');
|
|
|
|
}
|
2024-11-12 14:15:00 -05:00
|
|
|
}
|
|
|
|
|
2024-11-25 12:18:46 -05:00
|
|
|
private static _default: InstanceType<typeof GpuScreenRecorder> | undefined;
|
2024-11-20 23:38:41 -05:00
|
|
|
|
|
|
|
public static get_default() {
|
2024-11-25 12:18:46 -05:00
|
|
|
if (!GpuScreenRecorder._default) {
|
|
|
|
GpuScreenRecorder._default = new GpuScreenRecorder();
|
2024-11-20 23:38:41 -05:00
|
|
|
}
|
|
|
|
|
2024-11-25 12:18:46 -05:00
|
|
|
return GpuScreenRecorder._default;
|
2024-11-20 23:38:41 -05:00
|
|
|
}
|
|
|
|
|
2024-11-12 14:15:00 -05:00
|
|
|
public saveReplay() {
|
|
|
|
execAsync(['gpu-save-replay'])
|
|
|
|
.then(async() => {
|
|
|
|
this._lastNotifID = await notifySend({
|
|
|
|
appName: APP_NAME,
|
|
|
|
iconName: ICON_NAME,
|
|
|
|
title: 'Saving Replay',
|
|
|
|
body: 'Last 20 minutes',
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2024-11-25 12:18:46 -05:00
|
|
|
console.error(`[GpuScreenRecorder save-replay] ${err}`);
|
2024-11-12 14:15:00 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onSaved(path: string) {
|
|
|
|
notifySend({
|
|
|
|
appName: APP_NAME,
|
|
|
|
iconName: ICON_NAME,
|
|
|
|
replaceId: this._lastNotifID,
|
|
|
|
|
|
|
|
title: 'Replay Saved',
|
|
|
|
body: `Saved to ${path}`,
|
|
|
|
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
id: 'folder',
|
|
|
|
label: 'Open Folder',
|
|
|
|
|
|
|
|
callback: () => execAsync([
|
|
|
|
'xdg-open',
|
|
|
|
path.substring(0, path.lastIndexOf('/')),
|
|
|
|
]).catch(print),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
id: 'video',
|
|
|
|
label: 'Open Video',
|
|
|
|
|
|
|
|
callback: () => execAsync(['xdg-open', path]).catch(print),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
id: 'kdenlive',
|
|
|
|
label: 'Edit in kdenlive',
|
|
|
|
|
|
|
|
callback: () => execAsync([
|
|
|
|
'bash',
|
|
|
|
'-c',
|
|
|
|
`kdenlive -i ${path}`,
|
|
|
|
]).catch(print),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|