2009-10-12 15:25:57 +04:00
|
|
|
#include "Applet.h"
|
|
|
|
|
2009-11-20 20:08:29 +03:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2009-10-12 15:25:57 +04:00
|
|
|
#include <FL/Fl_Button.H>
|
|
|
|
#include <FL/Fl_Shared_Image.H>
|
|
|
|
#include <FL/x.H>
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
|
|
|
|
#include <stdio.h> /* needed for XKBrules.h */
|
|
|
|
|
2009-11-20 20:08:29 +03:00
|
|
|
#ifdef HAVE_XKBRULES
|
|
|
|
# include <X11/XKBlib.h>
|
|
|
|
# include <X11/extensions/XKBgeom.h>
|
|
|
|
# include <X11/extensions/XKBrules.h>
|
|
|
|
#endif
|
2009-10-12 15:25:57 +04:00
|
|
|
|
|
|
|
#include <edelib/Resource.h>
|
|
|
|
#include <edelib/Debug.h>
|
|
|
|
#include <edelib/List.h>
|
|
|
|
#include <edelib/Directory.h>
|
|
|
|
#include <edelib/Nls.h>
|
2009-11-13 14:53:02 +03:00
|
|
|
#include <edelib/Run.h>
|
|
|
|
#include <edelib/ForeignCallback.h>
|
|
|
|
|
|
|
|
/* they should match names in ede-keyboard-conf */
|
|
|
|
#define PANEL_APPLET_ID "ede-keyboard"
|
|
|
|
#define CONFIG_NAME "ede-keyboard"
|
2009-10-12 15:25:57 +04:00
|
|
|
|
|
|
|
EDELIB_NS_USING(Resource)
|
|
|
|
EDELIB_NS_USING(String)
|
|
|
|
EDELIB_NS_USING(list)
|
2009-11-13 14:53:02 +03:00
|
|
|
EDELIB_NS_USING(run_async)
|
|
|
|
EDELIB_NS_USING(foreign_callback_add)
|
|
|
|
EDELIB_NS_USING(foreign_callback_remove)
|
2009-10-12 15:25:57 +04:00
|
|
|
EDELIB_NS_USING(RES_SYS_ONLY)
|
|
|
|
|
|
|
|
static Atom _XA_XKB_RF_NAMES_PROP_ATOM = 0;
|
|
|
|
|
|
|
|
class KeyLayout : public Fl_Button {
|
|
|
|
private:
|
2009-11-13 14:53:02 +03:00
|
|
|
bool should_show_flag;
|
|
|
|
String path, curr_layout;
|
|
|
|
Fl_Image *img;
|
|
|
|
|
2009-10-12 15:25:57 +04:00
|
|
|
public:
|
|
|
|
KeyLayout();
|
2009-11-13 14:53:02 +03:00
|
|
|
~KeyLayout();
|
|
|
|
void update_flag(bool read_config);
|
|
|
|
void do_key_layout();
|
2009-10-12 15:25:57 +04:00
|
|
|
int handle(int e);
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef list<KeyLayout*> KeyLayoutList;
|
|
|
|
typedef list<KeyLayout*>::iterator KeyLayoutListIt;
|
|
|
|
|
|
|
|
static KeyLayoutList keylayout_objects;
|
|
|
|
|
2009-11-20 20:08:29 +03:00
|
|
|
#ifdef HAVE_XKBRULES
|
2009-10-12 15:25:57 +04:00
|
|
|
/* Xkb does not provide this */
|
|
|
|
static void xkbrf_names_prop_free(XkbRF_VarDefsRec &vd, char *tmp) {
|
|
|
|
XFree(tmp);
|
|
|
|
XFree(vd.model);
|
|
|
|
XFree(vd.layout);
|
|
|
|
XFree(vd.options);
|
|
|
|
XFree(vd.variant);
|
|
|
|
}
|
2009-11-20 20:08:29 +03:00
|
|
|
#endif
|
2009-10-12 15:25:57 +04:00
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
/*
|
|
|
|
* any layout changes on X will be reported here, executed either via ede-keyboard-conf or
|
|
|
|
* another tool, so there are no needs to read layout from configuration file
|
|
|
|
*/
|
2009-10-12 15:25:57 +04:00
|
|
|
static int xkb_events(int e) {
|
|
|
|
if(fl_xevent->xproperty.atom == _XA_XKB_RF_NAMES_PROP_ATOM) {
|
|
|
|
/* TODO: lock this */
|
|
|
|
KeyLayoutListIt it = keylayout_objects.begin(), it_end = keylayout_objects.end();
|
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
for(; it != it_end; ++it) {
|
2009-10-12 15:25:57 +04:00
|
|
|
(*it)->do_key_layout();
|
2009-11-13 14:53:02 +03:00
|
|
|
(*it)->update_flag(false);
|
|
|
|
}
|
2009-10-12 15:25:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
static void click_cb(Fl_Widget*, void*) {
|
|
|
|
run_async("ede-keyboard-conf");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_flag_cb(Fl_Window*, void *data) {
|
|
|
|
KeyLayout *k = (KeyLayout*)data;
|
|
|
|
k->update_flag(true);
|
|
|
|
k->redraw();
|
|
|
|
}
|
|
|
|
|
2009-10-12 15:25:57 +04:00
|
|
|
KeyLayout::KeyLayout() : Fl_Button(0, 0, 30, 25) {
|
2009-11-13 14:53:02 +03:00
|
|
|
should_show_flag = true;
|
|
|
|
curr_layout = "us"; /* default layout */
|
|
|
|
img = NULL;
|
|
|
|
|
2009-10-12 15:25:57 +04:00
|
|
|
box(FL_FLAT_BOX);
|
|
|
|
labelfont(FL_HELVETICA_BOLD);
|
|
|
|
labelsize(10);
|
|
|
|
label("??");
|
2009-11-13 14:53:02 +03:00
|
|
|
align(FL_ALIGN_CLIP);
|
2009-10-12 15:25:57 +04:00
|
|
|
|
|
|
|
tooltip(_("Current keyboard layout"));
|
2009-11-13 14:53:02 +03:00
|
|
|
callback(click_cb);
|
|
|
|
foreign_callback_add(window(), PANEL_APPLET_ID, update_flag_cb, this);
|
2009-10-12 15:25:57 +04:00
|
|
|
|
|
|
|
path = Resource::find_data("icons/kbflags/21x14", RES_SYS_ONLY, NULL);
|
|
|
|
|
|
|
|
do_key_layout();
|
2009-11-13 14:53:02 +03:00
|
|
|
update_flag(true);
|
2009-10-12 15:25:57 +04:00
|
|
|
|
|
|
|
/* TODO: lock this */
|
|
|
|
keylayout_objects.push_back(this);
|
|
|
|
|
|
|
|
/* with this, kb layout chages will be catched */
|
2009-11-20 20:08:29 +03:00
|
|
|
#if HAVE_XKBRULES
|
2009-10-12 15:25:57 +04:00
|
|
|
if(!_XA_XKB_RF_NAMES_PROP_ATOM)
|
|
|
|
_XA_XKB_RF_NAMES_PROP_ATOM = XInternAtom(fl_display, _XKB_RF_NAMES_PROP_ATOM, False);
|
2009-11-20 20:08:29 +03:00
|
|
|
#endif
|
2009-10-12 15:25:57 +04:00
|
|
|
|
|
|
|
Fl::add_handler(xkb_events);
|
|
|
|
}
|
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
KeyLayout::~KeyLayout() {
|
|
|
|
foreign_callback_remove(update_flag_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyLayout::update_flag(bool read_config) {
|
|
|
|
if(read_config) {
|
|
|
|
Resource r;
|
|
|
|
|
|
|
|
if(r.load(CONFIG_NAME))
|
|
|
|
r.get("Keyboard", "show_country_flag", should_show_flag, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove previous image if we aren't going to show it */
|
|
|
|
if(!should_show_flag)
|
|
|
|
img = NULL;
|
|
|
|
|
|
|
|
if(should_show_flag && !path.empty()) {
|
|
|
|
/* construct image path that has the same name as layout */
|
|
|
|
String full_path = path;
|
|
|
|
|
|
|
|
full_path += E_DIR_SEPARATOR_STR;
|
|
|
|
full_path += curr_layout;
|
|
|
|
full_path += ".png";
|
|
|
|
|
|
|
|
img = Fl_Shared_Image::get(full_path.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
image(img);
|
|
|
|
|
|
|
|
if(img)
|
|
|
|
label(NULL);
|
|
|
|
else
|
|
|
|
label(curr_layout.c_str());
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2009-10-12 15:25:57 +04:00
|
|
|
void KeyLayout::do_key_layout(void) {
|
2009-11-20 20:08:29 +03:00
|
|
|
#ifdef HAVE_XKBRULES
|
2009-10-12 15:25:57 +04:00
|
|
|
char *tmp = NULL;
|
|
|
|
XkbRF_VarDefsRec vd;
|
|
|
|
|
|
|
|
if(XkbRF_GetNamesProp(fl_display, &tmp, &vd)) {
|
|
|
|
/* do nothing if layout do not exists or the same was catched */
|
|
|
|
if(!vd.layout || (curr_layout == vd.layout))
|
|
|
|
return;
|
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
/* just store it, so update_flag() can do the rest */
|
2009-10-12 15:25:57 +04:00
|
|
|
curr_layout = vd.layout;
|
|
|
|
xkbrf_names_prop_free(vd, tmp);
|
|
|
|
}
|
2009-11-20 20:08:29 +03:00
|
|
|
#endif
|
2009-10-12 15:25:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int KeyLayout::handle(int e) {
|
|
|
|
switch(e) {
|
|
|
|
case FL_ENTER:
|
|
|
|
box(FL_THIN_UP_BOX);
|
|
|
|
redraw();
|
|
|
|
break;
|
|
|
|
case FL_LEAVE:
|
|
|
|
box(FL_FLAT_BOX);
|
|
|
|
redraw();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Fl_Button::handle(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
EDE_PANEL_APPLET_EXPORT (
|
|
|
|
KeyLayout,
|
|
|
|
EDE_PANEL_APPLET_OPTION_ALIGN_RIGHT,
|
|
|
|
"Keyboard Layout applet",
|
|
|
|
"0.1",
|
|
|
|
"empty",
|
|
|
|
"Sanel Zukan"
|
|
|
|
)
|