]>
Dogcows Code - chaz/openbox/blob - tools/obxprop/obxprop.c
b28b77a7c13ce931bd7ce0dedad9620c03814240
2 #include <X11/cursorfont.h>
9 gint
fail(const gchar
*s
) {
11 fprintf(stderr
, "%s\n", s
);
15 "Usage: obxprop [OPTIONS] [--] [PROPERTIES ...]\n\n"
17 " --help Display this help and exit\n"
18 " --display DISPLAY Connect to this X display\n"
19 " --id ID Show the properties for this window\n"
20 " --root Show the properties for the root window\n");
24 gint
parse_hex(gchar
*s
) {
28 if (*s
>= '0' && *s
<='9')
30 else if (*s
>= 'A' && *s
<='F')
32 else if (*s
>= 'a' && *s
<='f')
43 Window
find_client(Display
*d
, Window win
)
47 Atom state
= XInternAtom(d
, "WM_STATE", True
);
50 gulong ret_items
, ret_bytesleft
, *xdata
;
52 XQueryTree(d
, win
, &r
, &r
, &children
, &n
);
53 for (i
= 0; i
< n
; ++i
) {
54 Window w
= find_client(d
, children
[i
]);
59 res
= XGetWindowProperty(d
, win
, state
, 0, 1,
60 False
, state
, &ret_type
, &ret_format
,
61 &ret_items
, &ret_bytesleft
,
62 (unsigned char**) &xdata
);
64 if (res
!= Success
|| ret_type
== None
|| ret_items
< 1)
66 return win
; // found it!
69 static gboolean
get_all(Display
*d
, Window win
, Atom prop
,
70 Atom
*type
, gint
*size
,
71 guchar
**data
, guint
*num
)
76 gulong ret_items
, bytes_left
;
78 res
= XGetWindowProperty(d
, win
, prop
, 0l, G_MAXLONG
,
79 FALSE
, AnyPropertyType
, type
, size
,
80 &ret_items
, &bytes_left
, &xdata
);
85 *data
= g_malloc(ret_items
* (*size
/ 8));
86 for (i
= 0; i
< ret_items
; ++i
)
89 (*data
)[i
] = xdata
[i
];
92 ((guint16
*)*data
)[i
] = ((gushort
*)xdata
)[i
];
95 ((guint32
*)*data
)[i
] = ((gulong
*)xdata
)[i
];
98 g_assert_not_reached(); /* unhandled size */
108 gchar
*append_string(gchar
*before
, gchar
*after
, gboolean quote
)
111 const gchar
*q
= quote
? "\"" : "";
113 tmp
= g_strdup_printf("%s, %s%s%s", before
, q
, after
, q
);
115 tmp
= g_strdup_printf("%s%s%s", q
, after
, q
);
120 gchar
*append_int(gchar
*before
, guint after
)
124 tmp
= g_strdup_printf("%s, %u", before
, after
);
126 tmp
= g_strdup_printf("%u", after
);
131 gchar
* read_strings(gchar
*val
, guint n
, gboolean utf8
)
133 GSList
*strs
= NULL
, *it
;
138 while (p
< val
+ n
) {
139 strs
= g_slist_append(strs
, g_strndup(p
, n
- (p
- val
)));
140 p
+= strlen(p
) + 1; /* next string */
144 for (i
= 0, it
= strs
; it
; ++i
, it
= g_slist_next(it
)) {
148 if (g_utf8_validate(it
->data
, -1, NULL
))
149 data
= g_strdup(it
->data
);
154 data
= g_locale_to_utf8(it
->data
, -1, NULL
, NULL
, NULL
);
156 ret
= append_string(ret
, data
, TRUE
);
162 strs
= g_slist_delete_link(strs
, strs
);
167 gchar
* read_atoms(Display
*d
, guchar
*val
, guint n
)
173 for (i
= 0; i
< n
; ++i
)
174 ret
= append_string(ret
, XGetAtomName(d
, ((guint32
*)val
)[i
]), FALSE
);
178 gchar
* read_numbers(guchar
*val
, guint n
, guint size
)
184 for (i
= 0; i
< n
; ++i
)
187 ret
= append_int(ret
, ((guint8
*)val
)[i
]);
190 ret
= append_int(ret
, ((guint16
*)val
)[i
]);
193 ret
= append_int(ret
, ((guint32
*)val
)[i
]);
196 g_assert_not_reached(); /* unhandled size */
202 gboolean
read_prop(Display
*d
, Window w
, Atom prop
, const gchar
**type
, gchar
**val
)
210 if (get_all(d
, w
, prop
, &ret_type
, &size
, &ret
, &nret
)) {
211 *type
= XGetAtomName(d
, ret_type
);
213 if (strcmp(*type
, "STRING") == 0)
214 *val
= read_strings((gchar
*)ret
, nret
, FALSE
);
215 else if (strcmp(*type
, "UTF8_STRING") == 0)
216 *val
= read_strings((gchar
*)ret
, nret
, TRUE
);
217 else if (strcmp(*type
, "ATOM") == 0) {
218 g_assert(size
== 32);
219 *val
= read_atoms(d
, ret
, nret
);
222 *val
= read_numbers(ret
, nret
, size
);
230 void show_properties(Display
*d
, Window w
, int argc
, char **argv
)
235 props
= XListProperties(d
, w
, &n
);
237 for (i
= 0; i
< n
; ++i
) {
241 name
= XGetAtomName(d
, props
[i
]);
243 if (read_prop(d
, w
, props
[i
], &type
, &val
)) {
249 for (i
= 0; i
< argc
; i
++)
250 if (!strcmp(name
, argv
[i
])) {
256 g_print("%s(%s) = %s\n", name
, type
, val
);
266 int main(int argc
, char **argv
)
269 Window id
, userid
= None
;
272 gboolean root
= FALSE
;
274 for (i
= 1; i
< argc
; ++i
) {
275 if (!strcmp(argv
[i
], "--help")) {
278 else if (!strcmp(argv
[i
], "--root"))
280 else if (!strcmp(argv
[i
], "--id")) {
283 if (argv
[i
][0] == '0' && argv
[i
][1] == 'x') {
285 userid
= parse_hex(argv
[i
]+2);
289 userid
= atoi(argv
[i
]);
293 else if (!strcmp(argv
[i
], "--display")) {
298 else if (*argv
[i
] != '-')
300 else if (!strcmp(argv
[i
], "--")) {
308 d
= XOpenDisplay(dname
);
310 return fail("Unable to find an X display. "
311 "Ensure you have permission to connect to the display.");
315 userid
= RootWindow(d
, DefaultScreen(d
));
317 if (userid
== None
) {
319 j
= XGrabPointer(d
, RootWindow(d
, DefaultScreen(d
)),
320 False
, ButtonPressMask
,
321 GrabModeAsync
, GrabModeAsync
,
322 None
, XCreateFontCursor(d
, XC_crosshair
),
324 if (j
!= GrabSuccess
)
325 return fail("Unable to grab the pointer device");
330 if (ev
.type
== ButtonPress
) {
331 XUngrabPointer(d
, CurrentTime
);
332 userid
= ev
.xbutton
.subwindow
;
338 id
= find_client(d
, userid
);
341 return fail("Unable to find window with the requested ID");
343 show_properties(d
, id
, argc
- i
, &argv
[i
]);
This page took 0.04677 seconds and 3 git commands to generate.