GTKSDL - Handling events - c

For a 3D-modeling project I am using SDL and including in a GTK Widget (thanks to [GTKSDL], http://gtksdl.sourceforge.net "Direct Link to GTKSDL").
The widget is simply the loading of a .obj file with GTK and modeling it with OpenGL and then displaying it in the SDL Widget.
So far, everything is working.
The issue comes when I try to move the object in the widget using SDL events. Before the integration of the SDL window in GTK, the events were working without any troubles.
Also, once the 3D model is displayed, it is impossible for me to interact with GTK since apparently the event loop of SDL is waiting for something and whatever I do, it's not getting it.
I thought of forking the two event loop but it seems that GTK and SDL are trying to access the X server at the same time and it creates multiple conflicts.
I tried to remove the endless loop in SDL but it doesn't work too.
I am on Devian, I searched the internet for an implementation of "GTKSdl" but it seems outdated, any ideas how to patch it ?
UPDATE:
I'm already using SDLPollEvent and g_idle add.
So right after I choosed a file (with GTK), I fill the struct "t_stage" and use g_idle_add :
g_idle_add((GSourceFunc) &mainloop, stage);
gboolean mainloop(t_stage *stage)
{
SDL_Event evt;
while (SDL_PollEvent(&evt) != 0)
{
if (evt.type == SDL_MOUSEBUTTONDOWN)
on_mouse_down(stage, &(evt.button));
else if (evt.type == SDL_MOUSEBUTTONUP)
on_mouse_up(stage, &(evt.button));
else if (evt.type == SDL_MOUSEMOTION)
on_mouse_move(stage, &(evt.motion));
else if (evt.type == SDL_KEYDOWN)
handle_key(stage, evt.key.keysym.sym, 1);
else if (evt.type == SDL_KEYUP)
handle_key(stage, evt.key.keysym.sym, 0);
}
apply_keys(stage);
draw(stage, 0);
return (1);
}
But the events are still not received. Any ideas ?

GTKSDL seems to be an alpha-quality widget created back in 2001 and never maintained (nor widely used, I think). GTK has changed a lot since then, so I'm not sure it's the best solution for you.
However, one of the problems you're facing is that you can't use an endless loop inside an event handler in GTK (and the same is true for every event-driven toolkit using a message pump I know). If you do that, you just prevent GTK from getting a chance to process the pending events. For more information on how to solve this, give a look to Embedding SDL into GTK+
UPDATE:
g_idle_add allows to specify a callback and an argument to pass to that callback (the gpointer data argument). A gpointer is merely a pointer to void. You can pass it the address of a structure to read/write if you need your callback to be aware of several parameters.
In your callback registered with g_idle_add, you may read SDL events. Don't use SDL_WaitEvent for that because it will block until an SDL event occurred, use SDL_PollEvent (retrieves a pending event if any) or SDL_PeepEvents (retrieves all pending events if any). These functions are non-blocking, they return if no event is pending, whereas SDL_WaitEvent would block until an event is received.
You may then process the events, making sure it doesn't take too long until you exit the idele handler, otherwise the GTK UI will freeze. You may also prefer to just translate these events into GTK events and just dispatch them to GTK for it to process them.
You also have an old example of SDL integration with GTK 1.2. I think most of it would work with GTK 2, the basic idea is there, just need to update the code to replace symbols that were since then deprecated in GTK.

Related

How to understand "UI Thread" coverage from source code in WP

I wonder whether it is possible to understand which code pieces are executed on UI from source code just depending on static analysis in Windows Phone development.
I try to implement a static analysis finding places in which Dispatcher.(Begin)Invoke is used unnecessarily.
These are the places that UI thread definitely executes:
event handlers which gets "RoutedEventArgs" as a parameter
Constructors of UI elements
the definitions of method calls in above methods (means that transitively looking at call graphs of these event handler methods and UI constructors)
Is there any other place or is there something wrong about above list?
Every method called by using the Dispatcher or the right SynchronizationContext will execute on the UI thread. That makes exhaustive static analysis impossible. For instance, the callback of the WebClient class executes on the UI thread. How are you supposed to predict those corner cases?
A quick tip though, quite useful is you have a method that can be called both from a UI or a non-UI thread. By calling the method Dispatcher.CheckAccess() (this method isn't shown by the intellisense in Visual Studio, so it's hard to discover), you can know if you need to call the Dispatcher or not:
if (Dispatcher.CheckAccess())
{
// In the UI thread
SomeMethod();
}
else
{
// Not in the UI thread
Dispatcher.BeginInvoke(SomeMethod);
}
From there, you can write a wrapper:
public void CallDispatcherIfNeeded(Action method) // You might want a shorter name
{
if (Dispatcher.CheckAccess())
{
// In the UI thread
method();
}
else
{
// Not in the UI thread
Dispatcher.BeginInvoke(method);
}
}
And then you just have to call it, without worrying whether you're on the UI thread or not:
CallDispatcherIfNeeded(SomeMethod);
That said, if your code is correctly written, it's quite rare to need this kind of trick.
I would look at when Dispatcher.BeginInvoke is actually needed, not the other way around.
It is almost never needed, excepted when handing an async completed event which may start out on a background thread, and thus if you want to do something with the UI, you need to marshal it over to the UI thread.
In other words, unless you need to do something with the UI from a background thread, you don't need it.
Greg

How do I gracefully exit an X11 event loop?

Almost every tutorial I find tells me to do this for my event loop:
XEvent event;
while (true)
{
XNextEvent(display, &event);
switch (event.type)
{
case Expose:
printf("Expose\n");
break;
default:
break;
}
}
However, clicking the X to close the program results in this message.
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 10 requests (10 known processed) with 0 events remaining.
It is indeed strange to me that the examples suggest using an infinite loop. That doesn't sound natural, and my other X11 programs don't do that. So I searched around. I found out how to capture the window close event.
Atom wmDeleteMessage = XInternAtom(mDisplay, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteMessage, 1);
XEvent event;
bool running = true;
while (running)
{
XNextEvent(display, &event);
switch (event.type)
{
case Expose:
printf("Expose\n");
break;
case ClientMessage:
if (event.xclient.data.l[0] == wmDeleteMessage)
running = false;
break;
default:
break;
}
}
That works. It exits without errors. ... But I refuse to believe this is the normal way to do things. I mean, is this the only way to properly exit an X11 app? It seems like a lot of work just to capture the close event. How do I make a 'proper' event loop? Why is the close event so deeply buried? What am I missing?
The problem lays in the communication between X Server and the Window Manager.
When you call XCreateWindow or XCreateSimpleWindow, the X Server creates your window (not showing it until you explicitly map it on the screen by calling XMapWindow), and then the Window Manager is responsible for attaching all the decorations and buttons and system menu around your window.
You can call XDestroyWindow on your own to remove the window, and this usually means it just disappears from the screen, but your program is still running and the connection to the X Server is still open, so you can send it some more requests.
The problem begins when the user clicks that little X button attached to your window by the Window Manager, because it is not created by the X Server and it is not his business to decide what to do then. Now it's all in hands of Window Manager.
If the Window Manager simply called XDestroyWindow on your window, it would cause a problem if your application wanted to capture the closing event to do something before the window gets destroyed. So the convention has been established between the X Server and the Window Managers to handle this process.
The default behavior of most Window Managers is to destroy the window and close the connection with the X server, because this is what most users of Window Managers would expect: that when they close the window, the program will end (and the connection to the X Server will close with the closed window). And then, when you try to call XCloseDisplay(display), it will cause the IO error you've mentioned, because the connection to the server is already closed and the display structure is invalid.
Here's an excerpt from the Xlib documentation which explains this:
Clients that choose not to include WM_DELETE_WINDOW in the WM_PROTOCOLS property may be disconnected from the server if the user asks for one of the client's top-level windows to be deleted.
Yeah, it would be great if they didn't hide it so deep in their docs, though :-P
But when you already find it, fortunately it also hints for the solution.
If you want a different behavior (that is, to capture the closing event from the Window Manager), you need to use the WM_DESTROY_WINDOW protocol.
Another excerpt from the docs:
Clients, usually those with multiple top-level windows, whose server connection must survive the deletion of some of their top-level windows, should include the atom WM_DELETE_WINDOW in the WM_PROTOCOLS property on each such window. They will receive a ClientMessage event as described above whose data[0] field is WM_DELETE_WINDOW.
I had the same error and I wanted to know exactly what causes it and why. It took me some time to figure it out and find the proper explanation in the doc, so I put my explanation here to save the time of others uninformed.
There are no such things as "exit button" or "application" or "close event" in X11. This is by design.
Window decorations, exit buttons and many the other things we depend upon are not built into X11. They are implemented on top of the core X11 instead. The name of the particular set of conventions responsible for wmDeleteMessage is ICCCM, look it up.
Xlib only deals with the core X11 protocol. No built-in close event there.
There are toolkits that make dealing with ICCCM and all other things that are not built into X11 easier (GTK, wxWindows, Qt, ...) You probably want to use one of those.

Glade, GtkBuiler or Gtk unexpectedly swapped a signal handler

void
add_button_clicked(GtkButton * widget, GtkTreeView * treeview){
printf("%s\n",G_OBJECT_TYPE_NAME(widget));
} // Prints GtkTreeView
While the standard GtkButton:clicked signal is supposed to work like this (the widget itself followed by the user data) for some reason, the swapped signal handler has been called here and the two parameters are mixed up.
I could just shuffle the code around a bit to get past this but I would like to get to the bottom of why this is happening so I can fix/prevent it.
The glade swap checkbox is unchecked, and I didn't manually connect the signal anywhere, so I don't know why this is happening.
Edit: Ok, now it's happening to ALL my callbacks which is a big huge tremendous problem! Please help!
Edit: This "Feature" is apparently here so glade users can add a glade object to the callback and directly reference a gtk function so that they can for example hide a widget without having to write a single line of code. I'll roll with it.
Glade does this so that when you pass another object you can call a standard gtk function directly on it (Such as closing a window) without needing to write any code at all.

speedup or mute audio playback in a windows process via hooking calls

I have an application which I do not have source code for that plays back audio but lacks the facility to speed up or temporarily mute audio playback. I am trying to add these two functionalities by injecting a dll and hooking waveOutOpen, waveOutWrite (and either preventing playback or trying to waveOutSetPlaybackRate for mute and speedup respectively). I have a couple of problems however.
Lets first talk about the "mute" button since that was my first order of business:
I will refer to the hooked versions of functions as MyWaveOut...
1)
I am able to hook the functions no problem and I see them being called in dbgView log statements. My first attempt was to just intercept waveOutWrite function calls and if my "mute" button is pressed to just return MMSYSERR_NOERROR in MyWaveOutWrite function and not call the real waveOutWrite. This correctly prevents the audio from playing however I encounter a problem when I turn off the "mute" . The audio does not play again when I disable the mute, in fact the hooked function (nor the real one) is ever called again by the hooked process.
My first suspicion was that I was not calling the callback waveOutProc that is registered by the application when it calls WaveOutOpen. I therefore also hooked the waveOutOpen and kept and switched the address of the waveOutProc to MyWaveOutProc and added logic to call the real waveOutProc before returning the same MMSYSERR_NOERROR in MyWaveOutWrite. This had weird results causing the playback visual to skip forward very fast but then I had the same problem with the audio not returning (MyWaveOutWrite function never called again).
2)
As for the audio speedup portion I tried using waveOutSetPlaybackRate but this simply returned MSYSERR_NOTSUPPORTED so I arrived at a dead end with that. Hooking GetTickCount and QueryPerformanceCounter had no affect on the audio playback at all, the visual playback indicator would skip forward ahead fast if the audio is disabled, but when the audio device is enabled the visual seems even to be bound to the rate of the audio playback.
Any help with with those experienced with either of these problems would be appreciated. I do not usually work with windows audio so it is a bit of a learning experience for me.
Thanks
A suggestion for a different approach.
This assumes the OS is from Vista Kernnel or higher.
In these operation systems there is a pr application volume control, it might be simpler to mute it here then with any hooks.

