• 自制操作系统(八) 让鼠标动起来


    参考书籍《30天自制操作系统》、《自己动手写操作系统》

    qq:992591601 欢迎交流

    2016.05.26、2016.06. 30

    鼠标的中断和键盘的中断原理是一样的,都是写好中断函数,当使用鼠标时便会自动调用中断函数(为了更好处理中断,需要FIFO缓冲区)。

    但鼠标也有些不同,首先在于init方面:

    要让鼠标有效,首先要发送指令,让下面两个装置有效:1 鼠标控制电路  2 鼠标本身

    让鼠标控制电路有效:

    wait_KBC_sendready的作用是:让键盘控制电路做好准备动作。因为cpu执行程序的速度远远大于键盘控制电路的速度。为了不让cpu不顾设备接收数据能力而不停发送指令,而必须等待直到确认键盘控制电路init完毕。

    #define PORT_KEYDAT                0x0060
    #define PORT_KEYSTA                0x0064
    #define PORT_KEYCMD                0x0064
    #define KEYSTA_SEND_NOTREADY       0x02
    #define KEYCMD_WRITE_MODE          0x60
    #define KBC_MODE                   0x47
    
    void wait_KBC_sendready(void)
    {
        /* 等待键盘控制电路准备完毕 */
        for (;;) 
        {
            if ((io_in8(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY) == 0)
            {
                break;
            }
        }
        return;
    }
    
    void init_keyboard(void)
    {
        /* 初始化键盘控制电路 */
        wait_KBC_sendready();
        io_out8(PORT_KEYCMD, KEYCMD_WRITE_MODE);
        wait_KBC_sendready();
        io_out8(PORT_KEYDAT, KBC_MODE);
        return;
    }

    让鼠标本身有效:

    #define KEYCMD_SENDTO_MOUSE        0xd4
    #define MOUSECMD_ENABLE            0xf4
    
    void enable_mouse(struct MOUSE_DEC *mdec)
    {
        /* 激活鼠标 */
        wait_KBC_sendready();
        io_out8(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE);
        wait_KBC_sendready();
        io_out8(PORT_KEYDAT, MOUSECMD_ENABLE);
        mdec->phase = 0; 
        return; 
    }

    此时的C语言main函数:

    void HariMain(void)
    {
        struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
        char s[40], mcursor[256], keybuf[32], mousebuf[128];
        int mx, my, i;
        struct MOUSE_DEC mdec;
    
        init_gdtidt();
        init_pic();
        io_sti(); 
        fifo8_init(&keyfifo, 32, keybuf);
        fifo8_init(&mousefifo, 128, mousebuf);
        io_out8(PIC0_IMR, 0xf9); 
        io_out8(PIC1_IMR, 0xef); 
    
        init_keyboard();
    
        init_palette();
        init_screen8(binfo->vram, binfo->scrnx, binfo->scrny);
        mx = (binfo->scrnx - 16) / 2; 
        my = (binfo->scrny - 28 - 16) / 2;
        init_mouse_cursor8(mcursor, COL8_008484);
        putblock8_8(binfo->vram, binfo->scrnx, 16, 16, mx, my, mcursor, 16);
        sprintf(s, "(%3d, %3d)", mx, my);
        putfonts8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s);
    
        enable_mouse(&mdec);
    
        for (;;) 
        {
            io_cli();
            if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) == 0)
            {
                io_stihlt();
            } 
            else
            {
                if (fifo8_status(&keyfifo) != 0) 
                {
                    i = fifo8_get(&keyfifo);
                    io_sti();
                    sprintf(s, "%02X", i);
                    draw_box(binfo->vram, binfo->scrnx, COL8_008484,  0, 16, 15, 31);
                    putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_FFFFFF, s);
                } 
                else if (fifo8_status(&mousefifo) != 0)
                {
                    i = fifo8_get(&mousefifo);
                    io_sti();
                    if (mouse_decode(&mdec, i) != 0) 
                    {
                        /* 数据的3个字节都齐了,显示出来 */
                        sprintf(s, "[lcr %4d %4d]", mdec.x, mdec.y);
                        if ((mdec.btn & 0x01) != 0)
                        {
                            s[1] = 'L';
                        }
                        if ((mdec.btn & 0x02) != 0) 
                        {
                            s[3] = 'R';
                        }
                        if ((mdec.btn & 0x04) != 0)
                        {
                            s[2] = 'C';
                        }
                        draw_box(binfo->vram, binfo->scrnx, COL8_008484, 32, 16, 32 + 15 * 8 - 1, 31);
                        putfonts8_asc(binfo->vram, binfo->scrnx, 32, 16, COL8_FFFFFF, s);
                        /* 鼠标的移动 */
                        draw_box(binfo->vram, binfo->scrnx, COL8_008484, mx, my, mx + 15, my + 15); /* 隐藏鼠标 */
                        mx += mdec.x;
                        my += mdec.y;
                        if (mx < 0) 
                        {
                            mx = 0;
                        }
                        if (my < 0)
                        {
                            my = 0;
                        }
                        if (mx > binfo->scrnx - 16) 
                        {
                            mx = binfo->scrnx - 16;
                        }
                        if (my > binfo->scrny - 16) 
                        {
                            my = binfo->scrny - 16;
                        }
                        sprintf(s, "(%3d, %3d)", mx, my);
                        draw_box(binfo->vram, binfo->scrnx, COL8_008484, 0, 0, 79, 15); /* 隐藏坐标 */
                        putfonts8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s); /* 显示坐标 */
                        putblock8_8(binfo->vram, binfo->scrnx, 16, 16, mx, my, mcursor, 16); /* 描画鼠标 */
                    }
                }
            }
        }
    }

    这里面的逻辑主要在于,每次从鼠标那里送来的数据都应该是3个字节一组的,所以每当数据累积到3个字节,就把它显示在屏幕上。

    main函数里需要调用的mouse_decode,用来解析鼠标传来的数据:

    int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat)
    {
        if (mdec->phase == 0) {
            /* 等待鼠标的0xfa的阶段 */
            if (dat == 0xfa) 
            {
                mdec->phase = 1;
            }
            return 0;
        }
        if (mdec->phase == 1) 
        {
            /* 等待鼠标第一字节的阶段 */
            if ((dat & 0xc8) == 0x08) 
            {
                /* 如果第一字节正确 */
                mdec->buf[0] = dat;
                mdec->phase = 2;
            }
            return 0;
        }
        if (mdec->phase == 2)
        {
            /* 等待鼠标第二字节的阶段 */
            mdec->buf[1] = dat;
            mdec->phase = 3;
            return 0;
        }
        if (mdec->phase == 3) 
        {
            /* 等待鼠标第三字节的阶段 */
            mdec->buf[2] = dat;
            mdec->phase = 1;
            mdec->btn = mdec->buf[0] & 0x07;
            mdec->x = mdec->buf[1];
            mdec->y = mdec->buf[2];
            if ((mdec->buf[0] & 0x10) != 0) 
            {
                mdec->x |= 0xffffff00;
            }
            if ((mdec->buf[0] & 0x20) != 0) 
            {
                mdec->y |= 0xffffff00;
            }
            mdec->y = - mdec->y; /* 鼠标的y方向与画面符号相反 */
            return 1;
        }
        return -1; 
    }

    执行效果:

  • 相关阅读:
    使用C#调用C++类库
    C# IntPtr类型
    C# 调用C++ dll string类型返回
    C# try、catch、finally语句
    C语言 char *、char []、const char *、string的区别与相互转换
    C# 字符串string与char数组互转!
    C#如何调用C++(进阶篇)
    Springboot通过过滤器实现对请求头的修改
    【spring事务】
    命令行参数库:McMaster.Extensions.CommandLineUtils【转】
  • 原文地址:https://www.cnblogs.com/rixiang/p/5532735.html
Copyright © 2020-2023  润新知