2008-06-14 18:12:16 +02:00
|
|
|
/*
|
|
|
|
* systray.c - systray handling
|
|
|
|
*
|
2009-09-14 20:26:33 +02:00
|
|
|
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
|
2008-06-14 18:12:16 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xcb/xcb.h>
|
|
|
|
#include <xcb/xcb_icccm.h>
|
2008-06-30 13:06:23 +02:00
|
|
|
#include <xcb/xcb_atom.h>
|
2008-06-14 18:12:16 +02:00
|
|
|
|
2010-10-06 15:38:44 +02:00
|
|
|
#include "luaa.h"
|
2009-04-17 16:14:09 +02:00
|
|
|
#include "screen.h"
|
2008-06-14 18:12:16 +02:00
|
|
|
#include "systray.h"
|
2009-09-17 17:51:06 +02:00
|
|
|
#include "xwindow.h"
|
2010-05-21 21:18:12 +02:00
|
|
|
#include "common/array.h"
|
2008-06-30 18:55:14 +02:00
|
|
|
#include "common/atoms.h"
|
2009-04-17 16:52:25 +02:00
|
|
|
#include "common/xutil.h"
|
2010-10-06 15:38:44 +02:00
|
|
|
#include "objects/drawin.h"
|
2008-06-14 18:12:16 +02:00
|
|
|
|
|
|
|
#define SYSTEM_TRAY_REQUEST_DOCK 0 /* Begin icon docking */
|
|
|
|
|
2008-09-03 18:03:31 +02:00
|
|
|
/** Initialize systray information in X.
|
|
|
|
*/
|
2008-06-15 10:25:26 +02:00
|
|
|
void
|
2010-08-16 13:47:40 +02:00
|
|
|
systray_init(void)
|
2010-05-21 21:18:12 +02:00
|
|
|
{
|
2014-08-20 11:40:50 +02:00
|
|
|
xcb_intern_atom_cookie_t atom_systray_q;
|
|
|
|
xcb_intern_atom_reply_t *atom_systray_r;
|
|
|
|
char *atom_name;
|
2010-08-16 14:10:58 +02:00
|
|
|
xcb_screen_t *xscreen = globalconf.screen;
|
2010-05-21 21:18:12 +02:00
|
|
|
|
2010-08-16 13:55:12 +02:00
|
|
|
globalconf.systray.window = xcb_generate_id(globalconf.connection);
|
2010-10-04 09:21:09 +02:00
|
|
|
xcb_create_window(globalconf.connection, xscreen->root_depth,
|
2010-08-16 13:55:12 +02:00
|
|
|
globalconf.systray.window,
|
2010-05-21 21:18:12 +02:00
|
|
|
xscreen->root,
|
|
|
|
-1, -1, 1, 1, 0,
|
2010-10-14 12:54:38 +02:00
|
|
|
XCB_COPY_FROM_PARENT, xscreen->root_visual,
|
2010-10-17 09:26:09 +02:00
|
|
|
0, NULL);
|
2014-08-20 11:40:50 +02:00
|
|
|
|
|
|
|
atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf.default_screen);
|
|
|
|
if(!atom_name)
|
|
|
|
fatal("error getting systray atom name");
|
|
|
|
|
|
|
|
atom_systray_q = xcb_intern_atom_unchecked(globalconf.connection, false,
|
|
|
|
a_strlen(atom_name), atom_name);
|
|
|
|
|
|
|
|
p_delete(&atom_name);
|
|
|
|
|
|
|
|
atom_systray_r = xcb_intern_atom_reply(globalconf.connection, atom_systray_q, NULL);
|
|
|
|
if(!atom_systray_r)
|
|
|
|
fatal("error getting systray atom");
|
|
|
|
|
|
|
|
globalconf.systray.atom = atom_systray_r->atom;
|
|
|
|
p_delete(&atom_systray_r);
|
2010-05-21 21:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Register systray in X.
|
|
|
|
*/
|
2010-10-06 15:38:44 +02:00
|
|
|
static void
|
2010-08-16 13:47:40 +02:00
|
|
|
systray_register(void)
|
2008-06-15 10:25:26 +02:00
|
|
|
{
|
|
|
|
xcb_client_message_event_t ev;
|
2010-08-16 14:10:58 +02:00
|
|
|
xcb_screen_t *xscreen = globalconf.screen;
|
2008-06-15 10:25:26 +02:00
|
|
|
|
2014-08-20 11:47:47 +02:00
|
|
|
if(globalconf.systray.registered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
globalconf.systray.registered = true;
|
|
|
|
|
2008-06-15 10:25:26 +02:00
|
|
|
/* Fill event */
|
2008-06-24 19:54:54 +02:00
|
|
|
p_clear(&ev, 1);
|
2008-06-15 20:12:49 +02:00
|
|
|
ev.response_type = XCB_CLIENT_MESSAGE;
|
2008-06-24 17:38:44 +02:00
|
|
|
ev.window = xscreen->root;
|
2008-06-15 10:25:26 +02:00
|
|
|
ev.format = 32;
|
2008-06-30 18:55:14 +02:00
|
|
|
ev.type = MANAGER;
|
2008-06-15 10:25:26 +02:00
|
|
|
ev.data.data32[0] = XCB_CURRENT_TIME;
|
2014-08-20 11:40:50 +02:00
|
|
|
ev.data.data32[1] = globalconf.systray.atom;
|
2010-08-16 13:55:12 +02:00
|
|
|
ev.data.data32[2] = globalconf.systray.window;
|
2008-06-15 10:25:26 +02:00
|
|
|
ev.data.data32[3] = ev.data.data32[4] = 0;
|
|
|
|
|
|
|
|
xcb_set_selection_owner(globalconf.connection,
|
2010-08-16 13:55:12 +02:00
|
|
|
globalconf.systray.window,
|
2014-08-20 11:40:50 +02:00
|
|
|
globalconf.systray.atom,
|
2008-06-15 10:25:26 +02:00
|
|
|
XCB_CURRENT_TIME);
|
2008-06-15 11:22:34 +02:00
|
|
|
|
2008-06-24 19:54:54 +02:00
|
|
|
xcb_send_event(globalconf.connection, false, xscreen->root, 0xFFFFFF, (char *) &ev);
|
2008-06-15 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
2008-09-03 18:03:31 +02:00
|
|
|
/** Remove systray information in X.
|
|
|
|
*/
|
|
|
|
void
|
2010-08-16 13:47:40 +02:00
|
|
|
systray_cleanup(void)
|
2008-09-03 18:03:31 +02:00
|
|
|
{
|
2014-08-20 11:47:47 +02:00
|
|
|
if(!globalconf.systray.registered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
globalconf.systray.registered = false;
|
|
|
|
|
2008-09-03 18:03:31 +02:00
|
|
|
xcb_set_selection_owner(globalconf.connection,
|
|
|
|
XCB_NONE,
|
2014-08-20 11:40:50 +02:00
|
|
|
globalconf.systray.atom,
|
2008-09-03 18:03:31 +02:00
|
|
|
XCB_CURRENT_TIME);
|
|
|
|
|
2010-10-06 15:38:44 +02:00
|
|
|
xcb_unmap_window(globalconf.connection,
|
|
|
|
globalconf.systray.window);
|
2008-09-03 18:03:31 +02:00
|
|
|
}
|
|
|
|
|
2008-06-14 18:12:16 +02:00
|
|
|
/** Handle a systray request.
|
|
|
|
* \param embed_win The window to embed.
|
2009-08-30 07:26:19 +02:00
|
|
|
* \param info The embedding info
|
2009-07-27 06:20:00 +02:00
|
|
|
* \return 0 on no error.
|
2008-06-14 18:12:16 +02:00
|
|
|
*/
|
2008-06-30 13:06:23 +02:00
|
|
|
int
|
2010-08-16 13:47:40 +02:00
|
|
|
systray_request_handle(xcb_window_t embed_win, xembed_info_t *info)
|
2008-06-14 18:12:16 +02:00
|
|
|
{
|
2008-12-01 16:43:44 +01:00
|
|
|
xembed_window_t em;
|
2008-08-12 15:29:01 +02:00
|
|
|
xcb_get_property_cookie_t em_cookie;
|
2008-06-14 18:12:16 +02:00
|
|
|
const uint32_t select_input_val[] =
|
|
|
|
{
|
|
|
|
XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
|
|
|
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
|
|
|
| XCB_EVENT_MASK_ENTER_WINDOW
|
|
|
|
};
|
|
|
|
|
2008-06-29 13:38:02 +02:00
|
|
|
/* check if not already trayed */
|
2008-12-01 16:43:44 +01:00
|
|
|
if(xembed_getbywin(&globalconf.embedded, embed_win))
|
2008-06-30 13:06:23 +02:00
|
|
|
return -1;
|
2008-06-14 18:12:16 +02:00
|
|
|
|
2008-08-12 15:29:01 +02:00
|
|
|
p_clear(&em_cookie, 1);
|
|
|
|
|
|
|
|
if(!info)
|
|
|
|
em_cookie = xembed_info_get_unchecked(globalconf.connection, embed_win);
|
|
|
|
|
2008-06-14 18:12:16 +02:00
|
|
|
xcb_change_window_attributes(globalconf.connection, embed_win, XCB_CW_EVENT_MASK,
|
|
|
|
select_input_val);
|
|
|
|
|
2010-07-14 17:26:55 +02:00
|
|
|
/* we grab the window, but also make sure it's automatically reparented back
|
|
|
|
* to the root window if we should die.
|
|
|
|
*/
|
|
|
|
xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, embed_win);
|
2008-09-01 17:54:53 +02:00
|
|
|
xcb_reparent_window(globalconf.connection, embed_win,
|
2010-08-16 13:55:12 +02:00
|
|
|
globalconf.systray.window,
|
2008-09-01 17:54:53 +02:00
|
|
|
0, 0);
|
|
|
|
|
2008-12-01 16:43:44 +01:00
|
|
|
em.win = embed_win;
|
2008-08-12 15:29:01 +02:00
|
|
|
|
2008-06-14 18:12:16 +02:00
|
|
|
if(info)
|
2008-12-01 16:43:44 +01:00
|
|
|
em.info = *info;
|
2008-06-14 18:12:16 +02:00
|
|
|
else
|
2008-12-01 16:43:44 +01:00
|
|
|
xembed_info_get_reply(globalconf.connection, em_cookie, &em.info);
|
2008-06-14 18:12:16 +02:00
|
|
|
|
2008-12-01 16:43:44 +01:00
|
|
|
xembed_embedded_notify(globalconf.connection, em.win,
|
2010-08-16 13:55:12 +02:00
|
|
|
globalconf.systray.window,
|
2008-12-01 16:43:44 +01:00
|
|
|
MIN(XEMBED_VERSION, em.info.version));
|
|
|
|
|
|
|
|
xembed_window_array_append(&globalconf.embedded, em);
|
2010-10-06 15:38:44 +02:00
|
|
|
luaA_systray_invalidate();
|
2008-06-14 18:12:16 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Handle systray message.
|
|
|
|
* \param ev The event.
|
|
|
|
* \return 0 on no error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
systray_process_client_message(xcb_client_message_event_t *ev)
|
|
|
|
{
|
2010-08-16 13:47:40 +02:00
|
|
|
int ret = 0;
|
2008-06-14 18:12:16 +02:00
|
|
|
xcb_get_geometry_cookie_t geom_c;
|
|
|
|
xcb_get_geometry_reply_t *geom_r;
|
|
|
|
|
|
|
|
switch(ev->data.data32[1])
|
|
|
|
{
|
|
|
|
case SYSTEM_TRAY_REQUEST_DOCK:
|
2008-06-21 12:56:51 +02:00
|
|
|
geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
|
2008-06-14 18:12:16 +02:00
|
|
|
|
|
|
|
if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
|
|
|
|
return -1;
|
|
|
|
|
2010-08-16 14:10:58 +02:00
|
|
|
if(globalconf.screen->root == geom_r->root)
|
2010-08-16 13:47:40 +02:00
|
|
|
ret = systray_request_handle(ev->data.data32[2], NULL);
|
2008-06-14 18:12:16 +02:00
|
|
|
|
|
|
|
p_delete(&geom_r);
|
|
|
|
break;
|
|
|
|
}
|
2008-06-29 13:38:02 +02:00
|
|
|
|
|
|
|
return ret;
|
2008-06-14 18:12:16 +02:00
|
|
|
}
|
|
|
|
|
2008-06-30 13:06:23 +02:00
|
|
|
/** Check if a window is a KDE tray.
|
|
|
|
* \param w The window to check.
|
|
|
|
* \return True if it is, false otherwise.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
systray_iskdedockapp(xcb_window_t w)
|
|
|
|
{
|
|
|
|
xcb_get_property_cookie_t kde_check_q;
|
|
|
|
xcb_get_property_reply_t *kde_check;
|
|
|
|
bool ret;
|
|
|
|
|
2009-08-30 07:26:19 +02:00
|
|
|
/* Check if that is a KDE tray because it does not respect fdo standards,
|
2008-06-30 13:06:23 +02:00
|
|
|
* thanks KDE. */
|
|
|
|
kde_check_q = xcb_get_property_unchecked(globalconf.connection, false, w,
|
2008-06-30 18:55:14 +02:00
|
|
|
_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR,
|
2010-08-08 18:31:07 +02:00
|
|
|
XCB_ATOM_WINDOW, 0, 1);
|
2008-06-30 13:06:23 +02:00
|
|
|
|
|
|
|
kde_check = xcb_get_property_reply(globalconf.connection, kde_check_q, NULL);
|
|
|
|
|
|
|
|
/* it's a KDE systray ?*/
|
|
|
|
ret = (kde_check && kde_check->value_len);
|
|
|
|
|
|
|
|
p_delete(&kde_check);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-06-14 18:12:16 +02:00
|
|
|
/** Handle xembed client message.
|
|
|
|
* \param ev The event.
|
|
|
|
* \return 0 on no error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
xembed_process_client_message(xcb_client_message_event_t *ev)
|
|
|
|
{
|
|
|
|
switch(ev->data.data32[1])
|
|
|
|
{
|
|
|
|
case XEMBED_REQUEST_FOCUS:
|
|
|
|
xembed_focus_in(globalconf.connection, ev->window, XEMBED_FOCUS_CURRENT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-06 15:38:44 +02:00
|
|
|
/** Inform lua that the systray needs to be updated.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
luaA_systray_invalidate(void)
|
|
|
|
{
|
|
|
|
signal_object_emit(globalconf.L, &global_signals, "systray::update", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-04-02 15:25:03 +02:00
|
|
|
systray_update(int base_size, bool horizontal, bool reverse)
|
2010-10-06 15:38:44 +02:00
|
|
|
{
|
|
|
|
if(base_size <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Give the systray window the correct size */
|
|
|
|
uint32_t config_vals[4] = { base_size, base_size, 0, 0 };
|
|
|
|
if(horizontal)
|
|
|
|
config_vals[0] = base_size * globalconf.embedded.len;
|
|
|
|
else
|
|
|
|
config_vals[1] = base_size * globalconf.embedded.len;
|
|
|
|
xcb_configure_window(globalconf.connection,
|
|
|
|
globalconf.systray.window,
|
|
|
|
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
|
|
|
|
config_vals);
|
|
|
|
|
|
|
|
/* Now resize each embedded window */
|
|
|
|
config_vals[0] = config_vals[1] = 0;
|
|
|
|
config_vals[2] = config_vals[3] = base_size;
|
|
|
|
for(int i = 0; i < globalconf.embedded.len; i++)
|
|
|
|
{
|
2014-04-02 15:25:03 +02:00
|
|
|
xembed_window_t *em;
|
|
|
|
|
|
|
|
if(reverse)
|
|
|
|
em = &globalconf.embedded.tab[(globalconf.embedded.len - i - 1)];
|
|
|
|
else
|
|
|
|
em = &globalconf.embedded.tab[i];
|
|
|
|
|
2010-10-06 15:38:44 +02:00
|
|
|
xcb_configure_window(globalconf.connection, em->win,
|
|
|
|
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
|
|
|
|
config_vals);
|
|
|
|
xcb_map_window(globalconf.connection, em->win);
|
|
|
|
if(horizontal)
|
|
|
|
config_vals[0] += base_size;
|
|
|
|
else
|
|
|
|
config_vals[1] += base_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Update the systray
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam The drawin to display the systray in.
|
|
|
|
* \lparam x X position for the systray.
|
|
|
|
* \lparam y Y position for the systray.
|
|
|
|
* \lparam base_size The size (width and height) each systray item gets.
|
|
|
|
* \lparam horiz If true, the systray is horizontal, else vertical
|
2014-04-02 15:25:03 +02:00
|
|
|
* \lparam bg Color of the systray background
|
|
|
|
* \lparam revers If true, the systray icon order will be reversed, else default
|
2010-10-06 15:38:44 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_systray(lua_State *L)
|
|
|
|
{
|
2014-08-20 11:47:47 +02:00
|
|
|
systray_register();
|
2014-08-18 07:32:11 +02:00
|
|
|
|
2010-10-06 15:38:44 +02:00
|
|
|
if(lua_gettop(L) != 0)
|
|
|
|
{
|
2010-10-17 09:24:59 +02:00
|
|
|
size_t bg_len;
|
2010-10-06 15:38:44 +02:00
|
|
|
drawin_t *w = luaA_checkudata(L, 1, &drawin_class);
|
|
|
|
int x = luaL_checknumber(L, 2);
|
|
|
|
int y = luaL_checknumber(L, 3);
|
|
|
|
int base_size = luaL_checknumber(L, 4);
|
|
|
|
bool horiz = lua_toboolean(L, 5);
|
2010-10-17 09:24:59 +02:00
|
|
|
const char *bg = luaL_checklstring(L, 6, &bg_len);
|
2014-04-02 15:25:03 +02:00
|
|
|
bool revers = lua_toboolean(L, 7);
|
2010-10-17 09:24:59 +02:00
|
|
|
color_t bg_color;
|
|
|
|
|
|
|
|
if(color_init_reply(color_init_unchecked(&bg_color, bg, bg_len)))
|
|
|
|
{
|
|
|
|
uint32_t config_back[] = { bg_color.pixel };
|
|
|
|
xcb_change_window_attributes(globalconf.connection,
|
|
|
|
globalconf.systray.window,
|
|
|
|
XCB_CW_BACK_PIXEL, config_back);
|
|
|
|
}
|
2010-10-06 15:38:44 +02:00
|
|
|
|
2010-10-07 11:31:51 +02:00
|
|
|
if(globalconf.systray.parent != w)
|
2010-10-06 15:38:44 +02:00
|
|
|
xcb_reparent_window(globalconf.connection,
|
|
|
|
globalconf.systray.window,
|
|
|
|
w->window,
|
|
|
|
x, y);
|
2010-10-07 11:31:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
uint32_t config_vals[2] = { x, y };
|
|
|
|
xcb_configure_window(globalconf.connection,
|
|
|
|
globalconf.systray.window,
|
|
|
|
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
|
|
|
|
config_vals);
|
|
|
|
}
|
|
|
|
|
|
|
|
globalconf.systray.parent = w;
|
|
|
|
|
|
|
|
if(globalconf.embedded.len != 0)
|
|
|
|
{
|
2014-04-02 15:25:03 +02:00
|
|
|
systray_update(base_size, horiz, revers);
|
2010-10-06 15:38:44 +02:00
|
|
|
xcb_map_window(globalconf.connection,
|
|
|
|
globalconf.systray.window);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
xcb_unmap_window(globalconf.connection,
|
|
|
|
globalconf.systray.window);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushnumber(L, globalconf.embedded.len);
|
|
|
|
luaA_object_push(L, globalconf.systray.parent);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|