From 1e097b0e1d4288d16f518d69712671fdab10499e Mon Sep 17 00:00:00 2001
From: Sunderland93 <samoilov.lex@gmail.com>
Date: Wed, 28 Nov 2018 13:58:14 +0400
Subject: [PATCH 1/7] Implement xdg_toplevel_close event

---
 gfx/drivers_context/wayland_ctx.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c
index ac083651a1..d243a615ba 100644
--- a/gfx/drivers_context/wayland_ctx.c
+++ b/gfx/drivers_context/wayland_ctx.c
@@ -580,9 +580,15 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel,
     wl->configured = false;
 }
 
-/* TODO: implement xdg_toplevel close */
+static void handle_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
+{
+	gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
+	BIT_SET(wl->input.key_state, KEY_ESC);
+}
+
 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
     handle_toplevel_config,
+    handle_toplevel_close,
 };
 
 static void display_handle_geometry(void *data,

From d81e9a08b20e794ce1b2561890d44bc8c2d655f5 Mon Sep 17 00:00:00 2001
From: Sunderland93 <samoilov.lex@gmail.com>
Date: Wed, 28 Nov 2018 17:59:06 +0400
Subject: [PATCH 2/7] Add toplevel width and height

---
 gfx/drivers_context/wayland_ctx.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c
index d243a615ba..80919d8e1d 100644
--- a/gfx/drivers_context/wayland_ctx.c
+++ b/gfx/drivers_context/wayland_ctx.c
@@ -576,6 +576,10 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel,
     gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
     
     /* TODO: implement resizing */
+    (void)toplevel;
+    
+    wl->width = wl->buffer_scale * width;
+    wl->height = wl->buffer_scale * height;
     
     wl->configured = false;
 }

From 0f5729354e65174635e66baf2ef765dd3fe32423 Mon Sep 17 00:00:00 2001
From: Sunderland93 <samoilov.lex@gmail.com>
Date: Fri, 30 Nov 2018 21:36:42 +0400
Subject: [PATCH 3/7] Initial support for toplevel resizing

---
 gfx/drivers_context/wayland_ctx.c | 63 +++++++++++++++++++++++++------
 1 file changed, 52 insertions(+), 11 deletions(-)

diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c
index 80919d8e1d..f3803a96f7 100644
--- a/gfx/drivers_context/wayland_ctx.c
+++ b/gfx/drivers_context/wayland_ctx.c
@@ -76,6 +76,9 @@ typedef struct gfx_ctx_wayland_data
    bool maximized;
    bool resize;
    bool configured;
+   bool activated;
+   int prev_width;
+   int prev_height;
    unsigned width;
    unsigned height;
    unsigned physical_width;
@@ -571,17 +574,55 @@ static const struct xdg_surface_listener xdg_surface_listener = {
 };
 
 static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel,
-                                   int width, int height, struct wl_array *states)
+                                   int32_t width, int32_t height, struct wl_array *states)
 {
     gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
     
-    /* TODO: implement resizing */
-    (void)toplevel;
-    
-    wl->width = wl->buffer_scale * width;
-    wl->height = wl->buffer_scale * height;
-    
-    wl->configured = false;
+    printf("Configure event got, width: %d, height: %d\n", width, height);
+    wl->fullscreen = false;
+    wl->maximized = false;
+    enum xdg_toplevel_state *state;
+    wl_array_for_each(state, states) {
+		switch (*state) {
+			case XDG_TOPLEVEL_STATE_FULLSCREEN:
+			printf("Surface state: XDG_SURFACE_STATE_FULLSCREEN\n");
+			    wl->fullscreen = true;
+			    break;
+			case XDG_TOPLEVEL_STATE_MAXIMIZED:
+			printf("Surface state: XDG_SURFACE_STATE_MAXIMIZED\n");
+			    wl->maximized = true;
+			    break;
+			case XDG_TOPLEVEL_STATE_RESIZING:
+			printf("Surface state: XDG_SURFACE_STATE_RESIZING\n");
+			    wl->resize = true;
+			    break;
+			case XDG_TOPLEVEL_STATE_ACTIVATED:
+			printf("Surface state: XDG_SURFACE_STATE_ACTIVATED\n");
+			    wl->activated = true;
+                            break;
+			case XDG_TOPLEVEL_STATE_TILED_TOP:
+			printf("Surface state: XDG_SURFACE_STATE_TILED_TOP\n");
+			case XDG_TOPLEVEL_STATE_TILED_LEFT:
+			printf("Surface state: XDG_SURFACE_STATE_TILED_LEFT\n");
+			case XDG_TOPLEVEL_STATE_TILED_RIGHT:
+			printf("Surface state: XDG_SURFACE_STATE_TILED_RIGHT\n");
+			case XDG_TOPLEVEL_STATE_TILED_BOTTOM:
+			printf("Surface state: XDG_SURFACE_STATE_TILED_BOTTOM\n");
+                        break;
+			}
+	}
+	if (width > 0 && height >0) {
+				wl->prev_width = width;
+				wl->prev_height = height;
+				wl->width = width;
+				wl->height = height;
+			}
+			else {
+				wl->width = wl->prev_width;
+				wl->height = wl->prev_height;
+			}
+				
+	wl->configured = false;
 }
 
 static void handle_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
