Bug 392840 - Can't seem to use print() multiple times
Summary: Can't seem to use print() multiple times
Status: CONFIRMED
Alias: None
Product: kwin
Classification: Plasma
Component: scripting (show other bugs)
Version: 5.12.4
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords:
: 477069 (view as bug list)
Depends on:
Blocks:
 
Reported: 2018-04-07 18:52 UTC by LinG
Modified: 2023-12-08 11:10 UTC (History)
6 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
screenshot of Bustle (25.38 KB, image/png)
2020-08-02 22:22 UTC, Yuxin Wu
Details

Note You need to log in before you can comment on or make changes to this bug.
Description LinG 2018-04-07 18:52:59 UTC
When trying to use the print() method multiple times in a kwin script using the wm console only the first print() statement is printed to the console.

example:

script:
print("hi")
print("hihi")

output:
hi

expected output:
hi
hihi
Comment 1 Sigma 2018-05-26 09:15:54 UTC
I am not a KDE developer, but this works for me, are you using JavaScript or QML for your script.
And did you add the second print command after you ran your script with only the first print?
Comment 2 LinG 2018-05-26 09:26:08 UTC
just javascript, everything works fine when I only use the print() method once. But when I start to use the print() method multiple times in quick succession then it won't print() some of the print() methods. When I use the "plasma" option to execute the script it will print both but when I use the "kwin" option it will skip some print() statements.

It seems to depend on how long kwin is busy with executing the commands between the consecutive print statements.

for example, if I put a long loop between the two print() statements then both will execute, like this example:

print('hi');
var a = 0;
for (var i = 0 ; i < 10000000; i++)
{
  a += i;
}
print('hihi');

but doing only:

print('hi');
print('hihi');

seems to be too fast and kwin will not print the second statement to console.
Comment 3 Sigma 2018-05-26 09:31:40 UTC
Interesting, I can reproduce this on my KDE 5.12.5, but this happens only if I switch between the KWin and Plasma "modes".
It works if I leave it at KWin (the default).
Comment 4 LinG 2018-05-26 09:48:46 UTC
That's because of a different bug (which I should probably report as well...). But if you open "WM console" it shows the default state as "kwin" but it's actually on "plasma" because you don't have access to some of the kwin globals. You need to manually switch to "plasma" and then back to "kwin" to actually be able to run the script as a kwin script.
Comment 5 LinG 2018-05-26 09:51:30 UTC
you can test that you are not on "kwin" because you can't use the global `workspace` for example if you try this, when you first open the console:

print(workspace.desktopGridWidth);

but if you switch to "plasma" and back to "kwin" then you access to these globals.
Comment 6 Sigma 2018-05-26 10:00:23 UTC
I noticed that workspace was missing after my comment and went to test a few things.
The missing print outputs also seem to be a scripting console bug because all messages are printed in KWin's console output.
Comment 7 Sigma 2018-05-26 10:01:58 UTC
print("hi")
print("hihi")

prints "Hi" in the scripting console and "hi\nhihihi" to KWin's stdout if you enable debug output in KDebugSettings.
Comment 8 LinG 2018-05-26 10:07:37 UTC
Ah okay, so it seems to be purely a console thing. Btw I've only ever used the "wm console" to write my kwin scripts, could you explain to me how to use and set this "KDebugSettings" variable? That would make my kwin scripting a lot easier for now as long as this bug exists.
Comment 9 Sigma 2018-05-26 10:17:19 UTC
KDebugSettings is an application to configure logging for KDE applications ( https://community.kde.org/KDevelop/Debugging is the only documentation I was able to find).
Increase the level for "KWin Scripting" (I am using "all") to make KWin log the print()s.
I am viewing the log output by restarting KWin from Konsole using "kwin_x11 --replace".
Comment 10 Yuxin Wu 2020-08-02 12:10:18 UTC
I run into the same bug. By launching kwin under:
```
QDBUS_DEBUG=1 QT_LOGGING_RULES="kwin_scripting.debug=true"
```

I can see that the missing logs are printed by kwin, but was not sent through dbus -- occasionally only some of the messages are sent through dbus.

After adding a sleep statement before `emit print(message);` in `scripting.cpp`, all messages can be sent normally.
Comment 11 David Edmundson 2020-08-02 21:14:56 UTC
Running bustle it seems the kwin side is correct.

We get the print signal emitted twice before run returns.
Comment 12 David Edmundson 2020-08-02 22:15:59 UTC
Indeed, reproduced occasionally.

From what I understand of the bug a port to using an adaptor will fix this as then it won't need to scan our metaObject so to emit the signal from the DBus thread which might be behind our main thread.
Comment 13 Yuxin Wu 2020-08-02 22:22:13 UTC
Created attachment 130595 [details]
screenshot of Bustle

When I run bustle it seems to show kwin side is incorrect. I put the following code into the scripting console:
```
print(workspace.numScreens);
print(workspace.workspaceWidth);
print(workspace.workspaceWidth+1);
print(workspace.workspaceWidth+2);
print(workspace.workspaceWidth+3);
print(workspace.workspaceWidth+4);
```
and launch kwin under the environment `QDBUS_DEBUG=1 QT_LOGGING_RULES="kwin_scripting.debug=true"`.
When I click "execute" in the scripting console, I saw this in the output of kwin:
```
QDBusConnectionPrivate(0x7f67fc00f8c0) got message (signal): QDBusMessage(type=MethodCall, service=":1.2859", path="/Scripting", interface="", member="loadScript", signature="s", contents=("/home/
USER/.local/share/plasmashell/interactiveconsoleautosave.js") )                                                                                                                                      QDBusConnectionPrivate(0x7f67fc00f8c0) Automatically sending reply: (QVariant(int, 2))
QDBusConnectionPrivate(0x7f67fc00f8c0) sending message (no reply): QDBusMessage(type=MethodReturn, service="", signature="", contents=(2) )                                                         QDBusConnectionPrivate(0x7f67fc00f8c0) got message (signal): QDBusMessage(type=MethodCall, service=":1.2859", path="/2", interface="", member="run", signature="", contents=() )
kwin_scripting: "/home/USER/.local/share/plasmashell/interactiveconsoleautosave.js" : "2"
kwin_scripting: "/home/USER/.local/share/plasmashell/interactiveconsoleautosave.js" : "13440"
kwin_scripting: "/home/USER/.local/share/plasmashell/interactiveconsoleautosave.js" : "13441"
kwin_scripting: "/home/USER/.local/share/plasmashell/interactiveconsoleautosave.js" : "13442"
QThread(ptr=0x7f6811979060, name=QDBusConnectionManager) emitting signal at "/2"
kwin_scripting: "/home/USER/.local/share/plasmashell/interactiveconsoleautosave.js" : "13443"
kwin_scripting: "/home/USER/.local/share/plasmashell/interactiveconsoleautosave.js" : "13444"
QThread(ptr=0x7f6811979060, name=QDBusConnectionManager) emitting signal at "/2"
QDBusConnectionPrivate(0x7f67fc00f8c0) sending message (no reply): QDBusMessage(type=MethodReturn, service="", signature="", contents=() )
QThread(ptr=0x7f6811979060, name=QDBusConnectionManager) emitting signal at "/2"
```

According to the output, 6 messages are printed, but only 3 signals are sent (this bug occasionally happens, but not always).
In the scripting console, I only observed the first 3 lines of output.

In Bustle, I only observed 3 signals (see attachment).
Comment 14 David Edmundson 2023-12-08 10:41:04 UTC
*** Bug 477069 has been marked as a duplicate of this bug. ***