2009-11-06 18:45:48 +03:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* ede-keyboard-conf, a tool to configure keyboard
|
|
|
|
* Part of Equinox Desktop Environment (EDE).
|
|
|
|
* Copyright (c) 2009 EDE Authors.
|
|
|
|
*
|
|
|
|
* This program is licensed under terms of the
|
|
|
|
* GNU General Public License version 2 or newer.
|
|
|
|
* See COPYING for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2009-11-20 20:08:29 +03:00
|
|
|
#include <edelib/MessageBox.h>
|
2010-03-28 16:54:45 +04:00
|
|
|
#include <edelib/Ede.h>
|
2009-11-20 20:08:29 +03:00
|
|
|
EDELIB_NS_USING(alert)
|
|
|
|
|
|
|
|
#ifdef HAVE_XKBRULES
|
2009-11-06 18:45:48 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/x.H>
|
|
|
|
#include <FL/Fl_Button.H>
|
|
|
|
#include <FL/Fl_Tabs.H>
|
|
|
|
#include <FL/Fl_Group.H>
|
|
|
|
#include <FL/Fl_Hold_Browser.H>
|
|
|
|
#include <FL/Fl_Check_Button.H>
|
|
|
|
#include <FL/Fl_Box.H>
|
|
|
|
|
|
|
|
#include <X11/XKBlib.h>
|
|
|
|
#include <X11/extensions/XKBrules.h>
|
|
|
|
|
|
|
|
#include <edelib/Window.h>
|
2012-05-07 12:53:13 +04:00
|
|
|
#include <edelib/WindowUtils.h>
|
2009-11-06 18:45:48 +03:00
|
|
|
#include <edelib/Debug.h>
|
|
|
|
#include <edelib/String.h>
|
|
|
|
#include <edelib/Resource.h>
|
2009-11-13 14:53:02 +03:00
|
|
|
#include <edelib/File.h>
|
|
|
|
#include <edelib/Run.h>
|
|
|
|
#include <edelib/MessageBox.h>
|
|
|
|
#include <edelib/ForeignCallback.h>
|
2009-11-06 18:45:48 +03:00
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
#define CONFIG_NAME "ede-keyboard"
|
|
|
|
#define PANEL_APPLET_ID "ede-keyboard"
|
|
|
|
#define DEFAULT_X_LAYOUT "us"
|
2009-11-06 18:45:48 +03:00
|
|
|
|
|
|
|
EDELIB_NS_USING_AS(Window, AppWindow)
|
|
|
|
EDELIB_NS_USING(String)
|
|
|
|
EDELIB_NS_USING(Resource)
|
2009-11-13 14:53:02 +03:00
|
|
|
EDELIB_NS_USING(file_path)
|
|
|
|
EDELIB_NS_USING(run_sync)
|
|
|
|
EDELIB_NS_USING(foreign_callback_call)
|
2012-05-07 12:53:13 +04:00
|
|
|
EDELIB_NS_USING(window_center_on_screen)
|
2009-11-06 18:45:48 +03:00
|
|
|
|
2009-11-10 17:40:21 +03:00
|
|
|
static AppWindow *win;
|
2009-11-06 18:45:48 +03:00
|
|
|
static Fl_Hold_Browser *layout_browser;
|
|
|
|
static Fl_Check_Button *repeat_press;
|
|
|
|
static Fl_Check_Button *show_flag;
|
2010-02-23 19:18:09 +03:00
|
|
|
|
2010-02-23 19:28:34 +03:00
|
|
|
static bool dialog_canceled;
|
2010-02-23 19:18:09 +03:00
|
|
|
static bool show_flag_value;
|
|
|
|
static bool repeat_press_value;
|
2009-11-06 18:45:48 +03:00
|
|
|
|
|
|
|
static const char *x11_dirs[] = {
|
|
|
|
"/etc/X11/",
|
|
|
|
"/usr/share/X11/",
|
|
|
|
"/usr/local/share/X11/",
|
|
|
|
"/usr/X11R6/lib/X11/",
|
|
|
|
"/usr/X11R6/lib64/X11/",
|
|
|
|
"/usr/local/X11R6/lib/X11/",
|
|
|
|
"/usr/local/X11R6/lib64/X11/",
|
|
|
|
"/usr/lib/X11/",
|
|
|
|
"/usr/lib64/X11/",
|
|
|
|
"/usr/local/lib/X11/",
|
|
|
|
"/usr/local/lib64/X11/",
|
|
|
|
"/usr/pkg/share/X11/",
|
|
|
|
"/usr/pkg/xorg/lib/X11/",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *x11_rules[] = {
|
|
|
|
"xkb/rules/xorg",
|
|
|
|
"xkb/rules/xfree86",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2009-11-10 17:40:21 +03:00
|
|
|
static void cancel_cb(Fl_Widget*, void*) {
|
|
|
|
win->hide();
|
2009-11-06 18:45:48 +03:00
|
|
|
dialog_canceled = true;
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:40:21 +03:00
|
|
|
static void ok_cb(Fl_Widget*, void*) {
|
|
|
|
win->hide();
|
2009-11-06 18:45:48 +03:00
|
|
|
dialog_canceled = false;
|
|
|
|
}
|
|
|
|
|
2010-02-23 19:18:09 +03:00
|
|
|
static void show_flag_cb(Fl_Widget*, void*) {
|
|
|
|
show_flag_value = (bool)show_flag->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void repeat_press_cb(Fl_Widget*, void*) {
|
|
|
|
repeat_press_value = (bool)repeat_press->value();
|
|
|
|
}
|
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
static void fetch_current_layout(String ¤t) {
|
|
|
|
char *rules_file;
|
|
|
|
XkbRF_VarDefsRec vd;
|
2009-11-06 18:45:48 +03:00
|
|
|
|
|
|
|
/* get the layout straight from X since the layout can be changed during X session */
|
2009-11-13 14:53:02 +03:00
|
|
|
if(!XkbRF_GetNamesProp(fl_display, &rules_file, &vd)) {
|
|
|
|
/* use some values */
|
|
|
|
current = DEFAULT_X_LAYOUT;
|
|
|
|
} else {
|
|
|
|
current = vd.layout;
|
|
|
|
}
|
2009-11-06 18:45:48 +03:00
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
/* free everything */
|
|
|
|
XFree(rules_file);
|
2009-11-06 18:45:48 +03:00
|
|
|
XFree(vd.layout);
|
|
|
|
XFree(vd.model);
|
|
|
|
XFree(vd.options);
|
|
|
|
XFree(vd.variant);
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:40:21 +03:00
|
|
|
static XkbRF_RulesPtr fetch_all_layouts(const String ¤t) {
|
2009-11-06 18:45:48 +03:00
|
|
|
char buf[256];
|
|
|
|
XkbRF_RulesPtr xkb_rules = NULL;
|
|
|
|
|
|
|
|
/* try to locate rules file */
|
|
|
|
for(int i = 0; x11_dirs[i]; i++) {
|
|
|
|
for(int j = 0; x11_rules[j]; j++) {
|
|
|
|
snprintf(buf, sizeof(buf), "%s%s", x11_dirs[i], x11_rules[j]);
|
|
|
|
|
2009-11-23 18:09:06 +03:00
|
|
|
xkb_rules = XkbRF_Load(buf, (char*)"", True, True);
|
2009-11-06 18:45:48 +03:00
|
|
|
if(xkb_rules)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!xkb_rules) {
|
|
|
|
E_WARNING(E_STRLOC ": Unable to load keyboard rules file\n");
|
2009-11-10 17:40:21 +03:00
|
|
|
return NULL;
|
2009-11-06 18:45:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
for(int i = 0; i < xkb_rules->layouts.num_desc; i++) {
|
|
|
|
snprintf(buf, sizeof(buf), "%s\t%s", xkb_rules->layouts.desc[i].name, xkb_rules->layouts.desc[i].desc);
|
|
|
|
layout_browser->add(buf);
|
|
|
|
|
|
|
|
if(current == xkb_rules->layouts.desc[i].name) {
|
|
|
|
/* Fl_Browser counts items from 1 */
|
|
|
|
layout_browser->select(i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:40:21 +03:00
|
|
|
return xkb_rules;
|
2009-11-06 18:45:48 +03:00
|
|
|
}
|
|
|
|
|
2010-02-23 19:18:09 +03:00
|
|
|
static void read_config(String *ret_layout) {
|
2009-11-06 18:45:48 +03:00
|
|
|
Resource r;
|
2010-02-23 19:18:09 +03:00
|
|
|
if(!r.load(CONFIG_NAME)) {
|
|
|
|
/* setup some default values */
|
|
|
|
show_flag_value = repeat_press_value = true;
|
|
|
|
if(ret_layout)
|
|
|
|
*ret_layout = DEFAULT_X_LAYOUT;
|
2009-11-06 18:45:48 +03:00
|
|
|
return;
|
2010-02-23 19:18:09 +03:00
|
|
|
}
|
2009-11-06 18:45:48 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: keyboard layout is not read from config file to set applet flag/name;
|
|
|
|
* this is done using X call. Keyboard layout is only read when '--apply' was given
|
|
|
|
* to this program, so it can be set when EDE is started
|
|
|
|
*/
|
2010-02-23 19:18:09 +03:00
|
|
|
r.get("Keyboard", "show_country_flag", show_flag_value, true);
|
|
|
|
r.get("Keyboard", "repeat_key_press", repeat_press_value, true);
|
|
|
|
|
|
|
|
if(ret_layout) {
|
|
|
|
char buf[32];
|
2009-11-06 18:45:48 +03:00
|
|
|
|
2010-02-23 19:18:09 +03:00
|
|
|
if(r.get("Keyboard", "layout", buf, sizeof(buf)))
|
|
|
|
*ret_layout = buf;
|
|
|
|
else
|
|
|
|
*ret_layout = DEFAULT_X_LAYOUT;
|
|
|
|
}
|
2009-11-06 18:45:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void save_config(const String &layout) {
|
|
|
|
Resource r;
|
|
|
|
|
2010-02-23 19:18:09 +03:00
|
|
|
r.set("Keyboard", "show_country_flag", show_flag_value);
|
|
|
|
r.set("Keyboard", "repeat_key_press", repeat_press_value);
|
2009-11-06 18:45:48 +03:00
|
|
|
r.set("Keyboard", "layout", layout.c_str());
|
|
|
|
|
|
|
|
r.save(CONFIG_NAME);
|
|
|
|
}
|
|
|
|
|
2010-02-23 19:18:09 +03:00
|
|
|
static void apply_changes_on_x(const char *current, const String *previous) {
|
|
|
|
if(repeat_press_value)
|
2009-11-13 14:53:02 +03:00
|
|
|
XAutoRepeatOn(fl_display);
|
2009-11-10 17:40:21 +03:00
|
|
|
else
|
2009-11-13 14:53:02 +03:00
|
|
|
XAutoRepeatOff(fl_display);
|
|
|
|
|
2009-11-16 14:40:52 +03:00
|
|
|
/* do not do anything if selected layout is the same as previous */
|
|
|
|
if(!previous || *previous != current) {
|
|
|
|
/*
|
|
|
|
* believe me, it is easier to call this command than to reimplmement a mess for
|
|
|
|
* uploading keyboard layout on X server!
|
|
|
|
*/
|
|
|
|
String setxkbmap = file_path("setxkbmap");
|
|
|
|
|
|
|
|
if(setxkbmap.empty()) {
|
|
|
|
alert(_("Unable to find 'setxkbmap' tool.\n\nThis tool is used as helper tool for "
|
|
|
|
"easier keyboard setup and is standard tool shipped with every X package. "
|
|
|
|
"Please install it first and run this program again."));
|
|
|
|
} else {
|
|
|
|
int ret = run_sync("%s %s", setxkbmap.c_str(), current);
|
|
|
|
/* do not show dialog since we can fail if config has bad entry when called from apply_chages_from_config() */
|
|
|
|
if(ret != 0)
|
|
|
|
E_WARNING(E_STRLOC ": 'setxkbmap %s' failed with %i\n", current, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* force panel applet to re-read config file to see if flag should be displayed or not */
|
2009-11-13 14:53:02 +03:00
|
|
|
foreign_callback_call(PANEL_APPLET_ID);
|
2009-11-06 18:45:48 +03:00
|
|
|
}
|
2009-11-20 20:08:29 +03:00
|
|
|
#endif /* HAVE_XKBRULES */
|
2009-11-10 17:40:21 +03:00
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
int main(int argc, char **argv) {
|
2009-11-20 20:08:29 +03:00
|
|
|
#ifdef HAVE_XKBRULES
|
2009-11-13 14:53:02 +03:00
|
|
|
/* must be opened first */
|
2009-11-06 18:45:48 +03:00
|
|
|
fl_open_display();
|
2009-11-13 14:53:02 +03:00
|
|
|
|
|
|
|
/* only apply what was written in config */
|
|
|
|
if(argc > 1 && strcmp(argv[1], "--apply") == 0) {
|
2010-02-23 19:18:09 +03:00
|
|
|
String layout;
|
|
|
|
|
|
|
|
read_config(&layout);
|
|
|
|
apply_changes_on_x(layout.c_str(), NULL);
|
2009-11-13 14:53:02 +03:00
|
|
|
return 0;
|
2010-02-23 19:18:09 +03:00
|
|
|
} else {
|
|
|
|
read_config(NULL);
|
2009-11-13 14:53:02 +03:00
|
|
|
}
|
|
|
|
|
2010-03-28 16:54:45 +04:00
|
|
|
EDE_APPLICATION("ede-keyboard-conf");
|
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
String cl;
|
2009-11-06 18:45:48 +03:00
|
|
|
dialog_canceled = false;
|
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
/* get layout X holds */
|
|
|
|
fetch_current_layout(cl);
|
|
|
|
|
|
|
|
/* construct GUI */
|
2009-11-10 17:40:21 +03:00
|
|
|
win = new AppWindow(340, 320, _("Keyboard configuration tool"));
|
2009-11-06 18:45:48 +03:00
|
|
|
win->begin();
|
|
|
|
Fl_Tabs *tabs = new Fl_Tabs(10, 10, 320, 265);
|
|
|
|
tabs->begin();
|
|
|
|
Fl_Group *layout_group = new Fl_Group(15, 30, 310, 240, _("Layout"));
|
|
|
|
layout_group->begin();
|
|
|
|
layout_browser = new Fl_Hold_Browser(20, 40, 300, 190);
|
|
|
|
|
|
|
|
/* so things can be nicely aligned per column */
|
|
|
|
int cwidths [] = {100, 0};
|
|
|
|
layout_browser->column_widths(cwidths);
|
|
|
|
layout_browser->column_char('\t');
|
|
|
|
|
|
|
|
layout_group->resizable(layout_browser);
|
|
|
|
|
|
|
|
show_flag = new Fl_Check_Button(20, 240, 300, 25, _("Show country flag"));
|
2010-02-23 19:18:09 +03:00
|
|
|
show_flag->tooltip(_("Display country flag in panel keyboard applet"));
|
2009-11-06 18:45:48 +03:00
|
|
|
show_flag->down_box(FL_DOWN_BOX);
|
2010-02-23 19:18:09 +03:00
|
|
|
show_flag->value(show_flag_value);
|
|
|
|
show_flag->callback(show_flag_cb);
|
2009-11-06 18:45:48 +03:00
|
|
|
layout_group->end();
|
|
|
|
Fl_Group::current()->resizable(layout_group);
|
|
|
|
|
|
|
|
Fl_Group *details_group = new Fl_Group(15, 30, 310, 240, _("Details"));
|
|
|
|
details_group->hide();
|
|
|
|
details_group->begin();
|
|
|
|
repeat_press = new Fl_Check_Button(20, 45, 300, 25, _("Repeat pressed key"));
|
2010-02-23 19:18:09 +03:00
|
|
|
repeat_press->tooltip(_("Allow pressed key to be repeated"));
|
2009-11-06 18:45:48 +03:00
|
|
|
repeat_press->down_box(FL_DOWN_BOX);
|
2010-02-23 19:18:09 +03:00
|
|
|
repeat_press->value(repeat_press_value);
|
|
|
|
repeat_press->callback(repeat_press_cb);
|
2009-11-06 18:45:48 +03:00
|
|
|
details_group->end();
|
|
|
|
tabs->end();
|
|
|
|
|
|
|
|
/* resizable box */
|
|
|
|
Fl_Box *rbox = new Fl_Box(30, 163, 65, 52);
|
|
|
|
Fl_Group::current()->resizable(rbox);
|
|
|
|
|
|
|
|
Fl_Button *ok_button = new Fl_Button(145, 285, 90, 25, _("&OK"));
|
2009-11-10 17:40:21 +03:00
|
|
|
ok_button->callback(ok_cb);
|
2009-11-06 18:45:48 +03:00
|
|
|
|
|
|
|
Fl_Button *cancel_button = new Fl_Button(240, 285, 90, 25, _("&Cancel"));
|
2009-11-10 17:40:21 +03:00
|
|
|
cancel_button->callback(cancel_cb);
|
2009-11-06 18:45:48 +03:00
|
|
|
win->end();
|
|
|
|
|
2009-11-13 14:53:02 +03:00
|
|
|
/* read all XKB layouts */
|
|
|
|
XkbRF_RulesPtr xkb_rules = fetch_all_layouts(cl);
|
2009-11-06 18:45:48 +03:00
|
|
|
|
2012-05-07 12:53:13 +04:00
|
|
|
window_center_on_screen(win);
|
2009-11-06 18:45:48 +03:00
|
|
|
win->show(argc, argv);
|
|
|
|
Fl::run();
|
|
|
|
|
|
|
|
/* do not save configuration if was canceled */
|
2009-11-10 17:40:21 +03:00
|
|
|
if(!dialog_canceled && xkb_rules && layout_browser->value()) {
|
|
|
|
int i = layout_browser->value();
|
|
|
|
/* get the layout that matches browser row */
|
|
|
|
char *n = xkb_rules->layouts.desc[i - 1].name;
|
|
|
|
|
|
|
|
save_config(n);
|
2009-11-16 14:40:52 +03:00
|
|
|
apply_changes_on_x(n, &cl);
|
2009-11-10 17:40:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(xkb_rules)
|
|
|
|
XkbRF_Free(xkb_rules, True);
|
2009-11-06 18:45:48 +03:00
|
|
|
|
2009-11-20 20:08:29 +03:00
|
|
|
#else
|
|
|
|
alert(_("ede-keyboard-conf was compiled without XKB extension. Please recompile it again"));
|
|
|
|
#endif /* HAVE_XKBRULES */
|
|
|
|
|
2009-11-06 18:45:48 +03:00
|
|
|
return 0;
|
|
|
|
}
|