2011-09-24 4 views
0

dconf api를 사용하여 Ubuntu 11.04에서 백그라운드 변경 이벤트를 캡처하려고합니다. 클라이언트를 만들었고 백그라운드 값을 읽을 수 있지만 dconf 값 (dconf 편집기를 통해)을 변경하면 콜백 함수가 호출되지 않습니다.dconf watch 콜백 사용

콜백 기술은 어떻게 사용해야합니까?

감사합니다. 여기

코드입니다 :

#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 

답변

1

당신은 기다려야 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; 
} 
관련 문제