首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用dconf监视回调

使用dconf监视回调
EN

Stack Overflow用户
提问于 2011-09-24 18:13:29
回答 1查看 379关注 0票数 0

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

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

谢谢。

代码如下:

代码语言:javascript
复制
 #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;
}

下面是执行该程序的结果:

代码语言:javascript
复制
(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
EN

回答 1

Stack Overflow用户

发布于 2012-08-19 01:42:25

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

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7538366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档