Skip to content

Commit 758a7c3

Browse files
committed
hsr-outputs - command line tool to list screen sizes and positions
$ hsr-outputs screen_number: 0 x_org: 1920 y_org: 0 width: 1920 height: 1080 screen_number: 1 x_org: 0 y_org: 0 width: 1920 height: 1080 $ ./hsr-outputs --csv screen_number,x_org,y_org,width,height 0,1920,0,1920,1080 1,0,0,1920,1080 $ ./hsr-outputs --json [ { "screen": 0, "x": 1920, "y": 0, "width": 1920, "height": 1080 }, { "screen": 1, "x": 0, "y": 0, "width": 1920, "height": 1080 } ]
1 parent 140b658 commit 758a7c3

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.*.sw[po]
33
*.o
44
hsetroot
5+
hsr-outputs

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ LDFLAGS+=$(shell $(PKG_CONFIG) x11 --libs)
1818
CFLAGS+=$(shell $(PKG_CONFIG) imlib2 --cflags)
1919
LDFLAGS+=$(shell $(PKG_CONFIG) imlib2 --libs)
2020

21+
CFLAGS+=$(shell $(PKG_CONFIG) xinerama --cflags)
22+
LDFLAGS+=$(shell $(PKG_CONFIG) xinerama --libs)
23+
24+
all: hsetroot hsr-outputs
25+
2126
hsetroot: hsetroot.o outputs_xrandr.o
2227

2328
hsetroot.o: hsetroot.c outputs.h
2429

2530
outputs_xrandr.o: outputs_xrandr.c outputs.h
2631

32+
hsr-outputs: hsr-outputs.o
33+
2734
install: hsetroot
2835
install -st /usr/local/bin/ hsetroot
2936

3037
clean:
31-
rm -f hsetroot *.o
38+
rm -f *.o hsetroot hsr-outputs

hsr-outputs.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <X11/Xlib.h>
5+
#include <X11/extensions/Xinerama.h>
6+
7+
void
8+
print_text(XineramaScreenInfo *si) {
9+
if (! si) {
10+
printf("\n");
11+
return;
12+
}
13+
14+
printf("screen_number: %d\n", si->screen_number);
15+
printf("\tx_org:\t%d\n", si->x_org);
16+
printf("\ty_org:\t%d\n", si->y_org);
17+
printf("\twidth:\t%d\n", si->width);
18+
printf("\theight:\t%d\n", si->height);
19+
}
20+
21+
void
22+
print_csv(XineramaScreenInfo *si) {
23+
if (! si)
24+
return;
25+
26+
printf("%d,%d,%d,%d,%d\n", si->screen_number, si->x_org, si->y_org, si->width, si->height);
27+
}
28+
29+
void
30+
print_json(XineramaScreenInfo *si) {
31+
if (! si) {
32+
printf(",\n");
33+
return;
34+
}
35+
36+
printf(" {\n");
37+
printf(" \"screen\": %d,\n", si->screen_number);
38+
printf(" \"x\": %d,\n", si->x_org);
39+
printf(" \"y\": %d,\n", si->y_org);
40+
printf(" \"width\": %d,\n", si->width);
41+
printf(" \"height\": %d\n", si->height);
42+
printf(" }");
43+
}
44+
45+
enum Format { TEXT, CSV, JSON };
46+
47+
int
48+
main(int argc, char **argv)
49+
{
50+
Display *display = XOpenDisplay(NULL);
51+
52+
enum Format format = TEXT;
53+
void (*print)(XineramaScreenInfo*) = print_text;
54+
55+
if ((argc > 1) && !strcmp(argv[1], "--csv")) {
56+
print = print_csv;
57+
format = CSV;
58+
}
59+
if ((argc > 1) && !strcmp(argv[1], "--json")) {
60+
print = print_json;
61+
format = JSON;
62+
}
63+
64+
if (format == CSV)
65+
printf("screen_number,x_org,y_org,width,height\n");
66+
if (format == JSON)
67+
printf("[\n");
68+
69+
int nscreens = 0;
70+
XineramaScreenInfo *xisi = XineramaQueryScreens(display, &nscreens);
71+
for (int i = 0; i < nscreens; i++) {
72+
if (i)
73+
print(NULL);
74+
print(xisi + i);
75+
}
76+
XFree(xisi);
77+
78+
if (format == JSON)
79+
printf("\n]\n");
80+
81+
XCloseDisplay(display);
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)