Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <minix/drivers.h>
- #include "vbe.h"
- #include "i8042.h"
- #include "mouse.h"
- int main()
- {
- sef_startup();
- vg_init(MODE3);
- draw_square(50,50,400,8);
- swapThirdBuffer();
- swapVideoMem();
- int ipc_status;
- message msg;
- int irq_set_mouse, irq_set_kbd;
- int r;
- int ret = mouse_subscribe_int();
- if (ret < 0)
- {
- printf("Error subscribing Mouse.\n");
- return 1;
- }
- irq_set_mouse = BIT(ret);
- if(mouse_write(SET_STREAM_MODE))
- return 1;
- if(mouse_write(ENABLE_DATA_PACKETS))
- return 1;
- ret = keyboard_subscribe_int();
- if (ret < 0)
- {
- printf("Error subscribing Keyboard.\n");
- return 1;
- }
- irq_set_kbd = BIT(ret);
- while(1) {
- /* 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 */
- if(_ENDPOINT_P(msg.m_source)==HARDWARE) {
- if (msg.NOTIFY_ARG & irq_set_mouse) { /* subscribed interrupt */
- {
- if(draw_mouse()==STOP){
- break;
- }
- }
- }
- if (msg.NOTIFY_ARG & irq_set_kbd) {
- int result;
- unsigned long scan;
- result = keyboard_int_handler(&scan);
- switch (result) {
- case -1:
- return 1;
- break;
- case 1:
- break;
- case 2:
- // update_xpm(penguin, scancode);
- break;
- }
- }
- }
- }
- }
- if(mouse_write(DISABLE_STREAM_MODE))
- return 1;
- printf("End.\n");
- mouse_unsubscribe_int();
- keyboard_unsubscribe_int();
- vg_exit();
- return 0;
- }
- //-------------------------------------------mouse.c
- #include "mouse.h"
- #include "xpm.h"
- #include "sprite.h"
- #include "video_gr.h"
- static int hook_id;
- mouse_packet package;
- mouse_config config;
- static int vert_length = 0;
- static int hor_length = 0;
- int sameSignal(int x, int y)
- {
- if (x*y >= 0)
- return 1;
- else
- return 0;
- }
- int i8042_write(unsigned long port,unsigned long cmd)
- {
- unsigned long status;
- unsigned i = 0;
- while( i < NUMBER_TRIES ) {
- if (sys_inb(STATUS_REGISTER, &status) != OK)
- return -1;
- if( (status & IBF) == 0 ) {
- if(sys_outb(port, cmd) != OK)
- return -1;
- else
- return 0;
- }
- tickdelay(micros_to_ticks(DELAY_US));
- i++;
- }
- return -1;
- }
- int i8042_read(unsigned long *data)
- {
- unsigned long status;
- unsigned i = 0;
- while( i < NUMBER_TRIES ) {
- if (sys_inb(STATUS_REGISTER, &status) != OK)
- return -1;
- if( status & OBF ) {
- if (sys_inb(OUT_BUF, data) != OK)
- return -1;
- if ( (status &(PAR_ERR | TIMEOUT)) == 0 )
- return 0;
- else
- return -1;
- }
- tickdelay(micros_to_ticks(DELAY_US));
- i++;
- }
- }
- int mouse_subscribe_int(){
- hook_id = MOUSE_HOOK_NOTIFICATION;
- if (sys_irqsetpolicy(IRQ_MOUSE, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id) == OK)
- return MOUSE_HOOK_NOTIFICATION;
- else
- return -1;
- }
- int mouse_unsubscribe_int(){
- if (sys_irqrmpolicy(&hook_id) != OK)
- {
- printf("Error unsubscribing mouse.\n");
- return 1;
- }
- return 0;
- }
- int mouse_write(unsigned long cmd){
- int i=0;
- unsigned long data;
- while(i<NUMBER_TRIES){
- if(i8042_write(KBC_CMD_REG,WRITE_BYTE_TO_MOUSE)==-1)
- return 1;
- if(i8042_read(&data)==-1)
- return 1;
- if(i8042_write(KBD_CMD_REG,cmd)==-1)
- return 1;
- if(i8042_read(&data)==-1)
- return 1;
- if(data==ACK)
- return 0;
- i++;
- }
- return 1;
- }
- int mouse_packet_handler(){
- static int ind=0;
- unsigned long byte;
- if(sys_inb(OUT_BUF, &byte) != OK)
- return -1;
- if(ind==0 && synchronize(byte)==1){
- return 1;
- }
- package.packet[ind % 3]= (unsigned char) byte;
- ind = ind + 1;
- if ((ind % 3) == 0){
- setPackageValues();
- mouse_print_package();
- return 0;
- }
- else
- return 1;
- }
- int mouse_print_package(){
- printf("B1=0x%02X B2=0x%02X B3=0x%02X LB=%d MB=%d RB=%d XOV=%d YOV=%d X=%d Y=%d\n",
- package.packet[0], package.packet[1], package.packet[2], package.left_button, package.middle_button, package.right_button,
- package.x_ovf, package.y_ovf, package.x_value, package.y_value);
- }
- int synchronize(unsigned long byte){
- if((byte & BIT(3)) && (byte != ACK))
- return 0;
- else
- return 1;
- }
- void setPackageValues()
- {
- if (package.packet[0] & BIT(0))
- package.left_button = 1;
- else
- package.left_button = 0;
- if (package.packet[0] & BIT(1))
- package.right_button = 1;
- else
- package.right_button = 0;
- if (package.packet[0] & BIT(2))
- package.middle_button = 1;
- else
- package.middle_button = 0;
- if (package.packet[0] & BIT(4))
- package.x_sign = 1;
- else
- package.x_sign = 0;
- if (package.packet[0] & BIT(5))
- package.y_sign = 1;
- else
- package.y_sign = 0;
- if (package.packet[0] & BIT(6))
- package.x_ovf = 1;
- else
- package.x_ovf = 0;
- if (package.packet[0] & BIT(7))
- package.y_ovf = 1;
- else
- package.y_ovf = 0;
- if (package.x_sign) {
- package.x_value = (short) package.packet[1];
- package.x_value |= 0xFF00;
- }
- else
- package.x_value = (short) package.packet[1];
- if (package.y_sign)
- package.y_value = 0xFF00 | (short) package.packet[2];
- else
- package.y_value = (short) package.packet[2];
- }
- void setConfigValues()
- {
- config.right_button = config.packet[0] & BIT(0);
- config.middle_button = config.packet[0] & BIT(1);
- config.left_button = config.packet[0] & BIT(2);
- config.scaling = config.packet[0] & BIT(4);
- config.enable = config.packet[0] & BIT(5);
- config.remote = config.packet[0] & BIT(6);
- config.resolution = config.packet[1] & (BIT(1) | BIT(0));
- config.sample_rate = config.packet[2];
- }
- void mouse_print_config_package(){
- printf("\nMouse Config\n");
- printf("Remote: %s", config.remote ? "remote (polled) mode\n" : "stream mode\n");
- printf("Enable: %s", config.enable ? "data reporting enabled\n" : "disabled\n");
- printf("Scaling: %s", config.scaling ? "2:1\n" : "1:1\n");
- printf("Right button: %s", config.right_button ? "active\n" : "not active\n");
- printf("Left button: %s", config.left_button ? "active\n" : "not active\n");
- printf("Middle button: %s", config.middle_button ? "active\n" : "not active\n");
- // a documentação referente à configuração do rato troca os BITS relativos ao RB e LB
- // deixamos igual à documentação por razões de avaliação
- printf("Resolution: ");
- switch (config.resolution){
- case 0:
- printf ("1 count per mm\n");
- break;
- case 1:
- printf ("2 count per mm\n");
- break;
- case 2:
- printf ("4 count per mm\n");
- break;
- case 3:
- printf ("8 count per mm\n");
- break;
- }
- printf("Sample rate: %d\n", config.sample_rate);
- }
- int mouse_read_config(){
- if(mouse_write(STATUS_REQUEST))
- return 1;
- unsigned long data;
- int i;
- for (i=0; i < 3; i++) {
- if(i8042_read(&data))
- return 1;
- else
- config.packet[i]=(char)data;
- }
- setConfigValues();
- mouse_print_config_package();
- return 0;
- }
- int mouse_gesture_handler(short length, unsigned short tolerance)
- {
- static int ind=0;
- unsigned long byte;
- if(sys_inb(OUT_BUF, &byte) != OK)
- return -1;
- if(ind==0 && synchronize(byte)==1){
- return 1;
- }
- package.packet[ind % 3] = (char) byte;
- ind = ind + 1;
- if ((ind % 3) == 0){
- setPackageValues();
- event_t evt;
- if (package.right_button)
- {
- evt.type = RDOW;
- if (check_vert_line(&evt, length, tolerance) == STOP)
- return STOP;
- }
- else
- {
- evt.type = RUP;
- if (check_vert_line(&evt, length, tolerance) == STOP)
- return STOP;
- }
- if ((package.x_value != 0) || (package.y_value != 0))
- {
- printf("vertical length: %d \n",vert_length);
- printf("horizontal length: %d \n\n",hor_length);
- evt.type = MOVE;
- if (check_vert_line(&evt, length, tolerance) == STOP)
- return STOP;
- }
- return 0;
- }
- }
- int check_vert_line(event_t *evt, int length, int tolerance) {
- static state_t state = INIT; // initial state; keep state
- switch (state) {
- case INIT:
- if( evt->type == RDOW )
- state = DRAW;
- break;
- case DRAW:
- if( evt->type == MOVE ) {
- if (sameSignal(length, package.y_value))
- vert_length += package.y_value;
- else
- vert_length = 0;
- hor_length += package.x_value;
- if (abs(hor_length) >= tolerance)
- {
- vert_length = 0;
- hor_length = 0;
- }
- if (abs(vert_length) >= abs(length))
- state = COMP;
- }
- else if( evt->type == RUP )
- {
- state = INIT;
- vert_length = 0;
- hor_length = 0;
- }
- break;
- default:
- break;
- }
- if (state == COMP)
- return STOP;
- else
- return 0;
- }
- //proj
- static int initial_x =605;
- static int initial_y =300;
- int draw_mouse(){
- mouse_packet_handler();
- if(!(initial_x+package.x_value + 32 > 1024 || initial_y - package.y_value+15 > 768 || initial_x+package.x_value < 0 || initial_y - package.y_value < 0 )){
- initial_x +=package.x_value;
- initial_y -=package.y_value;
- }
- Sprite *s=create_sprite(pic2,initial_x, initial_y,0,0);
- draw_pixmap(s, THIRD_BUFFER);
- swapVideoMem();
- swapThirdBuffer();
- if(package.left_button){
- return STOP;
- }
- return 0;
- }
- //------------------------------------------videogr.c
- #include <minix/syslib.h>
- #include <minix/drivers.h>
- #include <machine/int86.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <math.h>
- #include "vbe.h"
- #include "sprite.h"
- /* Constants for VBE 0x105 mode */
- /* The physical address may vary from VM to VM.
- * At one time it was 0xD0000000
- * #define VRAM_PHYS_ADDR 0xD0000000
- * Currently on lab B107 is 0xF0000000
- * Better run my version of lab5 as follows:
- * service run `pwd`/lab5 -args "mode 0x105"
- */
- #define VRAM_PHYS_ADDR 0xE0000000
- #define H_RES 1024
- #define V_RES 768
- #define BITS_PER_PIXEL 8
- #define BYTE_SIZE 8
- /* Private global variables */
- static char *video_mem; /* Process address to which VRAM is mapped */
- static char *second_buffer;
- static char *third_buffer;
- static unsigned h_res = 1024; /* Horizontal screen resolution in pixels */
- static unsigned v_res = 768; /* Vertical screen resolution in pixels */
- static unsigned bits_per_pixel = 8; /* Number of VRAM bits per pixel */
- void*vg_init(unsigned short mode) {
- struct reg86u r;
- vbe_mode_info_t info;
- unsigned size;
- struct mem_range mr;
- r.u.b.ah = VBE_FUNCTION_PREFIX;
- r.u.b.intno = VBE_INTERRUPT_VECTOR;
- r.u.b.al = VBE_SET_MODE;
- r.u.w.bx = LINEAR_FRAME_BUFFER | mode;
- if (sys_int86(&r) == OK) {
- if (vbe_get_mode_info(mode, &info) != 1) {
- h_res = info.XResolution;
- v_res = info.YResolution;
- bits_per_pixel = info.BitsPerPixel;
- mr.mr_base = info.PhysBasePtr;
- size = h_res * v_res * bits_per_pixel;
- mr.mr_limit = mr.mr_base + size;
- if (sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mr))
- {
- return NULL;
- }
- video_mem = vm_map_phys(SELF, (void *) mr.mr_base, size);
- if(video_mem != MAP_FAILED)
- {
- if ((second_buffer = malloc(h_res * v_res * bits_per_pixel / 8)) != NULL)
- {
- if ((third_buffer = malloc(h_res * v_res * bits_per_pixel / 8)) != NULL)
- {
- return video_mem;
- }
- }
- }
- }
- }
- return NULL;
- }
- int vg_exit() {
- struct reg86u reg86;
- reg86.u.b.intno = VBE_INTERRUPT_VECTOR; /* BIOS video services */
- reg86.u.b.ah = VBE_VIDEO_MODE_FUNCTION; /* Set Video Mode function */
- reg86.u.b.al = SET_TEXT_MODE; /* 80x25 text mode*/
- if (sys_int86(®86) != OK) {
- printf("\tvg_exit(): sys_int86() failed \n");
- return 1;
- } else
- return 0;
- }
- int setPixel(unsigned short x, unsigned short y,unsigned long color,int buffer)
- {
- char *tmp;
- if(buffer)
- tmp = third_buffer;
- else
- tmp = second_buffer;
- if(x < 0 || x >= h_res || y < 0 || y >= v_res){
- return 1;
- }
- tmp += (x + y*h_res)*bits_per_pixel/BYTE_SIZE;
- *tmp=color;
- return 0;
- }
- void swapThirdBuffer(){
- memcpy(third_buffer, second_buffer, h_res * v_res * bits_per_pixel / 8);
- }
- void swapVideoMem(){
- memcpy(video_mem, third_buffer, h_res * v_res * bits_per_pixel / 8);
- }
- int draw_square(unsigned short x, unsigned short y, unsigned short size, unsigned long color)
- {
- if ((x+size > h_res) || (y+size > v_res))
- return 1;
- unsigned short h_pos;
- unsigned short v_pos;
- for (h_pos = x; h_pos < x + size; h_pos++) {
- for (v_pos=y; v_pos < y+size; v_pos++) {
- setPixel(h_pos,v_pos,color,SECOND_BUFFER);
- }
- }
- return 0;
- }
- int draw_line(unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, unsigned long color)
- {
- float dy = y2 - y1;
- float dx = x2 - x1;
- float size = sqrt(dx*dx+dy*dy);
- int d;
- for (d = 0; d < size; d++) {
- unsigned short y= y1+(dy*d)/size;
- unsigned short x= x1+(dx*d)/size;
- if(setPixel(x,y,color,SECOND_BUFFER)){
- return 1;
- }
- }
- return 0;
- }
- int draw_pixmap(Sprite *s, int buffer){
- int i,j;
- for (i = 0; i < s->height; i++) {
- for (j = 0; j < s->width; ++j) {
- if(setPixel((unsigned short) (j + s->x), (unsigned short) (i + s->y), s->map[(i*s->width + j)*bits_per_pixel/8], buffer))
- return 1;
- }
- }
- return 0;
- }
- //proj
- //-----------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement