This is an outstanding issue in Unity:
The code below has been updated, now it can use icons to show state. But it may get slow sometime as I has to update icon file on hard drive then reload it again. (See notes about this issue/limitation in libappindicator
)
A well packaged release was made available on webupd8 ppa (Thanks goes to Alin Andrei /Andrew/)
sudo add-apt-repository ppa:nilarimogard/webupd8
sudo apt-get update
sudo apt-get install indicator-xkbmod
Reference: Keyboard Modifiers State indicator For Ubuntu: Xkbmod Indicator
Original Answer:
This is not attended to be a canonical answer to the question. It could be counted as a work around. Hopping someone writes sophisticated solution for it.
This a simple prototype keyboard modifiers indicator for Unity.
Image starting from left: Icon, Shift, Locked Caps, Ctrl, Alt, Super, Locked AltGr
(Small circle to indicate locked state)
Source file unity-xkbmod.c
:
/*
* unity-xkbmod.c
*
* Copyright 2014 Sneetsher <sneetsher@localhost>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <string.h>
#include <X11/XKBlib.h>
#include <glib/gprintf.h>
#include <gtk/gtk.h>
#include <libappindicator/app-indicator.h>
//callback data structure
typedef struct _AppData {
Display *_display;
int *_deviceId;
AppIndicator *indicator;
} AppData;
//menu ui
static GtkActionEntry entries[] = {
{ "Quit", "application-exit", "_Quit", "<control>Q",
"Exit the application", G_CALLBACK (gtk_main_quit) },
};
static guint n_entries = G_N_ELEMENTS (entries);
static const gchar *ui_info =
"<ui>"
" <popup name='IndicatorPopup'>"
" <menuitem action='Quit' />"
" </popup>"
"</ui>";
//callback function, get xkb state, update indicator label (icon have limitation)
static gboolean update_xkb_state (gpointer data)
{
//get xkb state
XkbStateRec xkbState;
XkbGetState(((AppData*) data)->_display, *(((AppData*) data)->_deviceId), &xkbState);
//construct label
GString *label = g_string_new("");
register int i;
unsigned bit;
//loop taken from xkbwatch source
for (i = XkbNumModifiers - 1, bit = 0x80; i >= 0; i--, bit >>= 1)
{
//printf("base%d %s ", i, (xkbState.base_mods & bit) ? "on " : "off");
//printf("latched%d %s ", i, (xkbState.latched_mods & bit) ? "on " : "off");
//printf("locked%d %s ", i, (xkbState.locked_mods & bit) ? "on " : "off");
//printf("effective%d %s ", i, (xkbState.mods & bit) ? "on " : "off");
//printf("compat%d %s\n", i, (xkbState.compat_state & bit) ? "on " : "off");
//todo: change constant with xkb modifier constant (defined in the headers)
// show effective modifier stat
switch (i)
{
case 7:
g_string_prepend (label, ((xkbState.mods & bit) ? "⎇" : ""));
break;
case 6:
g_string_prepend (label, ((xkbState.mods & bit) ? "⌘" : ""));
break;
case 5:
g_string_prepend (label, ((xkbState.mods & bit) ? "5" : ""));
break;
case 4:
g_string_prepend (label, ((xkbState.mods & bit) ? "①" : ""));
break;
case 3:
g_string_prepend (label, ((xkbState.mods & bit) ? "⌥" : ""));
break;
case 2:
g_string_prepend (label, ((xkbState.mods & bit) ? "⋀" : ""));
break;
case 1:
g_string_prepend (label, ((xkbState.mods & bit) ? "⇬" : ""));
break;
case 0:
g_string_prepend (label, ((xkbState.mods & bit) ? "⇧" : ""));
break;
default:
break;
};
// show if modifier is locked
g_string_prepend (label, ((xkbState.locked_mods & bit) ? " ˳" : " "));
}
//g_string_prepend (label, "");
app_indicator_set_label (((AppData*) data)->indicator, label->str, NULL);
//g_free(label);
return TRUE;
}
int main (int argc, char **argv)
{
AppData appdata;
Display *_display;
int _deviceId;
char* displayName = strdup("");
int eventCode;
int errorReturn;
int major = XkbMajorVersion;
int minor = XkbMinorVersion;;
int reasonReturn;
AppIndicator *indicator;
GtkWidget *indicator_menu;
GtkUIManager *uim;
GtkActionGroup *action_group;
GError *error = NULL;
gtk_init (&argc, &argv);
XkbIgnoreExtension(False);
g_printf("Xkb client lib ver: %d.%d\n" , major , minor );
_display = XkbOpenDisplay(displayName, &eventCode, &errorReturn,
&major, &minor, &reasonReturn);
g_printf("Xkb server lib ver: %d.%d\n" , major , minor );
if (reasonReturn != XkbOD_Success)
{
g_printf("Unable to open display!\n");
return 1;
}
XkbDescRec* kbdDescPtr = XkbAllocKeyboard();
if (kbdDescPtr == NULL)
{
g_printf ("Failed to get keyboard description.\n");
return 2;
}
kbdDescPtr->dpy = _display;
_deviceId = kbdDescPtr->device_spec;
/*
//no need for event listener, used gtk_timeout timer
XkbSelectEventDetails(_display, XkbUseCoreKbd, XkbStateNotify,
XkbAllStateComponentsMask, XkbGroupStateMask);
*/
action_group = gtk_action_group_new ("AppActions");
gtk_action_group_add_actions (action_group, entries, n_entries, NULL);
indicator = app_indicator_new_with_path (
"Simple XKB Modifier Indicator",
"icon",
APP_INDICATOR_CATEGORY_HARDWARE,
g_get_current_dir());
uim = gtk_ui_manager_new ();
gtk_ui_manager_insert_action_group (uim, action_group, 0);
if (!gtk_ui_manager_add_ui_from_string (uim, ui_info, -1, &error))
{
g_printf ("Failed to build menus: %s\n", error->message);
g_error_free (error);
error = NULL;
return 3;
}
indicator_menu = gtk_ui_manager_get_widget (uim, "/ui/IndicatorPopup");
app_indicator_set_menu (indicator, GTK_MENU (indicator_menu));
app_indicator_set_status (indicator, APP_INDICATOR_STATUS_ACTIVE);
//app_indicator_set_label (indicator, " ⇧ ⋀ ⌥ ⎇ ⌘ ", NULL);
//symbols: shift U21E7 ctrl U22C0 alt/altgr U2325 U2387 cmd U2318
//from font: DejaVu Sans
appdata._display = _display;
appdata._deviceId = &_deviceId;
appdata.indicator = indicator;
gtk_timeout_add (120, (GtkFunction) update_xkb_state, &appdata);
//nice for realtime tasks, to replace gtk_timeout
//gtk_idle_add ((GtkFunction) idle_func, &appdata);
gtk_main ();
XCloseDisplay (_display);
return 0;
}
Installing needed headers/libs: (Not sure if I miss any)
sudo apt-get install libx11-dev libappindicator-dev libgtk2.0-dev
Compiling:
gcc -Wall unity-xkbmod.c -o unity-xkbmod `pkg-config --cflags --libs appindicator-0.1` -lX11
Run:
./unity-xkbmod
Note:
libappindicator
used for Unity indicators lack an important feature which make easy to ports other desktops indicators. See Bug #812067
API needed: pixbuf icon setting support
Without this feature, let's say we need (Shift, Ctrl, Alt, AltGr, Super) with sticky keys active; we have 3 main status for each (Off, On/Latched, Locked). So 3^5 combined icons should be generated. (Where normal case just 3x5 single icons)
That's why I used indicator label with symbols from DejaVu Sans font.
To put an icon, place it in same folder and name it icon.*
. Accepted formats: png, svg, ico, xpm ...
If you don't like any icon, just create a 1x1 px image instead.
References:
System Indicator Service
Well, it is really simpler then I expected. There is no specific API for it. Because it is just a GSimpleActionGroup & with corresponding GMenu's exported through DBus then Unity is told about their presence using declaration file with same name put in /usr/share/unity/indicators/
. No need for any other library.
Here a very small C language example:
Get a copy of tests/indicator-test-service.c
from libindicator
source
apt-get source libindicator
cp libindicator-*/tests/indicator-test-service.c .
cp libindicator-*/tests/com.canonical.indicator.test* .
indicator-test-service.c no changes
#include <gio/gio.h>
typedef struct
{
GSimpleActionGroup *actions;
GMenu *menu;
guint actions_export_id;
guint menu_export_id;
} IndicatorTestService;
static void
bus_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
IndicatorTestService *indicator = user_data;
GError *error = NULL;
indicator->actions_export_id = g_dbus_connection_export_action_group (connection,
"/com/canonical/indicator/test",
G_ACTION_GROUP (indicator->actions),
&error);
if (indicator->actions_export_id == 0)
{
g_warning ("cannot export action group: %s", error->message);
g_error_free (error);
return;
}
indicator->menu_export_id = g_dbus_connection_export_menu_model (connection,
"/com/canonical/indicator/test/desktop",
G_MENU_MODEL (indicator->menu),
&error);
if (indicator->menu_export_id == 0)
{
g_warning ("cannot export menu: %s", error->message);
g_error_free (error);
return;
}
}
static void
name_lost (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
IndicatorTestService *indicator = user_data;
if (indicator->actions_export_id)
g_dbus_connection_unexport_action_group (connection, indicator->actions_export_id);
if (indicator->menu_export_id)
g_dbus_connection_unexport_menu_model (connection, indicator->menu_export_id);
}
static void
activate_show (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
g_message ("showing");
}
int
main (int argc, char **argv)
{
IndicatorTestService indicator = { 0 };
GMenuItem *item;
GMenu *submenu;
GActionEntry entries[] = {
{ "_header", NULL, NULL, "{'label': <'Test'>,"
" 'icon': <'indicator-test'>,"
" 'accessible-desc': <'Test indicator'> }", NULL },
{ "show", activate_show, NULL, NULL, NULL }
};
GMainLoop *loop;
indicator.actions = g_simple_action_group_new ();
g_simple_action_group_add_entries (indicator.actions, entries, G_N_ELEMENTS (entries), NULL);
submenu = g_menu_new ();
g_menu_append (submenu, "Show", "indicator.show");
item = g_menu_item_new (NULL, "indicator._header");
g_menu_item_set_attribute (item, "x-canonical-type", "s", "com.canonical.indicator.root");
g_menu_item_set_submenu (item, G_MENU_MODEL (submenu));
indicator.menu = g_menu_new ();
g_menu_append_item (indicator.menu, item);
g_bus_own_name (G_BUS_TYPE_SESSION,
"com.canonical.indicator.test",
G_BUS_NAME_OWNER_FLAGS_NONE,
bus_acquired,
NULL,
name_lost,
&indicator,
NULL);
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
g_object_unref (submenu);
g_object_unref (item);
g_object_unref (indicator.actions);
g_object_unref (indicator.menu);
g_object_unref (loop);
return 0;
}
com.canonical.indicator.test modified to add lock & greeter mode
[Indicator Service]
Name=indicator-test
ObjectPath=/com/canonical/indicator/test
[desktop]
ObjectPath=/com/canonical/indicator/test/desktop
[desktop_greeter]
ObjectPath=/com/canonical/indicator/test/desktop
[desktop_lockscreen]
ObjectPath=/com/canonical/indicator/test/desktop
com.canonical.indicator.test.service remove .in
postfix from filename and change the executable path
[D-BUS Service]
Name=com.canonical.indicator.test
Exec=/usr/lib/x86_64-linux-gnu/indicator-test/indicator-test-service
Compile it
gcc -o indicator-test-service indicator-test-service.c `pkg-config --cflags --libs gtk+-3.0`
Manual Installation
sudo su
mkdir /usr/lib/x86_64-linux-gnu/indicator-test/
cp indicator-test-service /usr/lib/x86_64-linux-gnu/indicator-test/
cp com.canonical.indicator.test /usr/share/unity/indicators/
cp com.canonical.indicator.test.service /usr/share/dbus-1/services/
Configuration for Greeter, override the default indicators list
90_unity-greeter.gschema.override
[com.canonical.unity-greeter]
indicators=['ug-accessibility', 'com.canonical.indicator.keyboard', 'com.canonical.indicator.session', 'com.canonical.indicator.datetime', 'com.canonical.indicator.power', 'com.canonical.indicator.sound', 'com.canonical.indicator.test', 'application']
Install
cp 90_unity-greeter.gschema.override /usr/share/glib-2.0/schemas/
glib-compile-schemas /usr/share/glib-2.0/schemas/
Test
sudo service lightdm restart
Notes
DBus service is troublesome, if you want user to be able to close application anytime. It is better to use autostart instead, like default indicators do.
I have uploaded ready files here:
https://github.com/sneetsher/mysystemindicator_minimum
and a modified copy here:
https://github.com/sneetsher/mysystemindicator
Where I have tried different menu for different mode. It could be installed and tested quickly.
This seems too simple and can be easily ported to any other language that have support for GIO Gnome lib (including DBus). As I'm looking for python, I may add it later.
References:
System Indicator Plugin
This is not full standalone indicator as the one above, it is just a share lib plugin, similar to libappmenu.so
& libprintersmenu.so
(application menu & printer indicator). It could be shown only in regular user session & greeter (Not on lock screen).
I couldn't make it work in my current machine, but I did before. Here the steps, may be I'm missing something.
Using same source above of libindicator
test/libdummy-indicator-*.c
are examples (simple & visible the ones show up on the panel)
Compile
./autogen.sh
make
Install
sudo cp tests/.libs/libdummy-indicator-visible.so /usr/lib/indicators3/7/libdummy.so
Configure to show in greeter screen
90_unity-greeter.gschema.override use same name without lib
prefix and .so
extension.
[com.canonical.unity-greeter]
indicators=['ug-accessibility', 'com.canonical.indicator.keyboard', 'com.canonical.indicator.session', 'com.canonical.indicator.datetime', 'com.canonical.indicator.power', 'com.canonical.indicator.sound', 'application', 'dummy']
Install
cp 90_unity-greeter.gschema.override /usr/share/glib-2.0/schemas/
glib-compile-schemas /usr/share/glib-2.0/schemas/
Best Answer
I have also written an application indicator which, in my opinion, looks better.
indicator-keyboard-led
It shows the state of the locks using filled/unfilled circles.
Default appearance of the indicator with Num lock on and Caps and Scroll locks off.
Menu of the indicator, shown on click. The locks can be toggled by clicking the respective item in the menu.
Alternative (short) appearance of the indicator.
You can also change which locks are displayed and in what order.
Installation:
After installation the postinst script will prompt you for preferences configuration. Visit the GitHub repo for full explanation on these settings.
Usage:
The indicator will be configured to autostart on log-in. To start using the indicator after installation, log-out and log-in again, or manually start the indicator (search for "indicator-keyboard-led" in the dash).
The indicator should be shown at the top right corner, with a filled circle representing a lock turned on and an unfilled circle representing a lock turned off.
Clicking on the indicator should result in a menu with the three locks. Clicking on the menu item would cause the corresponding lock to toggle.
Bug reports and feature requests welcome.