Skip to content

Instantly share code, notes, and snippets.

@znz
Created October 14, 2009 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save znz/210010 to your computer and use it in GitHub Desktop.
Save znz/210010 to your computer and use it in GitHub Desktop.
dump my X window props
/* based on http://www.faqs.org/faqs/Xt-FAQ/ 's visual.c */
/*
* Some sample code to start up an application using something other
* than the default visual.
*
* To compile:
* cc -g visual.c -o visual -lXaw -lXmu -lXt -lXext -lX11 -lm
*
* To run:
* ./visual -geometry 300x300 -depth 24 -visual StaticColor -fg blue -bg yellow
*
* you need to move the mouse to get the particular visuals colormap
* to install.
*/
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xatom.h>
#include <stdio.h>
typedef struct
{
Visual *visual;
int depth;
} OptionsRec;
OptionsRec Options;
XtResource resources[] =
{
{"visual", "Visual", XtRVisual, sizeof (Visual *),
XtOffsetOf (OptionsRec, visual), XtRImmediate, NULL},
{"depth", "Depth", XtRInt, sizeof (int),
XtOffsetOf (OptionsRec, depth), XtRImmediate, NULL},
};
XrmOptionDescRec Desc[] =
{
{"-visual", "*visual", XrmoptionSepArg, NULL},
{"-depth", "*depth", XrmoptionSepArg, NULL}
};
static void
remove_close_from_allowed_actions(Widget widget)
{
Display *dpy;
Window win;
char *names[] = {
"_NET_WM_ALLOWED_ACTIONS",
"_NET_WM_ACTION_CLOSE",
};
Atom atoms[2];
Atom *prop_atoms;
int num_prop;
int i;
int istatus;
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop;
char *s;
dpy = XtDisplay(widget);
win = XtWindow(widget);
XInternAtoms(dpy, names, 2, False, atoms);
printf("atoms[0]=%s\n", s=XGetAtomName(dpy, atoms[0])); XFree(s);
printf("atoms[1]=%s\n", s=XGetAtomName(dpy, atoms[1])); XFree(s);
prop_atoms = XListProperties(dpy, win, &num_prop);
printf("num_prop=%d\n", num_prop);
for (i=0; i<num_prop; i++) {
printf("prop_atoms[%d]=%s\n", i, XGetAtomName(dpy, prop_atoms[i]));
istatus = XGetWindowProperty(dpy, win, prop_atoms[i], 0, 1000,
False, AnyPropertyType, &actual_type,
&actual_format, &nitems, &bytes_after,
&prop);
if (istatus == Success) {
printf(" XGetWindowProperty: Success\n");
printf(" actual_type=%s\n", s=XGetAtomName(dpy, actual_type)); XFree(s);
printf(" actual_format=%d\n", actual_format);
printf(" nitems=%lu\n", nitems);
printf(" bytes_after=%d\n", bytes_after);
if (prop_atoms[i] == atoms[0]) {
int j;
Atom *avalue = (Atom*)prop;
for (j=0; j<nitems; j++) {
printf(" prop[%d]=%s\n", j, s=XGetAtomName(dpy, avalue[j])); XFree(s);
if (avalue[j] == atoms[1]) {
avalue[j] = avalue[nitems-1];
}
}
for (j=0; j<nitems; j++) {
printf(" prop[%d]=%s\n", j, s=XGetAtomName(dpy, avalue[j])); XFree(s);
}
XChangeProperty(dpy, win, atoms[0], XA_ATOM, 32,
PropModeReplace,
prop,
nitems-1);
}
XFree(prop);
} else {
printf(" %d\n", istatus);
}
}
if (prop_atoms)
XFree(prop_atoms);
}
int
main (argc, argv)
int argc;
char **argv;
{
XtAppContext app; /* the application context */
Widget top; /* toplevel widget */
Display *dpy; /* display */
char **xargv; /* saved argument vector */
int xargc; /* saved argument count */
Colormap colormap; /* created colormap */
XVisualInfo vinfo; /* template for find visual */
XVisualInfo *vinfo_list; /* returned list of visuals */
int count; /* number of matchs (only 1?) */
Arg args[10];
Cardinal cnt;
char *name = "test";
char *class = "Test";
/*
* save the command line arguments
*/
xargc = argc;
xargv = (char **) XtMalloc (argc * sizeof (char *));
bcopy ((char *) argv, (char *) xargv, argc * sizeof (char *));
/*
* The following creates a _dummy_ toplevel widget so we can
* retrieve the appropriate visual resource.
*/
cnt = 0;
top = XtAppInitialize (&app, class, Desc, XtNumber (Desc), &argc, argv,
(String *) NULL, args, cnt);
dpy = XtDisplay (top);
cnt = 0;
XtGetApplicationResources (top, &Options, resources,
XtNumber (resources),
args, cnt);
cnt = 0;
if (Options.visual && Options.visual != DefaultVisualOfScreen (XtScreen (top)))
{
XtSetArg (args[cnt], XtNvisual, Options.visual); ++cnt;
/*
* Now we create an appropriate colormap. We could
* use a default colormap based on the class of the
* visual; we could examine some property on the
* rootwindow to find the right colormap; we could
* do all sorts of things...
*/
colormap = XCreateColormap (dpy,
RootWindowOfScreen (XtScreen (top)),
Options.visual,
AllocNone);
XtSetArg (args[cnt], XtNcolormap, colormap); ++cnt;
/*
* Now find some information about the visual.
*/
vinfo.visualid = XVisualIDFromVisual (Options.visual);
vinfo_list = XGetVisualInfo (dpy, VisualIDMask, &vinfo, &count);
if (vinfo_list && count > 0)
{
XtSetArg (args[cnt], XtNdepth, vinfo_list[0].depth);
++cnt;
XFree ((XPointer) vinfo_list);
}
}
XtDestroyWidget (top);
/*
* Now create the real toplevel widget.
*/
XtSetArg (args[cnt], XtNargv, xargv); ++cnt;
XtSetArg (args[cnt], XtNargc, xargc); ++cnt;
top = XtAppCreateShell ((char *) NULL, class,
applicationShellWidgetClass,
dpy, args, cnt);
XtAppAddTimeOut(app, 500, (XtTimerCallbackProc)remove_close_from_allowed_actions, top);
/*
* Display the application and loop handling all events.
*/
XtRealizeWidget (top);
XtAppMainLoop (app);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment