[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freedesktop-xorg
Subject:    Re: Any reliable way to find the X11 display id and authority file for local session running in actu
From:       Sai Prasanna <sai.r.prasanna () gmail ! com>
Date:       2017-08-11 11:58:14
Message-ID: CAHcjWa6DxgOiT-fQLgSazuVi1tpZT6FyS8FtqABS_gY7gLoRyg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thanks for the help.

I am developing in go lang and found a perfect binding in dbusbindings.

The logind dbus api was not intutive at first,  but figured it out after
some examples.


Posting the  go lang POC of above loginctl code to get active display. (So
anyone following this thread can spend less effort).




package main

import (
    "fmt"
    "os"

    "github.com/godbus/dbus"
)

func main() {
    conn, err := dbus.SystemBus()
    if err != nil {
        fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err)
        os.Exit(1)
    }

    logindDest := "org.freedesktop.login1"
    loginPath := dbus.ObjectPath("/org/freedesktop/login1")

    logindObj := conn.Object(logindDest, loginPath)

    var seat0Path dbus.ObjectPath
    err = logindObj.Call(logindDest+".Manager.GetSeat", 0,
"seat0").Store(&seat0Path)

    if err != nil {
        os.Exit(1)
    }

    seat0Obj := conn.Object(logindDest, seat0Path)
    activeSession, err := seat0Obj.GetProperty(logindDest +
".Seat.ActiveSession")
    if err != nil {
        os.Exit(1)
    }

    activeSessionPath :=
activeSession.Value().([]interface{})[1].(dbus.ObjectPath)

    activeSessionObj := conn.Object(logindDest, activeSessionPath)
    display, err := activeSessionObj.GetProperty(logindDest +
".Session.Display")
    if err != nil {
        os.Exit(1)
    }
    fmt.Println(display)

    // node, err := introspect.Call(conn.Object("org.freedesktop.login1",
"/org/freedesktop/login1/session/c2"))
    // if err != nil {
    //     panic(err)
    // }
    // data, _ := json.MarshalIndent(node, "", "    ")
    // os.Stdout.Write(data)
}





On Fri, Aug 11, 2017 at 1:18 PM, Michal Srb <msrb@suse.com> wrote:

> On Friday, August 11, 2017 12:29:01 PM CEST Sai Prasanna wrote:
> > Thanks Michal.  How to use Dbus API directly to get the same results?.
> > I think loginctl is missing in some old distros.
>
> Just pick a library that suits you:
> https://www.freedesktop.org/wiki/Software/DBusBindings/
>
> The logind dbus api:
> https://www.freedesktop.org/wiki/Software/systemd/logind/
>
> There is also C library that can get you some of the information without
> dbus:
> https://www.freedesktop.org/software/systemd/man/sd-login.html
>
> Michal Srb
>

[Attachment #5 (text/html)]

<div dir="ltr"><div><div><div>Thanks for the help. <br><br></div>I am developing in \
go lang and found a perfect binding in dbusbindings.<br><br></div>The logind dbus api \
was not intutive at first,   but figured it out after some \
examples.<br><br><br></div>Posting the   go lang POC of above loginctl code to get \
active display. (So anyone following this thread can spend less \
effort).<br><br><br><br><br>package main<br><br>import (<br>       \
&quot;fmt&quot;<br>       &quot;os&quot;<br><br>       &quot;<a \
href="http://github.com/godbus/dbus">github.com/godbus/dbus</a>&quot;<br>)<br><br>func \
main() {<br>       conn, err := dbus.SystemBus()<br>       if err != nil {<br>        \
fmt.Fprintln(os.Stderr, &quot;Failed to connect to session bus:&quot;, err)<br>       \
os.Exit(1)<br>       }<br><br>       logindDest := \
&quot;org.freedesktop.login1&quot;<br>       loginPath := \
dbus.ObjectPath(&quot;/org/freedesktop/login1&quot;)<br><br>       logindObj := \
conn.Object(logindDest, loginPath)<br><br>       var seat0Path dbus.ObjectPath<br>    \
err = logindObj.Call(logindDest+&quot;.Manager.GetSeat&quot;, 0, \
&quot;seat0&quot;).Store(&amp;seat0Path)<br><br>       if err != nil {<br>            \
os.Exit(1)<br>       }<br><br>       seat0Obj := conn.Object(logindDest, \
seat0Path)<br>       activeSession, err := seat0Obj.GetProperty(logindDest + \
&quot;.Seat.ActiveSession&quot;)<br>       if err != nil {<br>              \
os.Exit(1)<br>       }<br><br>       activeSessionPath := \
activeSession.Value().([]interface{})[1].(dbus.ObjectPath)<br><br>       \
activeSessionObj := conn.Object(logindDest, activeSessionPath)<br>       display, err \
:= activeSessionObj.GetProperty(logindDest + &quot;.Session.Display&quot;)<br>       \
if err != nil {<br>              os.Exit(1)<br>       }<br>       \
fmt.Println(display)<br><br>       // node, err := \
introspect.Call(conn.Object(&quot;org.freedesktop.login1&quot;, \
&quot;/org/freedesktop/login1/session/c2&quot;))<br>       // if err != nil {<br>     \
//        panic(err)<br>       // }<br>       // data, _ := json.MarshalIndent(node, \
&quot;&quot;, &quot;       &quot;)<br>       // \
os.Stdout.Write(data)<br>}<br><br><div><br><br><br></div></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 11, 2017 at 1:18 PM, \
Michal Srb <span dir="ltr">&lt;<a href="mailto:msrb@suse.com" \
target="_blank">msrb@suse.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><span class="">On Friday, August 11, 2017 12:29:01 PM CEST \
Sai Prasanna wrote:<br> &gt; Thanks Michal.   How to use Dbus API directly to get the \
same results?.<br> &gt; I think loginctl is missing in some old distros.<br>
<br>
</span>Just pick a library that suits you:<br>
<a href="https://www.freedesktop.org/wiki/Software/DBusBindings/" rel="noreferrer" \
target="_blank">https://www.freedesktop.org/<wbr>wiki/Software/DBusBindings/</a><br> \
<br> The logind dbus api:<br>
<a href="https://www.freedesktop.org/wiki/Software/systemd/logind/" rel="noreferrer" \
target="_blank">https://www.freedesktop.org/<wbr>wiki/Software/systemd/logind/</a><br>
 <br>
There is also C library that can get you some of the information without dbus:<br>
<a href="https://www.freedesktop.org/software/systemd/man/sd-login.html" \
rel="noreferrer" target="_blank">https://www.freedesktop.org/<wbr>software/systemd/man/sd-login.<wbr>html</a><br>
 <span class="HOEnZb"><font color="#888888"><br>
Michal Srb<br>
</font></span></blockquote></div><br></div>


[Attachment #6 (text/plain)]

_______________________________________________
xorg@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscription address: %(user_address)s

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic