Skip to content

Instantly share code, notes, and snippets.

@muktupavels
Last active October 7, 2024 02:58
Show Gist options
  • Save muktupavels/d03bb14ea6042b779df89b4c87df975d to your computer and use it in GitHub Desktop.
Save muktupavels/d03bb14ea6042b779df89b4c87df975d to your computer and use it in GitHub Desktop.
Simple app to toggle window decorations.
/*
* Copyright (C) 2017 Alberts Muktupāvels
*
* 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, see <http://www.gnu.org/licenses/>.
*
* Compile with:
* gcc toggle-decorations.c -Wall -o toggle-decorations `pkg-config --cflags --libs x11`
*
* Usage:
* toggle-decorations 0x1234567
*
* Support me:
* https://www.patreon.com/muktupavels
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
typedef struct
{
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long input_mode;
unsigned long status;
} MotifWmHints;
static MotifWmHints *
get_motif_wm_hints (Display *display,
Window window)
{
Atom property;
int result;
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *data;
property = XInternAtom (display, "_MOTIF_WM_HINTS", False);
result = XGetWindowProperty (display, window, property,
0, LONG_MAX, False, AnyPropertyType,
&actual_type, &actual_format,
&nitems, &bytes_after, &data);
if (result == Success && data != NULL)
{
size_t data_size;
size_t max_size;
MotifWmHints *hints;
data_size = nitems * sizeof (long);
max_size = sizeof (*hints);
hints = calloc (1, max_size);
memcpy (hints, data, data_size > max_size ? max_size : data_size);
XFree (data);
return hints;
}
return NULL;
}
static void
toggle_window_decorations (Display *display,
Window window)
{
MotifWmHints *hints;
Atom property;
int nelements;
hints = get_motif_wm_hints (display, window);
if (hints == NULL)
{
hints = calloc (1, sizeof (*hints));
hints->decorations = (1L << 0);
}
hints->flags |= (1L << 1);
hints->decorations = hints->decorations == 0 ? (1L << 0) : 0;
property = XInternAtom (display, "_MOTIF_WM_HINTS", False);
nelements = sizeof (*hints) / sizeof (long);
XChangeProperty (display, window, property, property, 32, PropModeReplace,
(unsigned char *) hints, nelements);
free (hints);
}
int
main (int argc,
char *argv[])
{
Display *display;
Window window;
display = XOpenDisplay (NULL);
if (display == NULL)
return 1;
window = 0;
if (argc > 1)
{
sscanf (argv[1], "0x%lx", &window);
if (window == 0)
sscanf (argv[1], "%lu", &window);
}
if (window == 0)
return 1;
toggle_window_decorations (display, window);
XCloseDisplay (display);
return 0;
}
@AquariusPower
Copy link

hi!

Any idea how to make the window stop receiving input focus? I will try to understand the code and do that :)

What exactly you want to achieve?

it is this:
https://unix.stackexchange.com/questions/590282/how-to-prevent-input-focus-for-a-single-window-in-gnome-shell-window-manager
https://stackoverflow.com/questions/56559726/how-to-change-x-window-properties

we are trying to change a specific tiny onTop window properties, protocols, hints etc to prevent it from receiving input,
because when it receives input, the useful windows below it (geany,netbeans etc) wont receive the input,
and it always receive input after I close yakuake (because that tiny window is onTop I guess).

@muktupavels
Copy link
Author

@AquariusPower I think you need to do two things - remove input and take_focus protocol...

Remove input:

static void
remove_input (Display *display,
              Window   window)
{
  XWMHints *hints;

  hints = XGetWMHints (display, window);

  hints->flags |= InputHint;
  hints->input = False;

  XSetWMHints (display, window, hints);
  XFree (hints);
}

Remove take_focus protocol:

static void
remove_take_focus_protocol (Display *display,
                            Window   window)
{
  Atom wm_take_focus;
  Atom *protocols;
  int count;

  wm_take_focus = XInternAtom (display, "WM_TAKE_FOCUS", False);

  if (XGetWMProtocols (display, window, &protocols, &count) != 0)
    {
      Atom new_list[count];
      int new_count;
      int i;

      new_count = 0;

      for (i = 0; i < count; i++)
        {
          if (protocols[i] == wm_take_focus)
            continue;

          new_list[new_count++] = protocols[i];
        }

      XSetWMProtocols (display, window, new_list, new_count);
      XFree (protocols);
    }
}

@AquariusPower
Copy link

cool thx! I will try it as soon I can! :D

@AquariusPower
Copy link

AquariusPower commented Jun 7, 2020

worked perfectly!

Later I will try to do the "simple things":

  • add command line options to let each of the 3 functionalities be chosen.
  • add "select window" in case no id is passed as param (xwininfo and xprop have such code I can use as reference I guess).

Obs.: Before this code, I had a script loop that would detect if the window is that one and it would activate (with xdotool) the previous window with focus. Quite complicated and didnt work well overall.

I just updated my fork with it as it is now, thx vm!
EDIT: I made each of the 3 optional, added that info at --help, changed app name also (despite it is widely known as toggle-decorations) and changed compilation a bit. It would be cool if we had pull requests here also :)

PS.: btw, I can still maximize/unmaximize the window to see full nethogs info what works great too :)

@muktupavels
Copy link
Author

@AquariusPower I don't receive notifications about edits...

Quickly looked at your fork. In remove_take_focus_protocol you want Atom new_list[count + 1]; or Atom new_list[count + (bAdd ? 1 : 0)]; to make sure new_list has space for extra Atom.

@AquariusPower
Copy link

Quickly looked at your fork. In remove_take_focus_protocol you want Atom new_list[count + 1]; or Atom new_list[count + (bAdd ? 1 : 0)]; to make sure new_list has space for extra Atom.

cool thx!! fixed there :)

@rysson
Copy link

rysson commented Jul 17, 2021

It's very useful, thanks.

argc is (almost) always grater than zero ([0] is the program name).

I suggest to change L119 to (it fixes crash):

  if (argc > 1)

@muktupavels
Copy link
Author

@rysson Thanks for suggestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment