Skip to content

Commit

Permalink
dbusServices: Port to ESM
Browse files Browse the repository at this point in the history
We want to replace gjs' custom (and now legacy) imports system
with standard EcmaScript modules: JS developers are already
familiar with them, they have better tooling support and using
standard features over non-standard ones is generally the right
thing to do.

Our D-Bus services are separate from the main process, and thus
can be ported separately (except for the few imports that are
shared with the main process' code base).

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2786>
  • Loading branch information
fmuellner committed Jun 21, 2023
1 parent eacabbf commit 612e041
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ extends:
overrides:
- files:
- js/ui/init.js
- js/dbusServices/**
parserOptions:
sourceType: module
sourceType: module
6 changes: 5 additions & 1 deletion js/dbusServices/dbus-service.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
imports.package.start({
import {programInvocationName, programArgs} from 'system';

imports.package.init({
name: '@PACKAGE_NAME@',
prefix: '@prefix@',
libdir: '@libdir@',
});
const {main} = await import(`${imports.package.moduledir}/main.js`);
await main([programInvocationName, ...programArgs]);
2 changes: 1 addition & 1 deletion js/dbusServices/dbus-service.service.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[D-BUS Service]
Name=@service@
Exec=@gjs@ @pkgdatadir@/@service@
Exec=@gjs@ -m @pkgdatadir@/@service@
19 changes: 9 additions & 10 deletions js/dbusServices/dbusService.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* exported DBusService, ServiceImplementation */
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';

const { Gio, GLib } = imports.gi;
import {programArgs} from 'system';

const Signals = imports.signals;

const IDLE_SHUTDOWN_TIME = 2; // s

const { programArgs } = imports.system;

var ServiceImplementation = class {
export class ServiceImplementation {
constructor(info, objectPath) {
this._objectPath = objectPath;
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(info, this);
Expand Down Expand Up @@ -150,10 +149,10 @@ var ServiceImplementation = class {
that._queueShutdownCheck();
};
}
};
}
Signals.addSignalMethods(ServiceImplementation.prototype);

var DBusService = class {
export class DBusService {
constructor(name, service) {
this._name = name;
this._service = service;
Expand All @@ -162,7 +161,7 @@ var DBusService = class {
this._service.connect('shutdown', () => this._loop.quit());
}

run() {
async runAsync() {
// Bail out when not running under gnome-shell
Gio.DBus.watch_name(Gio.BusType.SESSION,
'org.gnome.Shell',
Expand All @@ -183,6 +182,6 @@ var DBusService = class {
null,
() => this._loop.quit());

this._loop.run();
await this._loop.runAsync();
}
};
}
10 changes: 7 additions & 3 deletions js/dbusServices/extensions/extensionPrefsDialog.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported ExtensionPrefsDialog */

const { Adw, Gdk, Gio, GLib, GObject, Gtk } = imports.gi;
import Adw from 'gi://Adw?version=1';
import Gdk from 'gi://Gdk?version=4.0';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk?version=4.0';

const ExtensionUtils = imports.misc.extensionUtils;

var ExtensionPrefsDialog = GObject.registerClass({
export const ExtensionPrefsDialog = GObject.registerClass({
GTypeName: 'ExtensionPrefsDialog',
}, class ExtensionPrefsDialog extends Adw.PreferencesWindow {
_init(extension) {
Expand Down
14 changes: 7 additions & 7 deletions js/dbusServices/extensions/extensionsService.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported ExtensionsService */
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Shew from 'gi://Shew';

const { Gio, GLib, Shew } = imports.gi;
import {ExtensionPrefsDialog} from './extensionPrefsDialog.js';
import {ServiceImplementation} from './dbusService.js';

const ExtensionUtils = imports.misc.extensionUtils;

const { loadInterfaceXML } = imports.misc.dbusUtils;
const { ExtensionPrefsDialog } = imports.extensionPrefsDialog;
const { ServiceImplementation } = imports.dbusService;
const {loadInterfaceXML} = imports.misc.dbusUtils;

const ExtensionsIface = loadInterfaceXML('org.gnome.Shell.Extensions');
const ExtensionsProxy = Gio.DBusProxy.makeProxyWrapper(ExtensionsIface);

var ExtensionsService = class extends ServiceImplementation {
export const ExtensionsService = class extends ServiceImplementation {
constructor() {
super(ExtensionsIface, '/org/gnome/Shell/Extensions');

Expand Down
18 changes: 7 additions & 11 deletions js/dbusServices/extensions/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/* exported main */

imports.gi.versions.Adw = '1';
imports.gi.versions.Gdk = '4.0';
imports.gi.versions.Gtk = '4.0';

const { Adw, GObject } = imports.gi;
import Adw from 'gi://Adw?version=1';
import GObject from 'gi://GObject';
const pkg = imports.package;

const { DBusService } = imports.dbusService;
const { ExtensionsService } = imports.extensionsService;
import {DBusService} from './dbusService.js';
import {ExtensionsService} from './extensionsService.js';

function main() {
/** @returns {void} */
export async function main() {
Adw.init();
pkg.initFormat();

Expand All @@ -19,5 +15,5 @@ function main() {
const service = new DBusService(
'org.gnome.Shell.Extensions',
new ExtensionsService());
service.run();
await service.runAsync();
}
11 changes: 5 additions & 6 deletions js/dbusServices/notifications/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* exported main */
import {DBusService} from './dbusService.js';
import {NotificationDaemon} from './notificationDaemon.js';

const { DBusService } = imports.dbusService;
const { NotificationDaemon } = imports.notificationDaemon;

function main() {
/** @returns {void} */
export async function main() {
const service = new DBusService(
'org.gnome.Shell.Notifications',
new NotificationDaemon());
service.run();
await service.runAsync();
}
10 changes: 5 additions & 5 deletions js/dbusServices/notifications/notificationDaemon.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported NotificationDaemon */
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';

const { Gio, GLib } = imports.gi;
import {ServiceImplementation} from './dbusService.js';

const { loadInterfaceXML } = imports.misc.dbusUtils;
const { ServiceImplementation } = imports.dbusService;
const {loadInterfaceXML} = imports.misc.dbusUtils;

const NotificationsIface = loadInterfaceXML('org.freedesktop.Notifications');
const NotificationsProxy = Gio.DBusProxy.makeProxyWrapper(NotificationsIface);

Gio._promisify(Gio.DBusConnection.prototype, 'call');

var NotificationDaemon = class extends ServiceImplementation {
export const NotificationDaemon = class extends ServiceImplementation {
constructor() {
super(NotificationsIface, '/org/freedesktop/Notifications');

Expand Down
11 changes: 5 additions & 6 deletions js/dbusServices/screencast/main.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* exported main */
import {DBusService} from './dbusService.js';
import {ScreencastService} from './screencastService.js';

const {DBusService} = imports.dbusService;

function main() {
const {ScreencastService} = imports.screencastService;
/** @returns {void} */
export async function main() {
if (!ScreencastService.canScreencast())
return;

const service = new DBusService(
'org.gnome.Shell.Screencast',
new ScreencastService());
service.run();
await service.runAsync();
}
15 changes: 7 additions & 8 deletions js/dbusServices/screencast/screencastService.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported ScreencastService */
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Gst from 'gi://Gst?version=1.0';
import Gtk from 'gi://Gtk?version=4.0';

imports.gi.versions.Gst = '1.0';
imports.gi.versions.Gtk = '4.0';
import {ServiceImplementation} from './dbusService.js';

const { Gio, GLib, Gst, Gtk } = imports.gi;

const { loadInterfaceXML, loadSubInterfaceXML } = imports.misc.dbusUtils;
const {loadInterfaceXML, loadSubInterfaceXML} = imports.misc.dbusUtils;
const Signals = imports.misc.signals;
const { ServiceImplementation } = imports.dbusService;

const ScreencastIface = loadInterfaceXML('org.gnome.Shell.Screencast');

Expand Down Expand Up @@ -400,7 +399,7 @@ var Recorder = class extends Signals.EventEmitter {
}
};

var ScreencastService = class extends ServiceImplementation {
export const ScreencastService = class extends ServiceImplementation {
static canScreencast() {
if (!Gst.init_check(null))
return false;
Expand Down
11 changes: 5 additions & 6 deletions js/dbusServices/screensaver/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* exported main */
import {DBusService} from './dbusService.js';
import {ScreenSaverService} from './screenSaverService.js';

const { DBusService } = imports.dbusService;
const { ScreenSaverService } = imports.screenSaverService;

function main() {
/** @returns {void} */
export async function main() {
const service = new DBusService(
'org.gnome.ScreenSaver',
new ScreenSaverService());
service.run();
await service.runAsync();
}
10 changes: 5 additions & 5 deletions js/dbusServices/screensaver/screenSaverService.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported ScreenSaverService */
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';

const { Gio, GLib } = imports.gi;
import {ServiceImplementation} from './dbusService.js';

const { loadInterfaceXML } = imports.misc.dbusUtils;
const { ServiceImplementation } = imports.dbusService;
const {loadInterfaceXML} = imports.misc.dbusUtils;

const ScreenSaverIface = loadInterfaceXML('org.gnome.ScreenSaver');
const ScreenSaverProxy = Gio.DBusProxy.makeProxyWrapper(ScreenSaverIface);

var ScreenSaverService = class extends ServiceImplementation {
export const ScreenSaverService = class extends ServiceImplementation {
constructor() {
super(ScreenSaverIface, '/org/gnome/ScreenSaver');

Expand Down

0 comments on commit 612e041

Please sign in to comment.