Repository: libsdl-org/SDL
File: src/video/windows/SDL_windowsevents.c (C)

Code before:
                    } else {
                        /* Send relative motion if we didn't warp last frame (had good position data)
                           We also sometimes get large deltas due to coalesced mouse motion and warping,
                           so ignore those.
                         */
                        const int MAX_RELATIVE_MOTION = (h / 6);
                        if (SDL_abs(relX) < MAX_RELATIVE_MOTION &&
                            SDL_abs(relY) < MAX_RELATIVE_MOTION) {
                            SDL_SendMouseMotion(timestamp, window, mouseID, true, (float)relX, (float)relY);
                        }
                    }
                }
            } else if (mouse->pen_mouse_events) {
                const int MAXIMUM_TABLET_RELATIVE_MOTION = 32;
                if (SDL_abs(relX) > MAXIMUM_TABLET_RELATIVE_MOTION ||
                    SDL_abs(relY) > MAXIMUM_TABLET_RELATIVE_MOTION) {
                    /* Ignore this motion, probably a pen lift and drop */
                } else {
                    SDL_SendMouseMotion(timestamp, window, mouseID, true, (float)relX, (float)relY);
                }
            } else {
                int screen_x = virtual_desktop ? GetSystemMetrics(SM_XVIRTUALSCREEN) : 0;
                int screen_y = virtual_desktop ? GetSystemMetrics(SM_YVIRTUALSCREEN) : 0;

                if (!data->raw_input_fake_pen_id) {
                    data->raw_input_fake_pen_id = SDL_AddPenDevice(timestamp, "raw mouse input", window, NULL, SDL_malloc(1));
                }
                SDL_SendPenMotion(timestamp, data->raw_input_fake_pen_id, window, (float)(x + screen_x - window->x), (float)(y + screen_y - window->y));
            }

            data->last_raw_mouse_position.x = x;
            data->last_raw_mouse_position.y = y;
        }
    }

    if (haveButton) {
        for (int i = 0; i < SDL_arraysize(raw_buttons); ++i) {
            if (rawmouse->usButtonFlags & raw_buttons[i].usButtonFlags) {
                Uint8 button = raw_buttons[i].button;
                bool down = raw_buttons[i].down;

                if (button == SDL_BUTTON_LEFT) {
                    if (WIN_SwapButtons(hDevice)) {
                        button = SDL_BUTTON_RIGHT;
                    }
                } else if (button == SDL_BUTTON_RIGHT) {
                    if (WIN_SwapButtons(hDevice)) {
                        button = SDL_BUTTON_LEFT;
                    }
                }


Review comment (performance):
@icculus are you sure you're fine with this memory leak? It will happen when a pen is used after (re-)enabling raw input.

Since the pen with this handle will only be added once (no duplicates), it should be safe to pass a unique static pointer, like a pointer to a string in the data section, or to an address of a function.

For example:
```c
SDL_AddPenDevice(timestamp, "raw mouse input", window, NULL, "unique static pointer");
// or
SDL_AddPenDevice(timestamp, "raw mouse input", window, NULL, WIN_HandleRawMouseInput);
// or even
SDL_AddPenDevice(timestamp, "raw mouse input", window, NULL, (void*)-1);
```

Using -1 also has the added guarantee of never conflicting with the [other SDL_AddPenDevice call](https://github.com/libsdl-org/SDL/blob/25ab8c99dfefff653f95a75232d4cce960fb7897/src/video/windows/SDL_windowsevents.c#L1258-L1280) that uses `GET_POINTERID_WPARAM()`, which will only return values `& 0xffff`.


Code after:
                    } else {
                        /* Send relative motion if we didn't warp last frame (had good position data)
                           We also sometimes get large deltas due to coalesced mouse motion and warping,
                           so ignore those.
                         */
                        const int MAX_RELATIVE_MOTION = (h / 6);
                        if (SDL_abs(relX) < MAX_RELATIVE_MOTION &&
                            SDL_abs(relY) < MAX_RELATIVE_MOTION) {
                            SDL_SendMouseMotion(timestamp, window, mouseID, true, (float)relX, (float)relY);
                        }
                    }
                }
            } else if (mouse->pen_mouse_events) {
                const int MAXIMUM_TABLET_RELATIVE_MOTION = 32;
                if (SDL_abs(relX) > MAXIMUM_TABLET_RELATIVE_MOTION ||
                    SDL_abs(relY) > MAXIMUM_TABLET_RELATIVE_MOTION) {
                    /* Ignore this motion, probably a pen lift and drop */
                } else {
                    SDL_SendMouseMotion(timestamp, window, mouseID, true, (float)relX, (float)relY);
                }
            } else {
                int screen_x = virtual_desktop ? GetSystemMetrics(SM_XVIRTUALSCREEN) : 0;
                int screen_y = virtual_desktop ? GetSystemMetrics(SM_YVIRTUALSCREEN) : 0;

                if (!data->raw_input_fake_pen_id) {
                    data->raw_input_fake_pen_id = SDL_AddPenDevice(timestamp, "raw mouse input", window, NULL, (void *)(size_t)-1);
                }
                SDL_SendPenMotion(timestamp, data->raw_input_fake_pen_id, window, (float)(x + screen_x - window->x), (float)(y + screen_y - window->y));
            }

            data->last_raw_mouse_position.x = x;
            data->last_raw_mouse_position.y = y;
        }
    }

    if (haveButton) {
        for (int i = 0; i < SDL_arraysize(raw_buttons); ++i) {
            if (rawmouse->usButtonFlags & raw_buttons[i].usButtonFlags) {
                Uint8 button = raw_buttons[i].button;
                bool down = raw_buttons[i].down;

                if (button == SDL_BUTTON_LEFT) {
                    if (WIN_SwapButtons(hDevice)) {
                        button = SDL_BUTTON_RIGHT;
                    }
                } else if (button == SDL_BUTTON_RIGHT) {
                    if (WIN_SwapButtons(hDevice)) {
                        button = SDL_BUTTON_LEFT;
                    }
                }