Index: lab4/lab4.c
===================================================================
--- lab4/lab4.c	(revision 76)
+++ lab4/lab4.c	(revision 77)
@@ -37,6 +37,7 @@
 extern int counter;
 
 int (mouse_test_packet)(uint32_t cnt) {
+    int ret = 0;
     /// loop stuff
     int ipc_status, r;
     message msg;
@@ -44,7 +45,8 @@
     uint8_t mouse_irq_bit = 12;
     int mouse_id = 0;
     int mouse_irq = BIT(mouse_irq_bit);
-    if (mouse_set_data_report(true)) return 1;
+    if ((ret = mouse_set_data_report(true))) return ret;
+    //if ((ret = mouse_enable_data_reporting())) return ret;
 
     if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
     /// cycle
@@ -78,7 +80,6 @@
     }
     if (unsubscribe_interrupt(&mouse_id)) return 1;
     if (mouse_set_data_report(false)) return 1;
-
     return 0;
 }
 
@@ -187,8 +188,110 @@
     return 0;
 }
 
+enum state{
+    STATE_NEUTRAL,
+    STATE_FIRST,
+    STATE_MID,
+    STATE_SECOND
+};
+
 int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
-    /* To be completed */
-    printf("%s: under construction\n", __func__);
-    return 1;
+    int ret = 0;
+    /// loop stuff
+    int ipc_status, r;
+    message msg;
+    /// Mouse interrupt handling
+    uint8_t mouse_irq_bit = 12;
+    int mouse_id = 0;
+    int mouse_irq = BIT(mouse_irq_bit);
+    //if ((ret = mouse_set_data_report(true))) return ret;
+    if ((ret = mouse_enable_data_reporting())) return ret;
+
+    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
+    /// cycle
+    int good = 1;
+    //int st = STATE_NEUTRAL;
+    //int16_t dx, dy;
+    while (good) {
+        /* Get a request message. */
+        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
+            printf("driver_receive failed with %d", r);
+            continue;
+        }
+        if (is_ipc_notify(ipc_status)) { /* received notification */
+            switch (_ENDPOINT_P(msg.m_source)) {
+                case HARDWARE: /* hardware interrupt notification */
+                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
+                        mouse_ih();
+                        if(counter >= 3){
+                            struct packet pp = mouse_parse_packet(packet);
+                            mouse_print_packet(&pp);/*
+                            switch(st){
+                                case STATE_NEUTRAL:
+                                    if(pp.lb == 1 && pp.mb == 0 && pp.rb == 0){
+                                        st = STATE_FIRST;
+                                        dx = 0;
+                                        dy = 0;
+                                    }
+                                    break;
+                                case STATE_FIRST:
+                                    if(pp.lb == 1){
+                                        if(pp.delta_x >= -tolerance && pp.delta_y >= -tolerance){
+                                            dx += pp.delta_x;
+                                            dy += pp.delta_y;
+                                        }else{
+                                            st = STATE_NEUTRAL;
+                                        }
+                                    }else{
+                                        if(abs(dx) < abs(dy) && dx > 0 && dy > 0){
+                                            st = STATE_MID;
+                                        }else{
+                                            st = STATE_NEUTRAL;
+                                        }
+                                    }
+                                    break;
+                                case STATE_MID:
+                                    if(pp.lb == 0 && pp.mb == 0 && pp.rb == 1){
+                                        st = STATE_SECOND;
+                                        dx = 0;
+                                        dy = 0;
+                                    }else{
+                                        st = STATE_NEUTRAL;
+                                    }
+                                    break;
+                                case STATE_SECOND:
+                                    if(pp.rb == 1){
+                                        if(pp.delta_x >= -tolerance && pp.delta_y <= tolerance){
+                                            dx += pp.delta_x;
+                                            dy += pp.delta_y;
+                                        }else{
+                                            st = STATE_NEUTRAL;
+                                        }
+                                    }else{
+                                        if(abs(dx) < abs(dy) && dx > 0 && dy < 0){
+                                            st = STATE_NEUTRAL;
+                                            good = false;
+                                        }else{
+                                            st = STATE_NEUTRAL;
+                                        }
+                                    }
+                                    break;
+                                default:
+                                    return OTHER_ERROR;
+                                    break;
+                            }*/
+                        }
+                    }
+                    break;
+                default:
+                    break; /* no other notifications expected: do nothing */
+            }
+        } else { /* received standart message, not a notification */
+            /* no standart message expected: do nothing */
+        }
+    }
+    if (unsubscribe_interrupt(&mouse_id)) return 1;
+    if (mouse_set_data_report(false)) return 1;
+
+    return 0;
 }
Index: lab4/mouse.c
===================================================================
--- lab4/mouse.c	(revision 76)
+++ lab4/mouse.c	(revision 77)
@@ -61,8 +61,14 @@
 }
 
 int (mouse_set_data_report)(int on){
-    if(on) return mouse_issue_cmd(ENABLE_DATA_REP);
-    else   return mouse_issue_cmd(   DIS_DATA_REP);
+    int ret = 0;
+    if(on){
+        if((ret = mouse_issue_cmd(ENABLE_DATA_REP))) return ret;
+    }
+    else{
+        if((ret = mouse_issue_cmd(   DIS_DATA_REP))) return ret;
+    }
+    return ret;
 }
 
 int (mouse_read_data)(uint8_t *data) {
@@ -75,15 +81,12 @@
 int (mouse_issue_cmd)(uint32_t cmd) {
     int ret;
     uint8_t ack = 0;
-    for(unsigned i = 0; i < KBC_NUM_TRIES; ++i) {
-        if ((ret = kbc_issue_cmd(MOUSE_WRITE_B))) return ret;
-        if ((ret = kbc_issue_arg(cmd))) return ret;
-        if ((ret = mouse_read_byte(&ack))) return ret;
-        if (ack == ACK_OK) return SUCCESS;
-        if (ack == ACK_ERROR) return INVALID_COMMAND;
-        tickdelay(micros_to_ticks(DELAY));
-    }
-    return TIMEOUT_ERROR;
+    if ((ret = kbc_issue_cmd(MOUSE_WRITE_B))) return ret;
+    if ((ret = kbc_issue_arg(cmd))) return ret;
+    if ((ret = mouse_read_ack(&ack))) return ret;
+    if (ack == ACK_OK) return SUCCESS;
+    if (ack == ACK_ERROR) return INVALID_COMMAND;
+    return OTHER_ERROR;
 }
 
 int (mouse_read_byte)(uint8_t *byte) {
@@ -101,6 +104,21 @@
     return TIMEOUT_ERROR;
 }
 
+int (mouse_read_ack)(uint8_t *byte) {
+    int ret = 0;
+    uint8_t stat;
+    //for(int i = 0; i < KBC_NUM_TRIES; ++i){
+        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
+        //if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
+            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR;
+            if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
+            else return SUCCESS;
+        //}
+        //tickdelay(micros_to_ticks(DELAY));
+    //}
+    //return TIMEOUT_ERROR;
+}
+
 int16_t (sign_extend_byte)(uint8_t sign_bit, uint8_t byte) {
     return (int16_t)(((0xFF * sign_bit)<<8) | byte);
 }
Index: lab4/mouse.h
===================================================================
--- lab4/mouse.h	(revision 76)
+++ lab4/mouse.h	(revision 77)
@@ -49,6 +49,8 @@
  */
 int (mouse_read_byte)(uint8_t *byte);
 
+int (mouse_read_ack)(uint8_t *byte);
+
 /**
  * @brief Converts 9-bit number to 16-bit with sign extension
  * @param sign_bit  Sign bit identifiying the signal of the number
