Search code examples
mousecpu-registerscpu-cache

How does a computer process input of a moving mouse?


Are there registers involved or is it cache memory related?

An illustrative example for my question which perhaps is simple enough, I move my mouse across this screen I am currently typing on. I don;t click on anything, I just move the arrow left to right and up and down. How does the CPU handle the position changes of my mouse in relation to the monitors display which seems instantaneous?

Edit: I understand that this is more handled by the Operating system as a mouse is an external device and the CPU just calculates values and does logic. the mouse moves and on every clock signal the operating system gets an interrupt and handles it appropriately.


Solution

  • When you move/click your mouse, it generates an interrupt. An interrupt is basically a way to tell the cpu that an event has happened that needs to processed. The kernel will then run its interrupt handler to process the mouse events.

    For example, the PS/2 mouse communicates by means of a 3-byte packet:

    -----------------------------------------------
    Byte 1 | YV | XV | YS | XS | 1 | MB | RB | LB |
    -----------------------------------------------
    Byte 2 |             X movement               |
    -----------------------------------------------
    Byte 3 |             Y movement               |
    -----------------------------------------------
    

    The MB, RM, LB flags represent the Middle, Right and Left button clicks.

    The kernel will then eventually pass these events onto the application that is running.

    For example, in Linux, the X Window Server is the process that handles mouse events. Individual graphical applications are informed of them through a generic X event protocol.

    Registers and cache memory are always involved when running code. The kernel interrupt handlers are optimized to quickly process the interrupts and pass it on. The change is seen as near instantaneous because cpu's are extremely fast. Processors work with nanosecond resolution and there are a billion nanoseconds to every second.