I am using GTK as the user interface to a piece of hardware (a 3D printer). The hardware can create events as a result of user interactions with the machine (not the GUI). For example, they can remove the build plate which trips a switch thus sending a signal to my program that this has happened, but how do I get gtk_main() to recognize that this event has happened?
In other words, how do I get gtk_main() to watch for non-standard device input events?
You can use custom actions provided by GtkApplication.
In any case you must implement by yourself the monitoring code, either by polling your hardware status or by leveraging more advanced techniques (if possible).
/* gcc -o monitor monitor.c $(pkg-config --cflags --libs gtk+-3.0) */
#include <gtk/gtk.h>
static void
startup(GtkApplication *application)
{
GAction *action;
/* plate-removed is a custom action */
action = G_ACTION(g_simple_action_new("plate-removed", NULL));
g_action_map_add_action(G_ACTION_MAP(application), action);
g_object_unref(action);
/* You can connect your callbacks to the GAction::activate signal */
action = g_action_map_lookup_action(G_ACTION_MAP(application), "plate-removed");
g_signal_connect_swapped(action, "activate",
G_CALLBACK(g_print), "Plate has been removed\n");
}
static gpointer
worker_thread(gpointer user_data)
{
GApplication *application = G_APPLICATION(user_data);
for (;;) {
g_usleep(g_random_int_range(500000, 5000000));
/* Event occurred: emit the signal */
g_action_group_activate_action(G_ACTION_GROUP(application),
"plate-removed", NULL);
}
return NULL;
}
static gboolean
polling_loop(gpointer user_data)
{
GApplication *application = G_APPLICATION(user_data);
if (g_random_int_range(1, 20) == 8) {
/* Event occurred: emit the signal */
g_action_group_activate_action(G_ACTION_GROUP(application),
"plate-removed", NULL);
}
return G_SOURCE_CONTINUE;
}
static void
activate(GtkApplication *application)
{
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_application_add_window(application, GTK_WINDOW(window));
gtk_widget_show_all(window);
/* You can use whatever you want to monitor your hardware, in particular
* a polling loop or a worker thread */
g_timeout_add(50, polling_loop, application);
/* g_thread_new("WorkerThread", worker_thread, application); */
}
int
main(int argc, char *argv[])
{
GtkApplication *application;
int retval;
application = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect(application, "startup", G_CALLBACK(startup), NULL);
g_signal_connect(application, "activate", G_CALLBACK(activate), NULL);
retval = g_application_run(G_APPLICATION(application), argc, argv);
g_object_unref(application);
return retval;
}
Related
The problem is that program just handles the command line arguments and exits, instead of displaying a GUI too.
For example:
#include <gtk/gtk.h>
static gint print_cmd_arg(GApplication *app, GApplicationCommandLine *app_cmd, int *argc)
{
if (*argc > 1)
{
char **argv = g_application_command_line_get_arguments(app_cmd, argc);
GFile *file = g_file_new_for_commandline_arg(argv[1]);
if (g_file_query_exists(file, NULL))
{
char *text;
g_file_load_contents(file, NULL, &text, NULL, NULL, NULL);
g_print("%s", text);
g_free(text);
g_object_unref(file);
return 0;
}
else
{
g_print("File \'%s\' does not exist.\n", argv[1]);
g_object_unref(file);
return 1;
}
}
return 2;
}
static void activation(GtkApplication *app, gpointer user_data)
{
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_title(GTK_WINDOW(window), "Test");
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_widget_destroy), NULL);
GtkWidget *button = gtk_button_new_with_label("Quit");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
}
int main(int argc, char **argv)
{
int status;
GtkApplication *test = gtk_application_new("this.is.only.a.test", G_APPLICATION_NON_UNIQUE | G_APPLICATION_HANDLES_COMMAND_LINE);
g_signal_connect(test, "activate", G_CALLBACK(activation), NULL);
g_signal_connect(G_APPLICATION(test), "command-line", G_CALLBACK(print_cmd_arg), &argc);
status = g_application_run(G_APPLICATION(test), argc, argv);
return status;
}
Try running this, you'll see that this program completely ignores function activation.
What I want this program to do is to both handle command line arguments and display GUI.
Also, I know I'm supposed to use g_application_command_line_set_exit_status() in print_cmd_arg instead of returns, but I don't know how to do this and get compiler warnings.
Handling command line is a complex subject in the GtkApplication/GApplication context due to added flexibility.
If you are "overriding" the command line handling/parsing then a variety of methods to do so will be available.
Information on this subject is somewhat scattered in the documentation. Some of it is here:
https://wiki.gnome.org/HowDoI/GtkApplication
https://wiki.gnome.org/HowDoI/GtkApplication/CommandLine
https://developer.gnome.org/gio//2.30/GApplication.html#g-application-run
https://developer.gnome.org/gtk3/stable/GtkApplication.html
https://developer.gnome.org/gtk3/stable/gtk-getting-started.html
From 1) you can read:
Dealing with the command line
Normally, GtkApplication will assume that arguments passed on the
command line are files to be opened. If no arguments are passed, then
it assumes that an application is being launched to show its main
window or an empty document. In the case that files were given, you
will receive these files (in the form of GFile) from the open signal.
Otherwise you will receive an activate signal. It is recommended that
new applications make use of this default handling of command line
arguments.
If you want to deal with command line arguments in more advanced ways,
there are several (complementary) mechanisms by which you can do this.
And an important bit is:
Note in particular that it is possible to use action activations
instead of activate or open. It is perfectly reasonable that an
application could start without an activate signal ever being emitted.
activate is only supposed to be the default "started with no options" signal. Actions are meant to be used for anything else.
So, there are various ways to handle arguments. The questions goes to how and what are you trying to do. If the arguments are files then maybe handle them with the open signal and avoid handling the command line.
To solve the actual problem, as you may conclude, activate won't be fired but you can do it by using g_application_activate in your command line callback, e.g.:
static gint print_cmd_arg(GApplication *app, GApplicationCommandLine *app_cmd, int *argc)
{
/* ADD THIS TO YOUR CODE */
g_application_activate(app);
/* ENDS HERE */
if (*argc > 1)
{
char **argv = g_application_command_line_get_arguments(app_cmd, argc);
GFile *file = g_file_new_for_commandline_arg(argv[1]);
if (g_file_query_exists(file, NULL))
{
char *text;
g_file_load_contents(file, NULL, &text, NULL, NULL, NULL);
g_print("%s", text);
g_free(text);
g_object_unref(file);
return 0;
}
else
{
g_print("File \'%s\' does not exist.\n", argv[1]);
g_object_unref(file);
return 1;
}
}
return 2;
}
Today, I tried to use polkit - an application-level toolkit for defining and handling the policy that allows unprivileged processes to speak to privileged processes. In order to understand it better, I tried to download this code:
#include <polkit/polkit.h>
static gboolean
on_tensec_timeout (gpointer user_data)
{
GMainLoop *loop = user_data;
g_print ("Ten seconds has passed. Now exiting.\n");
g_main_loop_quit (loop);
return FALSE;
}
static void
check_authorization_cb (PolkitAuthority *authority,
GAsyncResult *res,
gpointer user_data)
{
GMainLoop *loop = user_data;
PolkitAuthorizationResult *result;
GError *error;
error = NULL;
result = polkit_authority_check_authorization_finish (authority, res, &error);
if (error != NULL)
{
g_print ("Error checking authorization: %s\n", error->message);
g_error_free (error);
}
else
{
const gchar *result_str;
if (polkit_authorization_result_get_is_authorized (result))
{
result_str = "authorized";
}
else if (polkit_authorization_result_get_is_challenge (result))
{
result_str = "challenge";
}
else
{
result_str = "not authorized";
}
g_print ("Authorization result: %s\n", result_str);
}
g_print ("Authorization check has been cancelled and the dialog should now be hidden.\n"
"This process will exit in ten seconds.\n");
g_timeout_add (10000, on_tensec_timeout, loop);
}
static gboolean
do_cancel (GCancellable *cancellable)
{
g_print ("Timer has expired; cancelling authorization check\n");
g_cancellable_cancel (cancellable);
return FALSE;
}
int
main (int argc, char *argv[])
{
pid_t parent_pid;
const gchar *action_id;
GMainLoop *loop;
PolkitSubject *subject;
PolkitAuthority *authority;
GCancellable *cancellable;
g_type_init ();
if (argc != 2)
{
g_printerr ("usage: %s <action_id>\n", argv[0]);
return 1;
}
action_id = argv[1];
loop = g_main_loop_new (NULL, FALSE);
authority = polkit_authority_get_sync (NULL, NULL);
/* Typically mechanisms will use a PolkitSystemBusName since most
* clients communicate with the mechanism via D-Bus. However for
* this simple example we use the process id of the calling process.
*
* Note that if the parent was reaped we have to be careful not to
* check if init(1) is authorized (it always is).
*/
parent_pid = getppid ();
if (parent_pid == 1)
{
g_printerr ("Parent process was reaped by init(1)\n");
return 1;
}
subject = polkit_unix_process_new (parent_pid);
cancellable = g_cancellable_new ();
g_print ("Will cancel authorization check in 10 seconds\n");
/* Set up a 10 second timer to cancel the check */
g_timeout_add (10 * 1000,
(GSourceFunc) do_cancel,
cancellable);
polkit_authority_check_authorization (authority,
subject,
action_id,
NULL, /* PolkitDetails */
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
cancellable,
(GAsyncReadyCallback) check_authorization_cb,
loop);
g_main_loop_run (loop);
g_object_unref (authority);
g_object_unref (subject);
g_object_unref (cancellable);
g_main_loop_unref (loop);
return 0;
}
But when I tried to compile it with gcc, I received this output:
ciao.c:35:27: fatal error: polkit/polkit.h: File o directory non esistente
compilation terminated.
The path polkit/polkit.h is not in the system include path. Make sure you've downloaded and installed PolKit so that the headers and libraries are available.
I solved my problem adding this
pkg-config --cflags glib-2.0 polkit-gobject-1
and this
pkg-config --libs glib-2.0 polkit-gobject-1
to gcc compilation's command in this way
gcc `pkg-config --cflags glib-2.0 polkit-gobject-1` -o polkit PolicyKit/example.c `pkg-config --libs glib-2.0`
This is in my continuation of GTK understanding::
Is it right to call GTK_MAIN() under pthread from Main?? sample code ::
from main i call dispInit(argc, argv); from which i call g_thread_create(main_callback, NULL, FALSE, NULL);
** also i have not included g_idle_add in this code..this is just a reference code.
Please guide
#include <stdio.h>
#include <glib.h>
#include <gtk/gtk.h>
//#include "dispimage.h"
#include <windows.h>
#define sleep(n) Sleep(1000 * n)
GtkWidget* window;
void dispInit(int argc, char* argv[]);
void dispInfoPage(char* fileName, int duration);
gpointer main_callback(gpointer data)
{
gtk_main();
return 0;
}
void dispInit(int argc, char* argv[])
{
gdk_threads_init();
gdk_threads_enter();
printf("Initializing the display library\n");
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_resize(GTK_WINDOW(window), 640, 480);
gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
gtk_widget_realize( window );
gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
g_thread_create(main_callback, NULL, FALSE, NULL);
gdk_threads_leave();
}
void dispInfoPage(char* fileName, int duration)
{
int index;
gdk_threads_enter();
printf("Initializing dispInfoPage\n");
destroyWidget();
printf("Initializing dispInfoPage1\n");
GtkWidget *image;
image = gtk_image_new_from_file(fileName);
printf("Initializing dispInfoPage2\n");
gtk_container_add(GTK_CONTAINER(window), image);
gtk_widget_show(image);
gtk_widget_show(window);
printf("Initializing dispInfoPage4\n");
printf("Initializing dispInfoPage5\n");
gdk_threads_leave();
printf("Initializing dispInfoPage6\n");
}
void destroyWidget()
{
GList *children, *iter;
struct WidgetsAlive *temp, *prev, *next, *depTemp;
children = gtk_container_get_children(GTK_CONTAINER(window));
for(iter = children; iter != NULL; iter = g_list_next(iter)){
gtk_container_remove(GTK_CONTAINER(window),GTK_WIDGET(iter->data));
printf("Deleting Widget\n");
}
g_list_free(iter);
g_list_free(children);
}
int dispTextPage(char* fileName, int isJustifyCenter)
{
int index;
GtkWidget *textv;
GdkWindow *textv_window;
GdkPixmap *pixmap = NULL;
GtkTextBuffer *textBuffer;
gdk_threads_enter();
GdkColor color;
char debugBuf[128] = { '\0' };
char newfName[100]={'\0'};
char ext[4]={'\0'};
char temp[100]={'\0'};
int i;
FILE * fd;
destroyWidget();
textBuffer = gtk_text_buffer_new(NULL);
textv = gtk_text_view_new_with_buffer(textBuffer);
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textv), 22);
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(textv), 20);
gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textv),1);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textv), GTK_WRAP_CHAR);
if (isJustifyCenter == 1)
{
gtk_text_view_set_justification(GTK_TEXT_VIEW(textv), GTK_JUSTIFY_CENTER);
}
else
{
gtk_text_view_set_justification(GTK_TEXT_VIEW(textv), GTK_JUSTIFY_LEFT);
}
gtk_text_view_set_editable(GTK_TEXT_VIEW(textv), FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textv), FALSE);
printf("tttt0");
gtk_container_add(GTK_CONTAINER(window), textv);
printf("tttt1");
textv_window = gtk_text_view_get_window (GTK_TEXT_VIEW (textv),
GTK_TEXT_WINDOW_TEXT);
gdk_color_parse ("#68604d", &color);
pixmap = gdk_pixmap_create_from_xpm ((GdkDrawable *) textv_window, NULL,
&color, fileName);
gdk_window_set_back_pixmap (textv_window, pixmap, FALSE);
g_object_unref(pixmap);
textBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textv));
gtk_text_buffer_create_tag (textBuffer, "Red", "foreground", "Red", NULL);
gtk_text_buffer_create_tag (textBuffer, "RedBold","foreground", "Red",NULL);
gtk_text_buffer_create_tag(textBuffer, "gray_bg", "background", "gray", NULL);
gtk_text_buffer_create_tag(textBuffer, "italic", "style", PANGO_STYLE_ITALIC, NULL);
gtk_text_buffer_create_tag(textBuffer, "bold","weight", PANGO_WEIGHT_BOLD, NULL);
gtk_text_buffer_create_tag (textBuffer, "RedFontWeight", "weight", 1000,NULL);
gtk_text_buffer_create_tag (textBuffer, "RedBoldFontWeight","weight", 1000,NULL);
gtk_widget_show(textv);
gtk_widget_show(window);
gdk_threads_leave();
return index;
}
void *fsmThread_RunFunction()
{
int pollMsgRetVal = -1;
printf("Now enter into for image");
dispInfoPage("../images/givefp.gif",1);
sleep(5);
dispInfoPage("../images/bootup.gif",1);
sleep(5);
dispInfoPage("../images/givefp.gif",1);
sleep(5);
dispInfoPage("../images/bootup.gif",1);
sleep(5);
printf("Now enter into for disptext");
dispTextPage("",0);
printf("Now exit for disptext");
}
int main(int argc, char *argv[])
{
GThread *fsmThreadId;
GError *error = NULL;
g_thread_init(NULL);
dispInit(argc, argv);
dispInfoPage("../images/bootup.gif",1);
sleep(5);
printf("Now creat ethread ");
fsmThreadId = g_thread_create(fsmThread_RunFunction,NULL,TRUE,&error);
if (error) {
fflush(stderr);
exit(1);
}
g_thread_join(fsmThreadId);
sleep(2);
printf("ENd of main");
return 0;
}
The short answer: yes, you can call gtk_main() from a thread other than the main C thread, as long as you consistently call all GTK API functions from the same thread throughout the lifetime of the process. More details follow.
According to the documentation, GTK and GDK are not thread-safe (they cannot be concurrently called from multiple threads), but they are thread-aware — they provide locking functions such as gdk_threads_enter and gdk_threads_leave that can be used to synchronize GTK calls accross multiple threads. However, the documentation goes on to say that "with the Win32 backend, GDK and GTK+ calls should not be attempted from multiple threads at all." So, if you care about portability to Windows, you will want to avoid attempting to call GTK API calls from multiple threads. Additionally, GTK 3 completely deprecated the use of the threading locks.
There is, however, one way to safely call into GTK from multiple threads that works on all architectures: place the GTK calls in a function and pass it as callback to g_idle_add (without any special locking) — from any thread. It will schedule the callback to be invoked by the GTK main loop, in whatever thread is running it. This is documented at the end of the description section of the documentation, above the start of the details section.
A word on terminology: the word "main thread" in C normally refers to the thread that runs the C main() function. In a GTK context, the "main thread" often confusingly refers to the thread running the GTK main loop. While the two can be (and typically are) one and the same, GTK doesn't really care what thread you call the main loop from, as long as you call all GTK functions (including gtk_main) from the same one. To avoid confusion, it is best to refer to this thread as the GTK thread, the main loop thread, or the GUI thread.
After a handler of an instance has been blocked with g_signal_handler_block, is it possible to check if the handler is still being blocked or has been unblocked by g_signal_handler_unblock in the meantime, apart from storing the state in a boolean variable for example?
I hoped something like that would be possible
g_signal_handler_block (selection, handler_id_row_selected);
if (g_signal_handler_is_blocked (selection, handler_id_row_selected))
g_print ("is still blocked");
But a "g_signal_handler_is_blocked" function does not exist. g_signal_handler_is_connected is not the right function to use, since the signal handler remains connected, thus the function returns TRUE.
I have tried g_signal_handler_find (), since there is G_SIGNAL_MATCH_UNBLOCKED as one of the match types, but it has not worked yet. Even though I have rewritten my code anyway, I still would like to know if it is possible, since i use the blocking/unblocking relatively often.
g_signal_handler_find here is working as expected. Here is my test case:
#include <gtk/gtk.h>
gboolean
g_signal_handlers_is_blocked_by_func(gpointer instance, GFunc func, gpointer data)
{
return g_signal_handler_find(instance,
G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA | G_SIGNAL_MATCH_UNBLOCKED,
0, 0, NULL, func, data) == 0;
}
static void
handler(void)
{
g_print("handler called\n");
}
static void
switch_blocking(GtkWidget *button)
{
GFunc func = (GFunc) handler;
if (g_signal_handlers_is_blocked_by_func(button, func, NULL)) {
g_signal_handlers_unblock_by_func(button, func, NULL);
g_print("handler unblocked\n");
} else {
g_signal_handlers_block_by_func(button, func, NULL);
g_print("handler blocked\n");
}
}
int
main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label("Click me");
g_signal_connect_after(button, "clicked", G_CALLBACK(switch_blocking), NULL);
g_signal_connect(button, "clicked", G_CALLBACK(handler), NULL);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I have a lightweight application that catches Xorg and dbus events. In order to do this I initialized dbus loop and started g_main_loop, but I don't know how to add Xorg event handling in a natural way:
GMainLoop * mainloop = NULL;
mainloop = g_main_loop_new(NULL,FALSE);
dbus_g_thread_init ();
dbus_init();
// <<<<<<<<<<<<<<<<<<<<<<<<<
//1 way using timeout
//g_timeout_add(100, kbdd_default_iter, mainloop);
//2nd way using pthread
//GThread * t = g_thread_create(kbdd_default_loop, NULL, FALSE, NULL);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>
g_main_loop_run(mainloop);
in default iter I'm checking if there is waiting X-event and handle them.
Both ways seems bad, first because I have unneeded calls for checking event, second because I make an additional thread and have to make additional locks.
P.S. I know I can use gtk lib, but I don't want to have dependencies on any toolkit.
If you want to add Xorg event handling to the main loop without using a timeout (which as you state is wasteful), you'll need to add a source that polls the X connection. For that, you'll need to get below the Xlib abstraction layer to get the underlying X connection file descriptor. That's what the complete program below does. It is an adaptation of C. Tronche's excellent X11 tutorial to use the glib main loop for polling. I also drew from "Foundations of GTK+ Development" by Andrew Krause.
If this doesn't seem very "natural", that's because I doubt there is a very "natural" way to do this - you're really re-implementing a core part of GDK here.
/* needed to break into 'Display' struct internals. */
#define XLIB_ILLEGAL_ACCESS
#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h> // I include this to test return values the lazy way
#include <glib.h>
typedef struct _x11_source {
GSource source;
Display *dpy;
Window w;
} x11_source_t;
static gboolean
x11_fd_prepare(GSource *source,
gint *timeout)
{
*timeout = -1;
return FALSE;
}
static gboolean
x11_fd_check (GSource *source)
{
return TRUE;
}
static gboolean
x11_fd_dispatch(GSource* source, GSourceFunc callback, gpointer user_data)
{
static gint counter = 0;
Display *dpy = ((x11_source_t*)source)->dpy;
Window window = ((x11_source_t*)source)->w;
XEvent e;
while (XCheckWindowEvent(dpy,
window,
EnterWindowMask,
&e))
{
if (e.type == EnterNotify)
g_print("We're in!!! (%d)\n", ++counter);
}
return TRUE;
}
static gboolean
msg_beacon(gpointer data)
{
static gint counter = 0;
g_print("Beacon %d\n", ++counter);
return TRUE;
}
int
main()
{
Display *dpy = XOpenDisplay(NULL);
assert(dpy);
int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, blackColor, blackColor);
XSelectInput(dpy, w, StructureNotifyMask | EnterWindowMask);
XMapWindow(dpy, w);
for (;;) {
XEvent e;
XNextEvent(dpy, &e);
if (e.type == MapNotify)
break;
}
GMainLoop *mainloop = NULL;
mainloop = g_main_loop_new(NULL, FALSE);
/* beacon to demonstrate we're not blocked. */
g_timeout_add(300, msg_beacon, mainloop);
GPollFD dpy_pollfd = {dpy->fd,
G_IO_IN | G_IO_HUP | G_IO_ERR,
0};
GSourceFuncs x11_source_funcs = {
x11_fd_prepare,
x11_fd_check,
x11_fd_dispatch,
NULL, /* finalize */
NULL, /* closure_callback */
NULL /* closure_marshal */
};
GSource *x11_source =
g_source_new(&x11_source_funcs, sizeof(x11_source_t));
((x11_source_t*)x11_source)->dpy = dpy;
((x11_source_t*)x11_source)->w = w;
g_source_add_poll(x11_source, &dpy_pollfd);
g_source_attach(x11_source, NULL);
g_main_loop_run(mainloop);
return 0;
}