]> git.sesse.net Git - vlc/blob - modules/services_discovery/xcb_apps.c
SD probe: fix return value symbolic constant
[vlc] / modules / services_discovery / xcb_apps.c
1 /**
2  * @file xcb_apps.c
3  * @brief List of application windows XCB module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 #include <stdarg.h>
27 #include <xcb/xcb.h>
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>
35 #include <poll.h>
36 #include <search.h>
37
38 static int  Open (vlc_object_t *);
39 static void Close (vlc_object_t *);
40 static int vlc_sd_probe_Open (vlc_object_t *);
41
42 /*
43  * Module descriptor
44  */
45 vlc_module_begin ()
46     set_shortname (N_("Screen capture"))
47     set_description (N_("Screen capture"))
48     set_category (CAT_PLAYLIST)
49     set_subcategory (SUBCAT_PLAYLIST_SD)
50     set_capability ("services_discovery", 0)
51     set_callbacks (Open, Close)
52
53     add_shortcut ("apps")
54
55     VLC_SD_PROBE_SUBMODULE
56 vlc_module_end ()
57
58 struct services_discovery_sys_t
59 {
60     xcb_connection_t *conn;
61     vlc_thread_t      thread;
62     xcb_atom_t        net_client_list;
63     xcb_atom_t        net_wm_name;
64     xcb_window_t      root_window;
65     void             *nodes;
66 };
67
68 static void *Run (void *);
69 static void Update (services_discovery_t *);
70 static void DelItem (void *);
71
72 static int vlc_sd_probe_Open (vlc_object_t *obj)
73 {
74     vlc_probe_t *probe = (vlc_probe_t *)obj;
75
76     char *display = var_CreateGetNonEmptyString (obj, "x11-display");
77     xcb_connection_t *conn = xcb_connect (display, NULL);
78     free (display);
79     if (xcb_connection_has_error (conn))
80         return VLC_PROBE_CONTINUE;
81     xcb_disconnect (conn);
82     return vlc_sd_probe_Add (probe, "xcb_apps", N_("Screen capture"));
83 }
84
85 /**
86  * Probes and initializes.
87  */
88 static int Open (vlc_object_t *obj)
89 {
90     services_discovery_t *sd = (services_discovery_t *)obj;
91     services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
92
93     if (p_sys == NULL)
94         return VLC_ENOMEM;
95     sd->p_sys = p_sys;
96
97     /* Connect to X server */
98     char *display = var_CreateGetNonEmptyString (obj, "x11-display");
99     int snum;
100     xcb_connection_t *conn = xcb_connect (display, &snum);
101     free (display);
102     if (xcb_connection_has_error (conn))
103     {
104         free (p_sys);
105         return VLC_EGENERIC;
106     }
107     p_sys->conn = conn;
108
109     /* Find configured screen */
110     const xcb_setup_t *setup = xcb_get_setup (conn);
111     const xcb_screen_t *scr = NULL;
112     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
113          i.rem > 0; xcb_screen_next (&i))
114     {
115         if (snum == 0)
116         {
117             scr = i.data;
118             break;
119         }
120         snum--;
121     }
122     if (scr == NULL)
123     {
124         msg_Err (obj, "bad X11 screen number");
125         goto error;
126     }
127
128     p_sys->root_window = scr->root;
129     xcb_change_window_attributes (conn, scr->root, XCB_CW_EVENT_MASK,
130                                &(uint32_t) { XCB_EVENT_MASK_PROPERTY_CHANGE });
131
132     /* TODO: check that _NET_CLIENT_LIST is in _NET_SUPPORTED
133      * (and _NET_SUPPORTING_WM_CHECK) */
134     xcb_intern_atom_reply_t *r;
135     xcb_intern_atom_cookie_t ncl, nwn;
136
137     ncl = xcb_intern_atom (conn, 1, strlen ("_NET_CLIENT_LIST"),
138                           "_NET_CLIENT_LIST");
139     nwn = xcb_intern_atom (conn, 0, strlen ("_NET_WM_NAME"), "_NET_WM_NAME");
140
141     r = xcb_intern_atom_reply (conn, ncl, NULL);
142     if (r == NULL || r->atom == 0)
143     {
144         dialog_Fatal (sd, _("Application list failure"),
145                   _("Your window manager does not support application list."));
146         msg_Err (sd, "application list not support (_NET_CLIENT_LIST absent)");
147         free (r);
148         goto error;
149     }
150     p_sys->net_client_list = r->atom;
151     free (r);
152     r = xcb_intern_atom_reply (conn, nwn, NULL);
153     if (r != NULL)
154     {
155         p_sys->net_wm_name = r->atom;
156         free (r);
157     }
158
159     p_sys->nodes = NULL;
160     Update (sd);
161
162     if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
163         goto error;
164     return VLC_SUCCESS;
165
166 error:
167     xcb_disconnect (p_sys->conn);
168     free (p_sys);
169     return VLC_EGENERIC;
170 }
171
172
173 /**
174  * Releases resources
175  */
176 static void Close (vlc_object_t *obj)
177 {
178     services_discovery_t *sd = (services_discovery_t *)obj;
179     services_discovery_sys_t *p_sys = sd->p_sys;
180
181     vlc_cancel (p_sys->thread);
182     vlc_join (p_sys->thread, NULL);
183     xcb_disconnect (p_sys->conn);
184     tdestroy (p_sys->nodes, DelItem);
185     free (p_sys);
186 }
187
188 static void *Run (void *data)
189 {
190     services_discovery_t *sd = data;
191     services_discovery_sys_t *p_sys = sd->p_sys;
192     xcb_connection_t *conn = p_sys->conn;
193     int fd = xcb_get_file_descriptor (conn);
194     if (fd == -1)
195         return NULL;
196
197     while (!xcb_connection_has_error (conn))
198     {
199         xcb_generic_event_t *ev;
200         struct pollfd ufd = { .fd = fd, .events = POLLIN, };
201
202         poll (&ufd, 1, -1);
203
204         int canc = vlc_savecancel ();
205         while ((ev = xcb_poll_for_event (conn)) != NULL)
206         {
207             if ((ev->response_type & 0x7F) == XCB_PROPERTY_NOTIFY)
208             {
209                  const xcb_property_notify_event_t *pn =
210                      (xcb_property_notify_event_t *)ev;
211                  if (pn->atom == p_sys->net_client_list)
212                      Update (sd);
213             }
214             free (ev);
215         }
216         vlc_restorecancel (canc);
217     }
218     return NULL;
219 }
220
221 struct app
222 {
223     xcb_window_t          xid; /* must be first for cmpapp */
224     input_item_t         *item;
225     services_discovery_t *owner;
226 };
227
228 static struct app *AddItem (services_discovery_t *sd, xcb_window_t xid)
229 {
230     services_discovery_sys_t *p_sys = sd->p_sys;
231     char *mrl, *name;
232
233     if (asprintf (&mrl, "window://0x%"PRIx8, xid) == -1)
234         return NULL;
235
236     xcb_get_property_reply_t *r =
237         xcb_get_property_reply (p_sys->conn,
238             xcb_get_property (p_sys->conn, 0, xid, p_sys->net_wm_name, 0,
239                               0, 1023 /* max size */), NULL);
240     if (r != NULL)
241     {
242         name = strndup (xcb_get_property_value (r),
243                         xcb_get_property_value_length (r));
244         if (name != NULL)
245             EnsureUTF8 (name); /* don't trust third party apps too much ;-) */
246         free (r);
247     }
248     /* TODO: use WM_NAME (Latin-1) for very old apps */
249     else
250         name = NULL;
251
252     input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
253                                                  name ? name : mrl,
254                                                  0, NULL, 0, -1,
255                                                  ITEM_TYPE_CARD /* FIXME */);
256     free (mrl);
257     free (name);
258     if (item == NULL)
259         return NULL;
260
261     struct app *app = malloc (sizeof (*app));
262     if (app == NULL)
263     {
264         vlc_gc_decref (item);
265         return NULL;
266     }
267     app->xid = xid;
268     app->item = item;
269     app->owner = sd;
270     services_discovery_AddItem (sd, item, _("Applications"));
271     return app;
272 }
273
274 static void DelItem (void *data)
275 {
276     struct app *app = data;
277
278     services_discovery_RemoveItem (app->owner, app->item);
279     vlc_gc_decref (app->item);
280     free (app);
281 }
282
283 static int cmpapp (const void *a, const void *b)
284 {
285     xcb_window_t wa = *(xcb_window_t *)a;
286     xcb_window_t wb = *(xcb_window_t *)b;
287
288     if (wa > wb)
289         return 1;
290     if (wa < wb)
291         return -1;
292     return 0;
293
294
295 static void Update (services_discovery_t *sd)
296 {
297     services_discovery_sys_t *p_sys = sd->p_sys;
298     xcb_connection_t *conn = p_sys->conn;
299
300     xcb_get_property_reply_t *r =
301         xcb_get_property_reply (conn,
302             xcb_get_property (conn, false, p_sys->root_window,
303                               p_sys->net_client_list, XA_WINDOW, 0, 1024),
304             NULL);
305     if (r == NULL)
306         return; /* FIXME: remove all entries */
307
308     xcb_window_t *ent = xcb_get_property_value (r);
309     int n = xcb_get_property_value_length (r) / 4;
310     void *newnodes = NULL, *oldnodes = p_sys->nodes;
311
312     for (int i = 0; i < n; i++)
313     {
314         xcb_window_t id = *(ent++);
315         struct app *app;
316
317         struct app **pa = tfind (&id, &oldnodes, cmpapp);
318         if (pa != NULL) /* existing entry */
319         {
320             app = *pa;
321             tdelete (app, &oldnodes, cmpapp);
322         }
323         else /* new entry */
324         {
325             app = AddItem (sd, id);
326             if (app == NULL)
327                 continue;
328         }
329
330         pa = tsearch (app, &newnodes, cmpapp);
331         if (pa == NULL /* OOM */ || *pa != app /* buggy window manager */)
332             DelItem (app);
333     }
334     free (r);
335
336     /* Remove old nodes */
337     tdestroy (oldnodes, DelItem);
338     p_sys->nodes = newnodes;
339 }