AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1260975
Accepted
pablomd
pablomd
Asked: 2020-07-22 15:40:23 +0800 CST2020-07-22 15:40:23 +0800 CST 2020-07-22 15:40:23 +0800 CST

关闭盖子时禁止睡眠的 JS 脚本不起作用。文件描述符管理问题还是其他?

  • 772

我正在尝试为 gnome 开发一个扩展,它允许我在直接从状态栏关闭盖子时禁止系统睡眠,而无需打开调整。

到目前为止,我有一个基本的 js 脚本,它成功连接到 gdbus 并能够执行命令,但我无法让抑制剂工作。我相信问题是我没有处理禁止函数以正确方式返回的文件描述符,因为文档说只有在文件描述符被引用和打开时,抑制剂才保留在原位。但也有可能我没有正确调用该函数。

如果有人能给我一些建议,我将非常感激。我正在运行 Ubuntu 18.04.4 和 gnome 3.28.2

/* -*- mode: js2 - indent-tabs-mode: nil - js2-basic-offset: 4 -*- */
/*jshint multistr:true */
/*jshint esnext:true */
/*global imports: true */
/*global global: true */
/*global log: true */
'use strict';

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Mainloop = imports.mainloop;
const Atk = imports.gi.Atk;


const DBusInterface = '<node>\
  <interface name="org.freedesktop.login1.Manager">\
    <method name="Inhibit">\
        <arg type="s" direction="in" />\
        <arg type="s" direction="in" />\
        <arg type="s" direction="in" />\
        <arg type="s" direction="in" />\
        <arg type="h" direction="out" />\
    </method>\
    <method name="ListInhibitors">\
      <arg type="a(ssssuu)" direction="out" />\
  </method>\
  </interface>\
</node>';

const DBusProxy = Gio.DBusProxy.makeProxyWrapper(DBusInterface);
let proxy = new DBusProxy(
    Gio.DBus.system,
    "org.freedesktop.login1",
    "/org/freedesktop/login1"
);

let inhibitors = proxy.ListInhibitorsSync();
print(inhibitors);


let fd = proxy.InhibitSync('handle-lid-switch',
                          'gnome-extension-lid-inhibitor',
                          'user preference',
                          'block');

inhibitors = proxy.ListInhibitorsSync();
print(inhibitors);

let loop = new GLib.MainLoop(null, false);
loop.run();
power-management gnome laptop gnome-shell-extension lid
  • 1 1 个回答
  • 63 Views

1 个回答

  • Voted
  1. Best Answer
    pablomd
    2020-07-25T08:05:00+08:002020-07-25T08:05:00+08:00

    好的,我是对的,FD 在功能结束时被关闭,释放了抑制。我找不到保持 FD 打开的方法,但我能够使用它异步保持函数运行。

    下一步是将其集成到我的扩展中。

    这是新代码:

    /*jshint multistr:true */
    /*jshint esnext:true */
    /*global imports: true */
    /*global global: true */
    /*global log: true */
    'use strict';
    
    const Gio = imports.gi.Gio;
    const GLib = imports.gi.GLib;
    const GObject = imports.gi.GObject;
    const Mainloop = imports.mainloop;
    const Atk = imports.gi.Atk;
    
    
    const DBusInterface = '<node>\
      <interface name="org.freedesktop.login1.Manager">\
        <method name="Inhibit">\
            <arg type="s" direction="in" />\
            <arg type="s" direction="in" />\
            <arg type="s" direction="in" />\
            <arg type="s" direction="in" />\
            <arg type="h" direction="out" />\
        </method>\
        </interface>\
      </node>';
    
    const DBusProxy = Gio.DBusProxy.makeProxyWrapper(DBusInterface);
    let proxy = new DBusProxy(
        Gio.DBus.system,
        "org.freedesktop.login1",
        "/org/freedesktop/login1"
    );
    
    var inhibitor = {loop:''};
    
    /* Inhibit remote starts the inhibitor asynchronously and locks itself with
       a GLib.MainLoop assigned to inhibitor.loop.
       To release the inhibit just quit the loop at inhibitor.loop
    */
    proxy.InhibitRemote('handle-lid-switch', 'lidsleep-inhibitor',
                        'el diego de la gente lo quiere', 'block',
                        (fileDescriptor) => {
    
                            if (fileDescriptor) {
                                let loop = new GLib.MainLoop(null, false);
                                inhibitor.loop = loop;
                                log("Inhibitor added to array");
                                loop.run();
                                log("Shutting down completed");
                                return;
                            }
                            else {
                                logError("Inhibit returned null");
                                return;
                            }
                        });
    
    
    /* A ten second timer function */
    Promise.timeoutSeconds = function(priority = GLib.PRIORITY_DEFAULT, interval = 10) {
        return new Promise(resolve => GLib.timeout_add_seconds(priority, interval, resolve));
    };
    
    /* A function that calls a timer and wait for it to finish. Then, if the arg
       cont is true it releases the inhibit and return false. Otherwise it quits  
       the main loop.
    */
    async function asyncCall(cont) {
        try {
          log('Waiting 10 sec');
          await Promise.timeoutSeconds();
          if (cont){
            log('10 sec completed, inhibitor shut down starting');
            inhibitor.loop.quit();
            return !cont;
          } else {
            log('Quitting main loop');
            loop.quit();
            return;
          }
      } catch (e) {
          throw e;
      }
    }
    
    
    let loop = new GLib.MainLoop(null, false);
    
    /* asyncCall is called twice.
       The first run releases the inhibit.
       The second run quits the main loop.
    */
    asyncCall(true).
      then(asyncCall);
    
    log("Starting main loop");
    loop.run();
    
    
    • 0

相关问题

  • 为什么我的时钟、指示器小程序和通知区域有时会在我重新启动时移动?我怎样才能防止这种情况?

  • 停止菜单图标闪烁

  • 是否有适用于 IMAP 邮件帐户的 Gnome 小程序?

  • 如果顶部面板中缺少会话小程序,如何注销?

  • 如何让“您的电池坏了”消息消失?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve