SFML community forums

Help => Window => Topic started by: Ixrec on October 18, 2013, 04:38:16 am

Title: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 18, 2013, 04:38:16 am
So I've been trying to figure out how to give an SFML program the right icon or set of icons on each OS so that you see all the right-sized icons in all the right places, just like any professional program.

On Windows and Mac I believe I've already accomplished that (after modifying the SFML source code a little), but on my Ubuntu VM I'm getting some ludicrously inconsistent results, and I have no idea whether to blame myself, SFML, Ubuntu, Unity, X, or VMware.

When building from source I use CMake just as the tutorial describes, with BUILD_SHARED_LIBS turned off (I like static linking) and SFML_BUILD_EXAMPLES turned on (since they make great test programs), and all other variables left on their default values.  The source code itself is the official 2.1 release (I don't recall seeing any chances since then which might affect icons on Linux).

For the record I did a LOT of tests besides what I describe here.  I'm posting this one because here I decided to "steal" Firefox's icon in order to make absolutely sure I wasn't using the wrong size or wrong format or wrong folder or anything like that.

So after building everything, what I did was make a simple desktop entry with "Icon=firefox", since that's the exact line used in Firefox's desktop entry, and have its Exec line point to the SFML example program called "opengl."  I also modified opengl.cpp so that it loads firefox.png into an sf::Image which it then passes to setIcon().  I've confirmed that I'm loading the right firefox.png because changing its name causes the real Firefox to lose its icon too.  I've also confirmed in Pinta that it's exactly 128x128, which is the size I pass to setIcon.

For some strange reason, at best I get the Firefox icon in the sidebar and alt+tab menu but very blurry.  Sometimes I get Ubuntu's placeholder question mark.  And sometimes I get no icon at all; my program simply doesn't appear in the sidebar or the alt+tab list at all.  I've taken screenshots of all three of these cases, with the real Firefox browser running at the same time so you can clearly see the difference in icon resolution.  You can also see the code I added in opengl.cpp and the desktop entry I used and part of the opengl test window itself (with the spinning wooden cube).

These pictures were taken with my iPhone because it appears impossible to take a regular screenshot with the alt+tab menu open.

Case 1: Blurry custom icon in both places, Firefox has non-blurry custom icon
(http://i.imgur.com/pJm1NsH.jpg)

Case 2: Default question mark icon in both places, Firefox still has non-blurry custom icon
(http://i.imgur.com/ZyoZ5lh.jpg)

Case 3: No icon at all in both places, Firefox still has non-blurry custom icon
(http://i.imgur.com/YSvS2zp.jpg)

These three screenshots were taken during the same series of tests.  All I did was close and reopen the "opengl" test program several times (and I opened it by double clicking on the desktop entry, of course) until all three cases appeared.  How common each one is seems to be completely random, though I feel like the frequency of #2 is correlated to how recently I recompiled.

So, any idea what layer of software I should be blaming for this bizarre behavior?  I want to think it's Unity/Ubuntu, but every other program consistently has a non-blurry custom icon.  So then I want to think it's SFML, but what the hell could it be doing to cause a random distribution of these three behaviors?  I guess it could be VMware, but then it makes no sense that this would only be happening to my test program and nothing else in the VM.

I *think* that's all the potentially relevant information, but do tell me if I left something out.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Laurent on October 18, 2013, 08:01:11 am
I have some code waiting to be tested, that potentially fixes this kind of problem. But the window manager that I use in my Linux VM adds no decoration to windows so there's no icon. So would you like to test some code for me? I can post the code to change in SFML if you're ok.

Quote
On Windows and Mac I believe I've already accomplished that (after modifying the SFML source code a little)
Where is yor pull request? :P
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 18, 2013, 08:36:25 am
I wanted to figure it out on all three OSes before I made any premature pull requests, and the only thing I really did was make the Windows setIcon() accept a bool to distinguish between the two icon sizes.  I don't even know for sure if other OSes have multiple "runtime" icon sizes to worry about yet, as opposed to "filesystem" icons that don't require the executable to go get them (I couldn't find any platform-agnostic terms for any of this icon stuff).  Though from what I've seen it looks like Mac/Linux only need one runtime size, since their windows don't have the little 16x16 titlebar icon that Windows windows do.

Testing code for you sounds awesome.  Post it wherever you want.  What's your window manager, out of curiosity?
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Laurent on October 18, 2013, 09:20:44 am
My window manager is Awesome.

I'll post some code later, thanks for your help :)
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Laurent on October 18, 2013, 03:48:05 pm
You can try to replace the implementation of WindowImplX11::setIcon with this one:

    std::vector<Uint32> icon(2 + width * height);
    icon[0] = width;
    icon[1] = height;
    for (std::size_t i = 0; i < width * height; ++i)
        icon[i + 2] = (pixels[i * 4 + 3] << 24) | (pixels[i * 4 + 1] << 16) | (pixels[i * 4 + 2] << 8) | pixels[i * 4 + 3];

    Atom iconAtom = XInternAtom(m_display, "_NET_WM_ICON", False);
    Atom cardinalAtom = XInternAtom(m_display, "CARDINAL", False);
    XChangeProperty(m_display, m_window, iconAtom, cardinalAtom, 32, PropModeReplace, reinterpret_cast<unsigned char*>(icon.data()), icon.size());

    XFlush(m_display);

This code may not work or even not compile, so I let you check for stupid (or other) errors ;)
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 18, 2013, 09:46:50 pm
Good news: It does compile! After I fixed some typos.  It's possible I made others I didn't catch.

Bad news: The results are exactly the same as before except now the icon is also purple for some reason. 
(http://i.imgur.com/6nbIj7j.jpg)

While rerunning the program a few times to confirm I could get the question mark, one time I got both the question mark and the blurry purple fox in the sidebar at the same time (though only the question mark in alt+tab):
(http://i.imgur.com/645YFCg.jpg)

I tried adding the RGB to BGR code in the current implementation of setIcon, but that just changed it from a purple fox to a bright blue fox.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Laurent on October 18, 2013, 09:50:59 pm
Hmm... this code was stolen from the Qt library, so it should work perfectly :-\
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 18, 2013, 09:53:10 pm
I'm wondering if I should make a Kubuntu VM just to see if this is Unity's fault somehow.

(for those who don't know, Kubuntu is an Ubuntu-based distro that uses KDE as its window manager instead of Unity)
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Laurent on October 18, 2013, 11:15:22 pm
Can't you install multiple WM and switch between them on the same OS installation?
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: FRex on October 18, 2013, 11:27:44 pm
What Ubuntu version(number) is that exactly?
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 18, 2013, 11:29:08 pm
I've been using the long term support release, so 12.04 I think.

Installing some other window managers now.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: FRex on October 18, 2013, 11:32:12 pm
Ok, no Mir there, XMir emulates X Server which may cause problems but it's optional in 13.10 or something so it shouldn't be in 12.x
Edit: Obviously no X emulation via XWayland either. ;D It's pure X so it's Unity + X problem. Soon to be Unity + Mir one. :P
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 19, 2013, 12:18:54 am
Well now I'm stuck in a window manager that's incapable of accepting my login info or switching back to any of the others, even though it shows me a list of the others.

Fuck.

Edit: Everything I can find online assumes you can login to the terminal, but even that's rejecting every username/password combination I can think of.  Is my VM bricked?

Edit: Crisis solved.  Turns out my login name doesn't start with a capital letter.  Who knew.  Openbox still completely fails to load a desktop even after accepting my login info so no testing on that one.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 19, 2013, 01:26:20 am
Okay, did tests with a bunch of icon sizes across a bunch of window managers all in the same Ubuntu VM using the same modified opengl executable (now it takes a single command line argument so I can tell it what icon size to try without recompiling everything; using the old setIcon code for this).

I'll use the window manager names as shown on Ubuntu's login screen.

Ubuntu: The one I tested with above. Custom icons appear blurry, or are replaced by the question mark, or something even weirder happens like double icons or no icons or both types of icon; never do I get a custom icon in the right resolution.

Ubuntu 2D: The icons never failed to appear in the alt+tab menu and were in the proper resolution (though this alt+tab menu uses much smaller icons than the regular Ubuntu one; maybe 32x32?).  The sidebar was just as sporadic as usual though, so I saw a perfect icon in alt+tab but either a blurry one or a question mark in the sidebar.  In fact, I swear the sidebar was far more likely to give me question marks than before.

KDE/Plasma: Now windows have a titlebar icon again, like on Windows.  But no matter what I did I never once got a custom icon of any resolution to appear in the titlebar or taskbar.  It was always the default X logo.

LXDE: Titlebar icons exist here too.  Mostly the same as KDE, except  one time the test program somehow magically got the icon up and during that test Firefox had the blurrier icon.  I never replicated that.

So it looks the two flavors of Ubuntu/Unity are tied for least broken by a huge margin.  This is just sad.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 19, 2013, 01:42:19 am
I've seen it claimed on a handful of Ubuntu-related pages that the desktop entry icon will be used on the application, one guy even said it would override the application's own icon.  So I compiled the ordinary opengl test program, with no setIcon() calls at all, and ran that in all of the above desktop managers.  Not once did it ever have a non-default icon in any of them.  Unless SFML is inhibiting the icon inheritance(?) somehow I'm going to have to assume those people don't know what they're talking about.

Think I should redo any of this testing with the stolen Qt code instead?
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: FRex on October 19, 2013, 01:44:18 am
Xfce is ok(see attachment).
Icon was this as png loaded with sf::Image : http://www.cafebink.com/wp-content/uploads/2013/01/mug_of_beer-7581.jpg
Nope, I did not resize. 1600x1200 icons all the way. 8)
Maybe it's your virtualiztion software messing it up?
I can try install some other DE (or two or three) and see if it runs ok too.
int main()
{
    sf::RenderWindow app(sf::VideoMode(640,480),"hehe");
   
    sf::Image img;
    img.loadFromFile("/home/frex/NetBeansProjects/resources/beer.png");
   
    app.setIcon(img.getSize().x,img.getSize().y,img.getPixelsPtr());
   
   
    while(app.isOpen())
    {
        sf::Event eve;
        while(app.pollEvent(eve)) if(eve.type==sf::Event::Closed) app.close();
        app.clear();
        app.display();
    }
}
Of course I'm not going to install Ubuntu over my beloved Fedora from my beloved Red Hat. ;)
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 19, 2013, 02:05:35 am
Maybe it's your virtualiztion software messing it up?

As mentioned before, if this was the case we'd expect it to affect other programs in the system, not just SFML test programs.  Yet I've never seen any of this behavior from any other program in any of the desktop managers.  So there must be something all the other programs are doing that SFML isn't.

I'll try xfce later.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 19, 2013, 05:32:05 am
Wow, you're right, xfce seems to perform correctly on that opengl test program.

Unfortunately some of the other test programs I'm using aren't working on it  For instance, I have a copy of firefox's desktop entry with no changes except the Exec line is "pinta" instead of "firefox", and in xfce and other DE's the result of that is Pinta with the default icon, not firefox's icon and not pinta's icon.

On Ubuntu this test program sometimes produces an icon which appears to be a blue square with a fuzzy white border...I have no idea wtf that is.

We're clearly missing something huge and fundamental about how icons work on Linux.  Neither the Icon line in a desktop entry nor the icon_pixmap hint sent to the X server come close to having a strong correlation with what actually appears onscreen.

P.S.: I can see why you use xfce though.  Just from doing this testing I'm already liking what I see of it.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 19, 2013, 07:28:02 am
Since Qt didn't work I decided to try wxWidgets.  I finally found the X11 icon code in their sources (utilsx11.cpp), so I'll try adapting it for SFML tomorrow.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: FRex on October 19, 2013, 01:59:50 pm
setIcon didn't work for me on KWin too(uninstalled KDE already too, sorry :P).
I use xfce because it's so cute, light, minimalistic and ok-ish out of the box, I kinda like KDE a tiny bit more and if KWin shipped with Wayland in Fedora and I had patience to learn KDE shortcuts and mouse gestures and configure everything in it throughly to my preferences it'd probably feel better than xfce.
Oh and I installed 19 from CD(17 GNOME and 18 KDE from DVDs and I wanted to try xfce now anyway) so GNOME and KDE wouldn't fit on it. ;D Cause I'm such pro h4cker I don't have a pendrive to install from. ;D
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 20, 2013, 04:23:03 am
Good news: I found some code that appears to be working consistently (ie, the same set of custom icons is ALWAYS used in ALL window managers, NEVER the default icons).  I'll give a detailed report once I've tested it inside SFML and played with more than just two sizes.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: FRex on October 20, 2013, 04:31:14 am
Quote
ALL window managers
There's like 20+ of them. ;D
Because Linux is about choice, and by Linux I mean Unix because choice extends to kernels. ;D
(Joke totally stolen from Daniel Stone :P)

Can't you already say what solution/code/dark magic ritual it is and which toolkit/library/manual/necronomicon you found it in?
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 20, 2013, 04:36:50 am
Well the source was http://stackoverflow.com/questions/10699927/xlib-argb-window-icon, but just getting that code to compile took several minutes, and merely plugging in the central parts to setIcon() doesn't seem to work (it's pretty similar to Laurent's Qt attempt actually), so I have a long way to go before I can claim to have the complete code for an actual solution.

And I meant the five I've tested with so far.  I'm a bit excited that something finally worked, so cut me some slack =P

Edit: Not making much progress so far.  Trying to change the test code and SFML's internals to be closer to each other, and it seems like everything is either a little change that has no effect, or a big one that completely breaks the one program so it no longer shows a window at all.  Great.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 21, 2013, 11:53:53 pm
Is it just me, or does the "X11" test program fail to exit without causing a fatal X server error?

(http://gyazo.com/2e988689da199a8bb9bd5e51ca23067f.png)
Note that you must run it from the terminal to see this error, and it doesn't appear until after you close the window.
I downloaded and rebuilt a fresh copy of the source code in a different folder just to be sure I wasn't the one causing this.

It looks like I've been wasting a huge amount of time trying to get the icon code I mentioned before to run without any X errors like this, much less run inside SFML without breaking anything, when the icon code itself was never causing the problems >_>
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 22, 2013, 10:55:59 am
I think I'm officially out of ideas for this one.  That little code snippet can be made to work in isolation but whenever I try to put the key parts inside SFML it no longer has any significant effect (at least in Unity), and I've failed to find even the slightest hint as to why that is.  Every experiment I do seems to have different results every time I do it.  Plus, not once have I ever gotten a non-blurry custom icon to appear in Unity (which is starting to seriously piss me off).

All I can say for sure is that the approach with _NET_WM_ICON and XChangeProperty() is probably the right one because every source other than the current SFML code seems to use that.  At least, out of the ones that had any X11 icon code at all.
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Laurent on October 22, 2013, 11:07:43 am
Quote
That little code snippet can be made to work in isolation
Do you mean that you tried it in a minimal X11 application written from scratch? Or something else?
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 22, 2013, 11:43:33 am
At first I replaced most of the SFML test program called X11 with that code, so that CMake magic took care of all the headers and linking for me.

Later on I've been using essentially the original test program's code with parts of that snippet added to it instead of replacing it, since that also seems to work (fatal errors aside).  I have no idea which iteration of this was the most successful (if any), so here's what my X11.cpp looks like right now after a quick cleanup: http://pastebin.com/ABDLqvjp
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Laurent on October 22, 2013, 11:51:57 am
What you should do is to write a really minimal X11 program that only creates a window, process its events and of course changes its icon. This one should work.
Then you write the same thing, but with the X11 implementation extracted from SFML. This one should fail.

Once you have two minimal apps, once that fails and one that works, it should be fairly easy to spot the difference.

If you have time and motivation, of course :)
Title: Re: Sporadic Icon Failure in Ubuntu
Post by: Ixrec on October 22, 2013, 09:47:44 pm
That's exactly what I thought I was doing the last three days.