Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <minix/sysutil.h>
- #include <minix/syslib.h>
- #include <minix/drivers.h>
- #include <stdio.h>
- #include "Mouse.h"
- #define defaultColor1 BLACK
- #define defaultColor2 WHITE
- #define defaultSize 3
- //STATIC VARIABLES
- //The controller sends the movement information to the PC via a serial
- //line in 3-byte data packet
- static long int data_packet[3]; //3 byte data packet
- static long unsigned int code;
- static long unsigned int data;
- //COUNTER
- static int counter_mouse;
- static int counter_packets;
- //COORDENATES
- static unsigned int horiz;
- static unsigned int vert;
- //As the previous lab we need a function to read from the input buffer
- //if OK it will return the data i.e the response, if not
- //we'll -1 (ERROR STATUS)
- static int hook_id = 12;
- int mouse_read_inb(){//sys_inb;no arguments
- unsigned long stat, data;
- int safetyCounter = 0;
- while( safetyCounter < 3 ) {
- if ( sys_inb(STAT_REG, &stat) != OK )
- return 1;
- if( stat & OBF ) {
- sys_inb(OUT_BUF, &data);
- //if ( (stat &(PAR_ERR | TO_ERR)) == 0 )
- return data;
- //else
- //return -1;
- }
- tickdelay(micros_to_ticks(DELAY_US));
- safetyCounter++;
- }
- return 1;
- }
- //in addition, on this lab we're going use a similar function than
- //the previous one but instead to read the input buffer, we're
- //going to check the output buffer
- int mouse_write_outb(unsigned char port, unsigned char cmd){
- unsigned long stat;
- while( 1 ) {
- sys_inb(STAT_REG, &stat);
- if(( stat & IBF ) == 0) {
- if ( sys_outb(port, cmd) != OK )
- return 1;
- sys_inb(OUT_BUF, &stat);
- if(stat == ACK)
- return 0;
- else
- return -1;
- }
- tickdelay(micros_to_ticks(DELAY_US));
- //timer_test_int(2);
- }
- }
- Mouse* mouse = NULL;
- Mouse mousePreviousState;
- Mouse* newMouse();
- void enableMouse(){
- printf("Aqui enMouse\n");
- mouse_write_outb(IN_BUF, KBCTOMOUSE);
- mouse_write_outb(OUT_BUF, EN_DATA_REP);
- }
- Mouse* getMouse(){
- printf("Aqui getMouse\n");
- if(!mouse){
- enableMouse();
- mouse = newMouse();
- }
- return mouse;
- }
- Mouse* newMouse(){
- printf("Aqui newMouse\n");
- Mouse* mouse = (Mouse*) malloc(sizeof(Mouse));
- mouse->x = 0;
- mouse->y = 0;
- mouse->size = defaultSize;
- mouse->color1 = defaultColor1;
- mouse->color2 = defaultColor2;
- }
- void updateMouse(){
- data_packet[counter_mouse] = mouse_read_inb();
- if ((counter_mouse) == 0) {
- if ((data_packet[counter_mouse] & BIT(3)) == 0) {
- return;
- }
- }
- counter_mouse++;
- if(counter_mouse == 3)
- {
- counter_mouse = 0;
- counter_packets++;
- printf("\nCheguei aqui1");
- //show_packet();
- }
- }
- void drawCrosshair(Mouse* mouse){
- printf("Aqui crosshair\n");
- int x = mouse->x, y=mouse->y;
- int size = mouse->size;
- printf("Aqui drawcross2\n");
- //black border
- vg_draw_line(x-size, y-1, x+size, y-1, mouse->color1);
- vg_draw_line(x-size, y+1, x+size, y+1, mouse->color1);
- vg_draw_line(x-1, y-size, x-1, y+size, mouse->color1);
- vg_draw_line(x+1, y-size, x+1, y+size, mouse->color1);
- printf("Aqui drawcross3\n");
- vg_draw_line(x-size, y, x+size, y, mouse->color2);
- vg_draw_line(x, y-size, x, y+size, mouse->color2);
- printf("Aqui drawcross4\n");
- }
- void drawMouse(){
- printf("Aqui drawMouse\n");
- drawCrosshair(getMouse());
- getMouse()-> draw = 0;
- printf("Aqui drawMouseFim\n");
- }
- void deleteMouse(){
- printf("Aqui deleteMouse\n");
- free(getMouse());
- }
- int mouseInside(int x1, int x2, int y1, int y2){
- return x1 <= getMouse()->x && getMouse()->x <= x2 && y1 <=getMouse()->y && getMouse()->y <= y2;
- }
- //SUBSCRIBERS
- int mouse_subscribe_int(void ) {//like we did on the previous lab, we need to subscribe some interrupts, in this case, the mouse interrupts.
- //SUBSCRIPTION
- int mask = BIT(hook_id);
- if (sys_irqsetpolicy(MOUSE_IRQ,IRQ_REENABLE | IRQ_EXCLUSIVE,&hook_id));
- if (sys_irqenable(&hook_id) != OK)
- return -1;
- return mask;
- }
- int mouse_unsubscribe_int() {
- //UNSUBSCRIPTION
- mouse_write_outb(IN_BUF, KBCTOMOUSE);
- mouse_write_outb(OUT_BUF, SET_DEF);
- if (sys_irqrmpolicy(&hook_id) != OK) //unsubscribes a previous interrupt with the specified hook_id
- return 1;
- return 0;
- if(sys_irqdisable(&hook_id) != OK) //disables interrupts on the IRQ line associated with the specified hook_id.
- return 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement