XDG-Shell: how to change window size? - c++

I added xdg-shell v5 to app to draw windows. When I send set_fullscreen or set_maximize commands, I see correctly set window states and correct sizes in configure event but nothing is happens.
My configure event function:
static void
xdg_surface_handle_configure(void *data,
struct xdg_surface *xdg_surface,
int32_t width,
int32_t height,
struct wl_array *states,
uint32_t serial) {
printf("Configure event got, width: %d, height: %d\n", width, height);
VirtIOGPU *g = (VirtIOGPU*) data;
g->window_state.activated = g->window_state.fullscreen =
g->window_state.maximized = g->window_state.resizing = false;
uint32_t *state;
wl_array_for_each(state, states) {
if(*state == XDG_SURFACE_STATE_MAXIMIZED) {
printf("Surface state: XDG_SURFACE_STATE_MAXIMIZED\n");
g->window_state.maximized = true;
} else if(*state == XDG_SURFACE_STATE_FULLSCREEN) {
printf("Surface state: XDG_SURFACE_STATE_FULLSCREEN\n");
g->window_state.fullscreen = true;
} else if(*state == XDG_SURFACE_STATE_RESIZING) {
printf("Surface state: XDG_SURFACE_STATE_RESIZING\n");
g->window_state.resizing = true;
} else if(*state == XDG_SURFACE_STATE_ACTIVATED) {
printf("Surface state: XDG_SURFACE_STATE_ACTIVATED\n");
g->window_state.activated = true;
}
}
if (width > 0 && height > 0) {
g->prev_width = g->width;
g->prev_height = g->height;
g->width = width;
g->height = height;
} else {
g->width = g->prev_width;
g->height = g->prev_height;
}
xdg_surface_ack_configure(xdg_surface, serial);
wl_surface_damage(g->surface, 0, 0, g->width, g->height);
wl_surface_commit(g->surface);
wl_display_dispatch_pending(g->display);
wl_display_flush(g->display);
}
So, how to see maximized window after I sent set_maximized?
Is it possible to unminimize minimized window programmatically (now by Super+Tab)?

Your client probably needs to attach and commit a new buffer with the correct dimensions to the surface. The compositor has done everything it's supposed to do, and expects your client to resize.

Related

How deep is mouse in Rectangle C++ & SDL2 (Position flickers between two positions)

