I need a event handler when ever shutdown message is send to system.
Can anyone help?
When ever we try to shutdown a system, and if any dialog box is open shutdown process terminates. I don't want this to happen in my application. i.e if any dialog box is open from my application and I try to shutdown my system then it should not block shutdown process. Is this implementation possible?
Thanks,
Rahul
Yes, look at NSWorkspaceWillPowerOffNotification
http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html
try overriding QApplication::commitData it should be called whenever user shuts down the system and your application is still running.
This function deals with session
management. It is invoked when the
QSessionManager wants the application
to commit all its data.
Usually this means saving all open
files, after getting permission from
the user. Furthermore you may want to
provide a means by which the user can
cancel the shutdown.
below is an example (never tried it with macs; though works fine on my ubuntu):
main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QSessionManager>
class MyApplication : public QApplication
{
public:
MyApplication(int &argc, char **argv);
virtual void commitData(QSessionManager& sm);
};
MyApplication::MyApplication(int &argc, char **argv):
QApplication(argc, argv)
{
//???
}
void MyApplication::commitData(QSessionManager& sm)
{
// do smth here....
QApplication::commitData(sm);
}
int main(int argc, char *argv[])
{
MyApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
hope this helps, regards
Related
I am developing a GTK+3 application in C using MSVC (Visual Studio) on windows for a college project.
I've run the debugger and found that the application crashes while returning from a libffi call. The stack is corrupted and hence the program's return address is garbage.
The thing is, it runs fine in Release mode, probably due to optimization, but crashes in Debug mode. What could be the cause?
I have no clue how to resolve the problem... Any help would be appreciated.
Here is the part of the code which causes the error:
ffi_call_win64 (stack, frame, closure);
} // Error here
Exception thrown: read access violation.
pn was 0xFFFFFFFFFFFFFFFB.
MCVE
#include <gtk/gtk.h>
static void on_activate(GtkApplication* app) {
// Create a new window
GtkWidget* window = gtk_application_window_new(app);
// Create a new button
GtkWidget* button = gtk_button_new_with_label("Hello, World!");
// When the button is clicked, destroy the window passed as an argument
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
}
int main(int argc, char* argv[]) {
// Create a new application
GtkApplication* app = gtk_application_new("com.example.GtkApplication",
G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
I'm writing a kernel module that need to ask an hid raw device periodically.
I tried hrtimer and a simple timer and each time I call hid_hw_raw_request I got a "BUG: scheduling while atomic".
If I try the same function outside my timer function (eg in the init), it works fine (no bug).
How could periodically call this function without generating any bug ?
You need to use a work queue to issue your hid_hw_raw_request as deferred work. This can be done as in the following example module:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/workqueue.h>
static void hid_work_handler(struct work_struct *hid_work);
static struct workqueue_struct *hid_workqueue;
static DECLARE_WORK(hid_work, hid_work_handler);
static void hid_work_handler(struct work_struct *hid_work)
{
...
hid_hw_raw_request(...);
...
}
static int __init hid_work_init(void)
{
if (!hid_workqueue)
hid_workqueue = create_singlethread_workqueue("hid_workqueue");
if (hid_workqueue)
queue_work(hid_workqueue, &hid_work);
return 0;
}
static void __exit hid_work_exit(void)
{
if (hid_workqueue) {
flush_workqueue(hid_workqueue);
destroy_workqueue(hid_workqueue);
}
}
module_init(hid_work_init);
module_exit(hid_work_exit);
MODULE_DESCRIPTION("hid_work_test");
MODULE_LICENSE("GPL");
Note that for the real implementation you'll need to create your own data struct with an included struct work_struct to be queued. This data struct will likely contain the hiddev, buffer, etc. that the hid_work_handler needs to do the actual transfer. See LDD3 Chapter 7 for more details (albeit syntax of calls is outdated, the basic explanation still applies).
I am using Qt 4.8. Is there any way to have a global try and catch block for whole project. Example, if my application has two .cpp files. Is it possible way to catch exception across both .cpp files?
First of all, be warned that Qt doesn't play nice with exceptions. It was designed back in those days when exceptions were rather obscure feature of C++ so the use of exceptions was not generally considered a good practice for a whole bunch of implementation-related reasons.
Also be warned that as of Qt 5.7 the exception safety is not feature complete as the official doc currently tells:
Preliminary warning: Exception safety is not feature complete! Common cases should work, but classes might still leak or even crash.
If you use signal-slot connections within your classes, it's best to handle exceptions inside the slots which may throw them. As of Qt 5.7 not doing so is considered undefined behaviour.
If you just want to do some cleanup and/or error logging on any occasionally uncaught exception, you can either wrap the entire main() contents into try/catch block as the previous answer suggests or alternatively you can wrap the Qt's main event loop into such a block:
QApplication app(argc, argv);
...
try {
app.exec();
}
catch (const std::exception &) {
// clean up here, e.g. save the session
// and close all config files.
return 0; // exit the application
}
You can put in brackets the entire contents of your main() function as follows::
int main(int argc, char *argv[])
{
int ret = 0;
try
{
QApplication a(argc, argv);
QWidget w;
w.show();
ret = a.exec();
}
catch(...)
{
/* ... */
}
return ret;
}
See also: std::set_terminate()
I want to debug the ffmpeg. I add the following code to print logs:
av_log(s, AV_LOG_PANIC, fmt, ...)
or
printf("msg....")
But it can't work. There isn't any debug information.
Then I enable the debug build option:
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-debug"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-debug=0"
It can't work.
I'm sure that the place where I added the trace will be executed.
I just want to print some simple informations, how to do it ?
I found a solution:
void my_log_callback(void *ptr, int level, const char *fmt, va_list vargs)
{
vprintf(fmt, vargs);
}
Set the log level and register the log callback:
av_log_set_level(AV_LOG_ERROR);
av_log_set_callback(my_log_callback);
this works fine:
int main(int argc, char *argv[])
{
av_log_set_level(AV_LOG_DEBUG);
...
}
I am developing a NPAPI plugin using firebreath. I am using a third party dll to integrate to a gaming device.The inputs on the devices are propagated to the plugin through a message only window(HWND) registered while opening a channel to the device.
Initially, handshake with the device driver,
handshake(HWND,...) and after which on user input, a callback is made on CustomWinProc() to notify.
I did the following,
-Created an Header&CPP file under the WIN-CustomCallbackHandler.h ,
#include "Win\PluginWindowWin.h"
#include "Win\WindowContextWin.h"
class CustomCallbackHandler : public FB::PluginWindowWin
{
public:
CustomCallbackHandler (const FB::WindowContextWin& ctx);
protected:
virtual bool CustomWinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM
lParamm,LRESULT & lRes);
};
-CustomCallbackHandler.cpp
[code]
#include "CustomCallbackHandler.h"
#include "PluginWindowForwardDecl.h"
#include "Win\WindowContextWin.h"
#include "Win\PluginWindowWin.h"
CustomCallbackHandler::CustomCallbackHandler(const FB::WindowContextWin& ctx) :
FB::PluginWindowWin(ctx){
}
bool CustomCallbackHandler::CustomWinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM
lParamm,LRESULT & lRes){
//if WPARAM is something some operation has to be performed.
return false;
}
[/code]
-Factory.cpp - Added the following method to override the PluginWindowWin
FB::PluginWindowWin* createPluginWindowWin(const FB::WindowContextWin& ctx)
{
return new CustomCallbackHandler(ctx);
}
-MyFirstPluginAPI.cpp-(The auto generated JSAPIAuto subclass)- JS method.
bool MyFirstPluginAPI::handshake(FB::JSObjectPtr &callback)
{
FB::WinMessageWindow window;
thirdpartymethod(window.getHWND());
}
Now,When I debug I could see the customcallbackhandler being invoked a couple of times for the regular plugin events but the events generated by the devices are not available.I believe a different instance of message window is passed on to the dll.
-How do I get the handle of the PluginWindowWin?
-Once I receive a callback on the CustomCallbackHandler,How do I generate a custom sendEvent()?
Your help is highly appreciated.
I am a Java developer and don't have much experience in C++ programming. I believe I am missing something fundamental.
What you want is to use WinMessageWindow:
https://github.com/firebreath/FireBreath/blob/master/src/PluginCore/Win/WinMessageWindow.h
You don't want to use PluginWindowWin; that's too specific for other things. WinMessageWindow was created specifically to do the types of things you are trying to do, and it allows you to make a winproc handler on the containing class.
I recently posted an example of using WinMessageWindow in order to receive WM_DEVICENOTIFY messages; I'm sure you can use it as an example of how the class works to get you started.