Skip to content

[WIP] Set application icon in glfw #7602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions libs/openFrameworks/app/ofAppGLFWWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"

#include "ofIcon.h"
#include "ofImage.h"
#ifdef TARGET_LINUX
#include "ofIcon.h"
#include "ofImage.h"


#define GLFW_EXPOSE_NATIVE_X11
#ifndef TARGET_OPENGLES
#define GLFW_EXPOSE_NATIVE_GLX
Expand Down Expand Up @@ -294,19 +296,12 @@ void ofAppGLFWWindow::setup(const ofGLFWWindowSettings & _settings){
}
glfwGetWindowSize( windowP, &currentW, &currentH );
}
#ifdef TARGET_LINUX
if(!iconSet){
ofPixels iconPixels;
#ifdef DEBUG
iconPixels.allocate(ofIconDebug.width,ofIconDebug.height,ofIconDebug.bytes_per_pixel);
GIMP_IMAGE_RUN_LENGTH_DECODE(iconPixels.getData(),ofIconDebug.rle_pixel_data,iconPixels.getWidth()*iconPixels.getHeight(),ofIconDebug.bytes_per_pixel);
#else
iconPixels.allocate(ofIcon.width,ofIcon.height,ofIcon.bytes_per_pixel);
GIMP_IMAGE_RUN_LENGTH_DECODE(iconPixels.getData(),ofIcon.rle_pixel_data,iconPixels.getWidth()*iconPixels.getHeight(),ofIcon.bytes_per_pixel);
#endif
setWindowIcon(iconPixels);

#ifdef TARGET_HAS_WINDOW_ICON
if(!iconSet){
setWindowIcon(getIcon());
}
#endif
#endif
if(settings.iconified){
iconify(true);
}
Expand Down Expand Up @@ -401,7 +396,6 @@ void ofAppGLFWWindow::setup(const ofGLFWWindowSettings & _settings){
#endif
}

#ifdef TARGET_LINUX
//------------------------------------------------------------
void ofAppGLFWWindow::setWindowIcon(const std::string & path){
ofPixels iconPixels;
Expand All @@ -411,23 +405,29 @@ void ofAppGLFWWindow::setWindowIcon(const std::string & path){

//------------------------------------------------------------
void ofAppGLFWWindow::setWindowIcon(const ofPixels & iconPixels){
#ifndef TARGET_HAS_WINDOW_ICON
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a ofLogWarning here saying platform doesn't support Window Icons :)

return;
#endif
iconSet = true;
int length = 2+iconPixels.getWidth()*iconPixels.getHeight();
vector<unsigned long> buffer(length);
buffer[0]=iconPixels.getWidth();
buffer[1]=iconPixels.getHeight();
for(size_t i=0;i<iconPixels.getWidth()*iconPixels.getHeight();i++){
buffer[i+2] = iconPixels[i*4+3]<<24;
buffer[i+2] += iconPixels[i*4+0]<<16;
buffer[i+2] += iconPixels[i*4+1]<<8;
buffer[i+2] += iconPixels[i*4+2];

// make sure we have a RGBA image
auto rgbaIcon = ofPixels(iconPixels);
rgbaIcon.setImageType(OF_IMAGE_COLOR_ALPHA);
if(rgbaIcon.getPixelFormat()==OF_PIXELS_BGRA){
rgbaIcon.swapRgb();
}

XChangeProperty(getX11Display(), getX11Window(), XInternAtom(getX11Display(), "_NET_WM_ICON", False), XA_CARDINAL, 32,
PropModeReplace, (const unsigned char*)buffer.data(), length);
XFlush(getX11Display());
// convert to a GLFW image
GLFWimage glfwIcon;
glfwIcon.width = rgbaIcon.getWidth();
glfwIcon.height = rgbaIcon.getHeight();
size_t iconByteSize = glfwIcon.width*glfwIcon.height*4;
glfwIcon.pixels = static_cast<unsigned char*>(malloc(iconByteSize));
std::memcpy(glfwIcon.pixels, rgbaIcon.getData(), iconByteSize);
// set icon(s) on window
glfwSetWindowIcon(windowP, 1, &glfwIcon);
// cleanup
free(glfwIcon.pixels);
}
#endif

//--------------------------------------------
ofCoreEvents & ofAppGLFWWindow::events(){
Expand Down
14 changes: 11 additions & 3 deletions libs/openFrameworks/app/ofAppGLFWWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,22 @@ class ofAppGLFWWindow : public ofAppBaseGLWindow {
OF_DEPRECATED_MSG("use ofGLFWWindowSettings to create the window instead", void setDepthBits(int depth));
OF_DEPRECATED_MSG("use ofGLFWWindowSettings to create the window instead", void setStencilBits(int stencil));
OF_DEPRECATED_MSG("use ofGLFWWindowSettings to create the window instead", void setMultiDisplayFullscreen(bool bMultiFullscreen)); //note this just enables the mode, you have to toggle fullscreen to activate it.

/// \brief Set current window icon.
///
/// \param path file to load icon from
void setWindowIcon(const std::string & path);

/// \brief Set current window icon.
///
/// \param iconPixels pixel definition of the icon
void setWindowIcon(const ofPixels & iconPixels);


#if defined(TARGET_LINUX) && !defined(TARGET_RASPBERRY_PI_LEGACY)
Display* getX11Display();
Window getX11Window();
XIC getX11XIC();

void setWindowIcon(const std::string & path);
void setWindowIcon(const ofPixels & iconPixels);
#endif

#if defined(TARGET_LINUX) && !defined(TARGET_OPENGLES)
Expand Down
14 changes: 3 additions & 11 deletions libs/openFrameworks/app/ofAppGlutWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,9 @@ void ofAppGlutWindow::setup(const ofGLWindowSettings & settings){
#endif

#ifdef TARGET_LINUX
if(!iconSet){
ofPixels iconPixels;
#ifdef DEBUG
iconPixels.allocate(ofIconDebug.width,ofIconDebug.height,ofIconDebug.bytes_per_pixel);
GIMP_IMAGE_RUN_LENGTH_DECODE(iconPixels.getData(),ofIconDebug.rle_pixel_data,iconPixels.getWidth()*iconPixels.getHeight(),ofIconDebug.bytes_per_pixel);
#else
iconPixels.allocate(ofIcon.width,ofIcon.height,ofIcon.bytes_per_pixel);
GIMP_IMAGE_RUN_LENGTH_DECODE(iconPixels.getData(),ofIcon.rle_pixel_data,iconPixels.getWidth()*iconPixels.getHeight(),ofIcon.bytes_per_pixel);
#endif
setWindowIcon(iconPixels);
}
if(!iconSet){
setWindowIcon(getIcon());
}
#endif
if (settings.isPositionSet()) {
setWindowPosition(settings.getPosition().x,settings.getPosition().y);
Expand Down
Loading