I am creating a program, and I have a rectangle. Basically, I am creating a custom window inside the SDL2 window, which is a Rect, with another Rect being its toolbar. I am struggling for hours trying to figure how to detect how deep is the mouse within the Rect, but I am failing every time. The most obvious equation for me was int deep = mousePos.x - x, but it flickers between two positions every time I move my mouse. I then have tried a LOT of other calculations, but none of them worked. Either they flickered between two positions with descending values, or they were completely static and didn't move, or always moved a lot in a specific direction. I have visually represented the calculations, which were mostly correct, but the flickering between two positions is always ruining it. Thanks for any help. I am providing source code, too.
SOURCE:
//
// main.cpp
// Open
//
// Created by Fildom on 28.12.2021.
//
// Library includes
#include <SDL2/SDL.h>
#include <stdio.h>
bool isdown = false;
// Screen rendering helper
void on_render(SDL_Window* window, SDL_Renderer* renderer);
// Concatenation (probably not spelt correctly but idrc) for easier use
const char * concat(const char * one, const char * two) {
char * buffer = new char[strlen(one) + strlen(two) + 1];
strcpy(buffer, one);
strcat(buffer, two);
return buffer;
}
// Main method, required for performing application run
int main(int argc, const char * argv[]) {
SDL_Renderer *renderer = NULL; // Initialize the renderer
SDL_Event event = { 0 }; // Create a null event
SDL_Window *win = NULL; // Initialize a window
int exit = 0; // If exit is 1, win closes
// Window pre-modifiers
const char * appName = "test";
// SDL VIDEO mode initialization and error check
if(SDL_Init(SDL_INIT_VIDEO) == -1) {
printf("SDL_Init() failed with \"%s.\"", SDL_GetError());
return 1;
}
// Create the window and load it into a previously defined variable
win = SDL_CreateWindow(concat(appName, " - Initialization in progress"), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
// Window creation was unsuccessfull
if(!win) {
printf("SDL_CreateWindow() failed with \"%s.\"", SDL_GetError());
return -1;
}
// Creating renderer
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
// If renderer failed to load...
if(!renderer) {
printf("SDL_CreateRenderer() failed with \"%s.\"", SDL_GetError());
return -1;
}
// Everything has gone OK, thus the window can be renamed
SDL_SetWindowTitle(win, appName);
// Game loop, as said previously, false = 0, true = 1.
// while !exit |
// while not exit <- |
// while exit is 0 (false) <-
while (!exit) {
// Event loop
if (SDL_WaitEvent(&event)) {
// Event types
switch(event.type) {
case SDL_QUIT:
exit = 1; // Exit = 1, thus app is being exitted
break;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE) exit = 1; // If ESC is pressed
break;
case SDL_MOUSEBUTTONUP:
isdown = false;
break;
case SDL_MOUSEBUTTONDOWN:
isdown = true;
break;
case SDL_MOUSEMOTION:
break;
case SDL_WINDOWEVENT:
switch(event.window.event) {
case SDL_WINDOWEVENT_CLOSE: // macOS and/or other OSes rely on right click + Quit to fully exit out of an application. This makes it easier by just hitting the close button.
exit = 1;
break;
}
break;
default: break;
}
}
// Render the screen
on_render(win, renderer);
// Swap buffers to display
SDL_RenderPresent(renderer);
}
// Cleanup
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
class Window {
public:
int x, y, w, h;
SDL_Color winc, wintc;
bool draggable;
int titleh;
Window(int wx, int wy, int ww, int wh, SDL_Color window_color = {255, 255, 255, 255}, SDL_Color window_title_color = {200, 200, 200, 255}) {
x = wx;
y = wy;
w = ww;
h = wh;
winc = window_color;
wintc = window_title_color;
draggable = true;
titleh = 50;
}
int tx, ty = 0;
void Render(SDL_Renderer* renderer) {
SDL_Rect _t;
_t.x = x;
_t.y = y;
_t.w = w;
_t.h = h;
SDL_Rect title;
title.x = x;
title.y = y;
title.w = w;
title.h = titleh;
SDL_SetRenderDrawColor(renderer, winc.r, winc.g, winc.b, winc.a);
SDL_RenderFillRect(renderer, &_t);
SDL_SetRenderDrawColor(renderer, wintc.r, wintc.g, wintc.b, wintc.a);
SDL_RenderFillRect(renderer, &title);
int mx, my;
SDL_PumpEvents();
SDL_GetMouseState(&mx, &my);
SDL_Point ms;
ms.x = mx;
ms.y = my;
if (SDL_PointInRect(&ms, &title) and isdown) {
x = mx - tx;
y = my - ty;
tx = x;
ty = y;
}
}
};
Window test1 = Window(200, 100, 300, 200);
void on_render(SDL_Window* window, SDL_Renderer* renderer) {
SDL_Rect wind = { 0 };
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_GetWindowSize(window, &wind.w, &wind.h);
test1.Render(renderer);
}
I ended up doing it in a different way. Instead of using mousePosition.x and y, I used relative X and Y which worked out perfectly.
code for that is
mousePosition.relX and mousePosition.relY;

The application crashes and runs at the same time

I draw the application menu, having previously written the menu class and the function of this class.
When I launch the application, the menu works for 5-7 seconds, but in Windows the cursor are spinning in the form of a blue circle, which does not bode well. If these 5-7 seconds are idle, the whole screen
turns white, and Windows says that the application is not responding. In this case, if you press where the buttons were broken, these buttons are triggered, the menu closes and the game starts. The problem lies in the description of the menu class, I think.
Code of Menu class:
class Menu
{
public:
Image menuImage1, menuImage2, menuImage3;
Texture menuTexture1, menuTexture2, menuTexture3;
int menuNum = 0;
bool isMenu;
list <Asteroid*> asteroidsMass;
list <Asteroid*> ::iterator it;
Sprite menu1, menu2, menu3;
Menu()
{
menuImage1.createMaskFromColor(Color::Black);
menuImage2.createMaskFromColor(Color::Black);
menuImage3.createMaskFromColor(Color::Black);
menuImage1.loadFromFile("title.jpg");
menuImage2.loadFromFile("start.jpg");
menuImage3.loadFromFile("exit.jpg");
menuTexture1.loadFromImage(menuImage1);
menuTexture2.loadFromImage(menuImage2);
menuTexture3.loadFromImage(menuImage3);
menu1.setTexture(menuTexture1);
menu2.setTexture(menuTexture2);
menu3.setTexture(menuTexture3);
menu1.setPosition(scrX / 2, 200);
menu2.setPosition((scrX / 2) - 172.5, 300);
menu3.setPosition((scrX / 2) - 140, 450);
menu1.setOrigin(300, 75);
for (int i = 0; i < 15; i++)
{
Asteroid* aster = new Asteroid(rand() % 1200, rand() % 800, 5, 25, 0);
asteroidsMass.push_back(aster);
}
isMenu = true;
}
void update(RenderWindow& app)
{
while (isMenu)
{
menu2.setColor(Color::White);
menu3.setColor(Color::White);
app.clear(Color::Black);
menuNum = 0;
if (IntRect((scrX / 2) - 172, 300, 360, 100).contains(Mouse::getPosition(app))) { menu2.setColor(Color::Green); menuNum = 2; }
else if (IntRect((scrX / 2) - 172, 450, 360, 100).contains(Mouse::getPosition(app))) { menu3.setColor(Color::Green);menuNum = 3; }
if (Mouse::isButtonPressed(Mouse::Left))
{
if (menuNum == 2)
{
for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
{
Asteroid* asteroid = *it;
it = asteroidsMass.erase(it);
delete asteroid;
}
isMenu = false;
}
if (menuNum == 3)
{
for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
{
Asteroid* asteroid = *it;
it = asteroidsMass.erase(it);
delete asteroid;
}
isMenu = false;
app.close();
}
}
for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
{
app.draw((*it)->EntityShape);
}
for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
{
(*it)->update();
}
app.draw(menu1);
app.draw(menu2);
app.draw(menu3);
app.display();
}
}
};
All code:
https://pastebin.pl/view/0c1d6c9e
I SOLVED MY PROBLEM!
If you have same problem, just write in "while" loop checking of closing window:
while(isMenu)
{
Event event;
while (app.pollEvent(event))
{
if (event.type == Event::Closed)
app.close();
}
//some code
}

How to display GIF as splash in visual studio c++

I am trying to display a GIF picture as splash as starting of my small program in visual studio. I am really getting crazy. I looked it is possible in Qt IDE but I really need it in visual studio because my other code works only with visual studio. And yes I tried to convert my code for Qt giving me too many errors.
I have seen this post.
I am using GDI+ but still dunno how to simply display it instead of play and stop. It's okay even instead of splash to display a small form that plays the GIF file, can you guys give me a small code snippet in how to do it in c++?
Thanks.
Here is an MFC window class that implements a splash screen using GDI Plus for displaying an (animated) GIF.
I've encapsulated everything inside a header file to simplify using it with your project. Save it as an .h file (maybe "SplashWnd.h"), then include it wherever you want to set up the splash. You app's InitInstance might be a good place to add it - something like this line (before any DoModal calls):
SplashWnd splash(_T("Filname.gif"));
The constructor can also take parameters for controlling the delay before auto-closing, and also specifying a function to call when the splash is closed.
The splash window has no border or caption - it appears only as the loaded image, floating on top of any other windows. Its icon doesn't appear in the taskbar, and will close when its timeout expires, or the user clicks the window or presses a key.
#pragma once
#include <functional>
#include <afxwin.h>
#include <gdiplus.h>
#pragma comment(lib,"gdiplus.lib")
inline void ManageGdiPlusInit(bool release=false) {
static int refcount = 0;
static ULONG_PTR token;
if(release) {
if(--refcount == 0) {
Gdiplus::GdiplusShutdown(token);
}
} else if(++refcount == 1) {
Gdiplus::GdiplusStartupInput startup_input;
Gdiplus::GdiplusStartup(&token, &startup_input, 0);
} }
inline void GdiPlusInit() { ManageGdiPlusInit(false); }
inline void GdiPlusRelease() { ManageGdiPlusInit(true); }
namespace {
class SplashWnd : public CWnd {
protected:
static CString WindowClass() {
static CString name;
if(name.IsEmpty()) {
name = AfxRegisterWndClass(CS_DROPSHADOW, 0, (HBRUSH)GetStockObject(GRAY_BRUSH), 0);
}
return name;
}
Gdiplus::Image *m_pImage;
UINT m_FrameCount;
unsigned char *m_FrameDelayData;
const UINT *m_FrameDelays;
UINT m_CurFrameIndex;
UINT m_AnimationTimerId;
UINT m_ExpireTimerId;
CRect m_WindowRect;
std::function<void()> m_DismissCallback;
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDown(UINT nFlags, CPoint point) {
DestroyWindow();
}
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
DestroyWindow();
}
afx_msg void OnDestroy() {
if(m_AnimationTimerId != UINT(-1)) {
KillTimer(m_AnimationTimerId);
}
if(m_ExpireTimerId != UINT(-1)) {
KillTimer(m_ExpireTimerId);
}
if(m_DismissCallback) {
m_DismissCallback();
}
CWnd::OnDestroy();
}
afx_msg void OnTimer(UINT nIDEvent) {
if(nIDEvent == m_AnimationTimerId) {
if(++m_CurFrameIndex >= m_FrameCount) {
m_CurFrameIndex = 0;
}
DrawCurFrame();
KillTimer(m_AnimationTimerId);
m_AnimationTimerId = SetTimer(1, m_FrameDelays[m_CurFrameIndex], 0);
return;
}
if(nIDEvent == m_ExpireTimerId) {
DestroyWindow();
return;
} }
void PostNcDestroy() {
if(m_DeleteSelf) {
delete this;
}
}
void DrawCurFrame() {
Gdiplus::Graphics g(m_hWnd);
GUID dim_select_id = Gdiplus::FrameDimensionTime;
m_pImage->SelectActiveFrame(&dim_select_id, m_CurFrameIndex);
g.DrawImage(m_pImage, 0, 0, m_WindowRect.Width(), m_WindowRect.Height());
}
public:
// set m_DeleteSelf to true if a SplashWnd is created with new, and you want it to
// auto-delete itself when the window expires or is dismissed.
bool m_DeleteSelf;
// file_path the gif file path
// ExpireMs the time, in milliseconds until the window automatically closes itself
// WidthFactor the fraction of the width of the primary display to use as the splash screen's width
// HeightFactor the fraction of the height of the primary display to use as the height
// If WidthFactor or HeightFactor are 0, the original image aspect ratio is preserved
// If both are 0, the original image size, in pixels is used
SplashWnd(CString file_path, DWORD ExpireMs=2000, double WidthFactor=0.4, double HeightFactor=0) {
GdiPlusInit();
m_pImage = new Gdiplus::Image(file_path);
// Set up an array of frame times for animated images
UINT dimension_count = m_pImage->GetFrameDimensionsCount();
GUID dimension_id;
m_pImage->GetFrameDimensionsList(&dimension_id, 1);
m_FrameCount = m_pImage->GetFrameCount(&dimension_id);
UINT frame_delay_size = m_pImage->GetPropertyItemSize(PropertyTagFrameDelay);
m_FrameDelayData = new unsigned char[frame_delay_size];
Gdiplus::PropertyItem* frame_delay_item = reinterpret_cast<Gdiplus::PropertyItem*>(m_FrameDelayData);
m_pImage->GetPropertyItem(PropertyTagFrameDelay, frame_delay_size, frame_delay_item);
m_FrameDelays = reinterpret_cast<const UINT*>(frame_delay_item->value);
// Figure out the size and location of the splash window
int primary_width = GetSystemMetrics(SM_CXFULLSCREEN);
int primary_height = GetSystemMetrics(SM_CYFULLSCREEN);
int splash_width = int(primary_width * WidthFactor);
int splash_height = int(primary_height * HeightFactor);
if(splash_width == 0) {
if(splash_height == 0) {
splash_width = m_pImage->GetWidth();
splash_height = m_pImage->GetHeight();
} else {
splash_width = primary_width * splash_height / primary_height;
}
} else if(splash_height == 0) {
splash_height = primary_height * splash_width / primary_width;
}
int l = (primary_width - splash_width) / 2;
int t = (primary_height - splash_height) / 2;
int r = l + splash_width;
int b = t + splash_height;
m_WindowRect.SetRect(
(primary_width - splash_width) / 2,
(primary_height - splash_height) / 2,
(primary_width + splash_width) / 2,
(primary_height + splash_height) / 2);
// WS_EX_TOPMOST makes the window cover up other, regular windows
// WS_EX_TOOLWINDOW prevents an icon for this window in the taskbar
// WS_POPUP prevents caption and border from being drawn
CreateEx(WS_EX_TOPMOST | WS_EX_TOOLWINDOW, WindowClass(), _T("Splash"), WS_VISIBLE | WS_POPUP, m_WindowRect, 0, 0);
// Show the first frame
m_CurFrameIndex = 0;
DrawCurFrame();
// Set up the frame-flipping animation timer
m_ExpireTimerId = m_AnimationTimerId = UINT(-1);
if(m_FrameCount > 1) {
m_AnimationTimerId = SetTimer(1, m_FrameDelays[m_CurFrameIndex], 0);
}
// Set up the expiration timer
if(ExpireMs != INFINITE) {
m_ExpireTimerId = SetTimer(2, ExpireMs, 0);
}
m_DeleteSelf = false;
}
// Constructor which takes a callback function which will be called when the splash window closes
template <typename F>
SplashWnd(CString file_path, DWORD ExpireMs, double WidthFactor, double HeightFactor, F DismissCallback)
: SplashWnd(file_path, ExpireMs, WidthFactor, HeightFactor)
{
m_DismissCallback = DismissCallback;
}
~SplashWnd() {
delete [] m_FrameDelayData;
delete m_pImage;
GdiPlusRelease();
}
};
// Message map, usually in an implementation file, but here encapsulated inside the header
// using an anonymous namespace to prevent possible ODR problems.
BEGIN_MESSAGE_MAP(SplashWnd, CWnd)
ON_WM_KEYDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
ON_WM_DESTROY()
END_MESSAGE_MAP()
}

