Index: state_machine.c
===================================================================
--- state_machine.c	(revision 90)
+++ state_machine.c	(revision 91)
@@ -7,58 +7,41 @@
 #include "utils.h"
 
 struct mouse_ev* mouse_get_event(struct packet *pp) {
-
     static struct mouse_ev event;
-    static struct packet last; // compare consecutive events
-    static int aux = 1; // first iteration
+    static uint8_t last = 0;
 
     if (pp == NULL)
         return &event;
 
-    // current packet
-    uint8_t lb_press = pp->bytes[0] & LEFT_BUTTON;
-    uint8_t rb_press = pp->bytes[0] & RIGHT_BUTTON;
-    uint8_t mb_press = pp->bytes[0] & MIDDLE_BUTTON;
+    // current button presses
+    uint8_t button_presses = pp->bytes[0] & (LEFT_BUTTON | RIGHT_BUTTON | MIDDLE_BUTTON);
     int16_t delta_x = pp->delta_x;
     int16_t delta_y = pp->delta_y;
 
-    if (aux) { // first event detected
-        if (lb_press && (rb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0)
-            event.type = LB_PRESSED;
-        else if (rb_press && (lb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0)
-            event.type = RB_PRESSED;
-        else if ((delta_x || delta_y) && (lb_press | rb_press | mb_press) == 0) {
-            event.type = MOUSE_MOV;
-            event.delta_x = delta_x;
-            event.delta_y = delta_y;
-        } else
-            event.type = BUTTON_EV;
-        aux = 0;
-    } else {
-        // last packet
-        uint8_t last_lb_press = last.bytes[0] & LEFT_BUTTON;
-        uint8_t last_rb_press = last.bytes[0] & RIGHT_BUTTON;
-        uint8_t last_mb_press = last.bytes[0] & MIDDLE_BUTTON;
+    if ((button_presses ^ last) == LEFT_BUTTON && !(last & LEFT_BUTTON)) {
+        event.type = LB_PRESSED;
+        last |= LEFT_BUTTON;
 
-        if (lb_press && (rb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0 && last_lb_press == 0)
-            event.type = LB_PRESSED;
-        else if (rb_press && (lb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0 && last_rb_press == 0)
-            event.type = RB_PRESSED;
-        else if ((lb_press | rb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0 && last_lb_press)
-            event.type = LB_RELEASED;
-        else if ((lb_press | rb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0 && last_rb_press)
-            event.type = RB_RELEASED;
-        else if ((delta_x || delta_y) && (lb_press | rb_press | mb_press) == (last_lb_press | last_rb_press | last_mb_press)) {
-            event.type = MOUSE_MOV;
-            event.delta_x = delta_x;
-            event.delta_y = delta_y;
-        } else
-            event.type = BUTTON_EV;
-    }
+    } else if ((button_presses ^ last) == RIGHT_BUTTON && !(last & RIGHT_BUTTON)) {
+        event.type = RB_PRESSED;
+        last |= RIGHT_BUTTON;
 
-    // update last packet for comparison
-    last = *pp;
+    } else if ((button_presses ^ last) == LEFT_BUTTON && (last & LEFT_BUTTON)) {
+        event.type = LB_RELEASED;
+        last &= ~LEFT_BUTTON;
 
+    } else if ((button_presses ^ last) == RIGHT_BUTTON && (last & RIGHT_BUTTON)) {
+        event.type = RB_RELEASED;
+        last &= ~RIGHT_BUTTON;
+
+    } else if ((delta_x || delta_y) && (button_presses == last)) {
+        event.type = MOUSE_MOV;
+
+    } else
+        event.type = BUTTON_EV;
+
+    event.delta_x = delta_x;
+    event.delta_y = delta_y;
     return &event;
 }
 
@@ -89,15 +72,8 @@
                 x_length += event->delta_x;
                 y_length += event->delta_y;
             } else if (event->type == LB_RELEASED) {
-                if (x_length == 0 || y_length == 0) {
+                if (y_length < x_length || x_length < x_len) {
                     state = INITIAL;
-                    break;
-                }
-
-                int slope = y_length / x_length;
-
-                if (slope <= 1 || x_length < x_len) {
-                    state = INITIAL;
                     x_length = 0;
                     y_length = 0;
                     break;
@@ -139,9 +115,8 @@
                     state = INITIAL;
                     break;
                 }
-                int slope = y_length / x_length;
 
-                if (slope >= -1 || x_length < x_len) {
+                if (y_length > -x_length || x_length < x_len) {
                     state = INITIAL;
                     x_length = 0;
                     y_length = 0;
