3 * @brief List of application windows XCB module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
28 typedef xcb_atom_t Atom;
29 #include <X11/Xatom.h> /* XA_WINDOW */
30 #include <vlc_common.h>
31 #include <vlc_services_discovery.h>
32 #include <vlc_dialog.h>
33 #include <vlc_charset.h>
34 #include <vlc_plugin.h>
40 static int Open (vlc_object_t *);
41 static void Close (vlc_object_t *);
42 static int vlc_sd_probe_Open (vlc_object_t *);
48 set_shortname (N_("Screen capture"))
49 set_description (N_("Screen capture"))
50 set_category (CAT_PLAYLIST)
51 set_subcategory (SUBCAT_PLAYLIST_SD)
52 set_capability ("services_discovery", 0)
53 set_callbacks (Open, Close)
57 VLC_SD_PROBE_SUBMODULE
60 struct services_discovery_sys_t
62 xcb_connection_t *conn;
64 xcb_atom_t net_client_list;
65 xcb_atom_t net_wm_name;
66 xcb_window_t root_window;
70 static void *Run (void *);
71 static void Update (services_discovery_t *);
72 static void DelItem (void *);
73 static void AddDesktopItem(services_discovery_t *);
75 static int vlc_sd_probe_Open (vlc_object_t *obj)
77 vlc_probe_t *probe = (vlc_probe_t *)obj;
79 char *display = var_InheritString (obj, "x11-display");
80 xcb_connection_t *conn = xcb_connect (display, NULL);
82 if (xcb_connection_has_error (conn))
83 return VLC_PROBE_CONTINUE;
84 xcb_disconnect (conn);
85 return vlc_sd_probe_Add (probe, "xcb_apps{longname=\"Screen capture\"}",
86 N_("Screen capture"), SD_CAT_MYCOMPUTER);
90 * Probes and initializes.
92 static int Open (vlc_object_t *obj)
94 services_discovery_t *sd = (services_discovery_t *)obj;
95 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
101 /* Connect to X server */
102 char *display = var_InheritString (obj, "x11-display");
104 xcb_connection_t *conn = xcb_connect (display, &snum);
106 if (xcb_connection_has_error (conn))
113 /* Find configured screen */
114 const xcb_setup_t *setup = xcb_get_setup (conn);
115 const xcb_screen_t *scr = NULL;
116 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
117 i.rem > 0; xcb_screen_next (&i))
128 msg_Err (obj, "bad X11 screen number");
132 /* Add a permanent item for the entire desktop */
135 p_sys->root_window = scr->root;
136 xcb_change_window_attributes (conn, scr->root, XCB_CW_EVENT_MASK,
137 &(uint32_t) { XCB_EVENT_MASK_PROPERTY_CHANGE });
139 /* TODO: check that _NET_CLIENT_LIST is in _NET_SUPPORTED
140 * (and _NET_SUPPORTING_WM_CHECK) */
141 xcb_intern_atom_reply_t *r;
142 xcb_intern_atom_cookie_t ncl, nwn;
144 ncl = xcb_intern_atom (conn, 1, strlen ("_NET_CLIENT_LIST"),
146 nwn = xcb_intern_atom (conn, 0, strlen ("_NET_WM_NAME"), "_NET_WM_NAME");
148 r = xcb_intern_atom_reply (conn, ncl, NULL);
149 if (r == NULL || r->atom == 0)
151 dialog_Fatal (sd, _("Screen capture"),
152 _("Your window manager does not provide a list of applications."));
153 msg_Err (sd, "client list not supported (_NET_CLIENT_LIST absent)");
155 p_sys->net_client_list = r ? r->atom : 0;
157 r = xcb_intern_atom_reply (conn, nwn, NULL);
160 p_sys->net_wm_name = r->atom;
167 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
172 xcb_disconnect (p_sys->conn);
181 static void Close (vlc_object_t *obj)
183 services_discovery_t *sd = (services_discovery_t *)obj;
184 services_discovery_sys_t *p_sys = sd->p_sys;
186 vlc_cancel (p_sys->thread);
187 vlc_join (p_sys->thread, NULL);
188 xcb_disconnect (p_sys->conn);
189 tdestroy (p_sys->nodes, DelItem);
193 static void *Run (void *data)
195 services_discovery_t *sd = data;
196 services_discovery_sys_t *p_sys = sd->p_sys;
197 xcb_connection_t *conn = p_sys->conn;
198 int fd = xcb_get_file_descriptor (conn);
202 while (!xcb_connection_has_error (conn))
204 xcb_generic_event_t *ev;
205 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
209 int canc = vlc_savecancel ();
210 while ((ev = xcb_poll_for_event (conn)) != NULL)
212 if ((ev->response_type & 0x7F) == XCB_PROPERTY_NOTIFY)
214 const xcb_property_notify_event_t *pn =
215 (xcb_property_notify_event_t *)ev;
216 if (pn->atom == p_sys->net_client_list)
221 vlc_restorecancel (canc);
228 xcb_window_t xid; /* must be first for cmpapp */
230 services_discovery_t *owner;
233 static struct app *AddItem (services_discovery_t *sd, xcb_window_t xid)
235 services_discovery_sys_t *p_sys = sd->p_sys;
238 if (asprintf (&mrl, "window://0x%"PRIx8, xid) == -1)
241 xcb_get_property_reply_t *r =
242 xcb_get_property_reply (p_sys->conn,
243 xcb_get_property (p_sys->conn, 0, xid, p_sys->net_wm_name, 0,
244 0, 1023 /* max size */), NULL);
247 name = strndup (xcb_get_property_value (r),
248 xcb_get_property_value_length (r));
250 EnsureUTF8 (name); /* don't trust third party apps too much ;-) */
253 /* TODO: use WM_NAME (Latin-1) for very old apps */
257 input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
260 ITEM_TYPE_CARD /* FIXME */);
266 struct app *app = malloc (sizeof (*app));
269 vlc_gc_decref (item);
275 services_discovery_AddItem (sd, item, _("Applications"));
279 static void DelItem (void *data)
281 struct app *app = data;
283 services_discovery_RemoveItem (app->owner, app->item);
284 vlc_gc_decref (app->item);
288 static int cmpapp (const void *a, const void *b)
290 xcb_window_t wa = *(xcb_window_t *)a;
291 xcb_window_t wb = *(xcb_window_t *)b;
300 static void Update (services_discovery_t *sd)
302 services_discovery_sys_t *p_sys = sd->p_sys;
303 xcb_connection_t *conn = p_sys->conn;
305 xcb_get_property_reply_t *r =
306 xcb_get_property_reply (conn,
307 xcb_get_property (conn, false, p_sys->root_window,
308 p_sys->net_client_list, XA_WINDOW, 0, 1024),
311 return; /* FIXME: remove all entries */
313 xcb_window_t *ent = xcb_get_property_value (r);
314 int n = xcb_get_property_value_length (r) / 4;
315 void *newnodes = NULL, *oldnodes = p_sys->nodes;
317 for (int i = 0; i < n; i++)
319 xcb_window_t id = *(ent++);
322 struct app **pa = tfind (&id, &oldnodes, cmpapp);
323 if (pa != NULL) /* existing entry */
326 tdelete (app, &oldnodes, cmpapp);
330 app = AddItem (sd, id);
335 pa = tsearch (app, &newnodes, cmpapp);
336 if (pa == NULL /* OOM */ || *pa != app /* buggy window manager */)
341 /* Remove old nodes */
342 tdestroy (oldnodes, DelItem);
343 p_sys->nodes = newnodes;
346 static void AddDesktopItem(services_discovery_t *sd)
350 item = input_item_NewWithType (VLC_OBJECT (sd), "screen://", _("Desktop"),
351 0, NULL, 0, -1, ITEM_TYPE_CARD);
355 services_discovery_AddItem (sd, item, NULL);