aboutsummaryrefslogtreecommitdiff
path: root/i3-nagbar
diff options
context:
space:
mode:
authorMichael Stapelberg <stapelberg@users.noreply.github.com>2015-03-29 19:54:32 +0200
committerMichael Stapelberg <stapelberg@users.noreply.github.com>2015-03-29 19:54:32 +0200
commit6cba530946684b98a00d591a859f71ad1222cee4 (patch)
tree1be095fb432a13e8b0e88f1a65d25c308ff78cec /i3-nagbar
parent700dc4bfdec658c015bfb73619f4e39142874406 (diff)
parent0ad926825394f17e556a65d8a6ca83afd0822ca1 (diff)
downloadi3-6cba530946684b98a00d591a859f71ad1222cee4.tar.gz
i3-6cba530946684b98a00d591a859f71ad1222cee4.zip
Merge pull request #1593 from Airblader/feature-nagbar-primary-output
Display i3-nagbar on primary output
Diffstat (limited to 'i3-nagbar')
-rw-r--r--i3-nagbar/i3-nagbar.mk4
-rw-r--r--i3-nagbar/main.c65
2 files changed, 63 insertions, 6 deletions
diff --git a/i3-nagbar/i3-nagbar.mk b/i3-nagbar/i3-nagbar.mk
index e54aa654..e98d6582 100644
--- a/i3-nagbar/i3-nagbar.mk
+++ b/i3-nagbar/i3-nagbar.mk
@@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3-nagbar
i3_nagbar_SOURCES := $(wildcard i3-nagbar/*.c)
i3_nagbar_HEADERS := $(wildcard i3-nagbar/*.h)
-i3_nagbar_CFLAGS = $(XCB_CFLAGS) $(PANGO_CFLAGS)
-i3_nagbar_LIBS = $(XCB_LIBS) $(PANGO_LIBS)
+i3_nagbar_CFLAGS = $(XCB_CFLAGS) $(XCB_WM_CFLAGS) $(PANGO_CFLAGS)
+i3_nagbar_LIBS = $(XCB_LIBS) $(XCB_WM_LIBS) $(PANGO_LIBS)
i3_nagbar_OBJECTS := $(i3_nagbar_SOURCES:.c=.o)
diff --git a/i3-nagbar/main.c b/i3-nagbar/main.c
index bf6d1cca..eacdca19 100644
--- a/i3-nagbar/main.c
+++ b/i3-nagbar/main.c
@@ -27,6 +27,7 @@
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_event.h>
+#include <xcb/randr.h>
#include "libi3.h"
#include "i3-nagbar.h"
@@ -288,6 +289,60 @@ static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
return 1;
}
+/**
+ * Return the position and size the i3-nagbar window should use.
+ * This will be the primary output or a fallback if it cannot be determined.
+ */
+static xcb_rectangle_t get_window_position(void) {
+ /* Default values if we cannot determine the primary output or its CRTC info. */
+ xcb_rectangle_t result = (xcb_rectangle_t){50, 50, 500, font.height + logical_px(8) + logical_px(8)};
+
+ xcb_randr_get_screen_resources_current_cookie_t rcookie = xcb_randr_get_screen_resources_current(conn, root);
+ xcb_randr_get_output_primary_cookie_t pcookie = xcb_randr_get_output_primary(conn, root);
+
+ xcb_randr_get_output_primary_reply_t *primary;
+ xcb_randr_get_screen_resources_current_reply_t *res;
+
+ if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL) {
+ DLOG("Could not determine the primary output.\n");
+ goto free_resources;
+ }
+
+ if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
+ goto free_resources;
+ }
+
+ xcb_randr_get_output_info_reply_t *output =
+ xcb_randr_get_output_info_reply(conn,
+ xcb_randr_get_output_info(conn, primary->output, res->config_timestamp),
+ NULL);
+ if (output == NULL || output->crtc == XCB_NONE)
+ goto free_resources;
+
+ xcb_randr_get_crtc_info_reply_t *crtc =
+ xcb_randr_get_crtc_info_reply(conn,
+ xcb_randr_get_crtc_info(conn, output->crtc, res->config_timestamp),
+ NULL);
+ if (crtc == NULL)
+ goto free_resources;
+
+ DLOG("Found primary output on position x = %i / y = %i / w = %i / h = %i",
+ crtc->x, crtc->y, crtc->width, crtc->height);
+ if (crtc->width == 0 || crtc->height == 0) {
+ DLOG("Primary output is not active, ignoring it.\n");
+ goto free_resources;
+ }
+
+ result.x = crtc->x;
+ result.y = crtc->y;
+ goto free_resources;
+
+free_resources:
+ FREE(res);
+ FREE(primary);
+ return result;
+}
+
int main(int argc, char *argv[]) {
/* The following lines are a terribly horrible kludge. Because terminal
* emulators have different ways of interpreting the -e command line
@@ -410,16 +465,18 @@ int main(int argc, char *argv[]) {
font = load_font(pattern, true);
set_font(&font);
+ xcb_rectangle_t win_pos = get_window_position();
+
/* Open an input window */
win = xcb_generate_id(conn);
xcb_create_window(
conn,
XCB_COPY_FROM_PARENT,
- win, /* the window id */
- root, /* parent == root */
- 50, 50, 500, font.height + logical_px(8) + logical_px(8) /* 8 px padding */, /* dimensions */
- 0, /* x11 border = 0, we draw our own */
+ win, /* the window id */
+ root, /* parent == root */
+ win_pos.x, win_pos.y, win_pos.width, win_pos.height, /* dimensions */
+ 0, /* x11 border = 0, we draw our own */
XCB_WINDOW_CLASS_INPUT_OUTPUT,
XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,