Welcome to the Linux Foundation Forum!
Changing a text cursor in GNOME
E_force
Posts: 10
Hi.
Is it possible to change a color of the text cursor for all GUI applications in GNOME? If it is, how can I achieve it?
Thanks.
0
Comments
-
Create the file /home/username/.gtkrc-2.0 , and add the following text to it:
style "custom" { GtkWidget::cursor-color = "#FF0000" GtkWidget::secondary-cursor-color = "#FF0000" } widget_class "*" style "custom"
After saving, logging out and logging in again, all GTK2 applications (including all gnome applications, and gimp, firefox, etc) should use the cursor color #FF0000, which is pure red. If you don't know how to find the hexadecimal color code you want, install the application gcolor2 to help you out.0 -
Can I change it dynamically depending on, for example, the currently keyboard layout?0
-
If it's possible, I'm afraid it's beyond my knowledge.
If all you want is a way to keep track of what keyboard layout you're currently using, enabling multiple keyboard layouts in System -> Preferences -> Keyboard -> Layouts should add a keyboard layout indicator to the system tray on your panel.0 -
Thanks for the help, Jabir.
My point is to improve my experience working with multiple layouts. When you type you usually look at the text cursor thus there's no better way to show the current layout as setting an indicator right before your eyes.0 -
Actually, after a bit of googling, it seems like it might be possible after all.
I found this python script on the interwebs, which makes all GTK2 applications reload the current settings in ~/.gtkrc-2.0:#!/usr/bin/python import pygtk import gtk events=gtk.gdk.Event(gtk.gdk.CLIENT_EVENT) data=gtk.gdk.atom_intern("_GTK_READ_RCFILES", True) events.data_format=8 events.send_event=True events.message_type=data events.send_clientmessage_toall()
After a few modifications of my own, I ended up with the following script:#!/usr/bin/python import pygtk, gtk, os keyboard1="us" keyboard2="no" os.chdir(os.path.abspath(os.environ['HOME'])) if os.readlink(".gtkrc-2.0") == ".gtkrc-custom-1": print "Switching to layout 2" os.remove(".gtkrc-2.0") os.symlink(".gtkrc-custom-2", ".gtkrc-2.0") os.system("setxkbmap " + keyboard2) else: print "Switching to layout 1" os.remove(".gtkrc-2.0") os.symlink(".gtkrc-custom-1", ".gtkrc-2.0") os.system("setxkbmap " + keyboard1) events=gtk.gdk.Event(gtk.gdk.CLIENT_EVENT) data=gtk.gdk.atom_intern("_GTK_READ_RCFILES", False) events.data_format=8 events.send_event=True events.message_type=data events.send_clientmessage_toall()
The variables keyboard1 and keyboard2 at the top should be set to two different keyboard maps; in my case, us for american and no for norwegian.
You should also have two styling files, ~/.gtkrc-custom-1:style "custom" { GtkWidget::cursor-color = "#FF0000" GtkWidget::secondary-cursor-color = "#FF0000" } widget_class "*" style "custom"
and ~/.gtkrc-custom-2:style "custom" { GtkWidget::cursor-color = "#00FF00" GtkWidget::secondary-cursor-color = "#00FF00" } widget_class "*" style "custom"
And make ~/.gtkrc-2.0 a symlink to ~/.gtkrc-custom-1:ln -sf ~/.gtkrc-custom-1 ~/.gtkrc-2.0
The concept is then simple. The script checks if the file ~/.gtkrc-2.0 points to ~/.gtkrc-custom-1 (red cursor), and in that case changes the link to point to ~/.gtkrc-custom-2 (green cursor) and at the same time switches keyboard map to keyboard2. If ~/.gtkrc-2.0 does not point to ~/.gtkrc-custom-1, it switches to ~/.gtkrc-custom-1 and sets keyboard map keyboard1.
The only thing you have to do, is map the script to a keyboard shortcut, and it should handle both switching of cursor color and keymap for you. (A cleaner solution might have been to use a script that calls gtk.rc_parse_string() instead of reloading gtkrc, but I'm not familiar enough with pygtk to get that working...)
Hope this helps :P0 -
In case you don't know how to add keyboard shortcuts for custom applications in Gnome, I should perhaps add that too.
First, you have to install gconf-editor with the package management tool of your distribution. Then, press Alt+F2 and enter gconf-editor into the popup dialog.
In the application that shows up, browse to /apps/metacity/keybinding_commands in the left pane. In the right pane, set command_1 to python /path/to/script.py.
Then go to /apps/metacity/global_keybindings in the left pane, and edit run_command_1 in the right pane to the desired keyboard shortcut; e.g. to use Ctrl+Shift+F12 to run the script, make it:<Ctrl><Shift>F12
0 -
Interesting approach but how about to base the script on some variable which contains the current layout or some method which returns it? This variable or method should exist in the system. Otherwise, how the GNOME's keyboard applet would determine the layout.
Here's how it would work.case system('echo $CURR_LAYOUT') # or ENV['CURR_LAYOUT'] or curr_layout() or smth. when 'us' then symlink('~/.gtkrc-custom-1', '~/.gtkrc-2.0') when 'ru' then symlink('~/.gtkrc-custom-2', '~/.gtkrc-2.0') else symlink('~/.gtkrc-custom-default', '~/.gtkrc-2.0') end
What do you think about it?0 -
That might be a superior solution
The only way I know to get this piece of information, would be to parse the output of the command setxkbmap -v. One of the output lines should start with either symbols: pc+us+inet or symbols: pc+ru+inet.
Note that the keymap should still be set by calling e.g. setxkbmap ru from within the script. Although it would be possible to first use your old method of switching keyboard layout, and then run the script - automating this would require either (1) running the script periodically or (2) daemonizing it and watch e.g. Xorg.0.log for changes... Both of these alternatives would be a bit overkill - at least in my humble opinion.
[edit]
I just stumbled over a project called python-xkb - I haven't tried it out, but I suspect it might provide a more pythonian way of checking and switching keyboard layout. If you have some free time, perhaps it isn't that far fetched to make a PyGTK systray application that waits for XKB events and changes GTK2 settings accordingly0 -
After about an hour of coding I get a simple application which changes the cursor's color according to the layout. I used two third party scripts: your Python script which reloads GTK settings and the xkblayout application written on the C language which returns the current keyboard layout. I combined this two in my own Ruby script which checks the current layout, changes the style of the text cursor and reloads settings for all of the opened applications. Yet it can only work with GTK applications but I would like to enable Qt and the console support.
The application has a simple structure.layout_indicator/ - reload_gtk_settings.py # a Python script - show_the_current_layout # a C script - switch_layout_style.rb # a Ruby scirpt
To enable this application to work you should have Python and Ruby installed. You should also create a bunch of GTK configuration files.
First you need to create ~/.gtkrc-2.0: touch ~/.gtkrc-2.0.
The file should contain a single line.include ".gtkrc-custom"
Second you need to to create ~/.gtkrc-custom-us, ~/.gtkrc-custom-<your_language> and ~/.gtk-custom-default: touch ~/.gtkrc-custom-us ~/.gtkrc-custom-<your_language> ~/.gtk-custom-default.
They should contain the following.
~/.gtk-custom-usstyle "custom" { GtkWidget::cursor-color = "#0000FF" GtkWidget::secondary-cursor-color = "#0000FF" } widget_class "*" style "custom"
This style specifies the blue color for the en-us layout.
~/.gtk-custom-<your-language>style "custom" { GtkWidget::cursor-color = "#FF0000" GtkWidget::secondary-cursor-color = "#FF0000" } widget_class "*" style "custom"
This style specifies the red color for your layout.
~/.gtk-custom-defaultstyle "custom" { GtkWidget::cursor-color = "#000000" GtkWidget::secondary-cursor-color = "#000000" } widget_class "*" style "custom"
This style specifies the black color for the default layout.
Third you need to create a symlink to the ~/.gtkrc-custom-us file: ln -sf /home/<username>/.gtkrc-custom-us /home/<username>/.gtkrc-custom.
Note the symlink should be named .gtkrc-custom.
Fourth you need to place an application directory somewhere. You could move it to the ~/my_scripts directory for example.
Fifth you should change the lines 30, 31 and 32 of the ruby script replacing the default Russian layout to your own.30. when 'RU' # change to your layout 31. if File.readlink("#{home}/.gtkrc-custom") != "#{home}/.gtkrc-custom-ru" # change the .gtkrc-custom-ru file to .gtkrc-custom-<your_language> here 32. ln_sf("#{home}/.gtkrc-custom-ru", "#{home}/.gtkrc-custom") # and here
And after all of that you can run the script <path_to_layout_indicator>/switch_layout_style.rb. Of course you should have the reload_gtk_settings.py, switch_layout_style.rb and show_the_current_layout files executable. You can use the chmod u+x <filename> command to change ther modes.
To have an ability to autoload that script you can add it to the application list which GNOME loads on the start. To achieve it you should start the application launcher (usually Alt+F2) and then type gnome-session-properties there. The session window should appear and after that you can add the script to the autoloaded application list.
Here's the list of the features that I would like to add to the application.
- an ability to load the application at the start of the system but not GNOME's;
- an ability to change another properties of the text cursor such as its width, blink rate etc.;
- an ability to change the cursor's properties not only for GTK applications but also for Qt and XUL.;
- an ability to change the cursor's color in the console;
- an ability to change the color of the rectangular selection box.
I'd like to hear what you think about how to realise them.
I attach the files of the application to the post. The archive contains the application itself in the layout_indicator directory and the source code of xkblayout. Probably, you'll need to compile it by itself. If so don't forget to rename the output executable file to show_the_current_layout and add it to the layout_indicator dir.
[file name=layout_indicator_source.tar.gz size=4086]http://www.linux.com/media/kunena/attachments/legacy/files/layout_indicator_source-fca906721321cd80791ca9b06754da7e.gz[/file]0 -
Good job, this project is definitely going in the right direction!
By the way, to make it easier for end users to configure, what do you think of the following approach:[ul][li]Rename ~/.gtkrc-custom-ru and ~/.gtkrc-custom-en to something like ~/.gtkrc-custom-main and ~/.gtkrc-custom-alt[/li][li]Make switch_layout_style.rb automatically run show_the_current_layout when it is started, and save the output to some variable (lets say kbd_default).[/li][li]Use ~/.gtkrc-custom-main whenever the current keyboard layout is set to $kbd_default, and use ~/.gtkrc-custom-alt when it is set to something else.[/li][/ul]If your keyboard daemon is automatically started when you login (see below), this approach would make the settings in ~/.gtkrc-custom-main be used whenever you use your default keymap (the one used first when you start X), and the settings in ~/.gtkrc-custom-alt would be used when you use your other keymaps. And the user wouldn't have to configure anything.
As for your questions:
1. An ability to load the application at the start of the system but not GNOME's;
According to the freedesktop.org standards, autostart applications should be defined in the directory $XDG_CONFIG_DIRS/autostart/ (system-wide configuration) or $XDG_CONFIG_HOME/autostart/ (per-user configuration). If the variables are not set, $XDG_CONFIG_DIRS defaults to /etc/xdg/ and $XDG_CONFIG_HOME defaults to ~/.config/. To get your application automatically started, you should place a .desktop-file for it in one of these directories.
Here's a simple .desktop file I created. I placed your program in /opt/layoutindicator/ and the following autostart file in /etc/xdg/autostart/layoutindicator.desktop, and it worked[Desktop Entry] Name=Keyboard Layout Indicator Comment=Changes settings based on current keyboard layout Exec=ruby /opt/layoutindicator/switch_layout_style.rb Terminal=false Type=Application
2. An ability to change another properties of the text cursor such as its width, blink rate etc.;
That should be easy by tweaking the custom ~/.gtkrc. Here are the relevant portions of the GTK+ Reference Manual:"gtk-cursor-blink" boolean
Whether the cursor should blink.
Default value: TRUE
"gtk-cursor-blink-time" integer
Length of the cursor blink cycle, in milleseconds.
Allowed values: >= 100
Default value: 1200
3. An ability to change the cursor's properties not only for GTK applications but also for Qt and XUL.
This should definitely be possible, but I'm afraid I don't know how to do it. Let's hope either Google or a friendly Qt wizard knows how :-)
4. An ability to change the cursor's color in the console
I don't think this is possible. You might be able to reconfigure the cursor color of specific terminal emulators – by using gconf for gnome-terminal and the file ~/.Xresources for most other terminals – but you would (1) have to reconfigure the settings of every terminal emulator separately, and (2) I'm not sure you could reload the settings without restarting the terminal.
5. An ability to change the color of the rectangular selection box.
I know that this one is possible. Remember that all GTK2 themes do their magic by using a gtkrc file, and different themes use different colors for their selection boxes. For inspiration, I tried to look inside /usr/share/themes/Industrial/gtk-2.0/gtkrc, and experimented with putting the options that looked relevant in my ~/.gtkrc-custom files. Modifying one of the ~/.gtkrc-custom files to the following did the trick:style "custom" { GtkWidget::cursor-color = "#FF0000" GtkWidget::secondary-cursor-color = "#FF0000" base[SELECTED] = "#FF0000" text[SELECTED] = "#000000" } widget_class "*" style "custom"
Suggestions
I also have a couple of other suggestions for your project. If you're planning a configuration utility of some kind, it should be able to:[ul][li]Enable/disable autostarting this daemon by modifying a .desktop file in ~/.config/autostart/. (Local settings always override the system defaults in /etc/xdg/autostart/.)[/li][li]Autogenerate ~/.gtkrc-custom-files if they don't exist[/li][li]Change the colors associated with the two keyboard layouts. If you plan to implement a graphical configuration utility, a color picker (similar to gcolor2) would be nice. There should also be a function to just swap the places of the two gtkrc-files.[/li][/ul]
It might also be possible to use the Ruby bindings for GTK2 to force re-reading gtkrc-files. That would mean that you could drop the Python dependencies of your application, and reduce the footprint by not needing two different interpreters.
[edit]
To mods: Perhaps the thread should be moved from Getting Started with Linux to Software Development now :P0 -
Wow, guys, this is some great stuff.0
Categories
- All Categories
- 167 LFX Mentorship
- 219 LFX Mentorship: Linux Kernel
- 795 Linux Foundation IT Professional Programs
- 355 Cloud Engineer IT Professional Program
- 179 Advanced Cloud Engineer IT Professional Program
- 82 DevOps Engineer IT Professional Program
- 127 Cloud Native Developer IT Professional Program
- 112 Express Training Courses
- 138 Express Courses - Discussion Forum
- 6.2K Training Courses
- 48 LFC110 Class Forum - Discontinued
- 17 LFC131 Class Forum
- 35 LFD102 Class Forum
- 227 LFD103 Class Forum
- 14 LFD110 Class Forum
- 39 LFD121 Class Forum
- 15 LFD133 Class Forum
- 7 LFD134 Class Forum
- 17 LFD137 Class Forum
- 63 LFD201 Class Forum
- 3 LFD210 Class Forum
- 5 LFD210-CN Class Forum
- 2 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum - Discontinued
- 1 LFD233 Class Forum
- 2 LFD237 Class Forum
- 23 LFD254 Class Forum
- 697 LFD259 Class Forum
- 109 LFD272 Class Forum
- 3 LFD272-JP クラス フォーラム
- 10 LFD273 Class Forum
- 154 LFS101 Class Forum
- 1 LFS111 Class Forum
- 1 LFS112 Class Forum
- 1 LFS116 Class Forum
- 1 LFS118 Class Forum
- LFS120 Class Forum
- 7 LFS142 Class Forum
- 7 LFS144 Class Forum
- 3 LFS145 Class Forum
- 1 LFS146 Class Forum
- 3 LFS147 Class Forum
- 1 LFS148 Class Forum
- 15 LFS151 Class Forum
- 1 LFS157 Class Forum
- 33 LFS158 Class Forum
- 8 LFS162 Class Forum
- 1 LFS166 Class Forum
- 1 LFS167 Class Forum
- 3 LFS170 Class Forum
- 2 LFS171 Class Forum
- 1 LFS178 Class Forum
- 1 LFS180 Class Forum
- 1 LFS182 Class Forum
- 1 LFS183 Class Forum
- 29 LFS200 Class Forum
- 736 LFS201 Class Forum - Discontinued
- 2 LFS201-JP クラス フォーラム
- 14 LFS203 Class Forum
- 102 LFS207 Class Forum
- 1 LFS207-DE-Klassenforum
- 1 LFS207-JP クラス フォーラム
- 301 LFS211 Class Forum
- 55 LFS216 Class Forum
- 48 LFS241 Class Forum
- 42 LFS242 Class Forum
- 37 LFS243 Class Forum
- 15 LFS244 Class Forum
- LFS245 Class Forum
- LFS246 Class Forum
- 50 LFS250 Class Forum
- 1 LFS250-JP クラス フォーラム
- LFS251 Class Forum
- 154 LFS253 Class Forum
- LFS254 Class Forum
- LFS255 Class Forum
- 5 LFS256 Class Forum
- 1 LFS257 Class Forum
- 1.3K LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 111 LFS260 Class Forum
- 159 LFS261 Class Forum
- 41 LFS262 Class Forum
- 82 LFS263 Class Forum - Discontinued
- 15 LFS264 Class Forum - Discontinued
- 11 LFS266 Class Forum - Discontinued
- 20 LFS267 Class Forum
- 24 LFS268 Class Forum
- 29 LFS269 Class Forum
- 1 LFS270 Class Forum
- 199 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- LFS274 Class Forum
- 3 LFS281 Class Forum
- 9 LFW111 Class Forum
- 261 LFW211 Class Forum
- 182 LFW212 Class Forum
- 13 SKF100 Class Forum
- 1 SKF200 Class Forum
- 1 SKF201 Class Forum
- 782 Hardware
- 198 Drivers
- 68 I/O Devices
- 37 Monitors
- 96 Multimedia
- 174 Networking
- 91 Printers & Scanners
- 83 Storage
- 758 Linux Distributions
- 80 Debian
- 67 Fedora
- 15 Linux Mint
- 13 Mageia
- 23 openSUSE
- 143 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 348 Ubuntu
- 461 Linux System Administration
- 39 Cloud Computing
- 70 Command Line/Scripting
- Github systems admin projects
- 90 Linux Security
- 77 Network Management
- 101 System Management
- 46 Web Management
- 64 Mobile Computing
- 17 Android
- 34 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 371 Off Topic
- 114 Introductions
- 174 Small Talk
- 19 Study Material
- 507 Programming and Development
- 285 Kernel Development
- 204 Software Development
- 1.8K Software
- 211 Applications
- 180 Command Line
- 3 Compiling/Installing
- 405 Games
- 309 Installation
- 97 All In Program
- 97 All In Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)