MCPcopy Create free account
hub / github.com/Zalafina/QKeyMapper / pasteText

Method pasteText

QKeyMapper/qkeymapper_worker.cpp:1646–2038  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1644}
1645
1646void QKeyMapper_Worker::pasteText(HWND window_hwnd, const QString &text, int mode)
1647{
1648 if (text.isEmpty()) {
1649 return;
1650 }
1651
1652#ifdef DEBUG_LOGOUT_ON
1653 qDebug().noquote().nospace() << "[pasteText] Text length: " << text.length()
1654 << ", TargetWnd: 0x" << Qt::hex << reinterpret_cast<quintptr>(window_hwnd);
1655#endif
1656
1657#ifdef PASTETEXT_RESTORE_CLIPBOARD
1658 // Static variables to handle concurrent pasteText calls
1659 // Strategy: Only restore clipboard if NO concurrent calls occurred during this session
1660 // This ensures safety when multiple different PasteText mappings are triggered simultaneously
1661 static QMutex s_pasteTextMutex;
1662 static HGLOBAL s_hOriginalDataCopy = NULL;
1663 static bool s_hasOriginalData = false;
1664 static int s_pasteTextRefCount = 0;
1665 static bool s_hadConcurrentCalls = false; // Track if concurrency occurred
1666 static bool s_backupSucceeded = false; // Track if backup operation succeeded (even if clipboard was empty)
1667 static bool s_isRestoring = false; // Track if clipboard restore is in progress
1668
1669 // Lock to safely update reference count and backup clipboard
1670 s_pasteTextMutex.lock();
1671
1672 // Wait if a restore operation is in progress from a previous session
1673 // This prevents new calls from interfering with ongoing clipboard restore
1674 while (s_isRestoring) {
1675 s_pasteTextMutex.unlock();
1676#ifdef DEBUG_LOGOUT_ON
1677 qDebug() << "[pasteText] Waiting for previous restore to complete...";
1678#endif
1679 QThread::msleep(10); // Brief wait before checking again
1680 s_pasteTextMutex.lock();
1681 }
1682
1683 s_pasteTextRefCount++;
1684 bool isFirstCall = (s_pasteTextRefCount == 1);
1685
1686 // If this is not the first call, mark that we had concurrent calls
1687 // Once marked, we won't restore clipboard for this session (safety measure)
1688 if (!isFirstCall) {
1689 s_hadConcurrentCalls = true;
1690#ifdef DEBUG_LOGOUT_ON
1691 qDebug().nospace() << "[pasteText] Concurrent call detected! RefCount: " << s_pasteTextRefCount << ", clipboard restore disabled for this session";
1692#endif
1693 }
1694#ifdef DEBUG_LOGOUT_ON
1695 else {
1696 qDebug().nospace() << "[pasteText] First call, RefCount: " << s_pasteTextRefCount;
1697 }
1698#endif
1699
1700 // Only the first call should backup the original clipboard content
1701 if (isFirstCall) {
1702 // Reset flags for new session
1703 s_hadConcurrentCalls = false;

Callers

nothing calls this directly

Calls 1

isEmptyMethod · 0.80

Tested by

no test coverage detected