forked from sarim/ibus-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-gjs.js
executable file
·151 lines (132 loc) · 5.65 KB
/
main-gjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env gjs
const IBus = imports.gi.IBus;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Avroparser = imports.avrolib.OmicronLab.Avro.Phonetic;
const utfconv = imports.utf8;
const eevars = imports.evars;
//check if running from ibus
exec_by_ibus = (ARGV[0] == '--ibus')
// Let's initialize ibus
IBus.init();
//get the ibus bus
var bus = new IBus.Bus();
if( bus.is_connected() ) {
var id = 0;
function _create_engine_cb(factory, engine_name) {
id += 1;
var engine = new IBus.Engine({
engine_name: engine_name,
object_path: '/org/freedesktop/IBus/Engine/' + id,
connection: bus.get_connection()
});
engine.connect('process-key-event', function(engine,keyval,keycode,state) {
//print keypress infos, helpful for debugging
print(keyval + " " + keycode + " " + state);
// ignore release event
if (!(state == 0 || state == 1 || state == 16 || state == 17)) {
return false;
}
// capture the shift key
if (keycode == 42) {
return true;
}
// process letter key events
if (keyval >= 33 && keyval <= 126) {
engine.buffertext += IBus.keyval_to_unicode(keyval);
let bntext = Avroparser.parse(engine.buffertext);
bntext = utfconv.utf8Decode(bntext);
let text = IBus.Text.new_from_string(bntext);
engine.update_preedit_text(text,bntext.length,true);
let entext = IBus.Text.new_from_string(engine.buffertext);
engine.update_auxiliary_text(entext,true);
return true;
}
else if (keyval == IBus.Return || keyval == IBus.space) {
let bntext = Avroparser.parse(engine.buffertext);
bntext = utfconv.utf8Decode(bntext);
let text = IBus.Text.new_from_string(bntext);
engine.commit_text(text);
engine.buffertext = "";
engine.hide_preedit_text();
engine.hide_auxiliary_text();
if (keyval == IBus.space) {
engine.commit_text( IBus.Text.new_from_string(" "));
return true;
}
/* i forgot what this commented block is here for !
else {
// engine.forward_key_event(engine, 115, 31, state);
engine.commit_text( IBus.Text.new_from_string("\n") );
return true;
}
*/
}
else if (keyval == IBus.BackSpace){
if (engine.buffertext.length > 0) {
engine.buffertext = engine.buffertext.substr(0,engine.buffertext.length -1);
let bntext = Avroparser.parse(engine.buffertext);
bntext = utfconv.utf8Decode(bntext);
let text = IBus.Text.new_from_string(bntext);
engine.update_preedit_text(text,bntext.length,true);
let entext = IBus.Text.new_from_string(engine.buffertext);
engine.update_auxiliary_text(entext,true);
return true;
}
}
else if (keyval == IBus.Left || keyval == IBus.Right || keyval == IBus.Up || keyval == IBus.Down || keyval == IBus.Control_L || keyval == IBus.Control_R || keyval == IBus.Insert || keyval == IBus.Delete || keyval == IBus.Home || keyval == IBus.Page_Up || keyval == IBus.Page_Down || keyval == IBus.End || keyval == IBus.Alt_L || keyval == IBus.Alt_R ){
let bntext = Avroparser.parse(engine.buffertext);
bntext = utfconv.utf8Decode(bntext);
let text = IBus.Text.new_from_string(bntext);
engine.commit_text(text);
engine.buffertext = "";
engine.hide_preedit_text();
engine.hide_auxiliary_text();
}
return false ;
});
engine.connect('focus-out', function (){
if (engine.buffertext.length > 0) {
let bntext = Avroparser.parse(engine.buffertext);
bntext = utfconv.utf8Decode(bntext);
let text = IBus.Text.new_from_string(bntext);
engine.commit_text(text);
engine.buffertext = "";
engine.hide_preedit_text();
engine.hide_auxiliary_text();
}
});
engine.buffertext = "";
return engine;
}
var factory = IBus.Factory.new( bus.get_connection() );
//factory.add_engine("avro-phonetic",GObject.type_from_name('IBusEngine'));
factory.connect('create-engine', _create_engine_cb);
var component = new IBus.Component({
name:"org.freedesktop.IBus.Avro",
description:"Avro phonetic",
version:"0.9",
license:"MPL",
author:"Sarim Khan <[email protected]>",
homepage:"https://github.com/sarim/ibus-avro",
exec: eevars.get_libexecdir() + "/main-gjs.js",
textdomain:"avro-phonetic"
});
var avroenginedesc = new IBus.EngineDesc({
name:"avro-phonetic",
longname:"avro phonetic",
description:"Avro Phonetic Engine",
language:"bn",
license:"MPL",
author:"Sarim Khan <[email protected]>",
icon: eevars.get_pkgdatadir() + "/avro-bangla.png",
layout:"bn"
});
component.add_engine (avroenginedesc);
if (exec_by_ibus) {
bus.request_name ("org.freedesktop.IBus.Avro",0);
} else {
bus.register_component (component);
}
IBus.main();
}