From 7064cfc2a07c71a6efce8462e40e789ef8d90908 Mon Sep 17 00:00:00 2001
From: Michael Stapelberg <michael@stapelberg.de>
Date: Mon, 19 Sep 2011 19:17:25 +0100
Subject: [PATCH] i3bar: Bugfix: Check if the X11 connection is unavailable

This fixes the condition where the i3 socket for some reason did not produce an
error, but the X server exited (earlier than i3?) and the left-over i3bar
process would consume 100% CPU.

How to reproduce the problem:
1) Start ./testcases/Xdummy :8
2) Start DISPLAY=:8 i3bar -s <socket path to i3 on :0>
3) Kill the Xdummy
---
 i3bar/src/xcb.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/i3bar/src/xcb.c b/i3bar/src/xcb.c
index 51a2781b..ac48ea5a 100644
--- a/i3bar/src/xcb.c
+++ b/i3bar/src/xcb.c
@@ -406,6 +406,12 @@ void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
  */
 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
     xcb_generic_event_t *event;
+
+    if (xcb_connection_has_error(xcb_connection)) {
+        ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
+        exit(1);
+    }
+
     while ((event = xcb_poll_for_event(xcb_connection)) == NULL) {
         return;
     }