Back to index

im-sdk  12.3.91
IIIMP_hotkey_profile.cpp
Go to the documentation of this file.
00001 #include <config.h>
00002 #include <stdio.h>
00003 #include <vector>
00004 #include <SunIM.h>
00005 #include "IMBasicObject.hh"
00006 #include "IMKeyUtils.hh"
00007 #include "IMHotkeyPrimitive.hh"
00008 #include "IIIMP_hotkey_profile.hh"
00009 #include "IIIMP_IMState.hh"
00010 
00011 IMHotkey::IMHotkey(
00012     int a_id, 
00013     const IMHotkeyStruct& a_hk,
00014     const u16string& a_label
00015 ): label(a_label){
00016     state_flag = a_hk.state_flag;
00017     action_flag = a_hk.action_flag;
00018     hotkey_id = a_id;
00019 
00020     for (int i = 0; i < a_hk.nkeys; i++)
00021        keys.push_back(IMKeySpec(a_hk.keys[i].keyCode, a_hk.keys[i].keyChar,
00022                              a_hk.keys[i].modifier, a_hk.keys[i].time_stamp));
00023 }
00024 
00025 // Passing the latest hk_id in constructor so that hotkey_id's
00026 // can be unique across all the hotkey profiles
00027 
00028 IMHotkeyProfile::IMHotkeyProfile(
00029     int a_id,
00030     int hk_id,
00031     const IMHotkeyProfileStruct *hkps
00032 )
00033 {
00034     if (a_id == 0) {
00035        // Clear the hklist when adding the first hotkey profile
00036        hklist.clear();
00037     }
00038 
00039     hotkey_profile_struct = hkps;
00040     for (int i = 0; i < hkps->num_hotkeys; i++) {
00041         u16string label = "Default Hotkey Label";
00042        // what shall we do with hkps->scope?
00043        if ((hkps->hks[i].label) && (strlen(hkps->hks[i].label))) {
00044          label = u16string(hkps->hks[i].label); 
00045         }
00046        hklist.push_back(IMHotkey(hk_id + i, hkps->hks[i], label));
00047     }
00048     profile_id = a_id;
00049 }
00050 
00051 IIIMP_hotkey_profile::IIIMP_hotkey_profile(
00052 )
00053 {
00054     default_hotkey_profile_id = 0;
00055     default_hotkey_trigger_notify_id = 0;
00056     default_hotkey_lang_switch_id = 0;
00057     default_hotkey_cycle_lang_switch_id = 0;
00058     default_hotkey_reverse_cycle_lang_switch_id = 0;
00059     default_hotkey_super_help_id = 0;
00060 }
00061 
00062 IMHotkeyStruct *
00063 IIIMP_hotkey_profile::get_super_hotkeys(const IMKeySpecList &triggerkeys,
00064                                    const char          *language,
00065                                    int                 *count_hotkeys)
00066 {
00067     IMHotkeyStruct *hks = NULL;
00068     HotKeyList *hlist = NULL;
00069     IMKeyEventStruct *kev;
00070     int n_keys = 0, n_hotkeys = 0;
00071     IMKeySpecList::const_iterator it;
00072 
00073     hks = new IMHotkeyStruct[4];
00074 
00075     // lang switch
00076     kev = new IMKeyEventStruct[2];
00077     n_keys = 0;
00078     kev[n_keys].keyCode = IM_VK_SPACE;
00079     kev[n_keys].keyChar = 0;
00080     kev[n_keys].modifier = IM_CTRL_MASK|IM_ALT_MASK;
00081     kev[n_keys].time_stamp = 0;
00082     n_keys++;
00083     kev[n_keys].keyCode = IM_VK_SPACE;
00084     kev[n_keys].keyChar = 0;
00085     kev[n_keys].modifier = IM_CTRL_MASK|IM_SHIFT_MASK;
00086     kev[n_keys].time_stamp = 0;
00087     n_keys++;
00088 
00089     hks[n_hotkeys].label = strdup("LE SWITCH");
00090     hks[n_hotkeys].state_flag = 0;
00091     hks[n_hotkeys].action_flag = 1;
00092     hks[n_hotkeys].nkeys = n_keys;
00093     hks[n_hotkeys].keys = kev;
00094     n_hotkeys++;
00095 
00096     // trigger keys
00097     kev = new IMKeyEventStruct[triggerkeys.size()];
00098 
00099     for (it = triggerkeys.begin(), n_keys = 0; it != triggerkeys.end(); it++, n_keys++) {
00100        kev[n_keys].keyCode = it->get_keycode();
00101        kev[n_keys].keyChar = it->get_keychar();
00102        kev[n_keys].modifier = it->get_modifier();
00103        kev[n_keys].time_stamp = it->get_timestamp();
00104     }
00105 
00106     hks[n_hotkeys].label = strdup("TRIGGER KEYS");
00107     hks[n_hotkeys].state_flag = 0;
00108     hks[n_hotkeys].action_flag = 1;
00109     hks[n_hotkeys].nkeys = n_keys;
00110     hks[n_hotkeys].keys = kev;
00111     n_hotkeys++;
00112 
00113     // cycle lang switch
00114     kev = new IMKeyEventStruct[1];
00115     n_keys = 0;
00116     kev[n_keys].keyCode = IM_VK_GREATER;
00117     kev[n_keys].keyChar = 0;
00118     kev[n_keys].modifier = IM_SHIFT_MASK|IM_CTRL_MASK;
00119     kev[n_keys].time_stamp = 0;
00120     n_keys++;
00121 
00122     hks[n_hotkeys].label = strdup("CYCLE LE SWITCH");
00123     hks[n_hotkeys].state_flag = 0;
00124     hks[n_hotkeys].action_flag = 1;
00125     hks[n_hotkeys].nkeys = n_keys;
00126     hks[n_hotkeys].keys = kev;
00127     n_hotkeys++;
00128 
00129     // cycle lang switch (reverse)
00130     kev = new IMKeyEventStruct[1];
00131     n_keys = 0;
00132     kev[n_keys].keyCode = IM_VK_LESS;
00133     kev[n_keys].keyChar = 0;
00134     kev[n_keys].modifier = IM_SHIFT_MASK|IM_CTRL_MASK;
00135     kev[n_keys].time_stamp = 0;
00136     n_keys++;
00137 
00138     hks[n_hotkeys].label = strdup("CYCLE LE SWITCH(reverse)");
00139     hks[n_hotkeys].state_flag = 0;
00140     hks[n_hotkeys].action_flag = 1;
00141     hks[n_hotkeys].nkeys = n_keys;
00142     hks[n_hotkeys].keys = kev;
00143     n_hotkeys++;
00144 
00145     *count_hotkeys = n_hotkeys;
00146 
00147     return hks;
00148 }
00149 
00150 bool
00151 IIIMP_hotkey_profile::add_super_hotkey_profiles(
00152     const u16string &curr_input_lang,
00153     const IMHotkeyStruct *hotkeys,
00154     int n_hotkeys
00155 )
00156 {
00157     IMHotkeyList hklist;
00158     int curr_hotkey_id, i, j;
00159     const char *function_ids[] = {
00160        "LE SWITCH",
00161        "TRIGGER KEYS",
00162        "CYCLE LE SWITCH",
00163        "CYCLE LE SWITCH(reverse)",
00164        NULL,
00165     };
00166 
00167     // Hack to get the latest hotkey_id which we can use for
00168     // setting the unique default_hotkey_lang_switch_id and
00169     // default_hotkey_trigger_notify_id 
00170 
00171     IMHotkeyProfileList::const_iterator it1 = profile_list.begin();
00172     for (curr_hotkey_id = 0 ; it1 != profile_list.end(); ++it1) {
00173         curr_hotkey_id += (it1->get_hotkey_list())->size();
00174     }
00175 
00176     for (i = 0; i < n_hotkeys; i++) {
00177        for (j = 0; function_ids[j] != NULL; j++) {
00178            if (!strcmp(hotkeys[i].label, function_ids[j])) {
00179               hklist.push_back(IMHotkey(curr_hotkey_id, hotkeys[i], u16string(hotkeys[i].label)));
00180               switch (j) {
00181               case 0: /* LE SWITCH */
00182                   default_hotkey_lang_switch_id = curr_hotkey_id;
00183                   j = (sizeof (function_ids) / sizeof (char *)) - 2;
00184                   break;
00185               case 1: /* TRIGGER KEYS */
00186                   default_hotkey_trigger_notify_id = curr_hotkey_id;
00187                   j = (sizeof (function_ids) / sizeof (char *)) - 2;
00188                   break;
00189               case 2: /* CYCLE LE SWITCH */
00190                   default_hotkey_cycle_lang_switch_id = curr_hotkey_id;
00191                   j = (sizeof (function_ids) / sizeof (char *)) - 2;
00192                   break;
00193               case 3: /* CYCLE LE SWITCH(reverse) */
00194                   default_hotkey_reverse_cycle_lang_switch_id = curr_hotkey_id;
00195                   j = (sizeof (function_ids) / sizeof (char *)) - 2;
00196                   break;
00197               default:
00198                   LOG_DEBUG("Unknown function id: %s", hotkeys[i].label);
00199                   curr_hotkey_id--;
00200                   break;
00201               }
00202               curr_hotkey_id++;
00203            }
00204        }
00205     }
00206     default_hotkey_profile_id = profile_list.size();
00207     profile_list.push_back(IMHotkeyProfile(profile_list.size(), hklist));
00208 
00209     // crack
00210     merge_default_hotkeys (hklist);
00211 
00212     return True;
00213 }
00214 
00215 IIIMP_hotkey_profile::~IIIMP_hotkey_profile()
00216 {
00217     LOG_DEBUG("IIIMP_hotkey_profile destructor \n");
00218     // profile_list.clear();
00219 }
00220 
00221 const int
00222 IIIMP_hotkey_profile::get_unique_profile_id(
00223     int le_profile_id,
00224     IMLEName *lename
00225 )
00226 {
00227     HotkeyProfileMap::iterator it;
00228 
00229     for (it = hotkey_profile_map.begin(); it != hotkey_profile_map.end(); ++it) {
00230       IMHotkeyProfileStruct *hkps = it->second;
00231       if ((hkps->name->id == lename->id) && (hkps->profile_id == le_profile_id)) {
00232         return it->first;
00233       }
00234    }
00235    return -1;
00236 }
00237 
00238 void
00239 IIIMP_hotkey_profile::merge_default_hotkeys(IMHotkeyList &phklist)
00240 {
00241     IMHotkeyProfileList::iterator it1 = profile_list.begin();
00242 
00243     for (; it1 != profile_list.end(); ++it1) {
00244        if (it1->get_profile_id() == default_hotkey_profile_id)
00245            continue;
00246        IMHotkeyList *hkl = it1->get_hotkey_list();
00247         IMHotkeyList::iterator it2 = phklist.begin();
00248        for (; it2 != phklist.end(); ++it2)
00249            hkl->push_back(*it2);
00250     }
00251 }
00252 
00253 bool
00254 IIIMP_hotkey_profile::init_hotkey_profile_list(IMHotkeyProfileStruct *hkps,
00255                                           int count_profiles)
00256 {
00257     IMHotkeyProfileStruct *p;
00258     int curr_hotkey_id = 0;
00259     profile_list.clear();
00260 
00261     hotkey_profile_map.clear();
00262     if (hkps) {
00263        for (p = hkps; p < &hkps[count_profiles]; p++) {
00264            if (p != NULL) {
00265                 hotkey_profile_map.insert(make_pair(profile_list.size(), p));
00266               profile_list.push_back(IMHotkeyProfile(profile_list.size(), curr_hotkey_id, p));
00267            }
00268            // 1. Hack to assign unique hotkey_ids for hotkey's under
00269            //    hotkey profiles registered by all LEs.
00270            // 2. Get the latest hotkey_id from the hotkey profiles added
00271            //    so far and pass it IMHotkeyProfile constructor
00272 
00273            curr_hotkey_id = 0;
00274             for (IMHotkeyProfileList::iterator it = profile_list.begin();
00275                  it != profile_list.end();
00276                  ++it)
00277             {
00278                 curr_hotkey_id += it->get_hotkey_list()->size();
00279            }
00280        }
00281     }
00282 
00283     return True;
00284 }
00285 
00286 int
00287 IIIMP_hotkey_profile::register_hotkeys(
00288     void* register_id,
00289     IMHotkeyList *hklist  
00290 )
00291 {
00292     // TODO ...
00293     // return profile_id
00294     return -1;
00295 }
00296 
00297 const IMKeySpecList *
00298 IIIMP_hotkey_profile::retrieve_trigger_keys(int profile_id)
00299 {
00300     int i, j;
00301 
00302     for (IMHotkeyProfileList::iterator it1 = profile_list.begin();
00303         it1 != profile_list.end(); it1++) {
00304        if (it1->get_profile_id() == default_hotkey_profile_id) {
00305            IMHotkeyList *phklist = it1->get_hotkey_list();
00306            IMHotkeyList::iterator it2;
00307 
00308            for (i = 0, it2 = phklist->begin(); it2 != phklist->end(), i < phklist->size(); it2++, i++) {
00309               if (it2->get_hotkey_id() == default_hotkey_trigger_notify_id) {
00310                   return it2->get_keylist();
00311               }
00312            }
00313        }
00314     }
00315     return NULL;
00316 }
00317 
00318 HOTKEY_LIST* 
00319 IIIMP_hotkey_profile::retrive_hotkeys(
00320     IIIMP_data_s *pdata_s,
00321     int count,
00322     int *profile_id
00323 )
00324 {
00325     IMHotkeyList::iterator it2; 
00326     IMKeySpecList::iterator it3;
00327     HOTKEY *hk;
00328     HOTKEY_LIST *hklist;
00329     IIIMP_keyevent *keys;
00330     size_t n, i, j;
00331     u16string label;
00332 
00333     IMHotkeyProfileList::iterator it1 = profile_list.begin();
00334     n = count;
00335     while (n-- > 0)
00336        it1++;
00337 
00338     *profile_id = it1->get_profile_id();
00339     IMHotkeyList *phklist = it1->get_hotkey_list();
00340     hk = new HOTKEY[phklist->size()];
00341     for (i = 0, it2 = phklist->begin(); it2 != phklist->end(), i < phklist->size(); it2++, i++) {
00342        hk[i].hotkeyctrl.hotkey_id = it2->get_hotkey_id();
00343        hk[i].hotkeyctrl.state_flag = it2->get_state_flag();
00344        hk[i].hotkeyctrl.action_flag = it2->get_action_flag();
00345 
00346        IMKeySpecList *keylist = it2->get_keylist();
00347        keys = new IIIMP_keyevent[keylist->size()];
00348 
00349        for (j = 0, it3 = keylist->begin(); it3 != keylist->end(), j < keylist->size(); it3++, j++) {
00350            keys[j].keycode = it3->get_keycode();
00351            keys[j].keychar = it3->get_keychar();
00352            keys[j].modifier = it3->get_modifier();
00353            keys[j].time_stamp = it3->get_timestamp();
00354        }
00355        hk[i].hotkeylist = iiimp_keyevent_list_new(pdata_s, keylist->size(), keys);
00356        delete[] keys;
00357        if (!hk[i].hotkeylist) {
00358            if (hk[i].hotkeylist) iiimp_keyevent_list_delete(pdata_s, hk[i].hotkeylist);
00359            return NULL;
00360        }
00361 
00362        label = it2->get_label();
00363        hk[i].label = iiimp_string_new(pdata_s, label.size(), label.data());
00364     }
00365     hklist = iiimp_hotkey_list_new(pdata_s, phklist->size(), hk);
00366     delete[] hk;
00367     return hklist;
00368 }
00369 
00370 void* 
00371 IIIMP_hotkey_profile::get_registerer_id_by_profile_id(
00372     int profile_id
00373 )
00374 {
00375     return NULL;
00376 }
00377 
00378 void* 
00379 IIIMP_hotkey_profile::get_profile_id_by_registerer_id(
00380     void* registerer_id
00381 )
00382 {
00383     return NULL;
00384 }
00385 
00386 /* Local Variables: */
00387 /* c-file-style: "iiim-project" */
00388 /* End: */