forked from chikobara/GPU-Switcher-Supergfxctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
181 lines (160 loc) · 5.51 KB
/
extension.js
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
import Gio from "gi://Gio";
import GObject from "gi://GObject";
import {
QuickMenuToggle,
SystemIndicator,
} from "resource:///org/gnome/shell/ui/quickSettings.js";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as Util from "resource:///org/gnome/shell/misc/util.js";
import * as PopupMenu from "resource:///org/gnome/shell/ui/popupMenu.js";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
// Define GPU profiles with their names, icons, and commands
const GPU_PROFILE_PARAMS = {
Integrated: {
name: "Integrated",
iconName: "computer-symbolic",
command: "supergfxctl -m Integrated",
},
Hybrid: {
name: "Hybrid",
iconName: "graphics-card-symbolic",
command: "supergfxctl -m Hybrid",
},
Dedicated: {
name: "Dedicated",
iconName: "graphics-card-symbolic",
command: "supergfxctl -m Dedicated",
},
};
const GpuProfilesToggle = GObject.registerClass(
class GpuProfilesToggle extends QuickMenuToggle {
_init() {
super._init({ title: "GPU Mode" });
this._profileItems = new Map();
this._activeProfile = null; // Will be set after running supergfxctl -g
this.connect("clicked", () => {
this._sync();
});
this._profileSection = new PopupMenu.PopupMenuSection();
this.menu.addMenuItem(this._profileSection);
this.menu.setHeader("graphics-card-symbolic", "GPU Mode");
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._addProfileToggles();
this._fetchCurrentProfile();
}
_fetchCurrentProfile() {
try {
let proc = Gio.Subprocess.new(
["supergfxctl", "-g"],
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
);
proc.communicate_utf8_async(null, null, (proc, res) => {
try {
let [ok, stdout, stderr] = proc.communicate_utf8_finish(res);
if (ok && stdout.trim() in GPU_PROFILE_PARAMS) {
this._setActiveProfile(stdout.trim());
} else {
console.error(`Failed to fetch current profile: ${stderr}`);
this._setActiveProfile("Hybrid"); // Fallback to default
}
} catch (e) {
console.error(`Error while fetching current profile: ${e.message}`);
this._setActiveProfile("Hybrid"); // Fallback to default
}
});
} catch (e) {
console.error(`Failed to execute supergfxctl: ${e.message}`);
this._setActiveProfile("Hybrid"); // Fallback to default
}
}
_addProfileToggles() {
for (const [profile, params] of Object.entries(GPU_PROFILE_PARAMS)) {
const item = new PopupMenu.PopupImageMenuItem(
params.name,
params.iconName
);
item.connect("activate", () => {
console.log(`Activating profile: ${profile}`);
this._activateProfile(profile, params.command);
});
this._profileItems.set(profile, item);
this._profileSection.addMenuItem(item);
}
}
_activateProfile(profile, command) {
try {
let proc = Gio.Subprocess.new(
["sh", "-c", command],
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
);
proc.communicate_utf8_async(null, null, (proc, res) => {
try {
let [ok, stdout, stderr] = proc.communicate_utf8_finish(res);
if (proc.get_successful()) {
console.log(`Profile ${profile} activated successfully`);
this._setActiveProfile(profile);
Util.spawnCommandLine("gnome-session-quit --logout");
} else {
console.error(
`Failed to activate profile ${profile}: ${stderr.trim()}`
);
}
} catch (e) {
console.error(
`Error while activating profile ${profile}: ${e.message}`
);
}
});
} catch (e) {
console.error(`Failed to execute command: ${e.message}`);
}
}
_setActiveProfile(profile) {
if (GPU_PROFILE_PARAMS[profile]) {
console.log(`Setting active profile: ${profile}`);
this._activeProfile = profile;
this._sync();
} else {
console.error(`Unknown profile: ${profile}`);
}
}
_sync() {
console.log(`Synchronizing profile: ${this._activeProfile}`);
const params = GPU_PROFILE_PARAMS[this._activeProfile];
if (!params) {
console.error(
`Active profile ${this._activeProfile} is not defined in GPU_PROFILE_PARAMS.`
);
return;
}
for (const [profile, item] of this._profileItems) {
item.setOrnament(
profile === this._activeProfile
? PopupMenu.Ornament.CHECK
: PopupMenu.Ornament.NONE
);
}
this.set({ subtitle: params.name, iconName: params.iconName });
this.checked = this._activeProfile !== "Hybrid";
}
}
);
export const Indicator = GObject.registerClass(
class Indicator extends SystemIndicator {
_init() {
super._init();
this.quickSettingsItems.push(new GpuProfilesToggle());
}
}
);
export default class GpuSwitcherExtension extends Extension {
enable() {
this._indicator = new Indicator();
Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator);
}
disable() {
this._indicator.quickSettingsItems.forEach((item) => item.destroy());
this._indicator.destroy();
this._indicator = null; // Null out the indicator
}
}