Skip to content

Instantly share code, notes, and snippets.

@AquariusPower
Forked from muktupavels/toggle-decorations.c
Last active January 13, 2023 00:01
Show Gist options
  • Save AquariusPower/113f4559a4ac8ccb0225a89b9c74c0ea to your computer and use it in GitHub Desktop.
Save AquariusPower/113f4559a4ac8ccb0225a89b9c74c0ea to your computer and use it in GitHub Desktop.
/*
* 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:
* c++ -Wall -c -o windowtweaks.o windowtweaks.c; c++ -o windowtweaks windowtweaks.o `pkg-config --cflags --libs x11`
*
* Usage:
* windowtweaks --help
*
* Support me:
* https://www.patreon.com/muktupavels
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
typedef struct
{
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long input_mode;
unsigned long status;
} MotifWmHints;
static void
remove_input (Display *display,
Window window, Bool bAdd)
{
XWMHints *hints;
hints = XGetWMHints (display, window);
if(bAdd){
hints->flags &= InputHint;
hints->input = True;
}else{
hints->flags |= InputHint;
hints->input = False;
}
XSetWMHints (display, window, hints);
XFree (hints);
}
static void
remove_take_focus_protocol (Display *display,
Window window, Bool bAdd)
{
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 + (bAdd ? 1 : 0)];
int new_count;
int i;
new_count = 0;
Bool bFound=False;
for (i = 0; i < count; i++){
if (protocols[i] == wm_take_focus){
bFound=True;
continue;
}
new_list[new_count++] = protocols[i];
}
if(!bFound && bAdd){
new_list[new_count++] = wm_take_focus;
}
XSetWMProtocols (display, window, new_list, new_count);
XFree (protocols);
}
}
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 = (MotifWmHints*)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, Bool bToggle, Bool bAdd)
{
MotifWmHints *hints;
Atom property;
int nelements;
hints = get_motif_wm_hints (display, window);
if (hints == NULL)
{
hints = (MotifWmHints*)calloc (1, sizeof (*hints));
hints->decorations = (1L << 0);
}
hints->flags |= (1L << 1);
if(bToggle) //priority
bAdd=hints->decorations == 0;
hints->decorations = bAdd ? (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;
Bool bRmInput=False;
Bool bRmFocus=False;
Bool bRmDeco=False;
Bool bTgDeco=False;
Bool bAdd=False;
window = 0;
for(int i=1;i<argc;i++){
std::string strOpt=argv[i];
if(strOpt=="--help"){
std::cerr<<"Help:"<<std::endl;
std::cerr<<"-d remove decorations"<<std::endl;
std::cerr<<"-D add decorations"<<std::endl;
std::cerr<<"-t toggle decorations (override)"<<std::endl;
std::cerr<<"-i remove input hint"<<std::endl;
std::cerr<<"-I set input hint"<<std::endl;
std::cerr<<"-f remove focus protocol"<<std::endl;
std::cerr<<"-F add focus protocol if missing"<<std::endl;
std::cerr<<"<windowId> can be hexa eg. 0x12345678"<<std::endl;
return 0;
}else
if(strOpt=="-d")bRmDeco=True;
else
if(strOpt=="-D"){bRmDeco=True;bAdd=True;}
else
if(strOpt=="-t"){bTgDeco=True;}
else
if(strOpt=="-i")bRmInput=True;
else
if(strOpt=="-I"){bRmInput=True;bAdd=True;}
else
if(strOpt=="-f")bRmFocus=True;
else
if(strOpt=="-F"){bRmFocus=True;bAdd=True;}
else{
sscanf (strOpt.c_str(), "0x%lx", &window);
if (window == 0)
sscanf (strOpt.c_str(), "%lu", &window);
}
}
if (window == 0){
std::cerr<<"ERROR: windowId is missing"<<std::endl;
return 1;
}
if(bRmDeco || bTgDeco)toggle_window_decorations (display, window, bTgDeco, bAdd);
if(bRmInput)remove_input (display, window, bAdd);
if(bRmFocus)remove_take_focus_protocol (display, window, bAdd);
XCloseDisplay (display);
return 0;
}
@ha7ak3
Copy link

ha7ak3 commented Jan 13, 2023

Thanks

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