File indexing completed on 2024-04-28 11:47:11

0001 /*
0002     SPDX-FileCopyrightText: 2001 Ellis Whitehead <ellis@kde.org>
0003 
0004     Win32 port:
0005     SPDX-FileCopyrightText: 2004 Jarosław Staniek <staniek@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 #include "kkeyserver.h"
0011 
0012 #include <QCoreApplication>
0013 
0014 namespace KKeyServer
0015 {
0016 //---------------------------------------------------------------------
0017 // Array Structures
0018 //---------------------------------------------------------------------
0019 
0020 struct ModInfo {
0021     int modQt;
0022     const char *psName;
0023     QString *sLabel; // this struct is used in static objects, so must use a pointer here.
0024 };
0025 
0026 //---------------------------------------------------------------------
0027 // Arrays
0028 //---------------------------------------------------------------------
0029 
0030 // Key names with this context are extracted elsewhere,
0031 // no need for I18N_NOOP2's here.
0032 #define KEYCTXT "keyboard-key-name"
0033 static ModInfo g_rgModInfo[4] = {
0034     {Qt::SHIFT, "Shift", nullptr},
0035     {Qt::CTRL, "Ctrl", nullptr},
0036     {Qt::ALT, "Alt", nullptr},
0037     {Qt::META, "Meta", nullptr},
0038 };
0039 
0040 //---------------------------------------------------------------------
0041 // Initialization
0042 //---------------------------------------------------------------------
0043 static bool g_bInitializedKKeyLabels;
0044 static bool g_bMacLabels;
0045 
0046 static void intializeKKeyLabels()
0047 {
0048     g_rgModInfo[0].sLabel = new QString(QCoreApplication::translate("KKeyServer", (g_rgModInfo[0].psName), KEYCTXT));
0049     g_rgModInfo[1].sLabel = new QString(QCoreApplication::translate("KKeyServer", (g_rgModInfo[1].psName), KEYCTXT));
0050     g_rgModInfo[2].sLabel = new QString(QCoreApplication::translate("KKeyServer", (g_rgModInfo[2].psName), KEYCTXT));
0051     g_rgModInfo[3].sLabel = new QString(QCoreApplication::translate("KKeyServer", (g_rgModInfo[3].psName), KEYCTXT));
0052     g_bMacLabels = (*g_rgModInfo[2].sLabel == QLatin1String("Command"));
0053     g_bInitializedKKeyLabels = true;
0054 }
0055 
0056 //---------------------------------------------------------------------
0057 // Public functions
0058 //---------------------------------------------------------------------
0059 
0060 static QString modToString(uint mod, bool bUserSpace)
0061 {
0062     if (bUserSpace && !g_bInitializedKKeyLabels) {
0063         intializeKKeyLabels();
0064     }
0065 
0066     QString s;
0067     for (int i = 3; i >= 0; i--) {
0068         if (mod & g_rgModInfo[i].modQt) {
0069             if (!s.isEmpty()) {
0070                 s += QLatin1Char('+');
0071             }
0072             s += (bUserSpace) ? *g_rgModInfo[i].sLabel : QLatin1String(g_rgModInfo[i].psName);
0073         }
0074     }
0075     return s;
0076 }
0077 
0078 QString modToStringUser(uint mod)
0079 {
0080     return modToString(mod, true);
0081 }
0082 
0083 uint stringUserToMod(const QString &mod)
0084 {
0085     for (int i = 3; i >= 0; i--) {
0086         if (mod.toLower() == g_rgModInfo[i].sLabel->toLower()) {
0087             return g_rgModInfo[i].modQt;
0088         }
0089     }
0090     return 0;
0091 }
0092 
0093 bool isShiftAsModifierAllowed(int keyQt)
0094 {
0095     // remove any modifiers
0096     keyQt &= ~Qt::KeyboardModifierMask;
0097 
0098     // Shift only works as a modifier with certain keys. It's not possible
0099     // to enter the SHIFT+5 key sequence for me because this is handled as
0100     // '%' by qt on my keyboard.
0101     // The working keys are all hardcoded here :-(
0102     if (keyQt >= Qt::Key_F1 && keyQt <= Qt::Key_F35) {
0103         return true;
0104     }
0105 
0106     // Returns false if not a unicode code point
0107     if (QChar::isLetter(keyQt)) {
0108         return true;
0109     }
0110 
0111     switch (keyQt) {
0112     case Qt::Key_Return:
0113     case Qt::Key_Space:
0114     case Qt::Key_Backspace:
0115     case Qt::Key_Tab:
0116     case Qt::Key_Backtab:
0117     case Qt::Key_Escape:
0118     case Qt::Key_Print:
0119     case Qt::Key_ScrollLock:
0120     case Qt::Key_Pause:
0121     case Qt::Key_PageUp:
0122     case Qt::Key_PageDown:
0123     case Qt::Key_Insert:
0124     case Qt::Key_Delete:
0125     case Qt::Key_Home:
0126     case Qt::Key_End:
0127     case Qt::Key_Up:
0128     case Qt::Key_Down:
0129     case Qt::Key_Left:
0130     case Qt::Key_Right:
0131     case Qt::Key_Enter:
0132     case Qt::Key_SysReq:
0133     case Qt::Key_CapsLock:
0134     case Qt::Key_NumLock:
0135     case Qt::Key_Help:
0136     case Qt::Key_Back:
0137     case Qt::Key_Forward:
0138     case Qt::Key_Stop:
0139     case Qt::Key_Refresh:
0140     case Qt::Key_Favorites:
0141     case Qt::Key_LaunchMedia:
0142     case Qt::Key_OpenUrl:
0143     case Qt::Key_HomePage:
0144     case Qt::Key_Search:
0145     case Qt::Key_VolumeDown:
0146     case Qt::Key_VolumeMute:
0147     case Qt::Key_VolumeUp:
0148     case Qt::Key_BassBoost:
0149     case Qt::Key_BassUp:
0150     case Qt::Key_BassDown:
0151     case Qt::Key_TrebleUp:
0152     case Qt::Key_TrebleDown:
0153     case Qt::Key_MediaPlay:
0154     case Qt::Key_MediaStop:
0155     case Qt::Key_MediaPrevious:
0156     case Qt::Key_MediaNext:
0157     case Qt::Key_MediaRecord:
0158     case Qt::Key_MediaPause:
0159     case Qt::Key_MediaTogglePlayPause:
0160     case Qt::Key_LaunchMail:
0161     case Qt::Key_Calculator:
0162     case Qt::Key_Memo:
0163     case Qt::Key_ToDoList:
0164     case Qt::Key_Calendar:
0165     case Qt::Key_PowerDown:
0166     case Qt::Key_ContrastAdjust:
0167     case Qt::Key_Standby:
0168     case Qt::Key_MonBrightnessUp:
0169     case Qt::Key_MonBrightnessDown:
0170     case Qt::Key_KeyboardLightOnOff:
0171     case Qt::Key_KeyboardBrightnessUp:
0172     case Qt::Key_KeyboardBrightnessDown:
0173     case Qt::Key_PowerOff:
0174     case Qt::Key_WakeUp:
0175     case Qt::Key_Eject:
0176     case Qt::Key_ScreenSaver:
0177     case Qt::Key_WWW:
0178     case Qt::Key_Sleep:
0179     case Qt::Key_LightBulb:
0180     case Qt::Key_Shop:
0181     case Qt::Key_History:
0182     case Qt::Key_AddFavorite:
0183     case Qt::Key_HotLinks:
0184     case Qt::Key_BrightnessAdjust:
0185     case Qt::Key_Finance:
0186     case Qt::Key_Community:
0187     case Qt::Key_AudioRewind:
0188     case Qt::Key_BackForward:
0189     case Qt::Key_ApplicationLeft:
0190     case Qt::Key_ApplicationRight:
0191     case Qt::Key_Book:
0192     case Qt::Key_CD:
0193     case Qt::Key_Clear:
0194     case Qt::Key_ClearGrab:
0195     case Qt::Key_Close:
0196     case Qt::Key_Copy:
0197     case Qt::Key_Cut:
0198     case Qt::Key_Display:
0199     case Qt::Key_DOS:
0200     case Qt::Key_Documents:
0201     case Qt::Key_Excel:
0202     case Qt::Key_Explorer:
0203     case Qt::Key_Game:
0204     case Qt::Key_Go:
0205     case Qt::Key_iTouch:
0206     case Qt::Key_LogOff:
0207     case Qt::Key_Market:
0208     case Qt::Key_Meeting:
0209     case Qt::Key_MenuKB:
0210     case Qt::Key_MenuPB:
0211     case Qt::Key_MySites:
0212     case Qt::Key_News:
0213     case Qt::Key_OfficeHome:
0214     case Qt::Key_Option:
0215     case Qt::Key_Paste:
0216     case Qt::Key_Phone:
0217     case Qt::Key_Reply:
0218     case Qt::Key_Reload:
0219     case Qt::Key_RotateWindows:
0220     case Qt::Key_RotationPB:
0221     case Qt::Key_RotationKB:
0222     case Qt::Key_Save:
0223     case Qt::Key_Send:
0224     case Qt::Key_Spell:
0225     case Qt::Key_SplitScreen:
0226     case Qt::Key_Support:
0227     case Qt::Key_TaskPane:
0228     case Qt::Key_Terminal:
0229     case Qt::Key_Tools:
0230     case Qt::Key_Travel:
0231     case Qt::Key_Video:
0232     case Qt::Key_Word:
0233     case Qt::Key_Xfer:
0234     case Qt::Key_ZoomIn:
0235     case Qt::Key_ZoomOut:
0236     case Qt::Key_Away:
0237     case Qt::Key_Messenger:
0238     case Qt::Key_WebCam:
0239     case Qt::Key_MailForward:
0240     case Qt::Key_Pictures:
0241     case Qt::Key_Music:
0242     case Qt::Key_Battery:
0243     case Qt::Key_Bluetooth:
0244     case Qt::Key_WLAN:
0245     case Qt::Key_UWB:
0246     case Qt::Key_AudioForward:
0247     case Qt::Key_AudioRepeat:
0248     case Qt::Key_AudioRandomPlay:
0249     case Qt::Key_Subtitle:
0250     case Qt::Key_AudioCycleTrack:
0251     case Qt::Key_Time:
0252     case Qt::Key_Select:
0253     case Qt::Key_View:
0254     case Qt::Key_TopMenu:
0255     case Qt::Key_Suspend:
0256     case Qt::Key_Hibernate:
0257     case Qt::Key_Launch0:
0258     case Qt::Key_Launch1:
0259     case Qt::Key_Launch2:
0260     case Qt::Key_Launch3:
0261     case Qt::Key_Launch4:
0262     case Qt::Key_Launch5:
0263     case Qt::Key_Launch6:
0264     case Qt::Key_Launch7:
0265     case Qt::Key_Launch8:
0266     case Qt::Key_Launch9:
0267     case Qt::Key_LaunchA:
0268     case Qt::Key_LaunchB:
0269     case Qt::Key_LaunchC:
0270     case Qt::Key_LaunchD:
0271     case Qt::Key_LaunchE:
0272     case Qt::Key_LaunchF:
0273         return true;
0274 
0275     default:
0276         return false;
0277     }
0278 }
0279 
0280 }