How to properly use SDL_BlitSurface() with SDL_CreateRGBSurface()?

(See "Edit 2" below for the solution.)
I need to create SDL surfaces from scratch, instead of loading them from a file. Unfortunately, SDL_BlitSurface() seems to render all colors as black when used with the surface generated through SDL_CreateRGBSurface(). This is my code:
int main(int argc, char** argv)
{
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
SDL_Surface* layer = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 100,
screen->format->BitsPerPixel,
screen->format->Rmask,
screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask
);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 100;
rect.h = 100;
Uint32 blue = SDL_MapRGB(screen->format, 0, 0, 255);
SDL_FillRect(layer, &rect, blue);
SDL_BlitSurface(screen, NULL, layer, NULL);
SDL_Flip(screen);
SDL_Delay(3000);
return 0;
}
What I get is a black screen, instead of a 100x100 blue rectangle. What I could find by Googling doesn't seem to help me, as those questions either apply to 8bit surfaces (and setting palettes — my bpp is 32 here) or are left unanswered.
So, I would like to know how should I properly blit a generated surface onto a SDL screen.
Edit: I see it was an error in the parameter ordering. The line in question should read
SDL_BlitSurface(layer, NULL, screen, NULL);
Still, I am having trouble to achieve the same effect in my more complex C++ program. I will post the relevant parts of the code here:
main.cpp:
int main(int argc, char** argv)
{
SDLScreen screen(1024, 700, "Hello, SDL!");
SDL_Event event;
SDLMenu menu;
bool shouldQuit = false;
menu.setBounds(200, 100, 200, 600);
menu.setFontName("NK211.otf");
menu.setFontSize(36);
menu.setEffect(sdlteShadowText);
menu.addItem("New game");
menu.addItem("Load game");
menu.addItem("Save game");
menu.addItem("Exit");
menu.render();
while (!shouldQuit)
{
menu.draw(screen.getSurface());
SDL_Flip(screen.getSurface());
SDL_Delay(10);
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
shouldQuit = true;
}
else if (event.type == SDL_KEYUP)
{
if (event.key.keysym.sym == SDLK_q)
{
shouldQuit = true;
}
}
}
}
}
SDLMenu.cpp:
void
SDLMenu::setSelectionColorRGB(int r, int g, int b)
{
SDL_VideoInfo* info = (SDL_VideoInfo*)SDL_GetVideoInfo();
selectionColor = SDL_MapRGB(info->vfmt, r, g, b);
}
void
SDLMenu::render()
{
SDLText* current = NULL;
SDL_VideoInfo* info = (SDL_VideoInfo*)SDL_GetVideoInfo();
if (!items->empty())
{
current = getItemAt(currentItem);
selectionRect = getItemRect(current);
setSelectionColorRGB(0,0,255);
selectionCanvas = SDL_CreateRGBSurface(SDL_HWSURFACE,
selectionRect->w, selectionRect->h,
info->vfmt->BitsPerPixel,
info->vfmt->Rmask,
info->vfmt->Gmask,
info->vfmt->Bmask,
info->vfmt->Amask);
SDL_FillRect(selectionCanvas, selectionRect, selectionColor);
SDL_SaveBMP(selectionCanvas, "selection.bmp"); // debug
}
for (list<SDLText*>::iterator i = items->begin();
i != items->end(); i++)
{
(*i)->render();
}
}
void
SDLMenu::draw(SDL_Surface* canvas)
{
int currentY = bounds.y;
if (selectionCanvas != NULL)
{
SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect);
}
for (list<SDLText*>::iterator i = items->begin();
i != items->end(); i++)
{
(*i)->draw(bounds.x, currentY, canvas);
currentY += fontSize + itemGap;
}
}
SDLScreen.cpp:
SDLScreen::SDLScreen(int w, int h, string t, int d)
: width(w), height(h), depth(d), title(t)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption(title.c_str(), NULL);
refresh();
}
void
SDLScreen::refresh()
{
screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE);
}
The selection rectangle for the active menu item should be blue, but it shows up in black. The file selection.bmp is also all black.
Edit 2: I found out what created the problem. The selectionRect was set relative to the screen, while the selectionCanvas had the width and height of a particular menu item. So, the filling was done out of bounds of the selectionCanvas. Adding separate SDL_Rect for filling solved the problem.
SDL_Rect fillRect;
fillRect.x = 0;
fillRect.y = 0;
fillRect.w = selectionRect->w;
fillRect.h = selectionRect->h;
SDL_FillRect(selectionCanvas, &fillRect, selectionColor);
// and later...
SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect);
You inverted source and destination. To blit on screen, it should be
SDL_BlitSurface(layer, NULL, screen, NULL);
doc for SDL_BlitSurface

