Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <minix/drivers.h>
  4. #include "vbe.h"
  5. #include "i8042.h"
  6. #include "mouse.h"
  7.  
  8. int main()
  9. {
  10.  
  11.     sef_startup();
  12.  
  13.  
  14.     vg_init(MODE3);
  15.     draw_square(50,50,400,8);
  16.     swapThirdBuffer();
  17.     swapVideoMem();
  18.  
  19.  
  20.     int ipc_status;
  21.     message msg;
  22.     int irq_set_mouse, irq_set_kbd;
  23.     int r;
  24.  
  25.  
  26.  
  27.     int ret = mouse_subscribe_int();
  28.     if (ret < 0)
  29.     {
  30.         printf("Error subscribing Mouse.\n");
  31.         return 1;
  32.     }
  33.  
  34.     irq_set_mouse = BIT(ret);
  35.  
  36.     if(mouse_write(SET_STREAM_MODE))
  37.         return 1;
  38.     if(mouse_write(ENABLE_DATA_PACKETS))
  39.         return 1;
  40.  
  41.  
  42.     ret = keyboard_subscribe_int();
  43.     if (ret < 0)
  44.     {
  45.         printf("Error subscribing Keyboard.\n");
  46.         return 1;
  47.     }
  48.  
  49.     irq_set_kbd = BIT(ret);
  50.  
  51.     while(1) {
  52.         /* Get a request message. */
  53.         if ( (r=driver_receive(ANY, &msg, &ipc_status)) !=0 ) {
  54.             printf("driver_receive failed with: %d", r);
  55.             continue;
  56.         }
  57.         if (is_ipc_notify(ipc_status)) { /* received notification */
  58.             if(_ENDPOINT_P(msg.m_source)==HARDWARE) {
  59.                 if (msg.NOTIFY_ARG & irq_set_mouse) { /* subscribed interrupt */
  60.                     {
  61.                         if(draw_mouse()==STOP){
  62.                             break;
  63.                         }
  64.                     }
  65.                 }
  66.  
  67.                 if (msg.NOTIFY_ARG & irq_set_kbd) {
  68.                     int result;
  69.                     unsigned long scan;
  70.                     result = keyboard_int_handler(&scan);
  71.                     switch (result) {
  72.                     case -1:
  73.                         return 1;
  74.                         break;
  75.                     case 1:
  76.                         break;
  77.                     case 2:
  78.                     //  update_xpm(penguin, scancode);
  79.                         break;
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     if(mouse_write(DISABLE_STREAM_MODE))
  87.         return 1;
  88.  
  89.  
  90.     printf("End.\n");
  91.     mouse_unsubscribe_int();
  92.     keyboard_unsubscribe_int();
  93.  
  94.  
  95.     vg_exit();
  96.  
  97.  
  98.  
  99.  
  100.     return 0;
  101.  
  102. }
  103. //-------------------------------------------mouse.c
  104. #include "mouse.h"
  105. #include "xpm.h"
  106. #include "sprite.h"
  107. #include "video_gr.h"
  108.  
  109. static int hook_id;
  110. mouse_packet package;
  111. mouse_config config;
  112.  
  113. static int vert_length = 0;
  114. static int hor_length = 0;
  115.  
  116.  
  117. int sameSignal(int x, int y)
  118. {
  119.     if (x*y >= 0)
  120.         return 1;
  121.     else
  122.         return 0;
  123. }
  124.  
  125.  
  126. int i8042_write(unsigned long port,unsigned long cmd)
  127. {
  128.     unsigned long status;
  129.  
  130.     unsigned i = 0;
  131.     while( i < NUMBER_TRIES ) {
  132.         if (sys_inb(STATUS_REGISTER, &status) != OK)
  133.             return -1;
  134.  
  135.         if( (status & IBF) == 0 ) {
  136.             if(sys_outb(port, cmd) != OK)
  137.                 return -1;
  138.             else
  139.                 return 0;
  140.         }
  141.         tickdelay(micros_to_ticks(DELAY_US));
  142.         i++;
  143.     }
  144.  
  145.     return -1;
  146. }
  147.  
  148. int i8042_read(unsigned long *data)
  149. {
  150.     unsigned long status;
  151.  
  152.     unsigned i = 0;
  153.     while( i < NUMBER_TRIES ) {
  154.  
  155.         if (sys_inb(STATUS_REGISTER, &status) != OK)
  156.             return -1;
  157.  
  158.         if( status & OBF ) {
  159.             if (sys_inb(OUT_BUF, data) != OK)
  160.                 return -1;
  161.  
  162.             if ( (status &(PAR_ERR | TIMEOUT)) == 0 )
  163.                 return 0;
  164.             else
  165.                 return -1;
  166.         }
  167.         tickdelay(micros_to_ticks(DELAY_US));
  168.         i++;
  169.     }
  170. }
  171.  
  172. int mouse_subscribe_int(){
  173.     hook_id = MOUSE_HOOK_NOTIFICATION;
  174.  
  175.     if (sys_irqsetpolicy(IRQ_MOUSE, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id) == OK)
  176.         return MOUSE_HOOK_NOTIFICATION;
  177.     else
  178.         return -1;
  179. }
  180. int mouse_unsubscribe_int(){
  181.     if (sys_irqrmpolicy(&hook_id) != OK)
  182.     {
  183.         printf("Error unsubscribing mouse.\n");
  184.         return 1;
  185.     }
  186.  
  187.     return 0;
  188. }
  189.  
  190. int mouse_write(unsigned long cmd){
  191.     int i=0;
  192.     unsigned long data;
  193.  
  194.     while(i<NUMBER_TRIES){
  195.         if(i8042_write(KBC_CMD_REG,WRITE_BYTE_TO_MOUSE)==-1)
  196.             return 1;
  197.         if(i8042_read(&data)==-1)
  198.             return 1;
  199.         if(i8042_write(KBD_CMD_REG,cmd)==-1)
  200.             return 1;
  201.         if(i8042_read(&data)==-1)
  202.             return 1;
  203.         if(data==ACK)
  204.             return 0;
  205.         i++;
  206.     }
  207.     return 1;
  208. }
  209.  
  210. int mouse_packet_handler(){
  211.     static int ind=0;
  212.     unsigned long byte;
  213.     if(sys_inb(OUT_BUF, &byte) != OK)
  214.         return -1;
  215.  
  216.     if(ind==0 && synchronize(byte)==1){
  217.         return 1;
  218.     }
  219.  
  220.      package.packet[ind % 3]= (unsigned char) byte;
  221.  
  222.      ind = ind + 1;
  223.      if ((ind % 3) == 0){
  224.          setPackageValues();
  225.          mouse_print_package();
  226.          return 0;
  227.      }
  228.      else
  229.          return 1;
  230. }
  231.  
  232. int mouse_print_package(){
  233.     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",
  234.             package.packet[0], package.packet[1], package.packet[2], package.left_button, package.middle_button, package.right_button,
  235.             package.x_ovf, package.y_ovf, package.x_value, package.y_value);
  236.  
  237. }
  238.  
  239. int synchronize(unsigned long byte){
  240.     if((byte & BIT(3)) && (byte != ACK))
  241.         return 0;
  242.     else
  243.         return 1;
  244. }
  245.  
  246.  
  247.  
  248. void setPackageValues()
  249. {
  250.     if (package.packet[0] & BIT(0))
  251.         package.left_button = 1;
  252.     else
  253.         package.left_button = 0;
  254.  
  255.     if (package.packet[0] & BIT(1))
  256.         package.right_button = 1;
  257.     else
  258.         package.right_button = 0;
  259.  
  260.     if (package.packet[0] & BIT(2))
  261.         package.middle_button = 1;
  262.     else
  263.         package.middle_button = 0;
  264.  
  265.     if (package.packet[0] & BIT(4))
  266.         package.x_sign = 1;
  267.     else
  268.         package.x_sign = 0;
  269.  
  270.     if (package.packet[0] & BIT(5))
  271.         package.y_sign = 1;
  272.     else
  273.         package.y_sign = 0;
  274.  
  275.     if (package.packet[0] & BIT(6))
  276.         package.x_ovf = 1;
  277.     else
  278.         package.x_ovf = 0;
  279.  
  280.     if (package.packet[0] & BIT(7))
  281.         package.y_ovf = 1;
  282.     else
  283.         package.y_ovf = 0;
  284.  
  285.  
  286.     if (package.x_sign) {
  287.         package.x_value = (short) package.packet[1];
  288.         package.x_value |= 0xFF00;
  289.     }
  290.     else
  291.         package.x_value = (short) package.packet[1];
  292.  
  293.     if (package.y_sign)
  294.         package.y_value = 0xFF00 | (short) package.packet[2];
  295.     else
  296.         package.y_value = (short) package.packet[2];
  297. }
  298.  
  299.  
  300.  
  301.  
  302. void setConfigValues()
  303. {
  304.     config.right_button = config.packet[0] & BIT(0);
  305.     config.middle_button = config.packet[0] & BIT(1);
  306.     config.left_button = config.packet[0] & BIT(2);
  307.     config.scaling = config.packet[0] & BIT(4);
  308.     config.enable = config.packet[0] & BIT(5);
  309.     config.remote = config.packet[0] & BIT(6);
  310.     config.resolution = config.packet[1] & (BIT(1) | BIT(0));
  311.     config.sample_rate = config.packet[2];
  312. }
  313.  
  314.  
  315.  
  316. void mouse_print_config_package(){
  317.     printf("\nMouse Config\n");
  318.     printf("Remote: %s", config.remote ? "remote (polled) mode\n" : "stream mode\n");
  319.     printf("Enable: %s", config.enable ? "data reporting enabled\n" : "disabled\n");
  320.     printf("Scaling: %s", config.scaling ? "2:1\n" : "1:1\n");
  321.     printf("Right button: %s", config.right_button ? "active\n" : "not active\n");
  322.     printf("Left button: %s", config.left_button ? "active\n" : "not active\n");
  323.     printf("Middle button: %s", config.middle_button ? "active\n" : "not active\n");
  324.     // a documentação referente à configuração do rato troca os BITS relativos ao RB e LB
  325.     // deixamos igual à documentação por razões de avaliação
  326.  
  327.     printf("Resolution: ");
  328.     switch (config.resolution){
  329.     case 0:
  330.         printf ("1 count per mm\n");
  331.         break;
  332.     case 1:
  333.         printf ("2 count per mm\n");
  334.         break;
  335.     case 2:
  336.         printf ("4 count per mm\n");
  337.         break;
  338.     case 3:
  339.         printf ("8 count per mm\n");
  340.         break;
  341.     }
  342.  
  343.     printf("Sample rate: %d\n", config.sample_rate);
  344. }
  345.  
  346.  
  347. int mouse_read_config(){
  348.     if(mouse_write(STATUS_REQUEST))
  349.         return 1;
  350.  
  351.     unsigned long data;
  352.     int i;
  353.     for (i=0; i < 3; i++) {
  354.         if(i8042_read(&data))
  355.             return 1;
  356.         else
  357.             config.packet[i]=(char)data;
  358.     }
  359.  
  360.     setConfigValues();
  361.     mouse_print_config_package();
  362.     return 0;
  363. }
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371. int mouse_gesture_handler(short length, unsigned short tolerance)
  372. {
  373.     static int ind=0;
  374.     unsigned long byte;
  375.     if(sys_inb(OUT_BUF, &byte) != OK)
  376.         return -1;
  377.  
  378.     if(ind==0 && synchronize(byte)==1){
  379.         return 1;
  380.     }
  381.     package.packet[ind % 3] = (char) byte;
  382.  
  383.     ind = ind + 1;
  384.     if ((ind % 3) == 0){
  385.         setPackageValues();
  386.  
  387.         event_t evt;
  388.         if (package.right_button)
  389.         {
  390.             evt.type = RDOW;
  391.             if (check_vert_line(&evt, length, tolerance) == STOP)
  392.                 return STOP;
  393.         }
  394.         else
  395.         {
  396.             evt.type = RUP;
  397.             if (check_vert_line(&evt, length, tolerance) == STOP)
  398.                 return STOP;
  399.         }
  400.  
  401.         if ((package.x_value != 0) || (package.y_value != 0))
  402.         {
  403.             printf("vertical length: %d \n",vert_length);
  404.             printf("horizontal length: %d \n\n",hor_length);
  405.             evt.type = MOVE;
  406.             if (check_vert_line(&evt, length, tolerance) == STOP)
  407.                     return STOP;
  408.         }
  409.  
  410.         return 0;
  411.     }
  412. }
  413.  
  414.  
  415. int check_vert_line(event_t *evt, int length, int tolerance) {
  416.     static state_t state = INIT; // initial state; keep state
  417.     switch (state) {
  418.     case INIT:
  419.         if( evt->type == RDOW )
  420.             state = DRAW;
  421.         break;
  422.     case DRAW:
  423.         if( evt->type == MOVE ) {
  424.             if (sameSignal(length, package.y_value))
  425.                 vert_length += package.y_value;
  426.             else
  427.                 vert_length = 0;
  428.  
  429.             hor_length += package.x_value;
  430.  
  431.             if (abs(hor_length) >= tolerance)
  432.             {
  433.                 vert_length = 0;
  434.                 hor_length = 0;
  435.             }
  436.  
  437.             if (abs(vert_length) >= abs(length))
  438.                 state = COMP;
  439.         }
  440.         else if( evt->type == RUP )
  441.         {
  442.             state = INIT;
  443.             vert_length = 0;
  444.             hor_length = 0;
  445.         }
  446.         break;
  447.     default:
  448.         break;
  449.     }
  450.  
  451.     if (state == COMP)
  452.         return STOP;
  453.     else
  454.         return 0;
  455. }
  456.  
  457.  
  458. //proj
  459. static int initial_x =605;
  460. static int initial_y =300;
  461.  
  462. int draw_mouse(){
  463.  
  464.     mouse_packet_handler();
  465.     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 )){
  466.     initial_x +=package.x_value;
  467.     initial_y -=package.y_value;
  468.     }
  469.  
  470.     Sprite *s=create_sprite(pic2,initial_x, initial_y,0,0);
  471.     draw_pixmap(s, THIRD_BUFFER);
  472.     swapVideoMem();
  473.     swapThirdBuffer();
  474.     if(package.left_button){
  475.         return STOP;
  476.     }
  477.     return 0;
  478. }
  479.  
  480. //------------------------------------------videogr.c
  481. #include <minix/syslib.h>
  482. #include <minix/drivers.h>
  483. #include <machine/int86.h>
  484. #include <sys/mman.h>
  485. #include <sys/types.h>
  486. #include <math.h>
  487. #include "vbe.h"
  488. #include "sprite.h"
  489.  
  490. /* Constants for VBE 0x105 mode */
  491.  
  492. /* The physical address may vary from VM to VM.
  493.  * At one time it was 0xD0000000
  494.  *  #define VRAM_PHYS_ADDR    0xD0000000
  495.  * Currently on lab B107 is 0xF0000000
  496.  * Better run my version of lab5 as follows:
  497.  *     service run `pwd`/lab5 -args "mode 0x105"
  498.  */
  499. #define VRAM_PHYS_ADDR          0xE0000000
  500. #define H_RES                   1024
  501. #define V_RES                   768
  502. #define BITS_PER_PIXEL          8
  503. #define BYTE_SIZE               8
  504.  
  505. /* Private global variables */
  506.  
  507. static char *video_mem;     /* Process address to which VRAM is mapped */
  508. static char *second_buffer;
  509. static char *third_buffer;
  510.  
  511.  
  512.  
  513.  
  514. static unsigned h_res = 1024;       /* Horizontal screen resolution in pixels */
  515. static unsigned v_res = 768;        /* Vertical screen resolution in pixels */
  516. static unsigned bits_per_pixel = 8; /* Number of VRAM bits per pixel */
  517.  
  518.  
  519.  
  520. void*vg_init(unsigned short mode) {
  521.     struct reg86u r;
  522.     vbe_mode_info_t info;
  523.     unsigned size;
  524.     struct mem_range mr;
  525.  
  526.     r.u.b.ah = VBE_FUNCTION_PREFIX;
  527.     r.u.b.intno = VBE_INTERRUPT_VECTOR;
  528.     r.u.b.al = VBE_SET_MODE;
  529.     r.u.w.bx = LINEAR_FRAME_BUFFER | mode;
  530.  
  531.     if (sys_int86(&r) == OK) {
  532.  
  533.         if (vbe_get_mode_info(mode, &info) != 1) {
  534.  
  535.             h_res = info.XResolution;
  536.             v_res = info.YResolution;
  537.             bits_per_pixel = info.BitsPerPixel;
  538.  
  539.             mr.mr_base = info.PhysBasePtr;
  540.             size = h_res * v_res * bits_per_pixel;
  541.             mr.mr_limit = mr.mr_base + size;
  542.  
  543.             if (sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mr))
  544.             {
  545.                 return NULL;
  546.             }
  547.  
  548.             video_mem = vm_map_phys(SELF, (void *) mr.mr_base, size);
  549.  
  550.             if(video_mem != MAP_FAILED)
  551.             {
  552.                 if ((second_buffer = malloc(h_res * v_res * bits_per_pixel / 8)) != NULL)
  553.                 {
  554.                     if ((third_buffer = malloc(h_res * v_res * bits_per_pixel / 8)) != NULL)
  555.                     {
  556.                         return video_mem;
  557.                     }
  558.                 }
  559.             }
  560.         }
  561.     }
  562.  
  563.  
  564.     return NULL;
  565. }
  566.  
  567.  
  568.  
  569. int vg_exit() {
  570.     struct reg86u reg86;
  571.  
  572.     reg86.u.b.intno = VBE_INTERRUPT_VECTOR;         /* BIOS video services */
  573.  
  574.     reg86.u.b.ah = VBE_VIDEO_MODE_FUNCTION;         /* Set Video Mode function */
  575.     reg86.u.b.al = SET_TEXT_MODE;                   /* 80x25 text mode*/
  576.  
  577.     if (sys_int86(&reg86) != OK) {
  578.         printf("\tvg_exit(): sys_int86() failed \n");
  579.         return 1;
  580.     } else
  581.         return 0;
  582. }
  583.  
  584.  
  585.  
  586. int setPixel(unsigned short x, unsigned short y,unsigned long color,int buffer)
  587. {
  588.     char *tmp;
  589.     if(buffer)
  590.         tmp = third_buffer;
  591.     else
  592.         tmp = second_buffer;
  593.  
  594.     if(x < 0 || x >= h_res || y < 0 || y >= v_res){
  595.         return 1;
  596.     }
  597.  
  598.     tmp += (x + y*h_res)*bits_per_pixel/BYTE_SIZE;
  599.     *tmp=color;
  600.  
  601.     return 0;
  602. }
  603.  
  604. void swapThirdBuffer(){
  605.     memcpy(third_buffer, second_buffer, h_res * v_res * bits_per_pixel / 8);
  606. }
  607.  
  608. void swapVideoMem(){
  609.     memcpy(video_mem, third_buffer, h_res * v_res * bits_per_pixel / 8);
  610. }
  611.  
  612.  
  613. int draw_square(unsigned short x, unsigned short y, unsigned short size, unsigned long color)
  614. {
  615.     if ((x+size > h_res) || (y+size > v_res))
  616.         return 1;
  617.  
  618.     unsigned short h_pos;
  619.     unsigned short v_pos;
  620.  
  621.     for (h_pos = x;  h_pos < x + size; h_pos++) {
  622.         for (v_pos=y; v_pos < y+size; v_pos++) {
  623.             setPixel(h_pos,v_pos,color,SECOND_BUFFER);
  624.         }
  625.     }
  626.  
  627.     return 0;
  628. }
  629.  
  630.  
  631.  
  632. int draw_line(unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, unsigned long color)
  633. {
  634.  
  635.       float dy = y2 - y1;
  636.       float dx = x2 - x1;
  637.  
  638.       float size = sqrt(dx*dx+dy*dy);
  639.  
  640.       int d;
  641.       for (d = 0; d < size; d++) {
  642.           unsigned short y= y1+(dy*d)/size;
  643.           unsigned short x= x1+(dx*d)/size;
  644.  
  645.           if(setPixel(x,y,color,SECOND_BUFFER)){
  646.               return 1;
  647.           }
  648.     }
  649.     return 0;
  650. }
  651.  
  652. int draw_pixmap(Sprite *s, int buffer){
  653.  
  654.     int i,j;
  655.     for (i = 0; i < s->height; i++) {
  656.         for (j = 0; j < s->width; ++j) {
  657.             if(setPixel((unsigned short) (j + s->x), (unsigned short) (i + s->y), s->map[(i*s->width + j)*bits_per_pixel/8], buffer))
  658.                 return 1;
  659.         }
  660.     }
  661.     return 0;
  662. }
  663.  
  664. //proj
  665.  
  666.  
  667. //-----------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement