diff --git a/app.c b/app.c index 598ad02..aebaa04 100644 --- a/app.c +++ b/app.c @@ -29,6 +29,7 @@ int appInit(App *app) { XCB_EVENT_MASK_EXPOSURE // event mask | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_BUTTON_PRESS + | XCB_EVENT_MASK_BUTTON_1_MOTION | XCB_EVENT_MASK_BUTTON_RELEASE }; xcb_create_window( app->xcb, // connection @@ -143,6 +144,14 @@ int appRun(App *app) { } break; } + case XCB_MOTION_NOTIFY: { + xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t*)event; + if (e->state & Button1Mask) { + LOGDBG("app: mouse motion: x=%d, y=%d", e->event_x, e->event_y); + keyboardMouseMotion(&app->keyboard, e->event_x, e->event_y); + } + break; + } default: break; } @@ -185,6 +194,7 @@ void appMove(App *app, int x, int y, int w, int h) { uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT; uint32_t values[] = { x, y, w, h }; xcb_configure_window(app->xcb, app->win, mask, values); + xcb_flush(app->xcb); } diff --git a/common.c b/common.c index 0701fbc..0b44fa9 100644 --- a/common.c +++ b/common.c @@ -2,3 +2,342 @@ #include "common.h" + +unsigned int keysymCharcode(unsigned int keySym) { + static const unsigned short codes_01a1_01ff[] = { + 0x0104, 0x02d8, 0x0141, 0x0000, 0x013d, 0x015a, 0x0000, // 0x01a0-0x01a7 + 0x0000, 0x0160, 0x015e, 0x0164, 0x0179, 0x0000, 0x017d, 0x017b, // 0x01a8-0x01af + 0x0000, 0x0105, 0x02db, 0x0142, 0x0000, 0x013e, 0x015b, 0x02c7, // 0x01b0-0x01b7 + 0x0000, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c, // 0x01b8-0x01bf + 0x0154, 0x0000, 0x0000, 0x0102, 0x0000, 0x0139, 0x0106, 0x0000, // 0x01c0-0x01c7 + 0x010c, 0x0000, 0x0118, 0x0000, 0x011a, 0x0000, 0x0000, 0x010e, // 0x01c8-0x01cf + 0x0110, 0x0143, 0x0147, 0x0000, 0x0000, 0x0150, 0x0000, 0x0000, // 0x01d0-0x01d7 + 0x0158, 0x016e, 0x0000, 0x0170, 0x0000, 0x0000, 0x0162, 0x0000, // 0x01d8-0x01df + 0x0155, 0x0000, 0x0000, 0x0103, 0x0000, 0x013a, 0x0107, 0x0000, // 0x01e0-0x01e7 + 0x010d, 0x0000, 0x0119, 0x0000, 0x011b, 0x0000, 0x0000, 0x010f, // 0x01e8-0x01ef + 0x0111, 0x0144, 0x0148, 0x0000, 0x0000, 0x0151, 0x0000, 0x0000, // 0x01f0-0x01f7 + 0x0159, 0x016f, 0x0000, 0x0171, 0x0000, 0x0000, 0x0163, 0x02d9, // 0x01f8-0x01ff + }; + + static const unsigned short codes_02a1_02fe[] = { + 0x0126, 0x0000, 0x0000, 0x0000, 0x0000, 0x0124, 0x0000, // 0x02a0-0x02a7 + 0x0000, 0x0130, 0x0000, 0x011e, 0x0134, 0x0000, 0x0000, 0x0000, // 0x02a8-0x02af + 0x0000, 0x0127, 0x0000, 0x0000, 0x0000, 0x0000, 0x0125, 0x0000, // 0x02b0-0x02b7 + 0x0000, 0x0131, 0x0000, 0x011f, 0x0135, 0x0000, 0x0000, 0x0000, // 0x02b8-0x02bf + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x010a, 0x0108, 0x0000, // 0x02c0-0x02c7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x02c8-0x02cf + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0120, 0x0000, 0x0000, // 0x02d0-0x02d7 + 0x011c, 0x0000, 0x0000, 0x0000, 0x0000, 0x016c, 0x015c, 0x0000, // 0x02d8-0x02df + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x010b, 0x0109, 0x0000, // 0x02e0-0x02e7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x02e8-0x02ef + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0121, 0x0000, 0x0000, // 0x02f0-0x02f7 + 0x011d, 0x0000, 0x0000, 0x0000, 0x0000, 0x016d, 0x015d, // 0x02f8-0x02ff + }; + + static const unsigned short codes_03a2_03fe[] = { + 0x0138, 0x0156, 0x0000, 0x0128, 0x013b, 0x0000, // 0x03a0-0x03a7 + 0x0000, 0x0000, 0x0112, 0x0122, 0x0166, 0x0000, 0x0000, 0x0000, // 0x03a8-0x03af + 0x0000, 0x0000, 0x0000, 0x0157, 0x0000, 0x0129, 0x013c, 0x0000, // 0x03b0-0x03b7 + 0x0000, 0x0000, 0x0113, 0x0123, 0x0167, 0x014a, 0x0000, 0x014b, // 0x03b8-0x03bf + 0x0100, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x012e, // 0x03c0-0x03c7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0116, 0x0000, 0x0000, 0x012a, // 0x03c8-0x03cf + 0x0000, 0x0145, 0x014c, 0x0136, 0x0000, 0x0000, 0x0000, 0x0000, // 0x03d0-0x03d7 + 0x0000, 0x0172, 0x0000, 0x0000, 0x0000, 0x0168, 0x016a, 0x0000, // 0x03d8-0x03df + 0x0101, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x012f, // 0x03e0-0x03e7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0117, 0x0000, 0x0000, 0x012b, // 0x03e8-0x03ef + 0x0000, 0x0146, 0x014d, 0x0137, 0x0000, 0x0000, 0x0000, 0x0000, // 0x03f0-0x03f7 + 0x0000, 0x0173, 0x0000, 0x0000, 0x0000, 0x0169, 0x016b, // 0x03f8-0x03ff + }; + + static const unsigned short codes_04a1_04df[] = { + 0x3002, 0x3008, 0x3009, 0x3001, 0x30fb, 0x30f2, 0x30a1, // 0x04a0-0x04a7 + 0x30a3, 0x30a5, 0x30a7, 0x30a9, 0x30e3, 0x30e5, 0x30e7, 0x30c3, // 0x04a8-0x04af + 0x30fc, 0x30a2, 0x30a4, 0x30a6, 0x30a8, 0x30aa, 0x30ab, 0x30ad, // 0x04b0-0x04b7 + 0x30af, 0x30b1, 0x30b3, 0x30b5, 0x30b7, 0x30b9, 0x30bb, 0x30bd, // 0x04b8-0x04bf + 0x30bf, 0x30c1, 0x30c4, 0x30c6, 0x30c8, 0x30ca, 0x30cb, 0x30cc, // 0x04c0-0x04c7 + 0x30cd, 0x30ce, 0x30cf, 0x30d2, 0x30d5, 0x30d8, 0x30db, 0x30de, // 0x04c8-0x04cf + 0x30df, 0x30e0, 0x30e1, 0x30e2, 0x30e4, 0x30e6, 0x30e8, 0x30e9, // 0x04d0-0x04d7 + 0x30ea, 0x30eb, 0x30ec, 0x30ed, 0x30ef, 0x30f3, 0x309b, 0x309c, // 0x04d8-0x04df + }; + + static const unsigned short codes_0590_05fe[] = { + 0x06f0, 0x06f1, 0x06f2, 0x06f3, 0x06f4, 0x06f5, 0x06f6, 0x06f7, // 0x0590-0x0597 + 0x06f8, 0x06f9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x0598-0x059f + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x066a, 0x0670, 0x0679, // 0x05a0-0x05a7 + + 0x067e, 0x0686, 0x0688, 0x0691, 0x060c, 0x0000, 0x06d4, 0x0000, // 0x05ac-0x05af + 0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667, // 0x05b0-0x05b7 + 0x0668, 0x0669, 0x0000, 0x061b, 0x0000, 0x0000, 0x0000, 0x061f, // 0x05b8-0x05bf + 0x0000, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627, // 0x05c0-0x05c7 + 0x0628, 0x0629, 0x062a, 0x062b, 0x062c, 0x062d, 0x062e, 0x062f, // 0x05c8-0x05cf + 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637, // 0x05d0-0x05d7 + 0x0638, 0x0639, 0x063a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x05d8-0x05df + 0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647, // 0x05e0-0x05e7 + 0x0648, 0x0649, 0x064a, 0x064b, 0x064c, 0x064d, 0x064e, 0x064f, // 0x05e8-0x05ef + 0x0650, 0x0651, 0x0652, 0x0653, 0x0654, 0x0655, 0x0698, 0x06a4, // 0x05f0-0x05f7 + 0x06a9, 0x06af, 0x06ba, 0x06be, 0x06cc, 0x06d2, 0x06c1, // 0x05f8-0x05fe + }; + + static const unsigned short codes_0680_06ff[] = { + 0x0492, 0x0496, 0x049a, 0x049c, 0x04a2, 0x04ae, 0x04b0, 0x04b2, // 0x0680-0x0687 + 0x04b6, 0x04b8, 0x04ba, 0x0000, 0x04d8, 0x04e2, 0x04e8, 0x04ee, // 0x0688-0x068f + 0x0493, 0x0497, 0x049b, 0x049d, 0x04a3, 0x04af, 0x04b1, 0x04b3, // 0x0690-0x0697 + 0x04b7, 0x04b9, 0x04bb, 0x0000, 0x04d9, 0x04e3, 0x04e9, 0x04ef, // 0x0698-0x069f + 0x0000, 0x0452, 0x0453, 0x0451, 0x0454, 0x0455, 0x0456, 0x0457, // 0x06a0-0x06a7 + 0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x0491, 0x045e, 0x045f, // 0x06a8-0x06af + 0x2116, 0x0402, 0x0403, 0x0401, 0x0404, 0x0405, 0x0406, 0x0407, // 0x06b0-0x06b7 + 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x0490, 0x040e, 0x040f, // 0x06b8-0x06bf + 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, // 0x06c0-0x06c7 + 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, // 0x06c8-0x06cf + 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, // 0x06d0-0x06d7 + 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, // 0x06d8-0x06df + 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, // 0x06e0-0x06e7 + 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, // 0x06e8-0x06ef + 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, // 0x06f0-0x06f7 + 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a, // 0x06f8-0x06ff + }; + + static const unsigned short codes_07a1_07f9[] = { + 0x0386, 0x0388, 0x0389, 0x038a, 0x03aa, 0x0000, 0x038c, // 0x07a0-0x07a7 + 0x038e, 0x03ab, 0x0000, 0x038f, 0x0000, 0x0000, 0x0385, 0x2015, // 0x07a8-0x07af + 0x0000, 0x03ac, 0x03ad, 0x03ae, 0x03af, 0x03ca, 0x0390, 0x03cc, // 0x07b0-0x07b7 + 0x03cd, 0x03cb, 0x03b0, 0x03ce, 0x0000, 0x0000, 0x0000, 0x0000, // 0x07b8-0x07bf + 0x0000, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, // 0x07c0-0x07c7 + 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, // 0x07c8-0x07cf + 0x03a0, 0x03a1, 0x03a3, 0x0000, 0x03a4, 0x03a5, 0x03a6, 0x03a7, // 0x07d0-0x07d7 + 0x03a8, 0x03a9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x07d8-0x07df + 0x0000, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, // 0x07e0-0x07e7 + 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, // 0x07e8-0x07ef + 0x03c0, 0x03c1, 0x03c3, 0x03c2, 0x03c4, 0x03c5, 0x03c6, 0x03c7, // 0x07f0-0x07f7 + 0x03c8, 0x03c9, // 0x07f8-0x07ff + }; + + static const unsigned short codes_08a4_08fe[] = { + 0x2320, 0x2321, 0x0000, 0x231c, // 0x08a0-0x08a7 + 0x231d, 0x231e, 0x231f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x08a8-0x08af + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x08b0-0x08b7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x2264, 0x2260, 0x2265, 0x222b, // 0x08b8-0x08bf + 0x2234, 0x0000, 0x221e, 0x0000, 0x0000, 0x2207, 0x0000, 0x0000, // 0x08c0-0x08c7 + 0x2245, 0x2246, 0x0000, 0x0000, 0x0000, 0x0000, 0x21d2, 0x0000, // 0x08c8-0x08cf + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x221a, 0x0000, // 0x08d0-0x08d7 + 0x0000, 0x0000, 0x2282, 0x2283, 0x2229, 0x222a, 0x2227, 0x2228, // 0x08d8-0x08df + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x08e0-0x08e7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x2202, // 0x08e8-0x08ef + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0192, 0x0000, // 0x08f0-0x08f7 + 0x0000, 0x0000, 0x0000, 0x2190, 0x2191, 0x2192, 0x2193, // 0x08f8-0x08ff + }; + + static const unsigned short codes_09df_09f8[] = { + 0x2422, // 0x09d8-0x09df + 0x2666, 0x25a6, 0x2409, 0x240c, 0x240d, 0x240a, 0x0000, 0x0000, // 0x09e0-0x09e7 + 0x240a, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x2500, // 0x09e8-0x09ef + 0x0000, 0x0000, 0x0000, 0x0000, 0x251c, 0x2524, 0x2534, 0x252c, // 0x09f0-0x09f7 + 0x2502, // 0x09f8-0x09ff + }; + + static const unsigned short codes_0aa1_0afe[] = { + 0x2003, 0x2002, 0x2004, 0x2005, 0x2007, 0x2008, 0x2009, // 0x0aa0-0x0aa7 + 0x200a, 0x2014, 0x2013, 0x0000, 0x0000, 0x0000, 0x2026, 0x2025, // 0x0aa8-0x0aaf + 0x2153, 0x2154, 0x2155, 0x2156, 0x2157, 0x2158, 0x2159, 0x215a, // 0x0ab0-0x0ab7 + 0x2105, 0x0000, 0x0000, 0x2012, 0x2039, 0x2024, 0x203a, 0x0000, // 0x0ab8-0x0abf + 0x0000, 0x0000, 0x0000, 0x215b, 0x215c, 0x215d, 0x215e, 0x0000, // 0x0ac0-0x0ac7 + 0x0000, 0x2122, 0x2120, 0x0000, 0x25c1, 0x25b7, 0x25cb, 0x25ad, // 0x0ac8-0x0acf + 0x2018, 0x2019, 0x201c, 0x201d, 0x211e, 0x2030, 0x2032, 0x2033, // 0x0ad0-0x0ad7 + 0x0000, 0x271d, 0x0000, 0x220e, 0x25c2, 0x2023, 0x25cf, 0x25ac, // 0x0ad8-0x0adf + 0x25e6, 0x25ab, 0x25ae, 0x25b5, 0x25bf, 0x2606, 0x2022, 0x25aa, // 0x0ae0-0x0ae7 + 0x25b4, 0x25be, 0x261a, 0x261b, 0x2663, 0x2666, 0x2665, 0x0000, // 0x0ae8-0x0aef + 0x2720, 0x2020, 0x2021, 0x2713, 0x2612, 0x266f, 0x266d, 0x2642, // 0x0af0-0x0af7 + 0x2640, 0x2121, 0x2315, 0x2117, 0x2038, 0x201a, 0x201e, // 0x0af8-0x0aff + }; + + static const unsigned short codes_0cdf_0cfa[] = { + 0x2017, // 0x0cd8-0x0cdf + 0x05d0, 0x05d1, 0x05d2, 0x05d3, 0x05d4, 0x05d5, 0x05d6, 0x05d7, // 0x0ce0-0x0ce7 + 0x05d8, 0x05d9, 0x05da, 0x05db, 0x05dc, 0x05dd, 0x05de, 0x05df, // 0x0ce8-0x0cef + 0x05e0, 0x05e1, 0x05e2, 0x05e3, 0x05e4, 0x05e5, 0x05e6, 0x05e7, // 0x0cf0-0x0cf7 + 0x05e8, 0x05e9, 0x05ea, // 0x0cf8-0x0cff + }; + + static const unsigned short codes_0da1_0df9[] = { + 0x0e01, 0x0e02, 0x0e03, 0x0e04, 0x0e05, 0x0e06, 0x0e07, // 0x0da0-0x0da7 + 0x0e08, 0x0e09, 0x0e0a, 0x0e0b, 0x0e0c, 0x0e0d, 0x0e0e, 0x0e0f, // 0x0da8-0x0daf + 0x0e10, 0x0e11, 0x0e12, 0x0e13, 0x0e14, 0x0e15, 0x0e16, 0x0e17, // 0x0db0-0x0db7 + 0x0e18, 0x0e19, 0x0e1a, 0x0e1b, 0x0e1c, 0x0e1d, 0x0e1e, 0x0e1f, // 0x0db8-0x0dbf + 0x0e20, 0x0e21, 0x0e22, 0x0e23, 0x0e24, 0x0e25, 0x0e26, 0x0e27, // 0x0dc0-0x0dc7 + 0x0e28, 0x0e29, 0x0e2a, 0x0e2b, 0x0e2c, 0x0e2d, 0x0e2e, 0x0e2f, // 0x0dc8-0x0dcf + 0x0e30, 0x0e31, 0x0e32, 0x0e33, 0x0e34, 0x0e35, 0x0e36, 0x0e37, // 0x0dd0-0x0dd7 + 0x0e38, 0x0e39, 0x0e3a, 0x0000, 0x0000, 0x0000, 0x0e3e, 0x0e3f, // 0x0dd8-0x0ddf + 0x0e40, 0x0e41, 0x0e42, 0x0e43, 0x0e44, 0x0e45, 0x0e46, 0x0e47, // 0x0de0-0x0de7 + 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x0e4d, 0x0000, 0x0000, // 0x0de8-0x0def + 0x0e50, 0x0e51, 0x0e52, 0x0e53, 0x0e54, 0x0e55, 0x0e56, 0x0e57, // 0x0df0-0x0df7 + 0x0e58, 0x0e59, // 0x0df8-0x0dff + }; + + static const unsigned short codes_0ea0_0eff[] = { + 0x0000, 0x1101, 0x1101, 0x11aa, 0x1102, 0x11ac, 0x11ad, 0x1103, // 0x0ea0-0x0ea7 + 0x1104, 0x1105, 0x11b0, 0x11b1, 0x11b2, 0x11b3, 0x11b4, 0x11b5, // 0x0ea8-0x0eaf + 0x11b6, 0x1106, 0x1107, 0x1108, 0x11b9, 0x1109, 0x110a, 0x110b, // 0x0eb0-0x0eb7 + 0x110c, 0x110d, 0x110e, 0x110f, 0x1110, 0x1111, 0x1112, 0x1161, // 0x0eb8-0x0ebf + 0x1162, 0x1163, 0x1164, 0x1165, 0x1166, 0x1167, 0x1168, 0x1169, // 0x0ec0-0x0ec7 + 0x116a, 0x116b, 0x116c, 0x116d, 0x116e, 0x116f, 0x1170, 0x1171, // 0x0ec8-0x0ecf + 0x1172, 0x1173, 0x1174, 0x1175, 0x11a8, 0x11a9, 0x11aa, 0x11ab, // 0x0ed0-0x0ed7 + 0x11ac, 0x11ad, 0x11ae, 0x11af, 0x11b0, 0x11b1, 0x11b2, 0x11b3, // 0x0ed8-0x0edf + 0x11b4, 0x11b5, 0x11b6, 0x11b7, 0x11b8, 0x11b9, 0x11ba, 0x11bb, // 0x0ee0-0x0ee7 + 0x11bc, 0x11bd, 0x11be, 0x11bf, 0x11c0, 0x11c1, 0x11c2, 0x0000, // 0x0ee8-0x0eef + 0x0000, 0x0000, 0x1140, 0x0000, 0x0000, 0x1159, 0x119e, 0x0000, // 0x0ef0-0x0ef7 + 0x11eb, 0x0000, 0x11f9, 0x0000, 0x0000, 0x0000, 0x0000, 0x20a9, // 0x0ef8-0x0eff + }; + + static const unsigned short codes_12a1_12fe[] = { + 0x1e02, 0x1e03, 0x0000, 0x0000, 0x0000, 0x1e0a, 0x0000, // 0x12a0-0x12a7 + 0x1e80, 0x0000, 0x1e82, 0x1e0b, 0x1ef2, 0x0000, 0x0000, 0x0000, // 0x12a8-0x12af + 0x1e1e, 0x1e1f, 0x0000, 0x0000, 0x1e40, 0x1e41, 0x0000, 0x1e56, // 0x12b0-0x12b7 + 0x1e81, 0x1e57, 0x1e83, 0x1e60, 0x1ef3, 0x1e84, 0x1e85, 0x1e61, // 0x12b8-0x12bf + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x12c0-0x12c7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x12c8-0x12cf + 0x0174, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1e6a, // 0x12d0-0x12d7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0176, 0x0000, // 0x12d8-0x12df + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x12e0-0x12e7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x12e8-0x12ef + 0x0175, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1e6b, // 0x12f0-0x12f7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0177, // 0x12f0-0x12ff + }; + + static const unsigned short codes_13bc_13be[] = { + 0x0152, 0x0153, 0x0178, // 0x13b8-0x13bf + }; + + static const unsigned short codes_14a1_14ff[] = { + 0x2741, 0x00a7, 0x0589, 0x0029, 0x0028, 0x00bb, 0x00ab, // 0x14a0-0x14a7 + 0x2014, 0x002e, 0x055d, 0x002c, 0x2013, 0x058a, 0x2026, 0x055c, // 0x14a8-0x14af + 0x055b, 0x055e, 0x0531, 0x0561, 0x0532, 0x0562, 0x0533, 0x0563, // 0x14b0-0x14b7 + 0x0534, 0x0564, 0x0535, 0x0565, 0x0536, 0x0566, 0x0537, 0x0567, // 0x14b8-0x14bf + 0x0538, 0x0568, 0x0539, 0x0569, 0x053a, 0x056a, 0x053b, 0x056b, // 0x14c0-0x14c7 + 0x053c, 0x056c, 0x053d, 0x056d, 0x053e, 0x056e, 0x053f, 0x056f, // 0x14c8-0x14cf + 0x0540, 0x0570, 0x0541, 0x0571, 0x0542, 0x0572, 0x0543, 0x0573, // 0x14d0-0x14d7 + 0x0544, 0x0574, 0x0545, 0x0575, 0x0546, 0x0576, 0x0547, 0x0577, // 0x14d8-0x14df + 0x0548, 0x0578, 0x0549, 0x0579, 0x054a, 0x057a, 0x054b, 0x057b, // 0x14e0-0x14e7 + 0x054c, 0x057c, 0x054d, 0x057d, 0x054e, 0x057e, 0x054f, 0x057f, // 0x14e8-0x14ef + 0x0550, 0x0580, 0x0551, 0x0581, 0x0552, 0x0582, 0x0553, 0x0583, // 0x14f0-0x14f7 + 0x0554, 0x0584, 0x0555, 0x0585, 0x0556, 0x0586, 0x2019, 0x0027, // 0x14f8-0x14ff + }; + + static const unsigned short codes_15d0_15f6[] = { + 0x10d0, 0x10d1, 0x10d2, 0x10d3, 0x10d4, 0x10d5, 0x10d6, 0x10d7, // 0x15d0-0x15d7 + 0x10d8, 0x10d9, 0x10da, 0x10db, 0x10dc, 0x10dd, 0x10de, 0x10df, // 0x15d8-0x15df + 0x10e0, 0x10e1, 0x10e2, 0x10e3, 0x10e4, 0x10e5, 0x10e6, 0x10e7, // 0x15e0-0x15e7 + 0x10e8, 0x10e9, 0x10ea, 0x10eb, 0x10ec, 0x10ed, 0x10ee, 0x10ef, // 0x15e8-0x15ef + 0x10f0, 0x10f1, 0x10f2, 0x10f3, 0x10f4, 0x10f5, 0x10f6, // 0x15f0-0x15f7 + }; + + static const unsigned short codes_16a0_16f6[] = { + 0x0000, 0x0000, 0xf0a2, 0x1e8a, 0x0000, 0xf0a5, 0x012c, 0xf0a7, // 0x16a0-0x16a7 + 0xf0a8, 0x01b5, 0x01e6, 0x0000, 0x0000, 0x0000, 0x0000, 0x019f, // 0x16a8-0x16af + 0x0000, 0x0000, 0xf0b2, 0x1e8b, 0x01d1, 0xf0b5, 0x012d, 0xf0b7, // 0x16b0-0x16b7 + 0xf0b8, 0x01b6, 0x01e7, 0x0000, 0x0000, 0x01d2, 0x0000, 0x0275, // 0x16b8-0x16bf + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x018f, 0x0000, // 0x16c0-0x16c7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x16c8-0x16cf + 0x0000, 0x1e36, 0xf0d2, 0xf0d3, 0x0000, 0x0000, 0x0000, 0x0000, // 0x16d0-0x16d7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x16d8-0x16df + 0x0000, 0x1e37, 0xf0e2, 0xf0e3, 0x0000, 0x0000, 0x0000, 0x0000, // 0x16e0-0x16e7 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 0x16e8-0x16ef + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0259, // 0x16f0-0x16f6 + }; + + static const unsigned short codes_1e9f_1eff[] = { + 0x0303, + 0x1ea0, 0x1ea1, 0x1ea2, 0x1ea3, 0x1ea4, 0x1ea5, 0x1ea6, 0x1ea7, // 0x1ea0-0x1ea7 + 0x1ea8, 0x1ea9, 0x1eaa, 0x1eab, 0x1eac, 0x1ead, 0x1eae, 0x1eaf, // 0x1ea8-0x1eaf + 0x1eb0, 0x1eb1, 0x1eb2, 0x1eb3, 0x1eb4, 0x1eb5, 0x1eb6, 0x1eb7, // 0x1eb0-0x1eb7 + 0x1eb8, 0x1eb9, 0x1eba, 0x1ebb, 0x1ebc, 0x1ebd, 0x1ebe, 0x1ebf, // 0x1eb8-0x1ebf + 0x1ec0, 0x1ec1, 0x1ec2, 0x1ec3, 0x1ec4, 0x1ec5, 0x1ec6, 0x1ec7, // 0x1ec0-0x1ec7 + 0x1ec8, 0x1ec9, 0x1eca, 0x1ecb, 0x1ecc, 0x1ecd, 0x1ece, 0x1ecf, // 0x1ec8-0x1ecf + 0x1ed0, 0x1ed1, 0x1ed2, 0x1ed3, 0x1ed4, 0x1ed5, 0x1ed6, 0x1ed7, // 0x1ed0-0x1ed7 + 0x1ed8, 0x1ed9, 0x1eda, 0x1edb, 0x1edc, 0x1edd, 0x1ede, 0x1edf, // 0x1ed8-0x1edf + 0x1ee0, 0x1ee1, 0x1ee2, 0x1ee3, 0x1ee4, 0x1ee5, 0x1ee6, 0x1ee7, // 0x1ee0-0x1ee7 + 0x1ee8, 0x1ee9, 0x1eea, 0x1eeb, 0x1eec, 0x1eed, 0x1eee, 0x1eef, // 0x1ee8-0x1eef + 0x1ef0, 0x1ef1, 0x0300, 0x0301, 0x1ef4, 0x1ef5, 0x1ef6, 0x1ef7, // 0x1ef0-0x1ef7 + 0x1ef8, 0x1ef9, 0x01a0, 0x01a1, 0x01af, 0x01b0, 0x0309, 0x0323, // 0x1ef8-0x1eff + }; + + static const unsigned short codes_20a0_20ac[] = { + 0x20a0, 0x20a1, 0x20a2, 0x20a3, 0x20a4, 0x20a5, 0x20a6, 0x20a7, // 0x20a0-0x20a7 + 0x20a8, 0x20a9, 0x20aa, 0x20ab, 0x20ac, // 0x20a8-0x20af + }; + + static const struct { + const unsigned int a, b; + const unsigned short *p; + } codes[] = { + { 0x01a1, 0x01ff, codes_01a1_01ff }, + { 0x02a1, 0x02fe, codes_02a1_02fe }, + { 0x03a2, 0x03fe, codes_03a2_03fe }, + { 0x04a1, 0x04df, codes_04a1_04df }, + { 0x0590, 0x05fe, codes_0590_05fe }, + { 0x0680, 0x06ff, codes_0680_06ff }, + { 0x07a1, 0x07f9, codes_07a1_07f9 }, + { 0x08a4, 0x08fe, codes_08a4_08fe }, + { 0x09df, 0x09f8, codes_09df_09f8 }, + { 0x0aa1, 0x0afe, codes_0aa1_0afe }, + { 0x0cdf, 0x0cfa, codes_0cdf_0cfa }, + { 0x0da1, 0x0df9, codes_0da1_0df9 }, + { 0x0ea0, 0x0eff, codes_0ea0_0eff }, + { 0x12a1, 0x12fe, codes_12a1_12fe }, + { 0x13bc, 0x13be, codes_13bc_13be }, + { 0x14a1, 0x14ff, codes_14a1_14ff }, + { 0x15d0, 0x15f6, codes_15d0_15f6 }, + { 0x16a0, 0x16f6, codes_16a0_16f6 }, + { 0x1e9f, 0x1eff, codes_1e9f_1eff }, + { 0x20a0, 0x20ac, codes_20a0_20ac }, + }; + + static const int count = COUNTOF(codes); + + if (keySym < 0x100) + return keySym; + if ((keySym & 0xff000000) == 0x01000000) + return keySym & 0x00ffffff; + for(int i = 0; i < count; ++i) + if (codes[i].a <= keySym && keySym <= codes[i].b) + return codes[i].p[keySym - codes[i].a]; + return 0; +} + + + +int keysymString(unsigned int keySym, char *buf5bytes) { + unsigned int uc = keysymCharcode(keySym); + if (uc <= 0x000000) { + buf5bytes[0] = 0; + return 0; + } + if (uc <= 0x00007f) { + buf5bytes[0] = uc; + buf5bytes[1] = 0; + return 1; + } + if (uc <= 0x0007ff) { + buf5bytes[0] = 0x80 | (uc & 0x6f); + buf5bytes[1] = 0xc0 | (uc >> 6); + buf5bytes[2] = 0; + return 2; + } + if (uc <= 0x00ffff) { + buf5bytes[0] = 0x80 | (uc & 0x6f); + buf5bytes[1] = 0x80 | ((uc >> 6) & 0x6f); + buf5bytes[2] = 0xe0 | (uc >> 12); + buf5bytes[3] = 0; + return 3; + } + if (uc <= 0x10ffff) { + buf5bytes[0] = 0x80 | (uc & 0x6f); + buf5bytes[1] = 0x80 | ((uc >> 6) & 0x6f); + buf5bytes[2] = 0x80 | ((uc >> 12) & 0x6f); + buf5bytes[3] = 0xf0 | (uc >> 18); + buf5bytes[4] = 0; + return 4; + } + buf5bytes[0] = 0; + return 0; +} + diff --git a/common.h b/common.h index 41992a2..d7f7f81 100644 --- a/common.h +++ b/common.h @@ -36,6 +36,10 @@ typedef struct App App; static inline int dummy(int i) { return i; } +unsigned int keysymCharcode(unsigned int keySym); +int keysymString(unsigned int keySym, char *buf5bytes); + + static inline void rectIntersect(int *x, int *y, int *w, int *h, int xx, int yy, int ww, int hh) { if (*x < xx) *w += *x - xx, *x = xx; if (*y < yy) *h += *y - yy, *y = xx; diff --git a/graph.c b/graph.c index 82f919a..76bdc48 100644 --- a/graph.c +++ b/graph.c @@ -37,9 +37,14 @@ int graphInit(Graph *g, App *app) { // associate palette with the window xcb_change_window_attributes(g->app->xcb, g->app->win, XCB_CW_COLORMAP, &g->cm); + // init font + const char *fontname = "sans"; + g->font = xcb_generate_id(g->app->xcb); + xcb_open_font(g->app->xcb, g->font, strlen(fontname), fontname); + // create contexts - uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES; - uint32_t values[3] = {0, 0, 0}; + uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT | XCB_GC_GRAPHICS_EXPOSURES; + uint32_t values[3] = {0, 0, g->font, 0}; for(int i = 0; i < GS_COUNT; ++i) { values[0] = colors[i*2 + 1]; // foreground values[1] = colors[i*2]; // background @@ -55,6 +60,7 @@ void graphDeinit(Graph *g) { LOGDBG("graph: deinit"); for(int i = 0; i < GS_COUNT; ++i) xcb_free_gc(g->app->xcb, g->gc[i]); + xcb_close_font(g->app->xcb, g->font); xcb_change_window_attributes(g->app->xcb, g->app->win, XCB_CW_COLORMAP, &g->app->screen->default_colormap); xcb_free_colormap(g->app->xcb, g->cm); } @@ -81,6 +87,7 @@ void graphDrawButton(Graph *g, int x, int y, int w, int h, int active) { void textLayoutInit(TextLayout *tl, Graph *g, const char *text) { tl->g = g; tl->text = text; + xcb_query_text_extents(g->app->xcb, g->font, strlen(text), text); } void textLayoutDeinit(TextLayout *tl) diff --git a/graph.h b/graph.h index fe46904..fe5f084 100644 --- a/graph.h +++ b/graph.h @@ -18,6 +18,7 @@ enum { typedef struct Graph { App *app; xcb_colormap_t cm; + xcb_font_t font; xcb_gcontext_t gc[GS_COUNT]; } Graph; diff --git a/input.c b/input.c index 5d142ba..0eeea83 100644 --- a/input.c +++ b/input.c @@ -8,6 +8,57 @@ #include + +// prepare and shoose keys by algorithm from here: +// https://www.x.org/releases/current/doc/libX11/libX11/libX11.html#Manipulating_the_Keyboard_Encoding + +void inputPrepareKeysyms(unsigned int *ks0, unsigned int *ks1, int *isLetter, int *isKeypad) { + if (!*ks0) *ks0 = *ks1; + + *isKeypad = (0xFF80 <= *ks1 && *ks1 <= 0xFFBD) || (0x11000000 <= *ks1 && *ks1 <= 0x1100FFFF); + *isLetter = 0; + if (*ks0) { + KeySym kl = 0, ku = 0; + XConvertCase(*ks0, &kl, &ku); + if (*ks0 == kl && kl != ku && ku) { + if (!*ks1) *ks1 = ku; + *isLetter = 1; + } + } + + if (!*ks1) *ks1 = *ks0; +} + + +int inputChooseKeysym(unsigned int modifiers, unsigned int ks0, unsigned int ks1, int isLetter, int isKeypad) { + if (!ks1) return 0; + if (!ks0) return 1; + + // normalize bits to zero or one only + isLetter = !!isLetter; + isKeypad = !!isKeypad; + int shift = !!(modifiers & IM_SHIFT_BIT); + int capsLock = !!(modifiers & IM_CAPSLOCK_BIT); + int shiftLock = !!(modifiers & IM_SHIFTLOCK_BIT); + int numLock = !!(modifiers & IM_NUMLOCK_BIT); + + if (capsLock) // capsLock priority + shiftLock = 0; + if (numLock && isKeypad) + return shift || shiftLock; + if (!shift && !capsLock && !shiftLock) + return 0; + if (!shift && capsLock) + return !!isLetter; + if (shift && capsLock) + return !isLetter; + return shift || shiftLock; +} + + + + + int inputInit(Input *in, App *app) { LOGDBG("input: init"); @@ -15,6 +66,7 @@ int inputInit(Input *in, App *app) { in->key0 = in->key1 = in->mapKey = 0; in->mapDown = 0; in->updated = 0; + in->modifiers = 0; memset(in->keys, 0, sizeof(in->keys)); memset(in->masks, 0, sizeof(in->masks)); @@ -31,6 +83,7 @@ int inputInit(Input *in, App *app) { LOGDBG("input: init: keycode range [%d, %d)", in->key0, in->key1); inputUpdateLayout(in); + inputUpdateModifiers(in); return 1; } @@ -105,72 +158,29 @@ void inputUpdateLayout(Input *in) { mcnt > 1 ? ks[1] : 0, mcnt > 2 ? ks[2] : 0, mcnt > 3 ? ks[3] : 0 }; - if (!k[2] && !k[3]) k[2] = k[0], k[3] = k[1]; - int isKeypad[2] = { - (0xFF80 <= k[1] && k[1] <= 0xFFBD) || (0x11000000 <= k[1] && k[1] <= 0x1100FFFF), - (0xFF80 <= k[3] && k[3] <= 0xFFBD) || (0x11000000 <= k[3] && k[3] <= 0x1100FFFF) }; - int isLetter[2] = {}; - for(int i = 0; i < 2; ++i) { - if (k[i*2]) { - KeySym kl = 0, ku = 0; - XConvertCase(k[i*2], &kl, &ku); - if (k[i*2] == kl && kl != ku && ku) - k[i*3+1] = ku, isLetter[i] = 1; - } - } - - if (!k[1]) k[1] = k[0]; - if (!k[3]) k[3] = k[2]; - - // fill keys by algorithm from here: - // https://www.x.org/releases/current/doc/libX11/libX11/libX11.html#Manipulating_the_Keyboard_Encoding - for(int j = 0; j < (1 << IM_COUNT); ++j) { - int shift = !!(j & (1 << IM_SHIFT )); - int capsLock = !!(j & (1 << IM_CAPSLOCK )); - int shiftLock = !!(j & (1 << IM_SHIFTLOCK)); - int numLock = !!(j & (1 << IM_NUMLOCK )); - int group = !!(j & (1 << IM_GROUP )); - - if (capsLock) shiftLock = 0; - - unsigned int k0 = k[group*2]; - unsigned int k1 = k[group*2 + 1]; - - if (numLock && isKeypad[group]) { - in->keys[i][j] = shift || shiftLock ? k1 : k0; - continue; - } - - if (!shift && !capsLock && !shiftLock) { - in->keys[i][j] = k0; - continue; - } - - if (!shift && capsLock) { - in->keys[i][j] = isLetter[group] ? k1 : k0; - continue; - } - - if (shift && capsLock) { - in->keys[i][j] = isLetter[group] ? k0 : k1; - continue; - } - - if (shift || shiftLock) { - in->keys[i][j] = k1; - continue; - } - - in->keys[i][j] = k0; + int isKeypad[2] = {}; + inputPrepareKeysyms(&k[0], &k[1], &isLetter[0], &isKeypad[0]); + + // fill key modes + for(unsigned int modifiers = 0; modifiers < (1 << IM_COUNT); ++modifiers) { + int g = !!(modifiers & IM_GROUP_BIT); + int ki = inputChooseKeysym(modifiers, k[g*2], k[g*2 + 1], isLetter[g], isKeypad[g]); + in->keys[i][modifiers] = k[g*2 + ki]; } } - // force shift mask to first modifier - in->masks[IM_SHIFT] = 1; + // Shift modifier index must be 0 + in->masks[IM_SHIFT] = 1 << 0; + // when caps and shift locks in the same modifier - this modifier is CapsLock + in->masks[IM_SHIFTLOCK] &= ~in->masks[IM_CAPSLOCK]; + // Lock modifier index must be 1 + in->masks[IM_CAPSLOCK] &= 1 << 1; + in->masks[IM_SHIFTLOCK] &= 1 << 1; + XFreeModifiermap(mods); for(int i = 0; i < IM_COUNT; ++i) @@ -178,23 +188,26 @@ void inputUpdateLayout(Input *in) { } +void inputUpdateModifiers(Input *in) { + LOGDBG("input: update modifiers"); + XkbStateRec state = {}; + XkbGetState(in->dpy, XkbUseCoreKbd, &state); + in->modifiers = 0; + for(int i = 0; i < IM_COUNT; ++i) + if (in->masks[i] & state.mods) + in->modifiers |= (1 << i); + LOGDBG("input: update modifiers: current state %08x, chosen mod %02x", state.mods, in->modifiers); +} + + int inputKeycode(Input *in, unsigned int keySym) { LOGDBG("input: keycode: keySym=%u", keySym); if (!keySym) return 0; - - XkbStateRec state = {}; - XkbGetState(in->dpy, XkbUseCoreKbd, &state); - int mod = 0; - for(int i = 0; i < IM_COUNT; ++i) - if (in->masks[i] & state.mods) - mod |= (1 << i); - - LOGDBG("input: keycode: current state %08x, chosen mod %04x", (unsigned int)state.mods, (unsigned int)mod); - + for(int i = in->key0; i < in->key1; ++i) { - if (in->keys[i][mod] == keySym) { + if (in->keys[i][in->modifiers] == keySym) { LOGDBG("input: keycode: found keycode %d", i); return i; } @@ -234,4 +247,3 @@ void inputEvent(Input *in, int keycode, int press) { XSync(in->dpy, False); } - diff --git a/input.h b/input.h index 5f74c12..22635fb 100644 --- a/input.h +++ b/input.h @@ -19,6 +19,13 @@ enum { IM_GROUP, IM_COUNT }; +enum { + IM_SHIFT_BIT = 1 << IM_SHIFT, + IM_CAPSLOCK_BIT = 1 << IM_CAPSLOCK, + IM_SHIFTLOCK_BIT = 1 << IM_SHIFTLOCK, + IM_NUMLOCK_BIT = 1 << IM_NUMLOCK, + IM_GROUP_BIT = 1 << IM_GROUP }; + typedef struct Input { App *app; @@ -30,15 +37,20 @@ typedef struct Input { int mapKey; int mapDown; int updated; + unsigned int modifiers; } Input; +void inputPrepareKeysyms(unsigned int *ks0, unsigned int *ks1, int *isLetter, int *isKeypad); +int inputChooseKeysym(unsigned int modifiers, unsigned int ks0, unsigned int ks1, int isLetter, int isKeypad); + int inputInit(Input *in, App *app); void inputDeinit(Input *in); - void inputUpdateLayout(Input *in); +void inputUpdateModifiers(Input *in); int inputKeycode(Input *in, unsigned int keySym); void inputEvent(Input *in, int keycode, int press); + #endif diff --git a/keyboard.c b/keyboard.c index ace60ad..3a60592 100644 --- a/keyboard.c +++ b/keyboard.c @@ -8,7 +8,12 @@ int keyInit(Key *k, Layout *l) { k->down = 0; k->downKeycode = 0; k->mx = k->my = 0; - + k->isLetter = k->isKeypad = 0; + + inputPrepareKeysyms(&k->keySym, &k->keySym2, &k->isLetter, &k->isKeypad); + if (!k->label[0]) keysymString(k->keySym, k->label); + if (!k->label2[0]) keysymString(k->keySym2, k->label2); + textLayoutInit(&k->tl, &k->l->kbd->app->graph, k->label); textLayoutInit(&k->tl, &k->l->kbd->app->graph, k->label2); @@ -27,25 +32,24 @@ void keyDraw(Key *k, int cx, int cy, int cw, int ch) { if ( k->x + k->w <= cx || k->x >= cx + cw || k->y + k->h <= cy || k->y >= cy + ch ) return; - int active = (k->flags & KF_CAPS) ? k->l->kbd->capslock : k->down; + App *app = k->l->kbd->app; + + int active = k->down; + if (k->flags & KF_MOD) + active = (app->input.modifiers & (unsigned int)k->optValue); graphDrawButton(&k->l->kbd->app->graph, k->x, k->y, k->w, k->h, active); TextLayout *tl = &k->tl; TextLayout *tl2 = &k->tl2; - if (k->label2) { - int sw = k->l->kbd->shiftsDown; - if (!(k->flags & KF_DOUBLE) && k->l->kbd->capslock) - sw = !sw; - if (sw) - tl = tl2, tl2 = &k->tl; - } + if (inputChooseKeysym(app->input.modifiers, k->keySym, k->keySym2, k->isLetter, k->isKeypad)) + tl = tl2, tl2 = &k->tl; // swap labels int lx = k->x + k->w/2; int ly = k->y + k->h/2; textLayoutDraw(tl, lx, ly, active); - if (k->flags & KF_DOUBLE) { + if (!k->isLetter) { int d = k->h/4; lx = k->x + k->w - d; ly = k->y + d; @@ -60,18 +64,16 @@ void keyDown(Key *k, int x, int y) { Keyboard *kbd = k->l->kbd; k->down = 1; - k->mx = x - k->x; - k->my = y - k->y; - // handle special keys - if (k->flags & KF_SHIFT) { - if (!kbd->shiftsDown) appInvalidateRect(kbd->app, 0, 0, kbd->w, kbd->h); - ++kbd->shiftsDown; - } - if ((k->flags & KF_CAPS) && !kbd->capslock) { - kbd->capslock = 1; - appInvalidateRect(kbd->app, 0, 0, kbd->w, kbd->h); + if (k->flags & KF_MOVE) { + k->mx = x; + k->my = y; + } else + if (k->flags & KF_SIZE) { + k->mx = kbd->app->w - x; + k->my = kbd->app->h - y; } + if (k->flags & KF_HOLD) { k->down = 2; // insert to held list @@ -83,18 +85,10 @@ void keyDown(Key *k, int x, int y) { } // choose keySym - int shift = kbd->shiftsDown; - int caps = kbd->capslock; - unsigned int keySym = k->keySym; - if (k->keySym2) { - int sw = shift; - if (!(k->flags & KF_DOUBLE) && caps) - sw = !sw; - if (sw) - keySym = k->keySym2; - } + unsigned int keySym = inputChooseKeysym(kbd->app->input.modifiers, k->keySym, k->keySym2, k->isLetter, k->isKeypad) + ? k->keySym2 : k->keySym; - // press key + // press if (keySym) { k->downKeycode = inputKeycode(&kbd->app->input, keySym); if (k->downKeycode) inputEvent(&kbd->app->input, k->downKeycode, 1); @@ -107,13 +101,11 @@ void keyDown(Key *k, int x, int y) { void keyMotion(Key *k, int x, int y) { App *app = k->l->kbd->app; - int dx = x - k->x - k->mx; - int dy = y - k->y - k->my; if (k->flags & KF_MOVE) { - appMove(app, app->x + dx, app->y + dy, app->w, app->h); + appMove(app, app->x + x - k->mx, app->y + y - k->my, app->w, app->h); } else if (k->flags & KF_SIZE) { - appMove(app, app->x, app->y, app->w + dx, app->h + dy); + appMove(app, app->x, app->y, x + k->mx, y + k->my); } } @@ -125,17 +117,6 @@ void keyUp(Key *k, int force) { int wasDown = k->down; k->down = 0; - // handle special keys - if (wasDown && (k->flags & KF_SHIFT)) { - --kbd->shiftsDown; - if (!kbd->shiftsDown) appInvalidateRect(kbd->app, 0, 0, kbd->w, kbd->h); - } - if ((k->flags & KF_CAPS) && kbd->capslock) { - kbd->capslock = 0; - appInvalidateRect(kbd->app, 0, 0, kbd->w, kbd->h); - } - if (k->flags & KF_CLOSE) appStop(kbd->app, 0); - // release key if (k->downKeycode) { inputEvent(&kbd->app->input, k->downKeycode, 0); @@ -148,13 +129,22 @@ void keyUp(Key *k, int force) { if (k->prevHeld) k->prevHeld->nextHeld = k; else kbd->firstHeld = k->nextHeld; k->nextHeld = k->prevHeld = NULL; } + + // following action should be done only for real button up + if (!wasDown) + return; + + if (k->flags & KF_CLOSE) + appStop(kbd->app, 0); + if (k->flags & KF_LAYOUT) + keyboardSwitchLayout(kbd, k->optValue); // on releasing a regular key release all held special keys - if (!force && k->flags & ~KF_DOUBLE) + if (!force && !k->flags) keyboardRelaseHeld(kbd); // redraw - if (wasDown) appInvalidateRect(kbd->app, k->x, k->y, k->w, k->h); + appInvalidateRect(kbd->app, k->x, k->y, k->w, k->h); } @@ -164,11 +154,48 @@ int layoutInit(Layout *l, Keyboard *kbd) { l->kbd = kbd; l->prev = NULL; l->downKey = NULL; + + if (!l->keysCount) + return 1; + + // fix keys coords + Key *k = &l->keys[0]; + int x0 = k->x; + int y0 = k->y; + int x1 = x0 + k->w; + int y1 = y0 + k->h; + for(int i = 1; i < l->keysCount; ++i) { + k = &l->keys[i]; + Key *p = k - 1; + + // coord < 0 means relative offset from previos key -1 + // zero coord of size means copy value from previous key + if (!k->x) k->x = p->x; else + if (k->x < 0) k->x = p->x + p->w - k->x - 1; + if (!k->y) k->y = p->y; else + if (k->y < 0) k->y = p->y + p->h - k->y - 1; + if (!k->w) k->w = p->w; + if (!k->h) k->h = p->h; + + // calc bounds + if (x0 > k->x) x0 = k->x; + if (y0 > k->y) y0 = k->y; + if (x1 < k->x + k->w) x1 = k->x + k->w; + if (y1 < k->y + k->h) y1 = k->y + k->h; + } + + // fix layout size + if (!l->w) l->w = x1 + x0; + if (!l->h) l->h = y1 + y0; + LOGDBG("layout: init: size x0=%d y0=%d x1=%d y1=%d w=%d h=%d", x0, y0, x1, y1, l->w, l->h); + + // init keys for(int i = 0; i < l->keysCount; ++i) if (!keyInit(&l->keys[i], l)) { for(--i; i >= 0; --i) keyDeinit(&l->keys[i]); return 0; } + return 1; } @@ -225,15 +252,22 @@ int keyboardInit(Keyboard *kbd, App *app) { LOGDBG("keyboard: init"); kbd->app = app; kbd->firstHeld = NULL; - kbd->shiftsDown = 0; - kbd->capslock = 0; kbd->current = kbd->layoutsCount > 0 ? kbd->layouts : NULL; + for(int i = 0; i < kbd->layoutsCount; ++i) { if (!layoutInit(&kbd->layouts[i], kbd)) { for(--i; i >= 0; --i) layoutDeinit(&kbd->layouts[i]); return 0; } } + + // fix size + for(int i = 0; i < kbd->layoutsCount; ++i) { + if (kbd->w < kbd->layouts[i].w) kbd->w = kbd->layouts[i].w; + if (kbd->h < kbd->layouts[i].h) kbd->h = kbd->layouts[i].h; + } + LOGDBG("keyboard: init: size w=%d h=%d", kbd->w, kbd->h); + appMove(app, app->x, app->y, kbd->w, kbd->h); return 1; } diff --git a/keyboard.h b/keyboard.h index 0fa9f0a..79a0d79 100644 --- a/keyboard.h +++ b/keyboard.h @@ -6,16 +6,17 @@ #include "input.h" +#define K_LBL_SIZE 16 + + // key flags enum { - KF_DOUBLE = 1 << 0, // show two lables, insensitive for capslock - KF_HOLD = 1 << 1, // key to switch layout - KF_SHIFT = 1 << 2, // key to switch layout - KF_CAPS = 1 << 3, // key to switch layout - KF_LAYOUT = 1 << 4, // key to switch layout - KF_MOVE = 1 << 5, // handle to move the window - KF_SIZE = 1 << 6, // key to resize the window - KF_CLOSE = 1 << 7 }; // key to close the window + KF_HOLD = 1 << 0, // key will be hold until it will pressed again or non-hold key will pressed + KF_MOD = 1 << 1, // visible value depends on modifier state (using optValue field) + KF_LAYOUT = 1 << 2, // key to switch layout (using optValue field); + KF_MOVE = 1 << 3, // handle to move the window + KF_SIZE = 1 << 4, // key to resize the window + KF_CLOSE = 1 << 5 }; // key to close the window // special layout indices enum { @@ -38,20 +39,22 @@ struct Key { int x, y, w, h; unsigned int keySym; unsigned int keySym2; - const char *label; - const char *label2; + char label[K_LBL_SIZE]; + char label2[K_LBL_SIZE]; unsigned int flags; - int layoutIndex; + int optValue; // these fields will be set while initialization Layout *l; + int isLetter; + int isKeypad; TextLayout tl; TextLayout tl2; // dynamic fields int down; int downKeycode; - int mx, my; // mouse down position, relative to key origin + int mx, my; // values for move and size buttons Key *nextHeld, *prevHeld; }; @@ -83,8 +86,6 @@ struct Keyboard { // dynamic fields Layout *current; Key *firstHeld; - int shiftsDown; - int capslock; }; diff --git a/keyboard.inc.c b/keyboard.inc.c deleted file mode 100644 index 0a2f1a2..0000000 --- a/keyboard.inc.c +++ /dev/null @@ -1,117 +0,0 @@ -#ifndef KEYBOARD_INC_C -#define KEYBOARD_INC_C - - -struct Keyboard; - - -typedef struct Key { - int x, y, w, h; - const char *label; - - struct Keyboard *kbd; - int lx, ly; - - int pressed; -} Key; - - -typedef struct Keyboard { - Key *keys; - int keysCount; - - Display* dpy; - Window win; - GC gc; - GContext gcid; - XFontStruct *font; - - Key *keyMousePressed; -} Keyboard; - - - -void keyInit(Key *k, Keyboard *kbd) { - k->pressed = 0; - k->kbd = kbd; - XFontStruct *font = XQueryFont(k->kbd->dpy, k->kbd->gcid); - XRectangle r = {}; - XTextExtents(font, k->label, strlen(k->label), NULL, &r); - k->lx = k->x + (k->w - r.width)/2 - r.x; - k->ly = k->y + (k->h - r.height)/2 - r.y; -} - -void keyDeinit(Key *k) { - k->pressed = 0; -} - -void keyDraw(Key *k, int cx, int cy, int cw, int ch) { - if ( k->x + k->w <= cx || k->x >= cx + cw - || k->y + k->h <= cy || k->y >= cy + ch ) return; - if (k->pressed) - XDrawRectangle(k->kbd->dpy, k->kbd->win, k->kbd->gc, k->x, k->y, k->w, k->h); - XDrawString(k->kbd->dpy, k->kbd->win); -} - -void keyMouseDown(Key *k) { - k->pressed = 1; -} - -void keyMouseUp(Key *k) { - k->pressed = 0; -} - - -void keyboardInit(Keyboard *kbd, Display *dpy, Window win) { - kbd->keyMousePressed = NULL; - kbd->dpy = dpy; - kbd->win = win; - kbd->gc = DefaultGC(kbd->dpy, kbd->win); - kbd->gcid = XGContextFromGC(kbd->gc); - kbd->font = XQueryFont(k->kbd->dpy, k->kbd->gcid); - for(int i = 0; i < kbd->keysCount; ++i) - keyInit(&kbd->keys[i], kbd); -} - -void keyboardDeinit(Keyboard *kbd) { - for(int i = 0; i < kbd->keysCount; ++i) - keyDeinit(kbd->keys[i]); - kbd->keyMousePressed = NULL; - kbd->font = XFreeFont(kbd->dpy, kbd->font); - kbd->gcid = NULL; - kbd->gc = NULL; - kbd->win = NULL; - kbd->dpy = NULL; -} - -void keyboardDraw(Keyboard *kbd, int cx, int cy, int cw, int ch) { - for(int i = 0; i < kbd->keysCount; ++i) - keyDraw(&kbd->keys[i], cx, cy, cw, ch); -} - -void keyboardMouseDown(Keyboard *kbd, int x, int y) { - for(int i = kbd->keysCount - 1; i >= 0; --i) { - Key *k = &kbd->keys[i]; - if ( k->x <= x && x < k->x + k->w - && k->y <= y && y < k->y + k->h ) - { - if (kbd->keyMousePressed != k) { - keyboardMouseUp(kbd); - kbd->keyMousePressed = k; - keyMouseDown(k); - } - break; - } - } -} - -void keyboardMouseUp(Keyboard *kbd) { - if (kbd->keyMousePressed) { - kbd->keyMouseUp(kbd->keyMousePressed); - kbd->keyMousePressed = NULL; - } -} - - - -#endif diff --git a/main.c b/main.c index a134577..1df85ec 100644 --- a/main.c +++ b/main.c @@ -8,29 +8,29 @@ #include "keyboard.c" +#define S 10 // spacing +#define N (-S-1) // place key next to previos key + Key keys[] = { - { 10, 10, 30, 20, XK_Armenian_vyun, XK_Armenian_VYUN, "ւ", "Ւ" }, - { 50, 10, 30, 20, XK_b, XK_B, "b", "B" }, - { 90, 10, 30, 20, XK_c, XK_X, "c", "X" }, - { 130, 10, 30, 20, XK_5, XK_percent, "5", "%", KF_DOUBLE }, - { 10, 40, 30, 20, XK_Cyrillic_a, XK_Cyrillic_a, "а", "а" }, - { 50, 40, 30, 20, XK_Cyrillic_be, XK_Cyrillic_BE, "б", "Б" }, - { 90, 40, 30, 20, XK_Cyrillic_ve, 0, "в" }, - { 130, 40, 30, 20, XK_6, XK_exclam, "6", "!", KF_DOUBLE }, - { 10, 70, 30, 20, 0, 0, "[x]", NULL, KF_CLOSE }, - { 50, 70, 30, 20, 0, 0, "[:::]", NULL, KF_MOVE }, - { 90, 70, 30, 20, 0, 0, "[<]", NULL, KF_LAYOUT, LI_REVERT }, - { 130, 70, 30, 20, 0, 0, "[x]", NULL, KF_SIZE }, + { S, S, 30, 20, XK_Armenian_vyun, XK_Armenian_VYUN, "ւ", "Ւ" }, + { N, 0, 0, 0, XK_b }, + { N, 0, 0, 0, XK_c, XK_X, "", "Iks" }, + { N, 0, 0, 0, XK_5, XK_percent, "5", "%" }, + { S, N, 0, 0, XK_Cyrillic_a }, + { N, 0, 0, 0, XK_Cyrillic_be }, + { N, 0, 0, 0, XK_Cyrillic_ve }, + { N, 0, 0, 0, XK_6, XK_exclam }, + { S, N, 0, 0, 0, 0, "[x]", "", KF_CLOSE }, + { N, 0, 0, 0, 0, 0, "[:::]", "", KF_MOVE }, + { N, 0, 0, 0, 0, 0, "[<]", "", KF_LAYOUT, LI_REVERT }, + { N, 0, 0, 0, 0, 0, "[x]", "", KF_SIZE }, }; -enum { keysCount = sizeof(keys)/sizeof(keys[0]) }; - Layout layouts[] = { - { keys, COUNTOF(keys), 170, 100 } }; - + { keys, COUNTOF(keys) } }; App app = { - { layouts, COUNTOF(layouts), 170, 100 } }; // keyboard + { layouts, COUNTOF(layouts) } }; // keyboard int main() {