Skip to content

Commit

Permalink
Make X11 support optional
Browse files Browse the repository at this point in the history
  • Loading branch information
wb9688 committed Feb 8, 2021
1 parent 8d8ecb3 commit f039615
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 21 deletions.
27 changes: 17 additions & 10 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,19 @@ AC_ARG_ENABLE(wayland,
[enable_wayland="${enableval}"],
[enable_wayland=no])

# Checks for libraries.

PKG_CHECK_MODULES(XLIB, "x11")
AC_SUBST(XLIB_CFLAGS)
AC_SUBST(XLIB_LIBS)
AC_ARG_ENABLE(x11,
[AC_HELP_STRING([--enable-x11],
[Enable X11 support])],
[enable_x11="${enableval}"],
[enable_x11=yes])

if test "x$enable_wayland" = "xno" && test "x&$enable_x11" = "xno"; then
AC_MSG_ERROR([At least one of X11 and Wayland needs to be enabled])
fi

gio_modules="gthread-2.0 gio-unix-2.0 >= 2.18.0"
PKG_CHECK_MODULES(GIO, [$gio_modules])
AC_SUBST(GIO_CFLAGS)
AC_SUBST(GIO_LIBS)
# Checks for libraries.
fm_modules="gthread-2.0 gio-unix-2.0 >= 2.18.0 glib-2.0 pango >= 1.20.0 libfm >= 1.0"

fm_modules="$gio_modules glib-2.0 pango >= 1.20.0 libfm >= 1.0"
case "$ac_with_gtk" in
3|3.*)
fm_modules="$fm_modules gtk+-3.0 libfm-gtk3 >= 1.0.1"
Expand All @@ -113,6 +114,12 @@ case "$ac_with_gtk" in
fi
;;
esac

if test "x$enable_x11" = "xyes"; then
fm_modules="$fm_modules x11"
AC_DEFINE(HAVE_X11, 1, [Have X11 support])
fi

PKG_CHECK_MODULES(FM, [$fm_modules])
AC_SUBST(FM_CFLAGS)
AC_SUBST(FM_LIBS)
Expand Down
2 changes: 0 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ EXTRA_DIST= \
include_HEADERS = pcmanfm-modules.h

pcmanfm_CFLAGS = \
$(XLIB_CFLAGS) \
$(FM_CFLAGS) \
$(G_CAST_CHECKS) \
-Wall \
-Werror-implicit-function-declaration \
$(NULL)

pcmanfm_LDADD = \
$(XLIB_LIBS) \
$(FM_LIBS) \
$(NULL)

Expand Down
61 changes: 55 additions & 6 deletions src/desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,26 @@

#include <glib/gi18n.h>

#ifdef HAVE_X11
#include <gdk/gdkx.h>
#else
#include <gdk/gdk.h>
#endif
#include <gdk/gdkkeysyms.h>
#ifdef HAVE_X11
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#endif
#ifdef HAVE_WAYLAND
#include <gtk-layer-shell/gtk-layer-shell.h>
#endif
#include <math.h>

#ifdef HAVE_X11
#include <cairo-xlib.h>
#else
#include <cairo.h>
#endif

#include "pref.h"
#include "main-win.h"
Expand All @@ -58,7 +68,15 @@
/* the search dialog timeout (in ms) */
#define DESKTOP_SEARCH_DIALOG_TIMEOUT (5000)

#ifdef HAVE_X11
#ifdef HAVE_WAYLAND
#define IS_X11() (GDK_IS_X11_DISPLAY(gdk_display_get_default()))
#else
#define IS_X11() (TRUE)
#endif
#else
#define IS_X11() (FALSE)
#endif