XNextEvent Blocking At Start of Application

I have the following application.
#include <FWWindow.h>
#include <FWApplication.h>
int main(int /*argc*/, char */*argv*/[])
{
FWApplication::Initialize();
FWWindow *win = new FWWindow(800, 600);
win->Show();
FWApplication::Run();
delete win;
}
When I run it it gets stuck on the XNextEvent() because it blocks until it gets the next event from the XServer. What I would like to know, is based on the code below, why is the XNextEvent not getting the ConfigureNotify or Expose events after I am calling XMapWindow(); I have checked to make sure my application provides the right Display based on the address in the watch window of my IDE. What am I missing to get the window to appear?
Initialize() does the following
-
FWApplication *FWApplication::Initialize()
{
if (!_instance)
{
_xDisplay = XOpenDisplay(NULL);
if (_xDisplay == NULL)
throw "Failed to get XDisplay";
_initialized = true;
_instance = new FWApplication(); // Calls an empty ctor
}
return _instance;
}
FWWindow *win = new FWWindow(800, 600); does the following
-
FWWindow::FWWindow(int width, int height) :
clientWidth(width),
clientHeight(height)
{
// These are all member variables
xDisplay = FWApplication::GetMainDisplay();
xScreen = DefaultScreen(xDisplay);
xDepth = DefaultDepth(xDisplay, xScreen);
xVisual = DefaultVisual(xDisplay,xScreen);
xAttributes.background_pixel = XWhitePixel(xDisplay, xScreen);
xAttributes.border_pixel = XBlackPixel(xDisplay, xScreen);
xAttributes.override_redirect = 0;
xWindow = XCreateWindow(
xDisplay,
RootWindow(xDisplay, xScreen),
0, 0,
width, height,
0,
xDepth,
InputOutput,
xVisual,
CWBorderPixel | CWColormap | CWEventMask,
&xAttributes
);
XSetStandardProperties(
xDisplay,
xWindow,
"glxsimple",
"glxsimple",
None,
NULL,
0,
NULL
);
}
win->Show(); does the following
-
void FWWindow::Show()
{
XMapWindow(xDisplay, xWindow); // xWindow and xDisplay defined in ctor above
}
And finaly FWApplication::Run(); does the following
-
int FWApplication::Run()
{
if (!_initialized)
return -1;
static bool run = true;
static Display *lDisplay = _xDisplay;
XEvent xEvent;
while (run)
{
do
{
XNextEvent(lDisplay, &xEvent);
switch (xEvent.type)
{
case ConfigureNotify:
{
unsigned int w = xEvent.xconfigure.width;
unsigned int h = xEvent.xconfigure.height;
// Do something to main widget
}
case Expose:
break;
}
} while (XPending(GetMainDisplay()));
}
return EXIT_SUCCESS;
}
You're not specifying an event mask in your window's attributes, so XNextEvent() won't report any event. You should write something like:
xAttributes.background_pixel = XWhitePixel(xDisplay, xScreen);
xAttributes.border_pixel = XBlackPixel(xDisplay, xScreen);
xAttributes.override_redirect = 0;
xAttributes.event_mask = StructureNotifyMask // for ConfigureNotify
| ExposureMask; // for Expose