From 7cffd79140b276e74d32500eb8c0c2522ae929a6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20L=C3=B6bl?= <lobl.pavel@gmail.com>
Date: Sat, 22 Sep 2012 00:21:39 +0200
Subject: [PATCH] Add new subscribe event 'mode' for binding mode changes

Introducing a new event to subscribe called mode. It's fired up
when i3 changes binding mode (like switching from default to resize).
IPC guide adjusted also.
---
 docs/ipc         | 14 ++++++++++++++
 include/i3/ipc.h |  3 +++
 src/config.c     |  7 +++++++
 3 files changed, 24 insertions(+)

diff --git a/docs/ipc b/docs/ipc
index f8dfa78e..2716f180 100644
--- a/docs/ipc
+++ b/docs/ipc
@@ -610,6 +610,8 @@ workspace (0)::
 output (1)::
 	Sent when RandR issues a change notification (of either screens,
 	outputs, CRTCs or output properties).
+mode (2)::
+	Sent whenever i3 changes its binding mode.
 
 *Example:*
 --------------------------------------------------------------------
@@ -651,6 +653,18 @@ This event consists of a single serialized map containing a property
 { "change": "unspecified" }
 ---------------------------
 
+=== mode event
+
+This event consists of a single serialized map containing a property
++change (string)+ which holds the name of current mode in use. The name
+is the same as specified in config when creating a mode. The default
+mode is simply named default.
+
+*Example:*
+---------------------------
+{ "change": "default" }
+---------------------------
+
 == See also
 
 For some languages, libraries are available (so you don’t have to implement
diff --git a/include/i3/ipc.h b/include/i3/ipc.h
index 86fa1b4b..93b2ae87 100644
--- a/include/i3/ipc.h
+++ b/include/i3/ipc.h
@@ -84,4 +84,7 @@
 /* The output event will be triggered upon changes in the output list */
 #define I3_IPC_EVENT_OUTPUT                     (I3_IPC_EVENT_MASK | 1)
 
+/* The output event will be triggered upon mode changes */
+#define I3_IPC_EVENT_MODE                       (I3_IPC_EVENT_MASK | 2)
+
 #endif
diff --git a/src/config.c b/src/config.c
index fcf3841e..0bd6811a 100644
--- a/src/config.c
+++ b/src/config.c
@@ -194,6 +194,13 @@ void switch_mode(const char *new_mode) {
         bindings = mode->bindings;
         translate_keysyms();
         grab_all_keys(conn, false);
+
+        char *event_msg;
+        sasprintf(&event_msg, "{\"change\":\"%s\"}", mode->name);
+
+        ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
+        FREE(event_msg);
+
         return;
     }