@@ -1423,9 +1464,9 @@ static bool gfx_ctx_wl_has_focus(void *data)
 
 static bool gfx_ctx_wl_suppress_screensaver(void *data, bool state)
 {
-	(void)data;
-	gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
-	
+        (void)data;
+        gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
+
     if (!wl->idle_inhibit_manager)
         return false;
     if (state == (!!wl->idle_inhibitor))

From 3d159d424e35813199e013de481ab8461c7731ed Mon Sep 17 00:00:00 2001
From: Aleksey Samoilov <samoilov.lex@gmail.com>
Date: Sun, 2 Dec 2018 17:53:03 +0400
Subject: [PATCH 4/7] Cleanup

---
 gfx/drivers_context/wayland_ctx.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c
index f3803a96f7..b4d9c6e564 100644
--- a/gfx/drivers_context/wayland_ctx.c
+++ b/gfx/drivers_context/wayland_ctx.c
@@ -585,33 +585,25 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel,
     wl_array_for_each(state, states) {
 		switch (*state) {
 			case XDG_TOPLEVEL_STATE_FULLSCREEN:
-			printf("Surface state: XDG_SURFACE_STATE_FULLSCREEN\n");
 			    wl->fullscreen = true;
 			    break;
 			case XDG_TOPLEVEL_STATE_MAXIMIZED:
-			printf("Surface state: XDG_SURFACE_STATE_MAXIMIZED\n");
 			    wl->maximized = true;
 			    break;
 			case XDG_TOPLEVEL_STATE_RESIZING:
-			printf("Surface state: XDG_SURFACE_STATE_RESIZING\n");
 			    wl->resize = true;
 			    break;
 			case XDG_TOPLEVEL_STATE_ACTIVATED:
-			printf("Surface state: XDG_SURFACE_STATE_ACTIVATED\n");
 			    wl->activated = true;
                             break;
 			case XDG_TOPLEVEL_STATE_TILED_TOP:
-			printf("Surface state: XDG_SURFACE_STATE_TILED_TOP\n");
 			case XDG_TOPLEVEL_STATE_TILED_LEFT:
-			printf("Surface state: XDG_SURFACE_STATE_TILED_LEFT\n");
 			case XDG_TOPLEVEL_STATE_TILED_RIGHT:
-			printf("Surface state: XDG_SURFACE_STATE_TILED_RIGHT\n");
 			case XDG_TOPLEVEL_STATE_TILED_BOTTOM:
-			printf("Surface state: XDG_SURFACE_STATE_TILED_BOTTOM\n");
-                        break;
+         break;
 			}
 	}
-	if (width > 0 && height >0) {
+	if (width > 0 && height > 0) {
 				wl->prev_width = width;
 				wl->prev_height = height;
 				wl->width = width;

From eda342262b9cc6c8b2265476cfa26ca9d613ca37 Mon Sep 17 00:00:00 2001
From: Sunderland93 <samoilov.lex@gmail.com>
Date: Sun, 2 Dec 2018 21:15:57 +0400
Subject: [PATCH 5/7] Fix

---
 gfx/drivers_context/wayland_ctx.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c
index b4d9c6e564..bd6cbba2da 100644
--- a/gfx/drivers_context/wayland_ctx.c
+++ b/gfx/drivers_context/wayland_ctx.c
@@ -578,7 +578,6 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel,
 {
     gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
     
-    printf("Configure event got, width: %d, height: %d\n", width, height);
     wl->fullscreen = false;
     wl->maximized = false;
     enum xdg_toplevel_state *state;
@@ -1456,8 +1455,9 @@ static bool gfx_ctx_wl_has_focus(void *data)
 
 static bool gfx_ctx_wl_suppress_screensaver(void *data, bool state)
 {
-        (void)data;
-        gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
+	(void)data;
+	
+	gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
 
     if (!wl->idle_inhibit_manager)
         return false;

From 72eb7c4f5950f881a3db3e6ce14d60f04f3e2a1c Mon Sep 17 00:00:00 2001
From: Sunderland93 <samoilov.lex@gmail.com>
Date: Mon, 3 Dec 2018 14:48:13 +0400
Subject: [PATCH 6/7] Temp fix resizing

---
 gfx/drivers_context/wayland_ctx.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c
index bd6cbba2da..b48b6ff801 100644
--- a/gfx/drivers_context/wayland_ctx.c
+++ b/gfx/drivers_context/wayland_ctx.c
@@ -608,10 +608,6 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel,
 				wl->width = width;
 				wl->height = height;
 			}
-			else {
-				wl->width = wl->prev_width;
-				wl->height = wl->prev_height;
-			}
 				
 	wl->configured = false;
 }

From dcec570d05d0c30c5394340df59f1e598f8658b8 Mon Sep 17 00:00:00 2001
From: Sunderland93 <samoilov.lex@gmail.com>
Date: Wed, 5 Dec 2018 21:20:15 +0400
Subject: [PATCH 7/7] Style fix

---
 gfx/drivers_context/wayland_ctx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c
index b48b6ff801..c66cbec88b 100644
--- a/gfx/drivers_context/wayland_ctx.c
+++ b/gfx/drivers_context/wayland_ctx.c
@@ -603,11 +603,11 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel,
 			}
 	}
 	if (width > 0 && height > 0) {
-				wl->prev_width = width;
-				wl->prev_height = height;
-				wl->width = width;
-				wl->height = height;
-			}
+		wl->prev_width = width;
+		wl->prev_height = height;
+		wl->width = width;
+		wl->height = height;
+	}
 				
 	wl->configured = false;
 }