struct _FmDesktopItem
{
Expand Down Expand Up @@ -108,11 +126,13 @@ static int n_screens = 0;
static guint icon_theme_changed = 0;
static GtkAccelGroup* acc_grp = NULL;

#ifdef HAVE_X11
static Atom XA_NET_WORKAREA = 0;
static Atom XA_NET_NUMBER_OF_DESKTOPS = 0;
static Atom XA_NET_CURRENT_DESKTOP = 0;
static Atom XA_XROOTMAP_ID = 0;
static Atom XA_XROOTPMAP_ID = 0;
#endif

static GdkCursor* hand_cursor = NULL;

Expand Down Expand Up @@ -371,6 +391,7 @@ static inline void reload_items(FmDesktop *desktop)
load_items(desktop);
}

#ifdef HAVE_X11
static gint get_desktop_for_root_window(GdkWindow *root)
{
gint desktop = -1;
Expand All @@ -389,6 +410,7 @@ static gint get_desktop_for_root_window(GdkWindow *root)
}
return desktop;
}
#endif

/* save position of desktop icons */
static void save_item_pos(FmDesktop* desktop)
Expand Down Expand Up @@ -2091,9 +2113,11 @@ static void paint_rubber_banding_rect(FmDesktop* self, cairo_t* cr, GdkRectangle
static void _free_cache_image(FmBackgroundCache *cache)
{
#if GTK_CHECK_VERSION(3, 0, 0)
#ifdef HAVE_X11
if(IS_X11())
XFreePixmap(cairo_xlib_surface_get_display(cache->bg),
cairo_xlib_surface_get_drawable(cache->bg));
#endif
cairo_surface_destroy(cache->bg);
#else
g_object_unref(cache->bg);
Expand Down Expand Up @@ -2128,9 +2152,11 @@ static void update_background(FmDesktop* desktop, int is_it)
cairo_pattern_t *pattern;
#endif

#ifdef HAVE_X11
Display* xdisplay;
Pixmap xpixmap;
Window xroot;
#endif
int screen_num = gdk_screen_get_number(screen);

char *wallpaper;
Expand Down Expand Up @@ -2306,16 +2332,18 @@ static void update_background(FmDesktop* desktop, int is_it)
#if GTK_CHECK_VERSION(3, 0, 0)
if(IS_X11())
{
#ifdef HAVE_X11
xdisplay = GDK_WINDOW_XDISPLAY(root);
/* this code is taken from libgnome-desktop */
xpixmap = XCreatePixmap(xdisplay, RootWindow(xdisplay, screen_num),
dest_w, dest_h, DefaultDepth(xdisplay, screen_num));
cache->bg = cairo_xlib_surface_create(xdisplay, xpixmap,
GDK_VISUAL_XVISUAL(gdk_screen_get_system_visual(screen)),
dest_w, dest_h);
} else {
cache->bg = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, dest_w, dest_h);
#endif
}
else
cache->bg = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, dest_w, dest_h);
cr = cairo_create(cache->bg);
#else
cache->bg = gdk_pixmap_new(window, dest_w, dest_h, -1);
Expand Down Expand Up @@ -2381,6 +2409,7 @@ static void update_background(FmDesktop* desktop, int is_it)
gdk_window_set_back_pixmap(window, cache->bg, FALSE);
#endif

#ifdef HAVE_X11
if(IS_X11())
{
/* set root map here */
Expand Down Expand Up @@ -2433,6 +2462,7 @@ static void update_background(FmDesktop* desktop, int is_it)
XFlush(xdisplay);
XUngrabServer(xdisplay);
}
#endif

if(pix)
g_object_unref(pix);
Expand Down Expand Up @@ -2610,6 +2640,7 @@ static void update_working_area(FmDesktop* desktop)
return;
}

#ifdef HAVE_X11
static GdkFilterReturn on_root_event(GdkXEvent *xevent, GdkEvent *event, gpointer data)
{
XPropertyEvent * evt = (XPropertyEvent*) xevent;
Expand All @@ -2632,6 +2663,7 @@ static GdkFilterReturn on_root_event(GdkXEvent *xevent, GdkEvent *event, gpointe
}
return GDK_FILTER_CONTINUE;
}
#endif

