diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d9c894..fa6d34f 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set (CMAKE_CXX_STANDARD 11) add_definitions(-DAVAHI_COMPAT_NOWARN) +add_definitions(-DX_DISPLAY_FIX) find_package(X11 REQUIRED) link_libraries(${X11_LIBRARIES}) include_directories(${X11_INCLUDE_DIR}) diff --git a/renderers/video_renderer_gstreamer.c b/renderers/video_renderer_gstreamer.c index 37bcea6..1ef0a74 100644 --- a/renderers/video_renderer_gstreamer.c +++ b/renderers/video_renderer_gstreamer.c @@ -21,12 +21,10 @@ #include #include #include -#include -#include -#include -Display* display; -Window root, my_window; +#ifdef X_DISPLAY_FIX +#include "x_display_fix.h" +#endif struct video_renderer_s { logger_t *logger; @@ -55,46 +53,20 @@ static gboolean check_plugins (void) return ret; } -Window enum_windows(Display* display, Window window, int depth) { - int i; - - XTextProperty text; - XGetWMName(display, window, &text); - char* name; - XFetchName(display, window, &name); - - if (name != 0 && strcmp((const char*) g_get_application_name(), name) == 0) { - return window; - } - - Window _root, parent; - Window* children; - unsigned int n; - XQueryTree(display, window, &_root, &parent, &children, &n); - if (children != NULL) { - for (i = 0; i < n; i++) { - Window w = enum_windows(display, children[i], depth + 1); - if (w) return w; - } - XFree(children); - } - - return (Window) NULL; -} - video_renderer_t *video_renderer_init(logger_t *logger, const char *server_name) { - display = XOpenDisplay(NULL); - root = XDefaultRootWindow(display); - video_renderer_t *renderer; GError *error = NULL; +#ifdef X_DISPLAY_FIX + get_x_display_root(); + g_set_application_name(server_name); +#endif + renderer = calloc(1, sizeof(video_renderer_t)); assert(renderer); gst_init(NULL, NULL); - g_set_application_name(server_name); - + renderer->logger = logger; assert(check_plugins ()); @@ -125,16 +97,9 @@ void video_renderer_render_buffer(video_renderer_t *renderer, raop_ntp_t *ntp, u gst_buffer_fill(buffer, 0, data, data_len); gst_app_src_push_buffer (GST_APP_SRC(renderer->appsrc), buffer); - if (!my_window) { - my_window = enum_windows(display, root, 0); - if (my_window) { - const char* str = g_get_application_name(); - Atom _NET_WM_NAME = XInternAtom(display, "_NET_WM_NAME", 0); - Atom UTF8_STRING = XInternAtom(display, "UTF8_STRING",0); - XChangeProperty(display, my_window, _NET_WM_NAME, UTF8_STRING, 8, 0, (const unsigned char *) str, strlen(str)); - XSync(display, False); - } - } +#ifdef X_DISPLAY_FIX + fix_x_window_name(); +#endif } void video_renderer_flush(video_renderer_t *renderer) { diff --git a/renderers/x_display_fix.h b/renderers/x_display_fix.h new file mode 100644 index 0000000..d29276c --- /dev/null +++ b/renderers/x_display_fix.h @@ -0,0 +1,86 @@ +/** + * RPiPlay - An open-source AirPlay mirroring server for Raspberry Pi + * Copyright (C) 2019 Florian Draschbacher + * + * 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 3 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 + */ + +/* code from David Ventura https://github.com/DavidVentura/UxPlay */ + +#ifndef X_DISPLAY_FIX_H +#define X_DISPLAY_FIX_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +static Display* display; +static Window root, my_window; + +void get_x_display_root() { + display = XOpenDisplay(NULL); + root = XDefaultRootWindow(display); +} + +Window enum_windows(Display* display, Window window, int depth) { + int i; + + XTextProperty text; + XGetWMName(display, window, &text); + char* name; + XFetchName(display, window, &name); + + if (name != 0 && strcmp((const char*) g_get_application_name(), name) == 0) { + return window; + } + + Window _root, parent; + Window* children; + unsigned int n; + XQueryTree(display, window, &_root, &parent, &children, &n); + if (children != NULL) { + for (i = 0; i < n; i++) { + Window w = enum_windows(display, children[i], depth + 1); + if (w) return w; + } + XFree(children); + } + + return (Window) NULL; +} + +void fix_x_window_name() { + if (!my_window) { + my_window = enum_windows(display, root, 0); + if (my_window) { + const char* str = g_get_application_name(); + Atom _NET_WM_NAME = XInternAtom(display, "_NET_WM_NAME", 0); + Atom UTF8_STRING = XInternAtom(display, "UTF8_STRING",0); + XChangeProperty(display, my_window, _NET_WM_NAME, UTF8_STRING, 8, 0, (const unsigned char *) str, strlen(str)); + XSync(display, False); + } + } +} + + +#ifdef __cplusplus +} +#endif + +#endif