使用 dconf 监视回调

发布于 2024-12-06 07:14:41 字数 1650 浏览 2 评论 0原文

我正在尝试在 Ubuntu 11.04 中使用 dconf api 捕获后台更改事件。我已经创建了一个客户端并且可以读取背景值,但是当我更改 dconf 值(通过 dconf-editor)时,不会调用回调函数。

我应该如何使用回调技术?

谢谢。

这是代码:

 #include <glib-2.0/glib.h>
 #include <glib-2.0/glib-object.h>
 #include <glib-2.0/gio/gio.h>
 #include </usr/include/dconf/dconf.h>
 #include <iostream>

void WatchBackground (DConfClient *client, const gchar* path, const gchar* const *items, gint n_items, const gchar* tag, gpointer user_data){

    if (g_strcasecmp(path, (gchar*)"/org/gnome/desktop/background/picture_uri") == 0){
        std::cout << "filename Ok" << std::endl;
    }
    std::cout << "Call callback" << std::endl;
}

int main(int argc, char** argv) {
    g_type_init();

    DConfClient *dcfc = dconf_client_new(NULL, WatchBackground, NULL, NULL);
    if (dcfc != NULL){
        std::cout << "DConfClient created." << std::endl;
        GVariant *res = dconf_client_read(dcfc, (gchar*)"/org/gnome/desktop/background/picture-uri");
        if (res != NULL){
            gsize s = 0;
            std::cout << "/org/gnome/desktop/background/picture-uri: " << g_variant_get_string(res, &s) << std::endl;
        } else {
            std::cout << "NULL read" << std::endl;
        }
    }

    while(true){
        sleep(1000);
    }

    return 0;
}

这是执行该程序的结果:

(process:6889): GLib-WARNING **: XDG_RUNTIME_DIR variable not set.  Falling back to XDG cache dir.
DConfClient created.
/org/gnome/desktop/background/picture-uri: /usr/share/backgrounds/space-02.png

I'm trying to use the dconf api to capture the background change event, in an Ubuntu 11.04. I've created a client and can read the background value, but when I change a dconf value (through dconf-editor), the callback function is not called.

How should I use the callback technique?

Thanks.

Here is the code:

 #include <glib-2.0/glib.h>
 #include <glib-2.0/glib-object.h>
 #include <glib-2.0/gio/gio.h>
 #include </usr/include/dconf/dconf.h>
 #include <iostream>

void WatchBackground (DConfClient *client, const gchar* path, const gchar* const *items, gint n_items, const gchar* tag, gpointer user_data){

    if (g_strcasecmp(path, (gchar*)"/org/gnome/desktop/background/picture_uri") == 0){
        std::cout << "filename Ok" << std::endl;
    }
    std::cout << "Call callback" << std::endl;
}

int main(int argc, char** argv) {
    g_type_init();

    DConfClient *dcfc = dconf_client_new(NULL, WatchBackground, NULL, NULL);
    if (dcfc != NULL){
        std::cout << "DConfClient created." << std::endl;
        GVariant *res = dconf_client_read(dcfc, (gchar*)"/org/gnome/desktop/background/picture-uri");
        if (res != NULL){
            gsize s = 0;
            std::cout << "/org/gnome/desktop/background/picture-uri: " << g_variant_get_string(res, &s) << std::endl;
        } else {
            std::cout << "NULL read" << std::endl;
        }
    }

    while(true){
        sleep(1000);
    }

    return 0;
}

And here's the result of executing this program:

(process:6889): GLib-WARNING **: XDG_RUNTIME_DIR variable not set.  Falling back to XDG cache dir.
DConfClient created.
/org/gnome/desktop/background/picture-uri: /usr/share/backgrounds/space-02.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-12-13 07:14:41

您必须使用GMainLoop来等待。这是一个简单的代码来演示。

#include <dconf.h>

void watch(DConfClient *c,
       const gchar *p,
       const gchar * const *i,
       gint n,
       const gchar *t,
       gpointer unused)
{
    g_message("%s", p);
}

int main(int argc, char *argv[])
{
    DConfClient *c;
    GMainLoop *l;

    g_type_init();

    c = dconf_client_new(NULL, watch, NULL, NULL);
    dconf_client_watch(c, "/", NULL, NULL);

    l = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(l);

    return 0;
}

You must use GMainLoop to wait. Here is a simple code to demonstrate.

#include <dconf.h>

void watch(DConfClient *c,
       const gchar *p,
       const gchar * const *i,
       gint n,
       const gchar *t,
       gpointer unused)
{
    g_message("%s", p);
}

int main(int argc, char *argv[])
{
    DConfClient *c;
    GMainLoop *l;

    g_type_init();

    c = dconf_client_new(NULL, watch, NULL, NULL);
    dconf_client_watch(c, "/", NULL, NULL);

    l = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(l);

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文