| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825 |
- #include "mouse.h"
- #include "bootinfo.h"
- #include "cirrus.h"
- #include "cursor.h"
- #include "intel.h"
- #include "radeon.h"
- #include "timer.h"
- #include "touchpad.h"
- #include "usbhid.h"
- #include "vga.h"
- #define PS2_DATA 0x60
- #define PS2_STATUS 0x64
- #define PS2_CMD 0x64
- #define STATUS_OUT_FULL 0x01
- #define STATUS_IN_FULL 0x02
- #define STATUS_AUX_DATA 0x20
- #define MOUSE_ACK 0xFA
- #define MOUSE_RESEND 0xFE
- /* One PS/2 wheel notch in scroll pixels; touchpads emit pixels directly. */
- #define MOUSE_WHEEL_PX 48
- #define MOUSE_SCROLL_MAX 100000
- static int mouse_present_flag;
- static int mouse_streaming_flag;
- static int mouse_auto_draw = 1; /* 0 while a WM frame owns the shadow */
- static unsigned int mouse_id;
- static int mouse_cursor_flag;
- static unsigned int mouse_pkt_size;
- static unsigned int mouse_buttons; /* bit0=left bit1=right bit2=middle */
- static int mouse_scroll_x, mouse_scroll_y;
- static int mouse_hw_cursor; /* 1 = Cirrus, 2 = Radeon, 3 = Intel */
- static int mouse_irq_mode; /* PS/2 AUX IRQ12 owns packet input */
- static int mouse_aux_irq_ok; /* controller config accepted AUX IRQ */
- static unsigned int fbw, fbh;
- static unsigned int cur_x, cur_y;
- static unsigned int saved_x, saved_y, saved_w, saved_h;
- static int saved_ox, saved_oy; /* unclipped image origin of the save */
- static unsigned char cur_saved[CURSOR_MAX_DIM * CURSOR_MAX_DIM * 4];
- static struct cursor_image cur_img; /* the frame on screen */
- static int cur_shape = CURSOR_DEFAULT;
- static unsigned int cur_frame, cur_frame_ms;
- static unsigned char pkt[4];
- static unsigned int pkt_idx;
- static inline unsigned char inb(unsigned short port)
- {
- unsigned char v;
- __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
- return v;
- }
- static inline void outb(unsigned short port, unsigned char v)
- {
- __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
- }
- static void ps2_wait_input_empty(void)
- {
- unsigned spin = 100000;
- while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
- ;
- }
- static void enable_aux_port(void)
- {
- ps2_wait_input_empty();
- outb(PS2_CMD, 0xA8);
- }
- static inline unsigned int mouse_irq_save(void)
- {
- unsigned int flags;
- __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
- return flags;
- }
- static inline void mouse_irq_restore(unsigned int flags)
- {
- __asm__ volatile ("pushl %0; popfl" :: "r"(flags));
- }
- /* Set the controller's AUX-interrupt bit. The PIC line stays masked until
- * mouse_init() has a hardware cursor and streaming is enabled. */
- static int ps2_aux_irq_config(void)
- {
- unsigned int flags, spin = 100000;
- unsigned char cfg;
- flags = mouse_irq_save();
- ps2_wait_input_empty();
- outb(PS2_CMD, 0x20);
- while (spin-- && !(inb(PS2_STATUS) & STATUS_OUT_FULL))
- ;
- if (!(inb(PS2_STATUS) & STATUS_OUT_FULL)) {
- mouse_irq_restore(flags);
- return -1;
- }
- cfg = inb(PS2_DATA);
- cfg |= 0x02; /* AUX (mouse) interrupt */
- ps2_wait_input_empty();
- outb(PS2_CMD, 0x60);
- ps2_wait_input_empty();
- outb(PS2_DATA, cfg);
- mouse_irq_restore(flags);
- return 0;
- }
- static void ps2_aux_irq_unmask(void)
- {
- unsigned char mask;
- /* Master IRQ2 is the cascade, slave IRQ12 is the AUX line. */
- mask = inb(0x21);
- outb(0x21, (unsigned char)(mask & ~0x04));
- mask = inb(0xA1);
- outb(0xA1, (unsigned char)(mask & ~0x10));
- }
- static void mouse_cmd_write(unsigned char val)
- {
- ps2_wait_input_empty();
- outb(PS2_CMD, 0xD4);
- ps2_wait_input_empty();
- outb(PS2_DATA, val);
- }
- static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
- {
- unsigned int t0 = get_ticks_ms();
- while (get_ticks_ms() - t0 < timeout_ticks) {
- unsigned char st = inb(PS2_STATUS);
- if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
- (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
- *out = inb(PS2_DATA);
- return 0;
- }
- }
- return -1;
- }
- static int mouse_expect_ack(void)
- {
- unsigned char b;
- if (mouse_read_aux(&b, 500) != 0)
- return -1;
- if (b == MOUSE_RESEND)
- return -2;
- if (b != MOUSE_ACK)
- return -1;
- return 0;
- }
- static int mouse_send_cmd_ack(unsigned char cmd)
- {
- int r;
- mouse_cmd_write(cmd);
- r = mouse_expect_ack();
- if (r == -2) {
- mouse_cmd_write(cmd);
- r = mouse_expect_ack();
- }
- return r;
- }
- /* ---- Cursor image ---------------------------------------------------------
- *
- * cur_x/cur_y are the hotspot. The image (kernel/cursor.c: an Adwaita
- * shape or the built-in arrow) is drawn with its top-left at
- * (cur_x - hot_x, cur_y - hot_y), so near the top/left edge part of it is
- * off-screen. The software cursor blends the premultiplied ARGB over the
- * saved background; the hardware backends take the same image. */
- static void cursor_calc_rect(unsigned int *rx, unsigned int *ry,
- unsigned int *rw, unsigned int *rh)
- {
- int ox = (int)cur_x - (int)cur_img.hot_x;
- int oy = (int)cur_y - (int)cur_img.hot_y;
- int x0 = ox, y0 = oy;
- int x1 = ox + (int)cur_img.w, y1 = oy + (int)cur_img.h;
- if (x0 < 0) x0 = 0;
- if (y0 < 0) y0 = 0;
- if (x1 > (int)fbw) x1 = (int)fbw;
- if (y1 > (int)fbh) y1 = (int)fbh;
- if (x1 <= x0 || y1 <= y0) {
- *rx = *ry = *rw = *rh = 0;
- return;
- }
- *rx = (unsigned int)x0;
- *ry = (unsigned int)y0;
- *rw = (unsigned int)(x1 - x0);
- *rh = (unsigned int)(y1 - y0);
- }
- static void cursor_save_under(void)
- {
- if (!vga_shadow_is_active()) {
- /* Boot: the shadow buffer is not up yet, so there is no
- * background to save (and the saved buffer is uninitialized). */
- saved_w = saved_h = 0;
- return;
- }
- cursor_calc_rect(&saved_x, &saved_y, &saved_w, &saved_h);
- saved_ox = (int)cur_x - (int)cur_img.hot_x;
- saved_oy = (int)cur_y - (int)cur_img.hot_y;
- if (saved_w && saved_h)
- vga_fb_read_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
- }
- static void cursor_restore_under(void)
- {
- if (saved_w && saved_h)
- vga_fb_write_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
- }
- /* Push the rectangle the cursor last touched to VRAM right away. The cursor
- * must not wait for the damage queue: over a large canvas (the browser) a
- * whole-window repaint can sit ahead of it for several flush iterations. */
- static void cursor_present(void)
- {
- if (saved_w && saved_h)
- vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
- }
- /* Blend the current image over the background saved by cursor_save_under()
- * (which must have run for the same position and image). */
- static void mouse_draw_cursor(void)
- {
- unsigned int pb = vga_fb_bpp() == 16 ? 2u : vga_fb_bpp() == 24 ? 3u : 4u;
- unsigned int r, c;
- if (!cur_img.px)
- return;
- for (r = 0; r < saved_h; r++) {
- unsigned int iy = saved_y + r - (unsigned int)saved_oy;
- const unsigned int *src = cur_img.px + iy * cur_img.w +
- (saved_x - (unsigned int)saved_ox);
- for (c = 0; c < saved_w; c++) {
- unsigned int s = src[c];
- unsigned int a = s >> 24;
- unsigned int sr = (s >> 16) & 0xFF, sg = (s >> 8) & 0xFF;
- unsigned int sb = s & 0xFF;
- if (a == 0)
- continue;
- if (a != 255) {
- const unsigned char *d =
- cur_saved + (r * saved_w + c) * pb;
- unsigned int dr, dg, db, inv = 255 - a;
- if (pb == 2) {
- unsigned int v = d[0] | (d[1] << 8);
- dr = ((v >> 11) & 0x1F) * 255 / 31;
- dg = ((v >> 5) & 0x3F) * 255 / 63;
- db = (v & 0x1F) * 255 / 31;
- } else {
- db = d[0];
- dg = d[1];
- dr = d[2];
- }
- /* Premultiplied source over the background. */
- sr += (dr * inv + 127) / 255;
- sg += (dg * inv + 127) / 255;
- sb += (db * inv + 127) / 255;
- if (sr > 255) sr = 255;
- if (sg > 255) sg = 255;
- if (sb > 255) sb = 255;
- }
- vga_fb_put_pixel(saved_x + c, saved_y + r, sr, sg, sb);
- }
- }
- }
- /* Hardware cursor position: the image's top-left, may be negative. */
- static void cursor_hw_move(void)
- {
- int ox = (int)cur_x - (int)cur_img.hot_x;
- int oy = (int)cur_y - (int)cur_img.hot_y;
- if (mouse_hw_cursor == 3)
- intel_cursor_move(ox, oy);
- else if (mouse_hw_cursor == 2)
- radeon_cursor_move(ox, oy);
- else if (mouse_hw_cursor == 1)
- cirrus_cursor_move(ox, oy);
- }
- static void cursor_hw_load(void)
- {
- if (mouse_hw_cursor == 3)
- intel_cursor_load(&cur_img);
- else if (mouse_hw_cursor == 2)
- radeon_cursor_load(&cur_img);
- else if (mouse_hw_cursor == 1)
- cirrus_cursor_load(&cur_img);
- }
- /* Switch to `img`: re-upload the hardware image, or erase and redraw the
- * software cursor (presenting the union of the old and new rectangles in
- * one go, so a shape change does not blink). */
- static void cursor_set_image(const struct cursor_image *img)
- {
- unsigned int ox, oy, ow, oh;
- if (!mouse_cursor_flag) {
- cur_img = *img;
- return;
- }
- if (mouse_hw_cursor) {
- unsigned int flags = mouse_irq_save();
- cur_img = *img;
- cursor_hw_load();
- cursor_hw_move();
- mouse_irq_restore(flags);
- return;
- }
- if (!mouse_auto_draw) {
- /* A WM frame owns the shadow; PRESENT redraws the cursor. */
- cur_img = *img;
- return;
- }
- cursor_restore_under();
- ox = saved_x; oy = saved_y; ow = saved_w; oh = saved_h;
- cur_img = *img;
- cursor_save_under();
- mouse_draw_cursor();
- if (ow && oh && saved_w && saved_h) {
- unsigned int x0 = ox < saved_x ? ox : saved_x;
- unsigned int y0 = oy < saved_y ? oy : saved_y;
- unsigned int x1 = ox + ow > saved_x + saved_w ?
- ox + ow : saved_x + saved_w;
- unsigned int y1 = oy + oh > saved_y + saved_h ?
- oy + oh : saved_y + saved_h;
- vga_fb_present_rect(x0, y0, x1 - x0, y1 - y0);
- } else {
- if (ow && oh)
- vga_fb_present_rect(ox, oy, ow, oh);
- cursor_present();
- }
- }
- static void cursor_select(int shape, unsigned int frame)
- {
- struct cursor_image img;
- cursor_get(shape, frame, &img);
- cursor_set_image(&img);
- }
- void mouse_set_shape(int shape)
- {
- if (shape < 0 || shape >= CURSOR_COUNT)
- shape = CURSOR_DEFAULT;
- if (shape == cur_shape)
- return;
- cur_shape = shape;
- cur_frame = 0;
- cur_frame_ms = get_ticks_ms();
- cursor_select(cur_shape, 0);
- }
- int mouse_get_shape(void)
- {
- return cur_shape;
- }
- void mouse_cursor_reload(void)
- {
- cursor_select(cur_shape, cur_frame);
- }
- /* Advance an animated cursor (wait/progress) from the poll loop. */
- static void mouse_cursor_tick(void)
- {
- unsigned int now;
- if (!mouse_cursor_flag || cur_img.frames <= 1)
- return;
- now = get_ticks_ms();
- if (now - cur_frame_ms < cur_img.delay_ms)
- return;
- cur_frame_ms = now;
- cur_frame = (cur_frame + 1) % cur_img.frames;
- cursor_select(cur_shape, cur_frame);
- }
- static void mouse_clamp_ptr(long *nx, long *ny)
- {
- long maxx = (long)fbw - 1;
- long maxy = (long)fbh - 1;
- if (maxx < 0)
- maxx = 0;
- if (maxy < 0)
- maxy = 0;
- if (*nx < 0)
- *nx = 0;
- else if (*nx > maxx)
- *nx = maxx;
- if (*ny < 0)
- *ny = 0;
- else if (*ny > maxy)
- *ny = maxy;
- }
- static void mouse_cursor_setup(void)
- {
- mouse_cursor_flag = 0;
- if (!vga_has_vesa())
- return;
- fbw = vga_fb_width();
- fbh = vga_fb_height();
- if (fbw < CURSOR_MAX_DIM || fbh < CURSOR_MAX_DIM)
- return;
- cursor_get(cur_shape, cur_frame, &cur_img);
- cur_frame_ms = get_ticks_ms();
- /* Cirrus hardware cursor: the card overlays it; no save/restore.
- * UEFI GOP modes are not programmed through the legacy VGA sequencer,
- * so use the software cursor there. */
- if (!bootinfo_efi() && cirrus_available()) {
- mouse_hw_cursor = 1;
- cur_x = 100;
- cur_y = 100;
- cirrus_cursor_setup(&cur_img);
- cursor_hw_move();
- mouse_cursor_flag = 1;
- return;
- }
- /* Radeon (R200/RV280) hardware cursor. */
- if (!bootinfo_efi() && radeon_available()) {
- mouse_hw_cursor = 2;
- cur_x = 100;
- cur_y = 100;
- radeon_cursor_setup(&cur_img);
- cursor_hw_move();
- mouse_cursor_flag = 1;
- return;
- }
- /* Intel Ironlake (Arrandale/X201) cursor plane. The cursor image is
- * mapped through the GTT by intel_init(); only enable it once a mouse
- * is actually present. */
- if (!bootinfo_efi() && intel_available()) {
- long nx = 100;
- long ny = 100;
- mouse_clamp_ptr(&nx, &ny);
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- intel_cursor_load(&cur_img);
- if (intel_cursor_setup((int)cur_x - (int)cur_img.hot_x,
- (int)cur_y - (int)cur_img.hot_y) == 0) {
- mouse_hw_cursor = 3;
- mouse_cursor_flag = 1;
- return;
- }
- mouse_hw_cursor = 0;
- }
- /* NVIDIA nv50-family (ION/MCP79): no hardware cursor -- the VBIOS keeps
- the display engine (see nv50.c); the software cursor is used. */
- mouse_hw_cursor = 0;
- cur_x = 100;
- cur_y = 100;
- {
- long nx = (long)cur_x;
- long ny = (long)cur_y;
- mouse_clamp_ptr(&nx, &ny);
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- }
- cursor_save_under();
- mouse_draw_cursor();
- cursor_present();
- mouse_cursor_flag = 1;
- }
- static int mouse_intellimouse_init(void)
- {
- /* First three rates are the Intellimouse enable magic (200/100/80);
- the last is the final report rate. 0xC8 = 200 Hz for a smooth cursor. */
- static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0xC8 };
- unsigned int i;
- for (i = 0; i < sizeof(rates); i++) {
- if (mouse_send_cmd_ack(0xF3) != 0)
- return -1;
- if (mouse_send_cmd_ack(rates[i]) != 0)
- return -1;
- }
- return 0;
- }
- void mouse_init(void)
- {
- unsigned char id = 0;
- mouse_present_flag = 0;
- mouse_streaming_flag = 0;
- mouse_id = 0;
- mouse_cursor_flag = 0;
- mouse_pkt_size = 3;
- mouse_irq_mode = 0;
- mouse_aux_irq_ok = 0;
- pkt_idx = 0;
- /* Built-in HID-over-I2C touchpad. The probe maps the LPSS I2C
- * window, so it must run at boot, before the kworker and command
- * processes clone the page directory. */
- touchpad_init();
- enable_aux_port();
- mouse_aux_irq_ok = (ps2_aux_irq_config() == 0);
- if (mouse_send_cmd_ack(0xF5) != 0)
- return;
- mouse_intellimouse_init();
- mouse_cmd_write(0xF2);
- if (mouse_expect_ack() != 0)
- return;
- if (mouse_read_aux(&id, 500) != 0)
- return;
- mouse_present_flag = 1;
- mouse_id = id;
- mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
- mouse_cursor_setup();
- if (mouse_send_cmd_ack(0xF4) != 0)
- return;
- mouse_streaming_flag = 1;
- /* A hardware cursor makes the IRQ12 path safe: the handler only has
- * to write the cursor registers, not touch the shadow buffer or the
- * damage list. Otherwise keep the kworker polling the controller. */
- if (mouse_hw_cursor && mouse_aux_irq_ok) {
- pkt_idx = 0;
- mouse_irq_mode = 1;
- ps2_aux_irq_unmask();
- }
- }
- /* Move the cursor by a relative delta (PS/2 sign convention: +dx right,
- +dy up), redrawing it with the current backend (hardware cursor or
- shadow-buffer save/restore). */
- static void mouse_move(int dx, int dy)
- {
- long nx, ny;
- if (!mouse_cursor_flag)
- return;
- nx = (long)cur_x + dx;
- ny = (long)cur_y - dy;
- mouse_clamp_ptr(&nx, &ny);
- if ((unsigned int)nx == cur_x && (unsigned int)ny == cur_y)
- return;
- if (mouse_hw_cursor) {
- /* The PS/2 AUX IRQ can preempt a USB-driven update in the
- * kworker; keep the position read/modify/write atomic. */
- unsigned int flags = mouse_irq_save();
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- cursor_hw_move();
- mouse_irq_restore(flags);
- } else if (!mouse_auto_draw) {
- /* A WM frame is between BEGIN and PRESENT: it erased the
- * software cursor and will redraw it at the new position. */
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- } else {
- cursor_restore_under();
- cursor_present(); /* erase the old position in VRAM */
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- cursor_save_under();
- mouse_draw_cursor();
- cursor_present(); /* draw the new position in VRAM */
- }
- }
- static void mouse_scroll_add(int dy)
- {
- mouse_scroll_y += dy;
- if (mouse_scroll_y > MOUSE_SCROLL_MAX)
- mouse_scroll_y = MOUSE_SCROLL_MAX;
- else if (mouse_scroll_y < -MOUSE_SCROLL_MAX)
- mouse_scroll_y = -MOUSE_SCROLL_MAX;
- }
- /* Decode one complete PS/2 packet from pkt[] (3 or 4 bytes). */
- static void mouse_handle_packet(void)
- {
- unsigned char b0 = pkt[0];
- int dx = (int)(signed char)pkt[1];
- int dy = (int)(signed char)pkt[2];
- mouse_buttons = b0 & 0x07;
- if (b0 & 0x10)
- dx |= ~0xFF;
- if (b0 & 0x20)
- dy |= ~0xFF;
- if ((b0 & 0xC0) != 0)
- return;
- mouse_move(dx, dy);
- if (mouse_pkt_size >= 4) {
- /* IntelliMouse wheel: +1 is toward the user (later content). */
- int wheel = (int)(signed char)pkt[3];
- if (wheel)
- mouse_scroll_add(wheel * MOUSE_WHEEL_PX);
- }
- }
- /* IRQ12: PS/2 AUX data. With a hardware cursor the position update is just
- * a few MMIO writes, so it is safe and fast in interrupt context. This path
- * is only enabled when mouse_irq_mode is set (hardware cursor present). */
- void mouse_irq(void)
- {
- unsigned int guard = 0;
- if (mouse_irq_mode) {
- for (;;) {
- unsigned char st;
- if (guard++ >= 16)
- break;
- st = inb(PS2_STATUS);
- if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
- (STATUS_OUT_FULL | STATUS_AUX_DATA))
- break;
- pkt[pkt_idx++] = inb(PS2_DATA);
- if (pkt_idx < mouse_pkt_size)
- continue;
- pkt_idx = 0;
- mouse_handle_packet();
- }
- }
- /* Slave first, then the master cascade. */
- outb(0xA0, 0x20);
- outb(0x20, 0x20);
- }
- void mouse_poll_update(void)
- {
- unsigned int guard = 0;
- unsigned int usb_buttons = 0;
- int usb_dx = 0, usb_dy = 0, usb_scroll = 0;
- int tp_buttons = 0, tp_dx = 0, tp_dy = 0, tp_scroll = 0;
- /* The cursor is set up for whichever backend shows up first (PS/2
- is probed at boot, USB may attach later). */
- if ((mouse_present_flag || usbhid_mouse_attached() ||
- touchpad_present()) && !mouse_cursor_flag)
- mouse_cursor_setup();
- mouse_cursor_tick();
- /* USB HID mouse: reports arrive through the armed interrupt
- endpoint; the backend also handles hotplug/detach. */
- if (usbhid_poll(&usb_buttons, &usb_dx, &usb_dy, &usb_scroll)) {
- mouse_buttons = usb_buttons;
- mouse_move(usb_dx, usb_dy);
- if (usb_scroll)
- mouse_scroll_add(usb_scroll);
- }
- /* Built-in touchpad: absolute positions converted to deltas. */
- if (touchpad_poll(&tp_buttons, &tp_dx, &tp_dy, &tp_scroll)) {
- mouse_buttons = tp_buttons;
- mouse_move(tp_dx, tp_dy);
- if (tp_scroll)
- mouse_scroll_add(tp_scroll);
- }
- if (mouse_irq_mode)
- return; /* IRQ12 owns the PS/2 packets */
- if (!mouse_streaming_flag)
- return;
- for (;;) {
- unsigned char st;
- /* Bound the drain: a stuck/emulated controller must not
- * wedge the WM loop; leftovers are read next iteration. */
- if (guard++ >= 64)
- return;
- st = inb(PS2_STATUS);
- if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
- (STATUS_OUT_FULL | STATUS_AUX_DATA))
- return;
- pkt[pkt_idx++] = inb(PS2_DATA);
- if (pkt_idx < mouse_pkt_size)
- continue;
- pkt_idx = 0;
- mouse_handle_packet();
- }
- }
- int mouse_is_present(void)
- {
- return mouse_present_flag || usbhid_mouse_attached() ||
- touchpad_present();
- }
- int mouse_ps2_present(void)
- {
- return mouse_present_flag;
- }
- int mouse_streaming_enabled(void)
- {
- return mouse_streaming_flag;
- }
- unsigned int mouse_device_id(void)
- {
- return mouse_id;
- }
- int mouse_cursor_visible(void)
- {
- return mouse_cursor_flag;
- }
- int mouse_cursor_backend(void)
- {
- return mouse_hw_cursor;
- }
- const char *mouse_cursor_backend_name(void)
- {
- switch (mouse_hw_cursor) {
- case 1: return "cirrus hw";
- case 2: return "radeon hw";
- case 3: return "intel hw";
- default: return "software";
- }
- }
- unsigned int mouse_packet_size(void)
- {
- return mouse_pkt_size;
- }
- unsigned int mouse_get_x(void)
- {
- return cur_x;
- }
- unsigned int mouse_get_y(void)
- {
- return cur_y;
- }
- unsigned int mouse_buttons_state(void)
- {
- return mouse_buttons;
- }
- void mouse_scroll_take(int *dx, int *dy)
- {
- unsigned int flags = mouse_irq_save();
- *dx = mouse_scroll_x;
- *dy = mouse_scroll_y;
- mouse_scroll_x = 0;
- mouse_scroll_y = 0;
- mouse_irq_restore(flags);
- }
- void mouse_redraw_cursor(void)
- {
- if (!mouse_cursor_flag || mouse_hw_cursor)
- return;
- /* If the pointer moved while a WM frame owned the shadow, the saved
- * rectangle is still the old position and has been restored to the
- * background there. Erase it in VRAM before drawing the new one, so
- * a moving cursor does not leave a trail behind a slow frame. */
- if (saved_w && saved_h) {
- unsigned int nx, ny, nw, nh;
- cursor_calc_rect(&nx, &ny, &nw, &nh);
- if (nx != saved_x || ny != saved_y || nw != saved_w ||
- nh != saved_h)
- vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
- }
- cursor_save_under();
- mouse_draw_cursor();
- cursor_present();
- }
- void mouse_cursor_erase(void)
- {
- if (!mouse_cursor_flag || mouse_hw_cursor)
- return;
- cursor_restore_under();
- /* No direct present here: the caller is about to redraw the scene.
- * Keeping the old cursor on screen until that flush avoids a visible
- * blink at the start of every window frame. The restore already
- * marked the rectangle dirty, so the flush erases it. */
- }
- void mouse_set_auto_draw(int on)
- {
- mouse_auto_draw = on ? 1 : 0;
- }
|