Intercept WM_DELETE_WINDOW on X11?

I'd like to intercept the WM_DELETE_WINDOW message that is posted to a certain selection of windows that an application I'm writing (AllTray), so that I can act on it instead of the application receiving it. I'm currently looking at trying this at the GDK level via gdk_display_add_client_message_filter if possible, but I'd be happy with an Xlib solution if there is one as well; it seems to be possible, but I just don't seem to be understanding how I am to do it successfully.
Currently, I have two programs (written in C) that I am trying to use to get this figured out, the first one does nothing but create a window and register that it knows about WM_DELETE_WINDOW, and the second one attempts to catch that message, but seems to fail in doing so; it appears to do precisely nothing. Am I understanding the documentation wrong on this, or is there something additional that I need to be doing (or do I need to avoid using GDK entirely for this)?
The background is this: Prior to my re-write of AllTray, the way it would do things appears to be to try to intercept a mouse-click on the X button itself. For some window managers, this worked properly, for others it didn't work at all, and for others, the user had to configure it manually and instruct AllTray where the button for closing the window was. What I am looking for is a solution that doesn't involve a LD_LIBRARY_PRELOAD and will work for any window manager/application combination that conforms to the current standards and sends a WM_DELETE_WINDOW ClientMessage when the window is closed.
UPDATE: I'm still looking for an answer. The route that I am taking at the moment is to try to reparent the window and manage it myself, but I just cannot make it work. Upon reparenting, I don't seem to be able to get it back in any way. I may be missing something very fundamental, but I can't figure out how to actually make it appear it my own window again, to bring it back on the screen.
UPDATE 2: Alright, so I've hit another brick wall. The X server documentation says to set the StructureNotifyMask on the window's event mask to receive both MapNotify and ReparentNotify events. I'd be interested in receiving either. My current thinking was to create a window that served just as an event receiver, and then when I get events for interesting things, act on them by creating and reparenting. However, this simply doesn't seem to be working. The only events I actually receive are PropertyNotify events. So, this route doesn't seem to be doing very much good, either.
I don't know X11, but I googled using "Intercept WM_DELETE_WINDOW X11" as keywords. Found 17k - MarkMail and Mplayer-commits r154 - trunk/libvo. In both cases they are doing the same thing.
/* This is used to intercept window closing requests. */
static Atom wm_delete_window;
within static void x11_init(),
XMapWindow(display, win);
wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, win, &wm_delete_window, 1);
then, within static int x11_check_events(),
XEvent Event;
while (XPending(display)) {
XNextEvent(display, &Event);
if (Event.type == ClientMessage) {
if ((Atom)Event.xclient.data.l[0] == wm_delete_window) {
/* your code here */
}
}
}
See XInternAtom, XSetWMProtocols and XNextEvent.
After I wrote the above, I found Handling window close in an X11 app:
When a user clicks the close button
[x] on our X11 application we want it
to pop a a dialog asking “do you
really want to quit?”. This is a plain
X app. No fancy GTK or QT widgets
here. So how to catch the “window is
being closed” message?
The answer is to tell the Window
Manager we are interested in these
event by calling XSetWMProtocols and
registering a WM_DELETE_WINDOW message
with it. Then we’ll get a client
message from the Window Manager if
someone tries to close the window, and
it won’t close it, it’ll leave that us
up to us. Here’s an example….
// example.cpp
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <iostream>
int main()
{
Display* display = XOpenDisplay(NULL);
Window window = XCreateSimpleWindow(display,
DefaultRootWindow(display),
0, 0,
500, 400,
0,
0, 0);
// register interest in the delete window message
Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteMessage, 1);
std::cout << "Starting up..." << std::endl;
XMapWindow(display, window);
while (true) {
XEvent event;
XNextEvent(display, &event);
if (event.type == ClientMessage &&
event.xclient.data.l[0] == wmDeleteMessage) {
std::cout << "Shutting down now!!!" << std::endl;
break;
}
}
XCloseDisplay(display);
return 0;
}
Unfortunately, the best answer to this question is a series of non-answers; there are technically ways to accomplish it, but they all have downfalls that make them extremely impractical:
Create an X11 proxy for an application, passing all X11 protocol messages back and forth between the application and the X server. The proxy would then filter out any interesting messages. The downside to this is that this is an awful lot of overhead for a single little tiny feature, and the X11 protocol is complex. There could also be unintended consequences, which makes this an even more unattractive option.
Launch as a standard application that acts as an intermediary between the window manager and “interesting” client applications. This breaks some things, such as XDnD. In effect, it is not unlike the first option, except that the proxy is at the Window level as opposed to the X11 protocol level.
Use the non-portable LD_PRELOAD library trick. This has several downsides:
It is non-portable across dynamic linkers: not all dynamic linkers support LD_PRELOAD, even among UNIX-like systems.
It is non-portable across operating systems: not all operating systems support featureful dynamic linkers.
It breaks network-transparency: the shared object/dynamic link library must reside on the host as the child process that is being executed.
Not all X11 applications use Xlib; it would be necessary to write one LD_PRELOAD module for each of the libraries that an application might use to talk with X11.
In addition to the last point, not all applications would be susceptible to LD_PRELOAD even if they ran under a linker that supported it, because they may not use a shared object or DLL in order to communicate with X; consider, for example, a Java application which uses an X11 protocol library written in Java itself.
On some UNIX-like operating systems, LD_PRELOAD libraries must be setuid/setgid if they are to be used with setuid/setgid programs. This is, of course, a potential security vulnerability.
I am quite sure that are more downsides that I cannot think of.
Implement an extension to the X Window system. Non-portable among X11 implementations, complex and convoluted as all get out, and absolutely out of the question.
Implement extensions or plug-ins to window managers. There are as many window managers as there are opinions on window managers, and therefore this is utterly infeasible.
Ultimately, I was able to finally accomplish my goal by using a completely separate mechanism; anyone who is interested, please see the Close-to-Tray support in AllTray 0.7.5.1dev and later, including the git master branch available on github.
Ok, to elaborate on my earlier suggestion, you might want to investigate XEmbed. At the least, that might give you some ideas to try.
Failing that, I'd have a look at how other similar software might be working (e.g. wmdock, or how GtkPlug/GtkSocket is implemented), though I believe in both those cases explicit support is required in the applications.
Hope that is more helpful.
You should read ICCCM that tells you how window manager communicates with client. Most of WM will create a frame window to contain your top-level window via reparenting. Thus, if your reparent may break the relationship known by WM and your client window.

Resources