0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

download 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

of 23

Transcript of 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    1/23

    Maemo Diablo Source code for the GLib

    D-Bus signal example

    Training Material

    February 9, 2009

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    2/23

    Contents

    1 Source code for the GLib D-Bus signal example 21.1 glib-dbus-signals/common-defs.h . . . . . . . . . . . . . . . . . . 2

    1.2 glib-dbus-signals/value-dbus-interface.xml . . . . . . . . . . . . 31.3 glib-dbus-signals/server.c . . . . . . . . . . . . . . . . . . . . . . 41.4 glib-dbus-signals/client.c . . . . . . . . . . . . . . . . . . . . . . . 151.5 glib-dbus-signals/Makefile . . . . . . . . . . . . . . . . . . . . . . 20

    1

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    3/23

    Chapter 1

    Source code for the GLibD-Bus signal example

    1.1 glib-dbus-signals/common-defs.h

    #ifndef INCLUDE_COMMON_DEFS_H

    #define INCLUDE_COMMON_DEFS_H

    /* *

    * T h is m a em o c o de e x am p le i s l i ce n se d u n de r a M IT - s t yl e l ic e ns e ,

    * t ha t c an b e f ou nd i n t he f il e c al le d " L ic en se " i n t he s am e

    * d i re c to r y a s t h is f il e .

    * C o py r ig h t ( c ) 2 00 7 - 20 0 8 N o ki a C o rp o ra t io n . A ll r i gh t s r e se r ve d .

    *

    * T h is f il e i n cl u de s t he c o mm o n s y mb o li c d e fi n es f or b o th c l ie n t a nd

    * t he s e rv e r . N o r ma l ly t h is k i nd o f i n fo r ma t io n w o ul d b e p a r t o f t h e

    * o b je c t u s ag e d o cu m en t at i on , b ut i n t h is e x am p le w e t a ke t he e a sy

    * w ay o ut .

    *

    * T o r e - i t er a te : Y ou c o ul d j u st a s e a si l y u s e s t ri n gs i n b o th c l ie n t

    * and server , and that would be the more common way .

    */

    / * W el l - k no w n n a me f or t h is s e rv i ce . * /

    #define VALUE_SERVICE_NAME "org.maemo.Platdev_ex"

    / * O b je c t p a th t o t he p r ov i de d o b je c t . * /

    #define VALUE_SERVICE_OBJECT_PATH "/GlobalValue"

    / * A nd w e r e i n te r es t ed i n u s in g i t t h ro u gh t hi s i n te r fa c e .

    T h is m us t m a tc h t he e n tr y i n t he i n te r fa c e d e fi n it i on X ML . * /

    #define VALUE_SERVICE_INTERFACE "org.maemo.Value"

    / * S y mb o li c c o ns t an t s f or t h e s i g n al n a me s t o u se w i th G L ib .

    T he s e n e e d t o m a p i n t o t h e D - B u s s i gn a l n a m e s. * /

    #define SIGNAL_CHANGED_VALUE1 "changed_value1"

    #define SIGNAL_CHANGED_VALUE2 "changed_value2"

    #define SIGNAL_OUTOFRANGE_VALUE1 "outofrange_value1"

    #define SIGNAL_OUTOFRANGE_VALUE2 "outofrange_value2"

    #endif

    Listing 1.1: glib-dbus-signals/common-defs.h

    2

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    4/23

    1.2 glib-dbus-signals/value-dbus-interface.xml

    < ? x m l v e r s io n ="1.0" encoding="UTF-8" ?>

    t ha t c an b e f ou nd i n t he f il e c al le d "License" i n t h e s a me

    d i re c to r y a s t hi s f i le .

    C o p y ri g h t ( c ) 2 0 0 7 - 20 0 8 N o k ia C o r p o ra t i o n . A l l r i g ht s r e s e rv e d . - -

    >

    w i l l h o l d a s i mp l e s t at e c o ns i st i ng o f t wo v a lu e s ( o ne a 32-bit

    i n te g e r , t h e o t h e r a double).

    Th e object w i l l a l w ay s g e n er a t e a s i g na l w h e n a v a lu e i s c h a n ge d

    ( c h a n g e d_ v a l u e1 o r c h a n ge d _ v a lu e 2 ) .

    I t h as a ls o a m in a nd m ax t h re s ho l ds : w he n a c l ie n t t r ie s t o

    s et t he value t oo h ig h o r t oo l ow , t he object w i l l g e n e ra t e a

    s i g na l ( o u t o f r an g e _ v al u e 1 o r o u t o f ra n g e _ va l u e 2 ) .

    T he t h re s ho l ds a re n ot m o di f ia b le ( n or v i ew a bl e ) v ia t h is

    i n te r fa c e . T h e y a re s p ec i fi e d i n i n te g er s a nd a p pl y t o b ot h

    i n t e rn a l v a l ue s . A d d in g p e r -value t h r e sh o l d s w o u l d b e a good

    i d e a . G e n e r a l iz i n g t h e w h o le i n t e rf a c e t o s u p p or t multiple

    c o nc u rr e nt v a lu e s w o ul d b e a n ot h er g o od i d ea .

    T h e i n t e rf a c e name is "org.maemo.Value" .

    O n e k n o w n r e f e re n c e i m p l e me n t a ti o n i s p r o v id e d fo r i t b y t he

    "/GlobalValue" object f o u nd v i a a well-known name of

    "org.maemo.Platdev_ex" . -- >

    s o t h at D - B us i n tr o sp e ct i on t o ol s a re m o re u s ef u l .

    O t h e rw i s e t h e a r g u me n t s w i l l b e a u t o m at i c a ll y n a m ed

    "arg0" , "arg1" a nd s o o n . - ->

    3

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    5/23

    a c t u al l y e n f o rc e t h e s i g n a l a r g u me n t s _ a t _ a l l _ . S i g n a l s n e e d

    t o b e d e cl a re d i n o r d e r t o b e p a ss e d t h r o ug h t he b us i ts el f ,

    b ut o t he r wi s e n o c h ec k s a re d o ne ! Fo r e x a mp l e , y o u c o u ld

    l e av e t he s i gn a l a r gu m en t s u n sp e ci f ie d c o mp l et e ly , a n d t he

    code w o ul d s t il l w or k . - ->

    W e s e nd a s t ri n g p a ra m et e r w i th t h em . T he y n e ve r c an h a ve

    a r g u me n t s w i t h d i r e c t i o n = in . - ->

    t h e i n t e r n a l l y c o n f i g u r ed r a n ge ( t h r e s ho l d s ) . - - >

    Listing 1.2: glib-dbus-signals/value-dbus-interface.xml

    1.3 glib-dbus-signals/server.c

    /* *

    * T h is p r og r am i m pl e me n ts a G O bj e ct f or a s i mp l e c l a ss , t he n

    * r e gi s te r s t h e o b je c t o n t he D - B us a nd s t ar t s s e rv i ng r e qu e st s .

    *

    * T h is m a em o c o de e x am p le i s l i ce n se d u n de r a M IT - s t yl e l ic e ns e ,* t ha t c an b e f ou nd i n t he f il e c al le d " L ic en se " i n t he s am e

    * d i re c to r y a s t h is f il e .

    * C o py r ig h t ( c ) 2 00 7 - 20 0 8 N o ki a C o rp o ra t io n . A ll r i gh t s r e se r ve d .

    *

    * I n m o re c o mp l ic a te d c od e , t he G O bj e ct d e fi n it i on , i m pl e me n ta t io n

    * a nd D - B us r e gi s tr a ti o n w o ul d a ll l i ve i n s e pa r at e f i le s . I n t h is

    * s er v er , t h ey r e a l l p r es e nt i n t hi s f il e .

    *

    * T h is p r og r am w i ll p ul l t h e a u to m at i ca l ly g e ne r at e d D - B us / G Li b s tu b

    * c o de ( w h ic h c o nt a in s t he m a rs h al i ng c od e a s w el l a s a b i nd i ng / c al l

    * t ab l e .

    *

    * T h is p r og r am a l so m i gh t s e rv e a s a n i n tr o du c ti o n i nt o i m pl e me n ti n g

    4

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    6/23

    * c u st o m G T yp e / GO bj e ct s , b u t i t i s n ot t he p r im a ry p u rp o se h er e .

    * I m po r ta n t t h in g s l i ke o b je c t l if e - t im e m a na g em e nt ( f r ee i ng o f

    * o b je c ts ) , s ub - c l as s in g a n d G O bj e ct p r op e rt i es a re n ot c o ve r ed a t

    * a ll .

    *

    * D e mo n st r at e s a s i mp l e i m pl e me n ta t io n o f " t r ac i ng " a s w el l ( v ia t he

    * N O _D A EM O N d e fi ne , a s w h en b ui l t a s n on - d a e mo n iz i ng v er s io n , w i ll

    * o u tp u t i n fo r ma t io n a b ou t w h at i s h a pp e ni n g a nd w h er e . A d di n g

    * t i me s ta m ps t o e ac h t r ac e m e ss a ge i s l ef t o ut ( s ee g e tt i me o fd a y ()

    * s y st e m c a ll f or a s i mp l e s o lu t io n ) .

    */

    #include

    #include

    #include / * e xi t , E X IT _ FA I LU R E * /

    #include / * d a em o n * /

    / * P u ll s y mb o li c c o ns t an t s t h at a re s h ar e d ( i n t hi s e x am p le ) b e tw e ent he c l ie n t a nd t he s e rv e r . * /

    #include "common-defs.h"

    /* *

    * D e fi n e e n um e ra t io n s f or t he d i ff e re n t s i gn a ls t h at w e c an g e ne r at e

    * ( s o t h a t w e c an r e fe r t o t h em w i th i n t he s ig n al s - a r ra y [ b el o w ]

    * u si n g s y m b ol i c n a m e s ). T h es e a re n ot t he s am e a s t h e s i gn a l n a m e

    * s t r in g s .

    *

    * N OT E : E _ S IG NA L_ CO UN T i s N OT a s ig na l e nu m. W e u se i t a s a

    * c on ve ni en t c on st an t g iv in g t he nu mb er of si gn al s d ef in ed so

    * far . It needs to be listed last .

    */

    typedef enum {

    E_SIGNAL_CHANGED_VALUE1,

    E_SIGNAL_CHANGED_VALUE2,E_SIGNAL_OUTOFRANGE_VALUE1,

    E_SIGNAL_OUTOFRANGE_VALUE2,

    E_SIGNAL_COUNT

    } V a l u e Si g n a l Nu m b e r ;

    typedef struct {

    / * T he p a re n t c la s s o b je c t s ta t e . * /

    G O b j ec t p a r e nt ;

    / * O ur f i rs t p er - o b je c t s t at e v a ri a bl e . * /

    g i n t v a l ue 1 ;

    / * O ur s e co n d p er - o b je c t s t at e v a ri a bl e . * /

    g d o u bl e v a l u e2 ;

    } V a l u eO b j e ct ;

    typedef struct {

    / * T he p a re n t c la s s s t at e . * /

    G O b j ec t C l as s p a r e nt ;

    / * T he m i ni m um n u mb e r u n d e r w h ic h v a lu e s w il l c a us e s i gn a ls t o b e

    e m i t te d . * /

    g i n t t h r e sh o l d M in ;

    / * T he m a xi m um n u mb e r o v e r w hi c h v a lu e s w i l l c a us e s i gn a ls t o b e

    e m i t te d . * /

    g i n t t h r e sh o l d M ax ;

    / * S i gn a ls c r ea t ed f or t h is c l as s . * /

    g u i nt s i g n al s [ E _ S I G N AL _ C O U NT ] ;

    } V a l u e Ob j e c tC l a s s ;

    / * F o rw a rd d e cl a ra t io n o f t h e f u n c ti o n t ha t w il l r e tu r n t h e G T yp e o f

    5

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    7/23

    t he V a lu e i m pl e me n ta t io n . N ot u s ed i n t h is p r og r am . * /

    G T y pe v a l u e _o b j e c t_ g e t _ ty p e (void );

    / * M ac r o f o r t h e a b o ve . I t i s c o mm o n t o d e fi n e m a c r os u s in g t h e

    n a m in g c o n v e nt i o n ( s e e n b e l ow ) f o r a l l G T y pe i m p l em e n ta t i o ns ,

    a nd t ha t s w hy w e r e g oi ng t o d o t ha t h er e a s w el l . * /

    #define VALUE_TYPE_OBJECT ( value_object_get_type () )

    #define V A L U E_ O B J E CT ( o b j e c t ) \

    ( G _ T Y P E _ CH E C K _ IN S T A N C E_ C A S T ( ( o b j ec t ) , \

    V A L U E_ T Y P E_ O B J EC T , V a l u eO b j e ct ) )

    #define V A L U E _O B J E C T_ C L A S S ( k la s s ) \

    ( G _ T Y P E _ CH E C K _ CL A S S _ CA S T ( ( k l as s ) , \

    VALUE_TYPE_OBJECT, ValueObjectClass))

    #define V A L U E _I S _ O B JE C T ( o b j ec t ) \

    ( G _ T Y P E _ CH E C K _ IN S T A N C E_ T Y P E ( ( o b j ec t ) , \

    VALUE_TYPE_OBJECT))

    #define V A L U E _I S _ O B JE C T _ C L AS S ( k l a s s ) \( G _ T Y P E _ CH E C K _ CL A S S _ TY P E ( ( k l as s ) , \

    VALUE_TYPE_OBJECT))

    #define V A L U E _O B J E C T _G E T _ C LA S S ( o b j ) \

    ( G _ T Y P E _ IN S T A N CE _ G E T _ CL A S S ( ( o b j ) , \

    VALUE_TYPE_OBJECT, ValueObjectClass))

    G _ D E F IN E _ T YP E ( V a l u eO b j ec t , v a l ue _ o b je c t , G _ T Y PE _ O B J EC T )

    /* *

    * S in c e t he s tu b g e ne r at o r w i ll r e fe r en c e t h e f u n c ti o ns f ro m a c a ll

    * t ab le , t he f u nc t io n s m u st b e d e cl a re d b e fo r e t he s tu b i s i n cl u de d .

    */

    g b o o le a n v a l u e _ ob j e c t _g e t v a lu e 1 ( V a l u e Ob j e c t * o b j , g i n t * v a l ue _ o ut ,

    G E r r or * * e r r or ) ;

    g b o o le a n v a l u e _ ob j e c t _g e t v a lu e 2 ( V a l u e Ob j e c t * o b j , g d o ub l e * v a l ue _ o ut ,

    G E r r or * * e r r or ) ;g b o o le a n v a l u e _ ob j e c t _s e t v a lu e 1 ( V a l u e Ob j e c t * o b j , g i n t v a l ue _ i n ,

    G E r r or * * e r r or ) ;

    g b o o le a n v a l u e _ ob j e c t _s e t v a lu e 2 ( V a l u e Ob j e c t * o b j , g d o ub l e v a l ue _ i n ,

    G E r r or * * e r r or ) ;

    /* *

    * P ul l i n t he s tu b f or t he s er ve r s id e .

    */

    #include "value-server-stub.h"

    / * A s m al l m a cr o t ha t w il l w r ap g _ pr i nt a nd e x pa n d t o e m pt y w he n

    s e rv e r w i ll d a em o ni z e . W e u se t h is t o a dd d e bu g gi n g i n fo o n

    t he s e rv e r s i d e , b ut i f s e rv e r w il l b e d ae m on i ze d , i t d o es n t

    m a ke s e ns e t o e ve n c o mp i le t h e c o d e i n .

    T he m a cr o i s q ui t e " h a i r y ", b ut v er y c o nv e ni e nt . * /

    #ifdef NO_DAEMON

    #define d b g ( f mt s t r , a r g s . .. ) \

    (g_print(PROGNAME " : % s: " fmtstr "\n" , _ _ f un c _ _ , # # a r gs ) )

    #else

    #define dbg(dummy...)

    #endif

    /* *

    * P er o b je c t i n it i al i ze r

    *

    * O n ly s et s u p i n te r na l s t at e ( b o t h v a lu e s s e t t o z e r o )

    */

    6

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    8/23

    static void v a l u e _o b j e c t_ i n i t ( V a l ue O b j ec t * o b j ) {

    dbg("Called" );

    g _ a ss e r t ( o bj ! = N U L L ) ;

    o bj - > v a lu e 1 = 0 ;

    o b j - > v a lu e 2 = 0 . 0 ;

    }

    /* *

    * P er c l as s i n it i al i ze r

    *

    * S e ts u p t h e t h re s ho l ds ( - 10 0 . . 1 00 ) , c r e a te s t he s i gn a ls t ha t w e

    * c an e m it f r om a n y o b j ec t o f t h i s c l as s a nd f i na l ly r e gi s te r s t h e

    * t yp e i nt o t he G Li b /D - Bu s w ra pp er s o t ha t i t m ay a dd i ts o wn m ag ic .

    */

    static void v a l u e _o b j e c t _c l a s s _i n i t ( V a l u eO b j e c tC l a s s * k l a ss ) {

    / * S i nc e a ll s i gn a ls h a ve t he s a me p r ot o ty p e ( e a c h w i ll g et o ne

    s t ri n g a s a p a ra m et e r ), w e c r e a te t he m i n a l o op b e lo w . T h e o nl y d i ff e re n ce b e tw e en t h em i s t he i n de x i n to t he k la ss - > s i gn a ls

    a rr ay , a nd t he s i gn a l n a me .

    S i nc e t he i n de x g oe s f ro m 0 t o E _ SI G NA L _C O UN T - 1 , w e j us t s p ec i fy

    t he s i gn a l n a m es i nt o a n a r ra y a nd i t er a te o ve r i t .

    N ot e t h at t he o r de r h e re m us t c o rr e sp o nd t o t h e o r de r o f t h e

    e n u m e ra t i o ns b e f or e . * /

    const g c h ar * s i g n al N a m es [ E _ S I G N A L_ C O U N T ] = {

    SIGNAL_CHANGED_VALUE1,

    SIGNAL_CHANGED_VALUE2,

    SIGNAL_OUTOFRANGE_VALUE1,

    SIGNAL_OUTOFRANGE_VALUE2 };

    / * L oo p v a ri a bl e * /

    in t i;

    dbg("Called" );

    g _ a ss e r t ( k l as s ! = N U L L ) ;

    / * S e tu p s a ne m i ni m um s a nd m a xi m um s f or t he t h re s ho l ds . T h er e i s n o

    w ay t o c h an g e t h es e a f te r wa r ds ( c u rr e nt l y ) , s o y o u c a n c o ns i de r

    t he m a s c o ns t an t s . * /

    k l as s - > t h r e s h ol d M i n = - 1 00 ;

    k l as s - > t h r e s h ol d M a x = 1 0 0 ;

    dbg(" C r e a ti n g s i g na l s ");

    / * C r e a te t he s i gn a ls i n o n e l oo p , s i nc e t he y a ll a r e s i m i la r

    ( e x ce p t f o r t h e n a me s ) . * /

    fo r ( i = 0 ; i < E _S IG NA L_ CO UN T ; i + + ) {

    g u i nt s i g n al I d ;

    / * M o st o f t he t i me y ou w i ll e n co u nt e r t h e f o l l ow i ng c od e w i th o ut

    c o mm e nt s . T hi s i s w hy a ll t he p a ra m et e rs a re d o cu m en t ed

    d i r e ct l y b e l ow . * /

    s i g n al I d =

    g_signal_new(signalNames[i], / * s t r n am e o f t h e s ig na l * /

    / * G Ty pe t o w hi ch s ig na l i s b ou nd t o * /

    G_OBJECT_CLASS_TYPE(klass),

    / * C o mb i na t io n o f G S ig n al F la g s w h ic h t el l t he

    s i gn a l d i sp a tc h m a ch i ne r y h ow a n d w he n t o

    d i sp a tc h t hi s s i gn a l . T h e m os t c o mm o n i s t he

    G _ S I G NA L _ R U N_ L A S T s p e c i fi c a t io n . * /

    G_SIGNAL_RUN_LAST,

    7

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    9/23

    / * O f fs e t i n t o t h e c l a ss s t ru c tu r e f or t he t y pe

    f u n c ti o n p o i n te r . S i n ce w e r e i m p l e m e nt i n g a

    s i mp l e c la s s /t yp e , w e l l l e av e t h is a t z er o . * /

    0,

    / * G S ig n al A cc u mu l at o r t o u se . W e d o n t n e ed o ne . * /

    NULL,

    / * U se r - d at a t o p as s t o t h e a c c u mu l at o r . * /

    NULL,

    / * F u nc t io n t o u s e t o m a r s ha l t he s i gn a l d a t a i nt o

    t he p a ra m et e rs o f t he s i gn a l c a ll . L u ck i ly f o r

    u s , G Li b ( G C Cl o su r e ) a l re a dy d e fi n es j u st t he

    f u nc t io n t ha t w e w an t f or a s i gn a l h a nd l er t ha t

    w e d on t e x pe c t a ny r e tu r n v a lu e s ( v o i d ) a nd

    o ne t ha t w il l a c ce p t o n e s t ri n g a s p a ra m et e r

    ( b e si d es t he i n st a nc e p o in t er a n d p o in t er t o

    user-data).

    I f n o s u ch f u nc t io n w ou l d e x i st , y ou w o ul d n ee d t o c r ea t e a n e w o n e ( b y u s in g g li b - g en m ar s ha l

    t o o l ) . * /

    g_cclosure_marshal_VOID__STRING,

    / * R e tu r n G Ty p e o f t he r e tu r n v a lu e . T h e h a nd l er

    d oe s n ot r e tu r n a n yt h in g , s o w e u se G _ TY P E_ N ON E

    t o m ar k t ha t . * /

    G_TYPE_NONE,

    / * N u mb e r o f p a ra m et e r G T yp e s t o f o ll o w . * /

    1,

    / * G T yp e ( s) o f t h e p a ra m et e rs . W e o n l y h a ve o ne . * /

    G_TYPE_STRING);

    / * S to re t he s ig na l I d i nt o t he c la ss s ta te , s o t ha t w e c an u se

    i t l a te r . * /

    k l as s - > s i g n a ls [ i ] = s i g n al I d ;

    / * P r oc e ed w i th t he n e xt s i gn a l c r ea t io n . * / }

    / * A ll s i gn a ls c r ea t ed . * /

    dbg(" B i n d in g t o G L i b /D - B u s ");

    / * T im e t o b in d t hi s G T yp e i n to t h e G L i b /D - B us w r ap p er s .

    N OT E : T h is i s n ot y et " p u bl i sh i ng " t he o b je c t o n t he D - Bu s , b u t

    s in ce i t i s o nl y a ll ow ed t o d o t hi s o nc e p er c la ss

    c re at io n , t he s af es t p la ce t o p ut i t i s i n t he c la ss

    initializer.

    S p e c if i c a ll y , t h i s f u n c ti o n a d d s " m e t h o d i n t r o sp e c t io n

    d a ta " t o t h e c l as s s o t h a t m e th o ds c an b e c a l l ed o ve r

    t he D - B us . * /

    dbus_g_object_type_install_info(VALUE_TYPE_OBJECT,

    &dbus_glib_value_object_object_info);

    dbg("Done" );

    / * A l l d on e . C l a s s i s r e ad y t o b e u s ed f or i n st a nt i at i ng o b je c ts * /

    }

    /* *

    * U t il i ty h e lp e r t o e mi t a s i gn a l g i ve n w i th i n te r na l e n um e ra t io n a nd

    * t he p a ss e d s t r i ng a s t h e s i gn a l d a t a .

    *

    * U s ed i n t he s e tt e r f u nc t io n s b e lo w .

    */

    static void value_object_emitSignal(ValueObject * obj,

    V a l u e Si g n a l Nu m b e r n u m ,

    8

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    10/23

    const g ch a r * m e ss a ge ) {

    / * I n o rd er t o a cc es s t he s ig na l i de nt if ie rs , w e n ee d t o g et a h ol d

    o f t h e c l as s s t ru c tu r e f i rs t . * /

    V a l u e Ob j e c tC l a s s * k l a ss = V A L U E _O B J E C T_ G E T _ CL A S S ( o b j ) ;

    / * C he ck t ha t t he g iv en n um i s v al id ( a bo rt i f n ot ) .

    G i ve n t h at t h is f il e i s t he m o du l e a c tu a ll y u s in g t h is u t il i ty ,

    y ou c an c o ns i de r t hi s c h ec k s u pe r fl u ou s ( b ut u s ef u l f or

    d e v e l op m e n t w o r k ). * /

    g _ as s er t ( ( nu m < E _ SI G NA L _C O UN T ) & & ( n um > = 0 ) );

    dbg(" E m it t in g s i gn a l i d % d , w it h m e ss a ge % s ", n um , m e ss a ge ) ;

    / * T h i s i s t h e s i m p le s t w a y o f e m it t in g s i gn a ls . * /

    g_signal_emit( / * I n st a nc e o f t he o b je c t t h a t i s g e ne r at i ng t hi s

    s i gn a l . T h i s w i ll b e p a ss e d a s t he f i rs t p a ra m et e r

    t o t he s i gn a l h a nd l er ( e v en t ua l ly ) . B ut o b vi o us l y w h en s p ea k in g a b ou t D - Bu s , a s i gn a l c a ug h t o n t h e

    o t he r s i de o f D - B u s w il l b e f i rs t p r oc e ss e d b y

    t he G Li b - w ra p pe r s ( t he o b je c t p r ox y ) a n d o nl y t he n

    p r oc e ss e d b y t he s i gn a l h a nd l er . * /

    obj,

    / * S i g n al i d f o r t he s i gn a l t o g e ne r at e . T h es e a re

    s t or e d i n si d e t he c l as s s t at e s t ru c tu r e . * /

    klass->signals[num],

    / * D e ta i l o f s i gn a l . S i n ce w e a re n ot u s in g d e ta i le d

    s ig n al s , w e l e av e t h is a t z e ro ( d e fa u lt ) . * /

    0,

    / * D at a t o m ar sh al i nt o t he s ig na l . I n o ur c as e i t s

    j u st o ne s t ri n g . * /

    message);

    / * g _ si g na l _e m it r e tu r ns v oi d , s o w e c a nn o t c h ec k f o r s u cc e ss . * /

    / * D on e e m it t in g s i gn a l . * /

    }

    /* *

    * U t il i ty t o c h ec k t he g i ve n i n te g er a g ai n st t he t h re s ho l ds .

    * W i ll r e tu r n T R UE i f t h re s ho l ds a r e n o t e x ce e de d , F A LS E o t he r wi s e .

    *

    * U s ed i n t he s e tt e r f u nc t io n s b e lo w .

    */

    static g b o o le a n v a l u e _o b j e c t _t h r e s ho l d s O k ( V a lu e O b j ec t * o b j ,

    g i nt v a lu e ) {

    / * T h r e sh o ld s a re i n c l a s s s t a te d at a , g et a c ce s s t o i t * /

    V a l u e Ob j e c tC l a s s * k l a ss = V A L U E _O B J E C T_ G E T _ CL A S S ( o b j ) ;

    return ( ( v a l ue > = k l as s - > t h r e s h ol d M i n ) & &

    ( v a l ue < = k l as s - > t h r e s h ol d M a x ) ) ;

    }

    /* *

    * F u nc t io n t h at g et s c a ll e d w h en s o me o ne t ri e s t o e x ec u te " s e tv a lu e 1 "

    * o v er t h e D - B u s. ( A c tu a ll y t he m a rs h al l in g c o de f r om t he s t ub s g et s

    * e x ec u te d f ir st , b ut t h ey w i ll e v en t ua l ly e x ec u te t hi s f u nc t io n . )

    *

    * N O TE : I f y o u c h an g e t h e n a me o f t hi s f u nc t io n , t he g e ne r at e d

    * st ub s wi ll no lon ger fin d it ! On the oth er hand , if you

    * d ec id e to m odi fy t he i nte rf ace XML , th is is one of th e pl ac es

    * that you ll have to modify as well .

    9

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    11/23

    * T hi s a pp li es to th e n ex t f ou r f un ct io ns ( in cl ud in g t hi s o ne ).

    */

    g b o o le a n v a l u e _ ob j e c t _s e t v a lu e 1 ( V a l u e Ob j e c t * o b j , g i n t v a l ue I n ,

    G E rr o r ** e r ro r ) {

    dbg(" C a l l ed ( v a l u eI n = % d ) ", v a l ue I n ) ;

    g _ a ss e r t ( o bj ! = N U L L ) ;

    / * C o mp a re t h e c u r r en t v a lu e a g ai n st o l d o n e . I f t h ey r e t h e s am e ,

    w e d o n t n e ed t o d o a n yt h in g ( e x ce p t r e tu r n s u cc e ss ) . * /

    if ( o bj - > v a lu e 1 ! = v a lu e In ) {

    / * C h an g e t h e v a lu e . * /

    o b j - > v al u e 1 = v a l u eI n ;

    / * E m it t he " c h an g ed _ va l ue 1 " s i gn a l . * /

    value_object_emitSignal(obj, E_SIGNAL_CHANGED_VALUE1 , "value1" );

    / * I f n ew v a lu e f a ll s o u ts i de t h e t h re s ho l ds , e m it

    " o u t o f r an g e _ v al u e 1 " s i g na l a s w e l l . * /

    if ( ! v a l u e _o b j e c t_ t h r e sh o l d s O k ( ob j , v a l u eI n ) ) {

    value_object_emitSignal(obj, E_SIGNAL_OUTOFRANGE_VALUE1 ,

    "value1" );

    }

    }

    / * R e tu r n s u cc e ss t o G L ib / D - Bu s w r ap p er s . I n t hi s c as e w e d o n t n e ed

    t o t o uc h t he s u pp l ie d e r ro r p o in te r - p o in t er . * /

    return TRUE;

    }

    /* *

    * F u nc t io n t h at g et s e x ec u te d o n " s e tv a lu e 2 ".

    * O th e r t ha n t h is f u nc t io n o p er a ti n g w i th d i ff e re n t t y pe i n pu t

    * p a ra m et e r ( a nd d i ff e re n t i n te r na l v a lu e ) , a ll t he c o mm e nt s f r om * s e t_ v al u e1 a p pl y h e re a s w e ll .

    */

    g b o o le a n v a l u e _ ob j e c t _s e t v a lu e 2 ( V a l u e Ob j e c t * o b j , g d o ub l e v a l ue I n ,

    G E rr o r ** e r ro r ) {

    dbg(" C a l l ed ( v a l u eI n = % . 3 f ) ", v a l u eI n ) ;

    g _ a ss e r t ( o bj ! = N U L L ) ;

    / * N o rm a ll y c o mp a ri n g d o ub l es a g ai n st o t he r d o ub l es i s a b ad i de a ,

    s i nc e m u lt i pl e v a lu e s c an " c o ll i de " i n to o ne b i na r y

    r e pr e se n ta t io n . I n o ur c as e , i t i s n ot a r e al p r ob le m , a s w e r e

    n ot i n te r es t ed i n n u me r ic c o mp a ri s on , b u t t e st i ng w h et h er t he

    b i na r y c o nt e nt i s a b ou t t o c h an g e . A l so , a s t he v a lu e h as b e en

    s en t b y c l ie n t o v e r t h e D - Bu s , i t h a s a l r e ad y b e en r e du c ed . * /

    if ( o bj - > v a lu e 2 ! = v a lu e In ) {

    o b j - > v al u e 2 = v a l u eI n ;

    value_object_emitSignal(obj, E_SIGNAL_CHANGED_VALUE2 , "value2" );

    if ( ! v a l u e _o b j e c t_ t h r e sh o l d s O k ( ob j , v a l u eI n ) ) {

    value_object_emitSignal(obj, E_SIGNAL_OUTOFRANGE_VALUE2 ,

    "value2" );

    }

    }

    return TRUE;

    }

    10

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    12/23

    /* *

    * F u nc t io n t h at g et s e x ec u te d o n " g e tv a lu e 1 ".

    * W e d o n t s i g n al t he g et o p er a ti o ns , s o t h i s w il l b e s i mp l e .

    */

    g b o o le a n v a l u e _ ob j e c t _g e t v a lu e 1 ( V a l u e Ob j e c t * o b j , g i n t * v a l ue O u t ,

    G E rr o r ** e r ro r ) {

    dbg(" C a ll e d ( i n te r na l v a lu e 1 i s % d ) ", o b j - > v al u e 1 ) ;

    g _ a ss e r t ( o bj ! = N U L L ) ;

    / * C h e c k t h a t t h e t a rg e t p o i n te r i s n o t N U LL .

    E ve n i s t h e o n l y c a ll e r f o r t h is w i ll b e t he G Li b - w ra p pe r c od e ,

    w e c a nn o t t r u s t t h e s t ub g e ne r at e d c o de a nd s h ou l d h a nd l e t h e

    s i tu a ti o n . W e w i ll t e rm i na t e w it h a n e rr o r i n t h is c as e .

    A n ot h er o p ti o n w o u l d b e t o c r ea t e a n ew G Er ro r , a nd s t or e

    t he e r ro r c o nd i ti o n t h er e . * / g _ a ss e r t ( v a l ue O u t ! = N U L L ) ;

    / * C op y t he c u rr e nt f i rs t v a lu e t o c a ll e r s p ec i fi e d m e mo r y . * /

    * v a l u eO u t = o b j - > v al u e 1 ;

    / * R e tu r n s u cc e ss . * /

    return TRUE;

    }

    /* *

    * F u nc t io n t h at g et s e x ec u te d o n " g e tv a lu e 2 ".

    * ( A ga in , s i mi l ar t o " g e tv a lu e 1 ") .

    */

    g b o o le a n v a l u e _ ob j e c t _g e t v a lu e 2 ( V a l u e Ob j e c t * o b j , g d o ub l e * v a l ue O u t ,

    G E rr o r ** e r ro r ) {

    dbg(" C a ll e d ( i n te r na l v a lu e 2 i s % . 3f ) ", o b j - > v a lu e 2 ) ;

    g _ a ss e r t ( o bj ! = N U L L ) ;

    g _ a ss e r t ( v a l ue O u t ! = N U L L ) ;

    * v a l u eO u t = o b j - > v al u e 2 ;

    return TRUE;

    }

    /* *

    * P ri n t o u t a n e rr o r m e s s ag e a n d o p t i on a ll y q u it ( i f f a t al i s T R U E )

    */

    static void handleError( const char* m sg , const char * r ea so n ,

    g b oo l ea n f a ta l ) {

    g_printerr(PROGNAME " : E R RO R : % s ( % s) \ n ", m sg , r e as o n );

    if ( f a ta l ) {

    exit(EXIT_FAILURE);

    }

    }

    /* *

    * T he m a in s e rv e r c o d e

    *

    * 1 ) I n it G T yp e / G Ob j ec t

    * 2 ) C r e a te a m a in l oo p

    * 3 ) C on ne ct t o t he S es si on b us

    * 4 ) G et a p r ox y o b je c t r e pr e se n ti n g t he b us i t se l f

    * 5 ) R e g i st e r t h e w el l - k no w n n a m e b y w h ic h c l ie n ts c a n f i n d u s .

    * 6 ) C r ea t e o n e V a lu e o b je c t t ha t w il l h a nd l e a l l c l ie n t r e qu e st s .

    11

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    13/23

    * 7 ) R e gi s te r i t o n t he b us ( w il l b e f o un d v ia " / G lo b al V al u e " o b j e ct

    * path )

    * 8 ) D a em o ni z e t he p r oc e ss ( i f n o t b u il t w i th N O _D A EM O N )

    * 9 ) S t ar t p r oc e ss i ng r e qu e st s ( r un G M ai n Lo o p )

    *

    * T h is p r og r am w i ll n ot e x it ( u n le s s i t e n co u nt e rs c r it i ca l e r ro r s ).

    */

    in t main(in t argc, char * * a rg v ) {

    / * T he G O bj e ct r e pr e se n ti n g a D - B us c o nn e ct i on . * /

    D B u s G Co n n e c ti o n * b u s = N U L L ;

    / * P r ox y o b je c t r e pr e se n ti n g t he D - B us s e rv i ce o b je c t . * /

    D B u s GP r o x y * b u s P r o x y = N U L L ;

    / * W il l h o ld o ne i n st a nc e o f V a lu e Ob j ec t t ha t w il l s e rv e a ll t he

    r e q u st s . * /

    V a l u eO b j e ct * v a l ue O b j = N U L L ;

    / * G M ai n Lo o p f or o ur p r og r am . * /

    G M a i nL o o p * m a i n lo o p = N U L L ;

    / * W i l l s t or e t he r e su l t o f t h e R e q u es t Na m e R P C h e re . * / g u i nt r e s ul t ;

    G E rr o r * e r ro r = N UL L ;

    / * I n it i al i ze t he G T yp e / G Ob j ec t s y st e m . * /

    g_type_init();

    / * C r ea t e a m a in l o op t h at w i ll d i sp a tc h c a ll b ac k s . * /

    ma i n l oo p = g _ m a i n_ l o o p _n e w ( N UL L , F A L SE ) ;

    if ( m a in l oo p = = N UL L ) {

    / * P r in t e r ro r a nd t e rm i na t e . * /

    handleError( " C o u l dn t c r e at e G M a i nL o o p ", "Unknown(OOM?)" , T R UE ) ;

    }

    g_print(PROGNAME " : m ai n C o nn e ct i ng t o t he S e ss i on D - B us . \ n");

    b u s = d b u s _ g_ b u s _g e t ( D B U S _B U S _ SE S S I ON , & e r r or ) ;

    if ( e rr or ! = N UL L ) {/ * P r in t e r ro r a nd t e rm i na t e . * /

    handleError( " C o ul d n t c o nn e ct t o s e ss i on b us ", e r ro r - > m e s sa g e , T R U E

    );

    }

    g_print(PROGNAME " : m a in R e g i st e r i n g t h e w e ll - k n o w n n a m e ( % s ) \ n ",

    VALUE_SERVICE_NAME);

    / * I n o rd er t o r eg is te r a w el l - kn ow n n am e , w e n ee d t o u se t he

    " R e q u e s tM e t h o d " o f t h e / o r g / f r ee d e s k to p / D B u s i n t e rf a c e . E a c h

    b us p r ov i de s a n o b je c t t h at w i ll i m pl e me n t t hi s i n te r fa c e .

    I n o rd er t o d o t he c al l , w e n ee d a p ro xy o bj ec t f ir st .

    D B U S _ SE R V I C E_ D B U S = " o r g . f r e ed e s k to p . D B u s "

    D B U S _ PA T H _ D BU S = " / o r g / f re e d e s kt o p / D B u s "

    D B U S _ I NT E R F A CE _ D B U S = " o r g . f r ee d e s kt o p . D B u s " * /

    b u s Pr o x y = d b u s _ g _p r o x y _n e w _ f or _ n a m e ( bu s ,

    DBUS_SERVICE_DBUS,

    DBUS_PATH_DBUS,

    DBUS_INTERFACE_DBUS);

    if ( b u sP r ox y = = N UL L ) {

    handleError( " F ai le d t o g et a p ro xy f or D - Bu s" ,

    "Unknown(dbus_g_proxy_new_for_name)" , T R UE ) ;

    }

    / * A t te m pt t o r e gi s te r t he w el l - k no w n n a me .

    T he R PC c al l r e qu i re s t wo p a ra m et e rs :

    - a r g0 : ( D - Bu s S T RI N G ): n a me t o r e qu e st

    12

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    14/23

    - a r g1 : ( D - Bu s U I NT 3 2 ): f la g s f or r e gi s tr a ti o n .

    ( p l e a se s e e " o r g . f r ee d e s k to p . D B u s . R e qu e s t Na m e " i n

    http://dbus.freedesktop.org/doc/dbus-specification.html)

    W il l r e tu r n o n e u i nt 3 2 g i v i ng t he r e su l t o f t he R PC c al l .

    W e re i n te r es t ed i n 1 ( w e r e n o w t he p r im a ry o w ne r o f t h e n a me )

    o r 4 ( we w er e a lr ea dy t he o wn er o f t he n am e , h ow ev er i n t hi s

    a p pl i ca t io n i t w o ul d n t m a ke m uc h s e ns e ) .

    T he f u nc t io n w il l r e tu r n F A L S E i f i t s e t s t he G E rr o r . * /

    if (!dbus_g_proxy_call(busProxy,

    / * M et ho d n am e t o c al l . * /

    "RequestName" ,

    / * W h e r e t o s t or e t h e G E r ro r . * /

    &error,

    / * P a ra m et e r t y pe o f a r gu m en t 0 . N o te t h at

    s i n ce w e r e d e a l i n g w i t h G L i b /D - B u s

    w r ap p er s , y ou w i ll n ee d t o f i nd a s u it a bl e

    G T yp e i n st e ad o f u s in g t he " n a ti v e " D - B u st y pe c o de s . * /

    G_TYPE_STRING,

    / * D at a o f a rg um en t 0 . I n o ur c as e , t hi s i s

    t he w el l - k no w n n am e f or o u r s e rv e r

    e x a m pl e ( " o r g . m ae m o . P l a t de v _ e x " ) . * /

    VALUE_SERVICE_NAME,

    / * P a ra m et e r t y pe o f a r gu m en t 1 . * /

    G_TYPE_UINT,

    / * D at a o f a rg um en t 0 . T hi s i s t he " f la gs "

    a r g um e n t o f t h e " R e q u e s t Na m e " m e t h od w h i ch

    c an b e u se t o s p ec i fy w h at t he b us s e rv i ce

    s h ou l d d o w h en t h e n a m e a l re a dy e x is t s o n

    t he b us . W e l l g o w i th d e fa u lt s . * /

    0,

    / * I n pu t a r gu m en t s a re t e rm i na t ed w i th a

    s p ec i al G T yp e m a rk e r . * / G_TYPE_INVALID,

    / * P a ra m et e r t y pe o f r e tu r n v a lu e 0 .

    F or " R e qu e st N am e " i t i s U I NT 3 2 s o w e p ic k

    t he G T yp e t h at m ap s i nt o U I NT 3 2 i n t he

    w r a pp e r s . * /

    G_TYPE_UINT,

    / * D a t a o f r e t u rn v a lu e 0 . T h es e w i ll a l wa y s

    b e p o in t er s t o t h e l o ca t io n s w h er e t he

    p r ox y c a n c o p y t h e r e su l ts . * /

    &result,

    / * T e rm i na t e l i st o f r e tu r n v a lu e s . * /

    G _ T Y P E_ I N V AL I D ) ) {

    handleError( " D - B u s . R eq u e s tN a m e R P C f a i le d " , e r ro r - > m e s sa g e ,

    TRUE);

    / * N o t e t h at t h e w h o le c a ll f ai le d , n ot " j u st " t he n a me

    r e gi s tr a ti o n ( w e d e al w i th t ha t b e lo w ) . T hi s m e an s t h at

    s o me t hi n g b a d p r ob a bl y h a s h a pp e ne d a n d t h er e s n o t m u ch w e c an

    d o ( h en c e p r og r am t e rm i na t io n ) . * /

    }

    / * C h ec k t he r e su l t c od e o f t h e r e g i st r at i on R PC . * /

    g_print(PROGNAME " : m a in R e q u es t N a m e r e t u rn e d % d . \ n ", r e su l t );

    if ( r es ul t ! = 1 ) {

    handleError( " F a il e d t o g et t he p r im a ry w el l - k no w n n a me . ",

    " R e qu e st N am e r e su l t ! = 1 ", T RU E ) ;

    / * I n t h is c a se w e c o ul d a l so c o nt i nu e i n st e ad o f t e rm i na t in g .

    W e c ou ld r et ry t he R PC l at er . O r " lu rk " o n t he b us w ai ti ng f or

    s om eo ne t o t el l u s w ha t t o d o. I f w e w ou ld b e p ub li sh in g

    mu l t i pl e s e r v ic e s a n d / or i n t er f a ce s , i t e v en m i g ht m a k e s e n s e

    13

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    15/23

    t o c o nt i nu e w i th t he r e st a n yw a y .

    I n o u r s i mp l e p r o gr a m , w e t e rm i na t e . N o t m uc h l e ft t o d o f or

    t h is p o or p r og r am i f t h e c l ie n ts w on t b e a b le t o f i n d t he

    V a lu e o b je c t u s in g t h e w el l - k no w n n a me . * /

    }

    g_print(PROGNAME " : m a in C r e a ti n g o n e V a l u e o b j e ct . \ n ");

    / * T he N UL L a t t he e nd m ea ns t ha t w e h av e s to pp ed l is ti ng t he

    p r op e rt y n a me s a nd t h ei r v a lu e s t h at w ou l d h av e b e en u s ed t o

    s et t he p r op e rt i es t o i n it i al v a lu e s . O u r s i mp l e V a lu e

    i m p l e me n t a t io n d o e s n o t s u p po r t G O b je c t p r o pe r t ie s , a n d a l s o

    d o e sn t i n h e ri t a n y t hi n g i n t e re s t i n g f r o m G O b j ec t d i r ec t l y , s o

    t h er e a re n o p r op e rt i es t o s et . F or m o re e x am p le s o n p r op e rt i es

    s ee t he f i rs t G TK + e x am p le p r og r am s f r om t he A p pl i ca t io n

    D e v e l op m e n t m a t e ri a l .

    N OT E : Y ou n e ed t o k e ep a t l e as t o ne r e fe r en c e t o t he p u bl i sh e d o b je c t a t a ll t im es , u n le s s y o u w an t i t t o d i sa p pe a r f r om

    t he D - B us ( i m pl i ed b y A PI r e fe r en c e f or

    dbus_g_connection_register_g_object (). */

    v a l ue O b j = g _ o b j ec t _ n ew ( V A L U E _ TY P E _ OB J E C T , N U L L ) ;

    if ( v a lu e Ob j = = N UL L ) {

    handleError( " F a il e d t o c r ea t e o n e V a lu e i n st a nc e . ",

    "Unknown(OOM?)" , T R UE ) ;

    }

    g_print(PROGNAME " : m ai n R e gi s te r in g i t o n t he D - B us . \ n");

    / * T h e f u nc t io n d oe s n ot r e tu r n a n y s ta t us , s o c a n t c h e ck f or

    e r ro r s h e re . * /

    dbus_g_connection_register_g_object(bus,

    VALUE_SERVICE_OBJECT_PATH,

    G_OBJECT(valueObj));

    g_print(PROGNAME " : m a in R e a dy t o s e r ve r e q ue s t s ( d a e m on i z i n g ) .\ n ");

    #ifndef NO_DAEMON

    / * T hi s w i ll a t te m pt t o d a em o ni z e t h is p r oc e ss . I t w i ll s w it c h t hi s

    p r oc e ss w o rk i ng d i re c to r y t o / ( c h di r ) a nd t h en r e op e n s td in ,

    s t do u t a nd s t de r r t o / d ev / n ul l . W h ic h m e an s t h at a ll p r in t ou t s

    t ha t w o ul d o cc u r a f te r t hi s , w il l b e l o st . O b vi o us l y t he

    d a em o ni z at i on w i ll a l so d e ta c h t he p r oc e ss f ro m t he c o nt r ol l in g

    t e rm i na l a s w el l . * /

    if ( d ae mo n (0 , 0 ) ! = 0 ) {

    g_error(PROGNAME " : F a il e d t o d a em o ni z e .\ n ");

    }

    #else

    g_print(PROGNAME

    " : N o t d a e m on i z i n g ( b u i l t w i t h N O _ D A E MO N - b u i l d d e f in e ) \ n ");

    #endif

    / * S t ar t s e rv i ce r e qu e st s o n t he D - B us f o re v er . * /

    g_main_loop_run(mainloop);

    / * T hi s p r og r am w il l n ot t e rm i na t e u n le s s t h er e i s a c r it i ca l

    e r ro r w h ic h w il l c a us e a b or t () t o b e u s ed . H e nc e i t w il l n e ve r

    r e ac h t h is p oi n t . * /

    / * I f i t d oe s , r et ur n f ai lu re e xi t c od e j us t i n c as e . * /

    return EXIT_FAILURE;

    }

    14

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    16/23

    Listing 1.3: glib-dbus-signals/server.c

    1.4 glib-dbus-signals/client.c

    /* *

    * A s im pl e t es t p ro gr am t o t es t u si ng o f t he V al ue o bj ec t o ve r t he

    * D - B us .

    *

    * T h is m a em o c o de e x am p le i s l i ce n se d u n de r a M IT - s t yl e l ic e ns e ,

    * t ha t c an b e f ou nd i n t he f il e c al le d " L ic en se " i n t he s am e

    * d i re c to r y a s t h is f il e .

    * C o py r ig h t ( c ) 2 00 7 - 20 0 8 N o ki a C o rp o ra t io n . A ll r i gh t s r e se r ve d .

    *

    * I t w i ll f i rs t c r ea t e a G Li b / D- B us p r ox y o b je c t c o nn e ct e d t o t he

    * s e rv e r s V a lu e o bj ec t , a n d t h e n s t ar t f i ri n g a wa y v a lu e s et

    * m e t ho d s .

    *

    * T he m e th o ds a re f i re d f r om a t i me r b a se d c a ll b ac k o n ce p er s e co n d .

    * I f t h e m e th o d c a l l s f a i l ( t h e o n es t h at m o di f y t h e v a lu e s ), t he

    * p ro gr am w il l s ti ll c on ti nu e t o r un ( so t ha t t he s er ve r c an b e

    * r e s t ar t e d i f n e c e ss a r y ) .

    *

    * A l so d e mo n st r at e s l i st e ni n g t o / ca t ch i ng o f D - B us s i gn a ls ( u s in g t he

    * G S ig n al m e ch a ni s m , i nt o w h ic h D - B us s i gn a ls a re a u to m at i ca l ly

    * c o nv e rt e d b y t he G Li b - b in d in g s .

    *

    * W h en v a lu e c h an g ed s i gn a ls a r e r ec e iv e d , w il l a l so r e qu e st t he

    * V al u e o b j e ct f or t he c u rr e nt v a lu e a nd p ri n t t h a t o ut .

    */

    #include

    #include

    #include / * e xi t , E X IT _ FA I LU R E * /

    #include / * s t rc m p * /

    / * P u ll t h e c o mm o n s y mb o li c d e fi n es . * /

    #include "common-defs.h"

    / * P u ll i n t he c l ie n t s t ub s t h at w e re g e ne r at e d w it h

    d b us - b i n d i ng - t o o l * /

    #include "value-client-stub.h"

    /* *

    * P ri n t o u t a n e rr o r m e s s ag e a n d o p t i on a ll y q u it ( i f f a t al i s T R U E )

    */static void handleError( const char* m sg , const char * r ea so n ,

    g b oo l ea n f a ta l ) {

    g_printerr(PROGNAME " : E R RO R : % s ( % s) \ n ", m sg , r e as o n );

    if ( f a ta l ) {

    exit(EXIT_FAILURE);

    }

    }

    /* *

    * H a nd l es s i gn a ls f r om V al ue - o b j ec t w h en e i th e r v a lu e i s o u ts i de

    * t he t h re s ho l ds ( m in o r m a x) .

    *

    * W e u se s t rc m p i n te r na l ly t o d i ff e re n ti a te b e tw e en t he v a lu e s .

    15

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    17/23

    * N O TE : T hi s s i gn a l h a nd l er s p a ra m et e rs a re " s e le c te d " b y t h e s e rv e r

    * w he n i t c re at es th e s ig na ls , an d f il te re d b y t he in te rf ac e

    * X ML . U nf or tu na te ly db us - bi nd in g -t oo l d oe s n ot kn ow ho w t o

    * e nf or ce th e a rg um en t s pe ci fi ca ti on s o f s ig na ls (f ro m X ML ),

    * so you ll ne ed to be car efu l to mat ch this f un ct io n wit h

    * t he e xa ct s ig na l c re at io n ( ma rs ha li ng i nf or ma ti on ).

    */

    static void outOfRangeSignalHandler(DBusGProxy * proxy,

    const char* v a l ue N a me ,

    g p o i nt e r u s e r Da t a ) {

    g_print(PROGNAME " : o ut - o f - r a n g e ( % s ) ! \ n" , v a l u eN a m e ) ;

    if (strcmp(valueName, "value1" ) = = 0 ) {

    g_print(PROGNAME " : o ut - o f - r a n g e V a l u e 1 i s o u t s id e t h r e sh o l d \ n ");

    } else {

    g_print(PROGNAME " : o ut - o f - r a n g e V a l u e 2 i s o u t s id e t h r e sh o l d \ n ");

    }

    }

    /* *

    * S i gn a l h a nd l er f or t he " c h an g ed " s i gn a ls . T h es e w i ll b e s e nt b y t he

    * V a lu e - o b j e c t w h e n e v e r i t s c o n t en t s c h a ng e ( w h e t he r w i t h in

    * t h re s ho l ds o r n o t) .

    *

    * L i ke b e fo re , w e u se s t rc m p t o d i ff e re n ti a te b e tw e en t he t wo

    * v al u es , a nd a ct a c co r di n gl y ( i n t hi s c a se b y r e tr i ev i ng

    * s y nc h ro n ou s ly t h e v a lu e s u s in g g e tv a lu e 1 o r g e tv a lu e 2 .

    *

    * N O TE : S i nc e w e u se s y nc h ro n ou s g et v al ue s , i t i s p o ss i bl e t o g et

    * thi s co de s tu ck if for so me r eas on t he s er ve r wou ld be st uck

    * in an eternal loop .

    */

    static void valueChangedSignalHandler( DBusGProxy* proxy,

    const char* v a l ue N a me ,g p o i nt e r u s e r Da t a ) {

    / * S i nc e m e th o d c a ll s o v er D - B us c an f ai l , w e ll n e ed t o c h ec k

    f or f a il u re s . T h e s e rv e r m i g h t b e s hu t d ow n i n t h e m i dd l e o f

    t hi ng s , o r m ig h t a c t b a dl y i n o t he r w a ys . * /

    G E rr o r * e r ro r = N UL L ;

    g_print(PROGNAME " : v a lu e - c h a n g ed ( % s ) \ n ", v a l u eN a m e ) ;

    / * F in d o ut w h ic h v a lu e c ha ng e d , a nd a c t a c c o rd i ng l y . * /

    if (strcmp(valueName, "value1" ) = = 0 ) {

    g in t v = 0 ;

    / * E xe cu te t he R PC t o g et v al ue 1 . * /

    o r g _ m a em o _ V a lu e _ g e tv a l u e 1 ( p ro x y , & v , & e r r or ) ;

    if ( e rr or = = N UL L ) {

    g_print(PROGNAME " : v a lu e - c h a n g ed V a l ue 1 n o w % d \ n " , v );

    } else {

    / * Y ou c o ul d i n te r ro g at e t he G E rr o r f u rt he r , t o f i nd o ut e x ac t ly

    w ha t t he e r ro r w as , b ut i n o u r c as e , w e l l j us t i g no r e t h e

    e r ro r w i th t he h o pe t h at s om e d a y ( p r e f er a bl y s o on ) , t he

    R PC w il l s u cc e ed a g ai n ( s e rv e r c o me s b a ck o n t he b us ) . * /

    handleError(" F a i l ed t o r e t r ie v e v a l ue 1 ", e r ro r - > m e s sa g e , F A L SE ) ;

    }

    } else {

    g do ub le v = 0 .0 ;

    o r g _ m a em o _ V a lu e _ g e tv a l u e 2 ( p ro x y , & v , & e r r or ) ;

    if ( e rr or = = N UL L ) {

    g_print(PROGNAME " : v a lu e - c h a n g ed V a l ue 2 n o w % . 3 f \ n" , v );

    } else {

    16

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    18/23

    handleError(" F a i l ed t o r e t r ie v e v a l ue 2 ", e r ro r - > m e s sa g e , F A L SE ) ;

    }

    }

    / * F r e e u p e r ro r o b je c t i f o ne w as a l lo c at e d . * /

    g_clear_error(&error);

    }

    /* *

    * T h is f u nc t io n w il l b e c a ll e d r e pe a te d ly f ro m w i th i n t he m a in l oo p

    * t im e r l a un c h c o de .

    *

    * T he f u nc t io n w i ll s t ar t w i th t wo s t at i ca l ly i n it i al i ze d v a ri a bl e s

    * ( i nt a n d d o u b le ) w h ic h w il l b e i n cr e me n te d a f te r e ac h t im e t h is

    * f u nc t io n r u ns a n d w i l l u s e t h e s e t v al u e * r e m ot e m e th o ds t o s e t t he

    * n ew v a lu e s . I f t he s et m e th o ds f ai l , p r og r am i s n o t a b or te d , b ut a n

    * m e ss a ge w i ll b e i s su e d t o t he u s er d e sc r ib i ng t he e r ro r .

    *

    * I t w i ll p u rp o se f ul l y s t ar t v a lu e 2 f r om b el o w t he d e fa u lt m i ni m um * t h re s ho l d ( s e t i n t he s e rv e r c o de ) .

    *

    * N O TE : T h er e i s a d e si g n i s s u e i n t he i m pl e me n ta t io n i n t h e V a lu e

    * o bj ec t: it do es no t p ro vi de "a dj us t" me th od wh ic h w ou ld ma ke

    * i t p os si bl e f or mu lt ip le cl ie nt s to a dj us t t he v al ue s ,

    * i ns te ad of se tt in g t he m s ep ar at el y. N ow , if yo u l au nc h

    * m ul ti pl e c li en ts (a t d if fe re nt ti me s) , t he ob je ct in te rn al

    * v al ue s w il l e nd u p f lu ct ua ti ng b et we en t he c li en ts .

    */

    static g b o o le a n t i m e rC a l l b ac k ( D B u s G Pr o x y * r e m o te o b j ) {

    / * L o ca l v a lu e s t ha t w e ll s ta r t u p da t in g t o t he r e mo t e o b je c t . * /

    static g in t l o ca l Va l ue 1 = - 80 ;

    static g d o u bl e l o c a lV a l u e 2 = - 1 2 0. 0 ;

    G E rr o r * e r ro r = N UL L ;

    / * S et t he f ir st v al ue . * /

    o r g _ m ae m o _ V a lu e _ s e tv a l u e 1 ( r em o t eo b j , l o c al V a lu e 1 , & e r r o r ) ;

    if ( e rr or ! = N UL L ) {

    handleError( " F a il e d t o s et v a lu e 1 ", e r ro r - > m e s sa g e , F A L SE ) ;

    } else {

    g_print(PROGNAME " : t im e rC a ll b ac k S et v a lu e 1 t o % d \ n" , l o c a lV a l u e1 ) ;

    }

    / * I f t h er e w as a n e r ro r w i th t he f ir st , r e le a se t he e rr or , a nd

    d on t a t te m pt t he s e co n d t im e . A l s o , d on t a dd t o t he l o ca l

    v a lu e s . W e a s su m e t h a t e r ro r s f r o m t he f i rs t s et a re c a us e d b y

    s e rv e r g o i ng o ff t he D - Bu s , b u t a re h o pe f ul t h at i t w i l l c o me

    b ac k , a nd h e nc e k ee p t r yi n g ( r e t u rn i ng T RU E ) . * /

    if ( e rr or ! = N UL L ) {

    g_clear_error(&error);

    return TRUE;

    }

    / * N o w t ry t o s e t t he s ec on d v al ue a s w e ll . * /

    o r g _ m ae m o _ V a lu e _ s e tv a l u e 2 ( r em o t eo b j , l o c al V a lu e 2 , & e r r o r ) ;

    if ( e rr or ! = N UL L ) {

    handleError( " F a il e d t o s et v a lu e 2 ", e r ro r - > m e s sa g e , F A L SE ) ;

    g_clear_error(&error); / * O r g _ er r or _ fr e e i n t h is c a se . * /

    } else {

    g_print(PROGNAME " : t i m e r Ca l l b a ck S e t v a l u e 2 t o % . 3 l f \n " ,

    localValue2);

    }

    17

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    19/23

    / * S te p t he l o ca l v a lu e s f o rw a rd . * /

    l o c a lV a l u e1 + = 1 0 ;

    l o c a lV a l u e2 + = 1 0 . 0;

    / * T e l l t he t i me r l a un c he r t h at w e w a n t t o r e m a in o n t h e t i me r

    c al l l i st i n t he f u tu r e a s w e ll . R e tu r ni n g F A LS E h e re w o ul d

    s to p t he l a un c h o f t h is t im e r c a ll b ac k . * /

    return TRUE;

    }

    /* *

    * T he t e st p r og r am i t se l f .

    *

    * 1 ) S e tu p G T yp e / G Si g na l

    * 2 ) C r ea t e G M ai n Lo o p o b je c t

    * 3 ) C on ne ct t o t he S es si on D - Bu s

    * 4 ) C r e a te a p r ox y G O bj e ct f or t he r e mo t e V a l u e o b j e ct* 5 ) R e gi s te r s i gn a ls t h at w e r e i n te r es t ed f r om t he V a lu e o b je c t

    * 6 ) R e gi s te r s i gn a l h a nd l er s f or t he m

    * 7 ) S t ar t a t i me r t h at w i ll l a un c h t i me r Ca l lb a ck o n ce p e r s e c o nd .

    * 8 ) R un m ai n - l oo p ( f o re v er )

    */

    in t main(in t argc, char * * a rg v ) {

    / * T he D - B us c o nn e ct i on o b je c t . P r ov i de d b y G L ib / D - Bu s w r ap p er s . * /

    D B u s G Co n n e c ti o n * b u s ;

    / * T hi s w i ll r e pr e se n t t he V al u e o b je c t l o ca l ly ( a c ti n g a s a p r ox y

    f or a ll m e th o d c a ll s a n d s i gn a l d e li v er y . * /

    D B u s GP r o x y * r e m o t eV a l u e ;

    / * T h i s w i ll r e fe r t o t h e G M ai n Lo o p o b je c t * /

    G M a i nL o o p * m a i n lo o p ;

    G E rr o r * e r ro r = N UL L ;

    / * I n it i al i ze t he G T yp e / G Ob j ec t s y st e m . * / g_type_init();

    / * C r ea t e a n ew G M ai n Lo o p w i th d e fa u lt c o nt e xt ( N UL L ) a n d i n it i al

    s t at e o f " n ot r u nn i ng " ( F AL S E ). * /

    ma i n l oo p = g _ m a i n_ l o o p _n e w ( N UL L , F A L SE ) ;

    / * F ai lu re t o c re at e t he m ai n l oo p i s f at al ( fo r u s) . * /

    if ( m a in l oo p = = N UL L ) {

    handleError( " F a il e d t o c r ea t e t h e m a in l oo p ", " U n k n ow n ( O O M ? ) ",

    TRUE);

    }

    g_print(PROGNAME " : m a in C o n n ec t i n g t o S e s s io n D - B u s . \ n");

    b u s = d b u s _ g_ b u s _g e t ( D B U S _B U S _ SE S S I ON , & e r r or ) ;

    if ( e rr or ! = N UL L ) {

    handleError( " C o ul d n t c o nn e ct t o t he S e ss i on b us ", e r ro r - > m e s sa g e ,

    TRUE);

    / * N o rm a ll y y ou d h av e t o a ls o g _ er r or _ fr e e () t he e r ro r o b je c t

    b ut s i nc e t h e p r og r am w i ll t e rm i na t e w i th i n h a nd l eE r ro r ,

    i t i s n o t n e c e ss a ry h e re . * /

    }

    g_print(PROGNAME " : m ai n C r ea t in g a G L ib p ro x y o b je c t f or V a lu e . \n ");

    / * C r e a te t he p ro x y o b j e ct t h at w e ll b e u s in g t o a c ce s s t h e o b je c t

    o n t h e s e r ve r . I f y o u w o u ld u s e d b u s _ g _ p r o x y_ f o r _ na m e _ o w ne r ( ) ,

    y ou w o ul d b e a ls o n o ti f ie d w h en t he s e rv e r t ha t i m pl e me n ts t he

    o b je c t i s r e mo v ed ( o r r at he r , t he i n te r fa c e i s r e mo v ed ) . S i nc e

    w e d o n t c a re w ho a c tu a ll y i m pl e me n ts t h e i nt e rf ac e , w e ll u se t he

    18

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    20/23

    m or e c o mm o n f u nc t io n . S ee t he A PI d o cu m en t at i on a t

    h t t p : // m a e m o . o rg / a p i _ r ef s / 4 . 0 / d bu s / f o r m o r e d e t ai l s . * /

    r e m o te V a l ue =

    dbus_g_proxy_new_for_name(bus,

    VALUE_SERVICE_NAME, / * n am e * /

    VALUE_SERVICE_OBJECT_PATH, / * o b j p at h * /

    VALUE_SERVICE_INTERFACE / * i n te r fa c e * / );

    if ( r e mo t eV a lu e = = N UL L ) {

    handleError( " C o ul d n t c r ea t e t he p r ox y o b je c t ",

    "Unknown(dbus_g_proxy_new_for_name)" , T R UE ) ;

    }

    / * R e gi s te r t he s i gn a tu r es f or t he s i gn a l h a nd l er s .

    I n o u r c a se , w e ll h a ve o ne s t ri n g p a ra m et e r p a ss e d t o u s e a l o ng

    t he s i gn a l i t se l f . T he p a ra m et e r l i st i s t e rm i na t ed w it h

    G _ TY P E_ I NV A LI D ( i . e. , t he G T yp e f or s t ri n g o b je c ts . * /

    g_print(PROGNAME " : m a in R e g i st e r i n g s i g na l h a n d le r s i g n at u r e s . \ n ");

    / * A dd t he a r gu m en t s i gn a tu r es f or t he s i gn a ls ( n ee d s t o b e d o ne

    b e fo r e c o nn e ct i ng t he s i gn a ls ) . T h is m ig h t g o a w ay i n t he f ut ur e ,

    w he n t he G Li b - b in d in g s w i ll d o a u to m at i c i n tr o sp e ct i on o ve r t h e

    D - Bu s , b ut f or n ow w e n e ed t he r e gi s tr a ti o n p h as e . * /

    { / * C r ea t e a l oc a l s c op e f or v a ri a bl e s . * /

    in t i;

    const g c h ar * s i g n al N a m e s [] = { S I G N AL _ C H A NG E D _ V AL U E 1 ,

    SIGNAL_CHANGED_VALUE2,

    SIGNAL_OUTOFRANGE_VALUE1,

    S I G N A L _O U T O F RA N G E _ VA L U E 2 } ;

    / * I t er a te o v er a ll t he e n tr i es i n t h e a b ov e a r ra y .

    T he u p pe r l i mi t f or i m ig h t s e e m s t ra n ge a t f i rs t g la nc e ,

    b ut i s q u it e c o mm o n i d io m t o e x tr a ct t he n u mb e r o f e l em e nt s

    i n a s t at i ca l ly a l lo c at e d a r ra y s i n C .N O TE : T h e i d io m w i ll n ot w o rk w i th d y na m ic a ll y a l lo c at e d

    a r ra y s . ( O r r a th e r i t w il l , b ut t he r e su l t i s p r ob a bl y

    n o t w h a t y ou e x pe c t .) * /

    fo r (i = 0; i < sizeof(signalNames)/ sizeof( s i g n a lN a m e s [ 0 ]) ; i + + ) {

    / * S i nc e t he f u nc t io n d o es n t r e tu r n a ny t hi n g , w e c a nn o t c h ec k

    f or e r ro r s h e re . * /

    dbus_g_proxy_add_signal( / * P r o xy t o u s e * /

    remoteValue,

    / * S i gn a l n a m e * /

    signalNames[i],

    / * W i ll r e ce i ve o ne s t ri n g a r gu m en t * /

    G_TYPE_STRING,

    / * T e rm i na t io n o f t he a r gu m en t l is t * /

    G_TYPE_INVALID);

    }

    } / * e n d o f l o c al s co pe * /

    g_print(PROGNAME " : m a in R e g i st e r i n g D - B u s s i g na l h a n d le r s . \ n ");

    / * W e c o nn e ct e a ch o f t h e f o ll o wi n g s i gn a ls o ne a t a t im e ,

    s i nc e w e l l b e u s in g t w o d i ff e re n t c a ll b ac k s . * /

    / * A g a in , n o r e tu r n v al ue s , c a nn o t h e n c e c h ec k f or e r ro r s . * /

    dbus_g_proxy_connect_signal( / * P r ox y o b je c t * /

    remoteValue,

    / * S i gn a l n a m e * /

    SIGNAL_CHANGED_VALUE1,

    / * S i gn a l h a n d le r t o u s e . N ot e t ha t t he

    19

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    21/23

    t y pe c as t i s j u st t o m a ke t he c o mp i le r

    h a pp y a b ou t t he f u nc t io n , s i nc e t h e

    pr o t o ty p e i s n o t c o m p at i b l e w i t h

    r e g ul a r s i g n al h a n d le r s . * /

    G_CALLBACK(valueChangedSignalHandler),

    / * U se r - d at a ( w e d o n t u se a ny ) . * /

    NULL,

    / * G C l o s ur e N o t if y f u n c ti o n t h a t i s

    r e s p on s i b le i n f r e e in g t h e p a s s e d

    u se r - d at a ( w e h av e n o d a ta ) . * /

    NULL);

    dbus_g_proxy_connect_signal( remoteValue, SIGNAL_CHANGED_VALUE2,

    G_CALLBACK(valueChangedSignalHandler),

    N U LL , N U L L ) ;

    dbus_g_proxy_connect_signal( remoteValue, SIGNAL_OUTOFRANGE_VALUE1,

    G_CALLBACK(outOfRangeSignalHandler),N U LL , N U L L ) ;

    dbus_g_proxy_connect_signal( remoteValue, SIGNAL_OUTOFRANGE_VALUE2,

    G_CALLBACK(outOfRangeSignalHandler),

    N U LL , N U L L ) ;

    / * A ll s i gn a ls a r e n o w r e gi s te r ed a nd w e re r e ad y t o h a nd l e t h em . * /

    g_print(PROGNAME " : m ai n S t ar t in g m ai n l o op ( f ir s t t i me r i n 1 s ) .\ n ");

    / * R e g i st e r a t im e r c a l l ba c k t h a t w i ll d o R P C s et s o n t h e v a lu e s .

    T he u s er d at a p o in t er i s u s e d t o p a s s t he p r ox y o b je c t t o t he

    c a ll b ac k s o t ha t i t c an l a un c h m o di f ic a ti o ns t o t he o b je c t . * /

    g _ t i me o u t _ ad d ( 1 0 00 , ( G S o u r c eF u n c ) t i m er C a ll b a c k , r e m o te V a l ue ) ;

    / * R u n t he p r og r am . * / g_main_loop_run(mainloop);

    / * S i n c e t h e m a in l o op i s n o t s t op p ed ( b y t h i s c o d e ) , w e s h ou l dn t

    e ve r g et h e re . T he p r og r am m i gh t a b or t ( ) f o r o t he r r e as o ns . * /

    / * I f i t d oe s , r et ur n f ai lu re a s e xi t c od e. * /

    return EXIT_FAILURE;

    }

    Listing 1.4: glib-dbus-signals/client.c

    1.5 glib-dbus-signals/Makefile

    # T h is i s a r e la t iv e ly s i mp l is t ic M a ke f il e s u it a bl e f o r p r oj e ct s w h ic h

    # u se t he G L ib b i nd i ng s f or D - B us .

    #

    # O ne e x tr a t a rg e t ( w hi c h r e qu i re s x ml l in t , f ro m p a ck a ge l ib x ml 2 - u t il s )

    # i s a v ai l ab l e t o v e ri f y t h e w el l - f o rm e dn e ss a nd t he s t ru c tu r e o f t he

    # i n te r fa c e d e fi n it i on x ml f i le .

    #

    # U se t he c he c kx ml t a rg e t t o r un t he i n te r fa c e X ML t h ro u gh x m ll i nt

    # v e ri f ic a ti o n . Y o u ll n e ed t o b e c o nn e ct e d t o t he I n te r ne t i n o r de r

    # f or x m ll i nt t o r e tr i ev e t h e D T D f ro m f d .o ( u n le s s y o u s e tu p l o ca l

    # c a ta l og s , w h ic h a re n ot c o ve r ed h er e ) .

    #

    # I f y o u w an t t o m ak e t he s e rv e r d a e mo n iz e d , p l ea s e s e e b e lo w f or t he

    20

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    22/23

    # N O_ D AE M ON s e tt i ng . C o mm e nt i ng t ha t l in e w il l d i sa b li n g t r ac i ng i n

    # t he s er ve r A ND m ak e i t i nt o a d ae mo n .

    # I n te r fa c e X ML n a me ( u se d i n m u lt i pl e t a rg e ts )

    i n t e r fa c e _ xm l : = v a lu e - d b u s - i n t er f a c e . xm l

    # D ef in e a l is t o f p kg - c on fi g p ac ka ge s w e w an t t o u se

    p k g _ p ac k a g e s : = d b us - 1 d b us - g l i b - 1

    P K G_ C FL A GS : = $ ( s he l l p kg - c o nf i g - - c fl a gs $ ( p kg _ pa c ka g es ) )

    P K G _ LD F L A G S : = $ ( s h e ll p k g - c o nf i g - - l i bs $ ( p k g _ p ac k a g e s ) )

    # A dd a d di t io n al f l ag s :

    # - g : a dd d e bu g gi n g s y mb o ls

    # - W al l : e n ab l e m o s t g cc w a rn i ng s

    # - D G _ D I SA B L E _ DE P R E C A TE D : d i s a bl e G L i b f u n c t i o ns m a r k ed a s d e p r ec a t e d

    A D D _ CF L A G S : = - g - W a l l - D G _ D I SA B L E _ DE P R E C AT E D

    # - D NO _ DA E MO N : d o n o t d a em o ni z e t h e s e rv e r ( o n a s e pa r at e l i ne s o c a n

    # be disabled just by commenting the line )A D D _ CF L A G S + = - D N O _ D AE M O N

    # C o mb i ne f l ag s

    C F LA G S : = $ ( P KG _ CF L AG S ) $ ( A DD _ CF L AG S ) $ ( C FL A GS )

    L D F L AG S : = $ ( P K G _ LD F L A G S ) $ ( L D F L A G S )

    # D ef in e a l is t o f g en er at ed f il es s o t ha t t he y c an b e c le an ed a s w el l

    c l e a nf i l e s : = v a lu e - c l i en t - s t u b . h \

    value-server-stub.h

    t a rg e ts = s e rv e r c l ie n t

    . P H O N Y : a l l c l e an c h e c kx m l

    a l l : $ ( t a r g et s )

    # W e d o n t u s e t he i m pl i ci t p a tt e rn r u le s b ui l t i n t o G NU m ak e , s i nc e# t h ey p ut t he L D FL A GS i n t h e w r on g l o ca t io n ( a nd l i nk i ng c o ns e qu e nt l y

    # f a i ls s o m e ti m e s ) .

    #

    # N O TE : Y o u c o ul d a c tu a ll y c o ll a ps e t he c o mp i la t io n a n d l i nk i ng p h as e s

    # t og et he r , b ut t hi s a rr an ge me nt i s m uc h m or e c om mo n.

    s e r ve r : s e r ve r . o

    $ ( CC ) $ ^ - o $ @ $ ( L DF L AG S )

    c l i en t : c l i en t . o

    $ ( CC ) $ ^ - o $ @ $ ( L DF L AG S )

    # T he s e rv e r a n d c l ie n t d e pe n d o n t h e r e sp e ct i ve i m pl e me n ta t io n s o ur c e

    # f il es , b ut a ls o o n t h e c o mm o n i n te r fa c e a s w e ll a s t h e g e ne r at e d

    # s t ub i n te r fa c es .

    s e r ve r . o : s e r v er . c c o mm o n - d e f s . h v a lu e - s e r ve r - s t u b . h

    $ ( C C ) $ ( C F L A GS ) - D P R O GN A M E = \" $ ( b a s e na m e $ @ ) \ " - c $ < -o $@

    c l i en t . o : c l i e nt . c c o mm o n - d e f s . h v a lu e - c l i en t - s t u b . h

    $ ( C C ) $ ( C F L A GS ) - D P R O GN A M E = \" $ ( b a s e na m e $ @ ) \ " - c $ < -o $@

    # I f t he i n te r fa c e X ML c ha n ge s , t he r e sp e ct i ve s tu b i n te r fa c es w i ll b e

    # a u to m at i ca l ly r e ge n er a te d . N o rm a ll y t h is w o ul d a l so m e an t h at y o ur

    # b u il d s w o ul d f a il a f te r t hi s s i nc e y ou d b e m i ss i ng i m pl e me n ta t io n

    # c o de .

    v a lu e - s e r ve r - s t u b . h : $ ( i n t e r fa c e _ x ml )

    d b us - b i n d in g - t o o l - - p r e fi x = v a l u e _o b j e c t - - m o de = g l ib - s e r v e r \

    $ < > $@

    21

  • 8/4/2019 0a283372fdb911dd80aed97025304c974c97 Source Code for the Glib D-bus Signal Example

    23/23

    v a lu e - c l i en t - s t u b . h : $ ( i n t e r fa c e _ x ml )

    d b us - b i n d in g - t o o l - - p r e fi x = v a l u e _o b j e c t - - m o de = g l ib - c l i e n t \

    $ < > $@

    # S p ec i al t a rg e t t o r un D TD v a li d at i on o n t h e i n te r fa c e X M L . N ot r u n

    # a u to m at i ca l ly ( s i nc e x m ll i nt i sn t a l wa y s a v ai l ab l e a nd a l so n e ed s

    # I n t er n e t c o n n ec t i v i ty ) .

    c h e c kx m l : $ ( i n t e r fa c e _ x ml )

    @ x m ll i n t - - v a li d - - n o ou t $