From 81fbff96b806398fc0d0929bbd1a05a8359dec97 Mon Sep 17 00:00:00 2001
From: Abdelrahman <said.abdelrahman89@gmail.com>
Date: Mon, 15 Jan 2024 20:18:41 +0000
Subject: [PATCH] Filter events by window ID

---
 src/compositor.c | 64 ++++++++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 26 deletions(-)

diff --git a/src/compositor.c b/src/compositor.c
index 4936b74..ac7226c 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -26,7 +26,8 @@ struct compositor {
   bool move_node;
 };
 
-void add_node(compositor_t *comp, const char *path, i32 x, i32 y);
+void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
+              i32 y);
 
 i32 run_main_loop(void) {
   if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
@@ -57,44 +58,53 @@ i32 run_main_loop(void) {
         comp.running = false;
         break;
       case SDL_MOUSEBUTTONDOWN:
-        if (comp.node_hovered != -1) {
-          comp.move_node = true;
-        }
+        if (comp.event.button.windowID == main_window->id) {
+          if (comp.node_hovered != -1) {
+            comp.move_node = true;
+          }
 
-        break;
+          break;
+        }
       case SDL_MOUSEBUTTONUP:
         comp.move_node = false;
 
         break;
       case SDL_MOUSEMOTION:
-        comp.mouse_x = comp.event.motion.x;
-        comp.mouse_y = comp.event.motion.y;
+        if (comp.event.motion.windowID == main_window->id) {
+          comp.mouse_x = comp.event.motion.x;
+          comp.mouse_y = comp.event.motion.y;
 
-        if (comp.move_node) {
-          i32 dx = comp.event.motion.xrel;
-          i32 dy = comp.event.motion.yrel;
+          if (comp.move_node) {
+            i32 dx = comp.event.motion.xrel;
+            i32 dy = comp.event.motion.yrel;
 
-          node_t *node = &(comp.nodes[comp.node_hovered]);
+            node_t *node = &(comp.nodes[comp.node_hovered]);
 
-          node->rect.topleft.x += dx;
-          node->rect.topleft.y += dy;
-        } else {
-          comp.node_hovered = -1;
+            node->rect.topleft.x += dx;
+            node->rect.topleft.y += dy;
+          } else {
+            comp.node_hovered = -1;
 
-          for (u64 i = comp.count - 1; i >= 0; --i) {
-            node_t *node = &(comp.nodes[i]);
+            for (u64 i = comp.count - 1; i >= 0; --i) {
+              node_t *node = &(comp.nodes[i]);
 
-            if (aabb(node, comp.mouse_x, comp.mouse_y)) {
-              comp.node_hovered = i;
-              break;
+              if (aabb(node, comp.mouse_x, comp.mouse_y)) {
+                comp.node_hovered = i;
+                break;
+              }
             }
           }
-        }
 
-        break;
+          break;
+        }
       case SDL_DROPFILE:
-        add_node(&comp, comp.event.drop.file, comp.mouse_x, comp.mouse_y);
-        break;
+        if (comp.event.drop.windowID == main_window->id) {
+          node_data_t data = (node_data_t){.path = comp.event.drop.file};
+
+          add_node(&comp, NODE_TYPE_IO, data, comp.mouse_x, comp.mouse_y);
+
+          break;
+        }
       }
     }
 
@@ -113,7 +123,8 @@ i32 run_main_loop(void) {
   return EXIT_SUCCESS;
 }
 
-void add_node(compositor_t *comp, const char *path, i32 x, i32 y) {
+void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
+              i32 y) {
   if (comp->count + 1 >= MAX_NODES) {
     return;
   }
@@ -126,6 +137,7 @@ void add_node(compositor_t *comp, const char *path, i32 x, i32 y) {
               .w = NODE_WIDTH,
               .h = NODE_HEIGHT,
           },
-      .path = path,
+      .type = type,
+      .data.path = data.path,
   };
 }