Handling KDE Window Rules

This is not a trivial matter, but like any other, it can be resolved with perseverance. We have persevered, with the following pleasant results.

KDE Window Rules are stored in ~/.config/kwinrulesrc.

The (not very complete) available documentation for KDE activities and window rules:

https://docs.kde.org/trunk5/en/kde workspace/kcontrol/windowspecific/windowspecific.pdf

 describes the various options for parameter settings but does not connect those (alphabetical) choices to the numeric choices in kwinrulesrc.

kwinrulesrc parameter blocks

KDE Window Rules are stored in ~/.config/kwinrulesrc. This file comprises a block of text for each window that is opened in the course of a Linux KDE session.

Each block contains two primary fields ([n] and Description) and then a number of parameter and rule pair fields:

[n]
Description=name of_the_window_rule
parameter1
parameter1rule
parameter2
parameter2rule

Sequence number field [n]

The [n] field is a sequence number determining the order in which the window rule for a particular window appears in the Window Rule manager application.

You must first sort the blocks by Description and then renumber the blocks in order to have the rules appear in alphabetical order in the Window Rule manager application.

Description field

The Description field value is an arbitrary string. We choose to name them canonically as

[Application] [Activity], e.g.:
Konqueror Finance
Geany Finance
Chromium Plans

so an alphabetical sort groups the rules by application.

Parameter fields

A complete list of all possible fields is available in

https://github.com/KDE/kwin/blob/master/rules.cpp

Not all are present in each rule block.

The parameter pair fields comprise the parameter name and the parameter rule. Each may be either the default, explicit, or absent altogether.

  • Default rules are indicated in the canonical form name[$d]
  • Explicit rules are indicated in the canonical form name=value
  • Absent rules appear inactive and greyed out in the Window Rules manager.

We must ensure that the rule exists in the block if we wish to change its value. There may be a clever way to use sed or awk to insert a missing rule between two existing rules, but we haven’t taken the time to figure that out. It’s simply more convenient at present to edit kwinrulesrc in the Window Rules editor to select (and thereby activate) the desired parameter(s), after which the file can be edited with automation.

Numerical rule values

We have experimented extensively and come up with what appears to be the correct list, but there is more experimentation needed and insufficient time at present to do it. The list appears to be:

1 Do not affect
2 Force
3 Apply initially
4 Remember
5 Apply now
6 Force temporarily

Initial Parameters

We would like to be able to specify a set of parameters to be applied initially when a window opens. In particular, we wish to specify

  • The activity or activities in which it appears
  • Its initial size and position

The default settings for the various parameters seem to be stored in code, not in a specific file, so we cannot simply edit such a file.

What we can do is:

  • Determine the alphanumeric values for the parameters of interest
  • Edit kwinrulesrc globally to embed those values.
  • Restart the window manager kwin with a new kwinrulesrc.

Determine the alphanumeric values for the parameters of interest

  • Activity values

Activities are specified by a complicated hexadecimal number determined by your system. See Shortcuts

Our values are (YMMV):

activity=a-complicated-hexadecimal-number-determined-by-your-system-see-Shortcuts
activityrule=2 #Force

  • Positional values
    Our values are (YMMV):
desktop=1
desktoprule=4 #Force
ignorgeometry=True
ignorgeometryrule=4 #Force
placement=Centered
placementrule=4 #Force
position=179,111
positionrule=5 #Apply now
size=1250,700
sizerule=3 #Apply initially
strictgeometry=True
strictgeometryrule=4 #Force
titlematch=2

Editing the rules

  • We cannot edit kwinrulesrc directly because kwin detects any changes and overwrites them with the values in memory.
    • We copy kwinrulesrc to kwinrulesrc.lcd to make the edits and then transfer the changes when we are done.
  • We cannot use a text editor to change a parameter rule because the parameter value and rule start with the same string.
    • We use some clever sed code to first change all default [$d] strings to an explicit string, then another bit of sed code to change any explicit value to the desired value:

We use sed in a script kwin_edit to convert default rules to an explicit rule:

sed i 's/parameter[][]$d[]]/parameter=1/g' ~/.config/kwinrulesrc.lcd
changes parameter[$d] to parameter=1 globally (/g) throughout the file.

sed i 's/parameterrule[][]$d[]]/parameterrule=1/g' ~/.config/kwinrulesrc.lcd
changes parameterrule[$d] to parameterrule=1 globally (/g) throughout the file.

We then use sed to globally change our now default explicit value to the desired value:

sed i 's/parameter=.*/marameter=desired_value/g' ~/.config/kwinrulesrc.lcd
sed i 's/parameterrule=.*/marameterrule=desired_value/g' ~/.config/kwinrulesrc.lcd
We repeat this for each parameter and rule.
You must then revisit

Description=Plasmashell

and set all its values to default [$d]. It will set the entire screen and task bar off if you do not make this correction.

  • We use msort to sort alphabetically by Description
  • We use Renumber to renumber the blocks sequentially

Restart the window manager kwin with a new kwinrulesrc

The kwin_replace script:

  • Stops the current version of kwin with the kill -9 switch
  • Copies kwinrulesrc.lcd to kwinrulesrc
  • Restarts kwin with the –replace switch

Now the Window Rules appear alphabetically sorted by Description in the Windows Rule manager and all windows begin Centered on the screen (at position 179,111, the default) with a size of 1250,700 in the specified activity.

Easy when you know how.