static void on_screen_size_changed(GdkScreen* screen, FmDesktop* desktop)
{
Expand Down Expand Up @@ -3111,6 +3143,7 @@ static void _focus_and_select_focused_item(FmDesktop *desktop, FmDesktopItem *it
set_focused_item(desktop, item);
}

#ifdef HAVE_X11
/* This function is taken from xfdesktop */
static void forward_event_to_rootwin(GdkScreen *gscreen, GdkEvent *event)
{
Expand Down Expand Up @@ -3182,6 +3215,7 @@ static void forward_event_to_rootwin(GdkScreen *gscreen, GdkEvent *event)
XSendEvent(dpy, xev2.window, False, ButtonPressMask | ButtonReleaseMask,
(XEvent *) & xev2);
}
#endif


#if GTK_CHECK_VERSION(3, 0, 0)
Expand Down Expand Up @@ -3483,8 +3517,10 @@ static gboolean on_button_press(GtkWidget* w, GdkEventButton* evt)
else if(evt->button != 1 && evt->button == self->button_pressed)
{
self->forward_pending = TRUE;
#ifdef HAVE_X11
if(IS_X11())
forward_event_to_rootwin(gtk_widget_get_screen(w), (GdkEvent*)evt);
#endif
}

if(! gtk_widget_has_focus(w))
Expand Down Expand Up @@ -3521,8 +3557,10 @@ static gboolean on_button_release(GtkWidget* w, GdkEventButton* evt)
/* forward the event to root window */
if (self->button_pressed == evt->button)
{
#ifdef HAVE_X11
if (self->forward_pending && IS_X11())
forward_event_to_rootwin(gtk_widget_get_screen(w), (GdkEvent*)evt);
#endif
self->button_pressed = 0;
self->forward_pending = FALSE;
}
Expand Down Expand Up @@ -4860,8 +4898,10 @@ static void fm_desktop_destroy(GtkObject *object)
if(self->icon_render) /* see bug #3533958 by korzhpavel@SF */
{
screen = gtk_widget_get_screen((GtkWidget*)self);
#ifdef HAVE_X11
if(IS_X11())
gdk_window_remove_filter(gdk_screen_get_root_window(screen), on_root_event, self);
#endif

g_signal_handlers_disconnect_by_func(screen, on_screen_size_changed, self);
#if FM_CHECK_VERSION(1, 2, 0)
Expand Down Expand Up @@ -5034,17 +5074,22 @@ static GObject* fm_desktop_constructor(GType type, guint n_construct_properties,

if(IS_X11())
{
#ifdef HAVE_X11
root = gdk_screen_get_root_window(screen);
gdk_window_set_events(root, gdk_window_get_events(root)|GDK_PROPERTY_CHANGE_MASK);
gdk_window_add_filter(root, on_root_event, self);
}
g_signal_connect(screen, "monitors-changed", G_CALLBACK(on_screen_size_changed), self);

n = IS_X11() ? get_desktop_for_root_window(root) : 0;
if(n < 0)
n = get_desktop_for_root_window(root);
if(n < 0)
n = 0;
#endif
}
else
n = 0;
self->cur_desktop = (guint)n;

g_signal_connect(screen, "monitors-changed", G_CALLBACK(on_screen_size_changed), self);

/* init dnd support */
self->dnd_src = fm_dnd_src_new((GtkWidget*)self);
#if !FM_CHECK_VERSION(1, 2, 1)
Expand Down Expand Up @@ -5099,9 +5144,11 @@ static void fm_desktop_class_init(FmDesktopClass *klass)
{
GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass);
typedef gboolean (*DeleteEvtHandler) (GtkWidget*, GdkEventAny*);
#ifdef HAVE_X11
char* atom_names[] = {"_NET_WORKAREA", "_NET_NUMBER_OF_DESKTOPS",
"_NET_CURRENT_DESKTOP", "_XROOTMAP_ID", "_XROOTPMAP_ID"};
Atom atoms[G_N_ELEMENTS(atom_names)] = {0};
#endif
GObjectClass* object_class = G_OBJECT_CLASS(klass);

#if GTK_CHECK_VERSION(3, 0, 0)
Expand Down Expand Up @@ -5140,6 +5187,7 @@ static void fm_desktop_class_init(FmDesktopClass *klass)
#endif
/* widget_class->drag_data_get = on_drag_data_get; */

#ifdef HAVE_X11
if(IS_X11() &&
XInternAtoms(gdk_x11_get_default_xdisplay(), atom_names,
G_N_ELEMENTS(atom_names), False, atoms))
Expand All @@ -5150,6 +5198,7 @@ static void fm_desktop_class_init(FmDesktopClass *klass)
XA_XROOTMAP_ID = atoms[3];
XA_XROOTPMAP_ID = atoms[4];
}
#endif

object_class->constructor = fm_desktop_constructor;
object_class->set_property = fm_desktop_set_property;
Expand Down
21 changes: 18 additions & 3 deletions src/pcmanfm.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
#endif

#include <gtk/gtk.h>
#ifdef HAVE_X11
#include <gdk/gdkx.h>
#else
#include <gdk/gdk.h>
#endif
#include <stdio.h>
#include <glib/gi18n.h>

Expand Down Expand Up @@ -227,7 +231,9 @@ int main(int argc, char** argv)
inst.prog_name = "pcmanfm";
inst.cb = single_inst_cb;
inst.opt_entries = opt_entries + 3;
#ifdef HAVE_X11
inst.screen_num = gdk_x11_get_default_screen();
#endif
switch(single_inst_init(&inst))
{
case SINGLE_INST_CLIENT: /* we're not the first instance. */
Expand Down Expand Up @@ -353,13 +359,18 @@ gboolean pcmanfm_run(gint screen_num)
{
if(!desktop_running)
{
GdkDisplay *default_display = gdk_display_get_default();
#if HAVE_WAYLAND
if(!GDK_IS_X11_DISPLAY(default_display) && !gtk_layer_is_supported())
#if HAVE_X11
if(!GDK_IS_X11_DISPLAY(gdk_display_get_default()) && !gtk_layer_is_supported())
{
fm_show_error(NULL, NULL, _("Only X11 and Wayland (with wlr-layer-shell) are supported"));
#else
if(!GDK_IS_X11_DISPLAY(default_display))
if(!gtk_layer_is_supported())
{
fm_show_error(NULL, NULL, _("Only Wayland (with wlr-layer-shell) is supported"));
#endif
#else
if(!GDK_IS_X11_DISPLAY(gdk_display_get_default()))
{
fm_show_error(NULL, NULL, _("Only X11 is supported"));
#endif
Expand Down Expand Up @@ -542,6 +553,7 @@ void pcmanfm_unref()
gtk_main_quit();
}

#ifdef HAVE_X11
static void move_window_to_desktop(FmMainWin* win, FmDesktop* desktop)
{
GdkScreen* screen = gtk_widget_get_screen(GTK_WIDGET(desktop));
Expand Down Expand Up @@ -569,6 +581,7 @@ static void move_window_to_desktop(FmMainWin* win, FmDesktop* desktop)
(SubstructureNotifyMask | SubstructureRedirectMask),
(XEvent *) &xev);
}
#endif

gboolean pcmanfm_open_folder(GAppLaunchContext* ctx, GList* folder_infos, gpointer user_data, GError** err)
{
Expand All @@ -592,9 +605,11 @@ gboolean pcmanfm_open_folder(GAppLaunchContext* ctx, GList* folder_infos, gpoint
FmFileInfo* fi = (FmFileInfo*)l->data;
fm_main_win_open_in_last_active(fm_file_info_get_path(fi));
}
#ifdef HAVE_X11
if(user_data && FM_IS_DESKTOP(user_data) &&
GDK_IS_X11_WINDOW(gtk_widget_get_window(GTK_WIDGET(user_data))))
move_window_to_desktop(fm_main_win_get_last_active(), user_data);
#endif
return TRUE;
}

Expand Down

0 comments on commit f039615

Please sign in to comment.