I have no say in whether or not this gets applied, but what is your use
case for this?

Steven N. Oliver


On Wed, Jul 3, 2013 at 10:16 AM, Jean-Sébastien Pédron <
[email protected]> wrote:

> This allows one to run multiple awesome instances on the same machine
> and still be able to target one specific instance using
> awesome-client(1).
>
> Because some characters are forbidden in a bus name, characters not
> matching [A-Za-z0-9_-] are replaced by '_'.
>
> When $DISPLAY is not set, ":0.0" is assumed. Also, when $DISPLAY doesn't
> specify the screen number (eg. ":1"), ".0" is appended to normalize the
> bus name (eg. ":1" becomes ":1.0").
>
> The bus name is computed in a_dbus_init() (in dbus.c) and is exposed
> through the dbus.bus_name variable to Lua.
>
> Note that when using a remote display, the bus name will contain the
> X server host name. In this case, awesome-client(1) will be able to
> target awesome only if both run on the same host (ie. are attached to
> the same D-Bus). Otherwise, awesome-client(1) will do nothing. Before
> this patch, awesome-client(1) could target the local awesome instance
> instead.
>
> Example:
>   Previous bus name: org.naquadah.awesome.awful
>   New bus name:      org.naquadah.awesome.awful._0_0 (for DISPLAY=:0.0)
> ---
>  dbus.c                           | 90
> +++++++++++++++++++++++++++++++++++++++-
>  lib/awful/dbus.lua.in            |  8 ++--
>  lib/awful/remote.lua.in          |  2 +-
>  luaa.c                           |  8 ++--
>  luadoc/dbus.lua                  |  5 +++
>  manpages/awesome-client.1.fr.txt |  2 +
>  manpages/awesome-client.1.txt    |  2 +
>  utils/awesome-client             | 14 ++++++-
>  8 files changed, 121 insertions(+), 10 deletions(-)
>
> diff --git a/dbus.c b/dbus.c
> index 5bc7e93..a7a8d53 100644
> --- a/dbus.c
> +++ b/dbus.c
> @@ -33,6 +33,10 @@
>  #include "event.h"
>  #include "luaa.h"
>  +#define DBUS_BUS_NAME_PREFIX "org.naquadah.awesome.awful"
> +
> +static char *dbus_bus_name = NULL;
> +
>  static DBusConnection *dbus_connection_session = NULL;
>  static DBusConnection *dbus_connection_system = NULL;
>  static GSource *session_source = NULL;
> @@ -635,6 +639,63 @@ a_dbus_connect(DBusBusType type, const char
> *type_name, GSourceFunc cb, GSource
>  void
>  a_dbus_init(void)
>  {
> +    const char *display;
> +    int dbus_bus_name_length;
> +
> +    display = (const char *)getenv("DISPLAY");
> +    if(display == NULL)
> +        display = ":0.0";
> +
> +    dbus_bus_name_length = snprintf(NULL, 0,
> +        DBUS_BUS_NAME_PREFIX ".%s", display);
> +
> +    /* We add 2 to the length to make room for a potential ".0" suffix. */
> +    dbus_bus_name = malloc(dbus_bus_name_length + 2 + 1);
> +
> +    if(dbus_bus_name != NULL)
> +    {
> +        int i;
> +        char *cursor;
> +
> +        snprintf(dbus_bus_name, dbus_bus_name_length + 1,
> +            DBUS_BUS_NAME_PREFIX ".%s", display);
> +
> +        /*
> +         * If $DISPLAY only contains ":0" for instance, append ".0" to
> +         * normalize it.
> +         */
> +        cursor = strrchr(dbus_bus_name, ':');
> +        if(cursor != NULL)
> +        {
> +            cursor = strchr(cursor, '.');
> +            if(cursor == NULL)
> +            {
> +                dbus_bus_name_length += 2;
> +                snprintf(dbus_bus_name, dbus_bus_name_length + 1,
> +                    "%s.0", dbus_bus_name);
> +            }
> +        }
> +
> +        /*
> +         * Replace all characters not allowed in a bus name by '_'. See
> +         * "Valid Names" paragraph in the D-Bus specification.
> +         *
> +         * We start right after the '.' following DBUS_BUS_NAME_PREFIX.
> +         */
> +        i = strlen(DBUS_BUS_NAME_PREFIX) + 1;
> +        for(; i < dbus_bus_name_length; ++i)
> +        {
> +            if(!(dbus_bus_name[i] >= 'A' && dbus_bus_name[i] <= 'Z') &&
> +                !(dbus_bus_name[i] >= 'a' && dbus_bus_name[i] <= 'z') &&
> +                !(dbus_bus_name[i] >= '0' && dbus_bus_name[i] <= '9') &&
> +                dbus_bus_name[i] != '_' &&
> +                dbus_bus_name[i] != '-')
> +            {
> +                dbus_bus_name[i] = '_';
> +            }
> +        }
> +    }
> +
>      dbus_connection_session = a_dbus_connect(DBUS_BUS_SESSION, "session",
>
> a_dbus_process_requests_session, &session_source);
>      dbus_connection_system = a_dbus_connect(DBUS_BUS_SYSTEM, "system",
> @@ -648,6 +709,8 @@ a_dbus_cleanup(void)
>  {
>      a_dbus_cleanup_bus(dbus_connection_session, &session_source);
>      a_dbus_cleanup_bus(dbus_connection_system, &system_source);
> +
> +    free(dbus_bus_name);
>  }
>   /** Retrieve the D-Bus bus by it's name
> @@ -789,7 +852,27 @@ luaA_dbus_disconnect_signal(lua_State *L)
>      return 0;
>  }
>  -const struct luaL_Reg awesome_dbus_lib[] =
> +/** dbus global table.
> + * \param L The Lua VM state.
> + * \return The number of elements pushed on stack.
> + * \luastack
> + * \lfield bus_name The default D-Bus bus name.
> + */
> +static int
> +luaA_dbus_index(lua_State *L)
> +{
> +    const char *attr = luaL_checkstring(L, 2);
> +
> +    if(A_STREQ(attr, "bus_name"))
> +    {
> +        lua_pushstring(L, dbus_bus_name);
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +const struct luaL_Reg awesome_dbus_methods[] =
>  {
>      { "request_name", luaA_dbus_request_name },
>      { "release_name", luaA_dbus_release_name },
> @@ -797,6 +880,11 @@ const struct luaL_Reg awesome_dbus_lib[] =
>      { "remove_match", luaA_dbus_remove_match },
>      { "connect_signal", luaA_dbus_connect_signal },
>      { "disconnect_signal", luaA_dbus_disconnect_signal },
> +    { "__index", luaA_dbus_index },
> +    { NULL, NULL }
> +};
> +const struct luaL_Reg awesome_dbus_meta[] =
> +{
>      { NULL, NULL }
>  };
>  diff --git a/lib/awful/dbus.lua.in b/lib/awful/dbus.lua.in
> index 323df02..24b2036 100644
> --- a/lib/awful/dbus.lua.in
> +++ b/lib/awful/dbus.lua.in
> @@ -8,12 +8,14 @@
>  local dbus = dbus
>   --- D-Bus module for awful.
> --- This module simply request the org.naquadah.awesome.awful name on
> the D-Bus
> --- for futur usage by other awful modules.
> +-- This module simply request the org.naquadah.awesome.awful.$DISPLAY
> +-- name (where $DISPLAY is modified to replace bus name forbidden
> +-- characters by '_') on the D-Bus for futur usage by other awful
> +-- modules.
>  -- awful.dbus
>   if dbus then
> -    dbus.request_name("session", "org.naquadah.awesome.awful")
> +    dbus.request_name("session", dbus.bus_name)
>  end
>   -- vim:
> filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
> diff --git a/lib/awful/remote.lua.in b/lib/awful/remote.lua.in
> index ccc9ec2..61b0152 100644
> --- a/lib/awful/remote.lua.in
> +++ b/lib/awful/remote.lua.in
> @@ -18,7 +18,7 @@ local type = type
>  -- awful.remote
>   if dbus then
> -    dbus.connect_signal("org.naquadah.awesome.awful.Remote",
> function(data, code)
> +    dbus.connect_signal(dbus.bus_name .. ".Remote", function(data, code)
>          if data.member == "Eval" then
>              local f, e = load(code)
>              if f then
> diff --git a/luaa.c b/luaa.c
> index c86a0e8..ec536d3 100644
> --- a/luaa.c
> +++ b/luaa.c
> @@ -47,7 +47,8 @@
>  #include "common/backtrace.h"
>   #ifdef WITH_DBUS
> -extern const struct luaL_Reg awesome_dbus_lib[];
> +extern const struct luaL_Reg awesome_dbus_methods[];
> +extern const struct luaL_Reg awesome_dbus_meta[];
>  #endif
>  extern const struct luaL_Reg awesome_keygrabber_lib[];
>  extern const struct luaL_Reg awesome_mousegrabber_lib[];
> @@ -552,9 +553,8 @@ luaA_init(xdgHandle* xdg)
>      lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
>   #ifdef WITH_DBUS
> -    /* Export D-Bus lib */
> -    luaA_registerlib(L, "dbus", awesome_dbus_lib);
> -    lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
> +    /* Export D-Bus */
> +    luaA_openlib(L, "dbus", awesome_dbus_methods, awesome_dbus_meta);
>  #endif
>       /* Export keygrabber lib */
> diff --git a/luadoc/dbus.lua b/luadoc/dbus.lua
> index 98894a4..44883a5 100644
> --- a/luadoc/dbus.lua
> +++ b/luadoc/dbus.lua
> @@ -41,3 +41,8 @@ module("dbus")
>  -- @param func The function to call.
>  -- @name disconnect_signal
>  -- @class function
> +
> +--- D-Bus library.
> +-- @field bus_name The default D-Bus bus name.
> +-- @class table
> +-- @name dbus
> diff --git a/manpages/awesome-client.1.fr.txt
> b/manpages/awesome-client.1.fr.txt
> index 3559fca..e99d06c 100644
> --- a/manpages/awesome-client.1.fr.txt
> +++ b/manpages/awesome-client.1.fr.txt
> @@ -23,6 +23,8 @@ UTILISATION
>  awesome-client lit les commandes à partir de l'entrée standard et les
> envoie à awesome via D-Bus.
>  Si 'rlwrap' est installé, il sera utilisé pour fournir une interface en
> ligne de commande de readline.
>  +awesome-client se base sur la variable d'environnement $DISPLAY pour
> cibler une instance d'awesome en particulier.
> +
>  Le module 'awful.remote' doit être chargé pour que cette commande
> fonctionne.
>   VOIR AUSSI
> diff --git a/manpages/awesome-client.1.txt b/manpages/awesome-client.1.txt
> index ddff197..275fe28 100644
> --- a/manpages/awesome-client.1.txt
> +++ b/manpages/awesome-client.1.txt
> @@ -22,6 +22,8 @@ USAGE
>  awesome-client reads commands from standard input and sends them via
> D-Bus to awesome.
>  If 'rlwrap' is installed, it will be used to provide a readline command
> line interface.
>  +awesome-client uses the $DISPLAY environment variable to target a
> specific awesome instance.
> +
>  The 'awful.remote' module has to be loaded if you want this command to
> work.
>   SEE ALSO
> diff --git a/utils/awesome-client b/utils/awesome-client
> index 658787d..52f1e02 100755
> --- a/utils/awesome-client
> +++ b/utils/awesome-client
> @@ -38,7 +38,19 @@ then
>  fi
>   DBUS_PATH=/
> -DBUS_DEST=org.naquadah.awesome.awful
> +DBUS_DEST=$DISPLAY
> +if test -z "$DBUS_DEST"
> +then
> +    DBUS_DEST=:0.0
> +else
> +    # Append ".0" if the screen number is not specified.
> +    case "$DBUS_DEST" in
> +        *:*.*) ;;
> +        *) DBUS_DEST="${DBUS_DEST}.0" ;;
> +    esac
> +fi
> +DBUS_DEST=$(printf "$DBUS_DEST" | tr -C '[A-Za-z0-9_-]' '_')
> +DBUS_DEST="org.naquadah.awesome.awful.${DBUS_DEST}"
>  DBUS_METHOD=${DBUS_DEST}.Remote.Eval
>   a_dbus_send()
> --
> 1.8.3.1
>
>
> --
> To unsubscribe, send mail to [email protected].
>

Reply via email to