xhost + # Workaround for: QXcbConnection: Could not connect to display :0
[appimagekit.git] / notify.c
blobca51e04d4e008859c6c278beb0b57c88a5e509bc
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <dlfcn.h>
4 #include <unistd.h>
6 /* Try to show a notification on the GUI, fall back to the command line
7 * timeout is the timeout in milliseconds. timeout = NULL seems to trigger a
8 * GUI error dialog rather than a notification */
9 int notify(char *title, char *body, int timeout)
11 /* http://stackoverflow.com/questions/13204177/how-to-find-out-if-running-from-terminal-or-gui */
12 if (isatty(fileno(stdin))){
13 /* We were launched from the command line. */
14 printf("\n%s\n", title);
15 printf("%s\n", body);
17 else
19 /* We were launched from inside the desktop */
20 printf("\n%s\n", title);
21 printf("%s\n", body);
22 /* https://debian-administration.org/article/407/Creating_desktop_notifications */
23 void *handle, *n;
24 typedef void (*notify_init_t)(char *);
25 typedef void *(*notify_notification_new_t)( char *, char *, char *, char *);
26 typedef void (*notify_notification_set_timeout_t)( void *, int );
27 typedef void (*notify_notification_show_t)(void *, char *);
28 handle = NULL;
29 if(handle == NULL)
30 handle= dlopen("libnotify.so.3", RTLD_LAZY);
31 if(handle == NULL)
32 handle= dlopen("libnotify.so.4", RTLD_LAZY);
33 if(handle == NULL)
34 handle= dlopen("libnotify.so.5", RTLD_LAZY);
35 if(handle == NULL)
36 handle= dlopen("libnotify.so.6", RTLD_LAZY);
37 if(handle == NULL)
38 handle= dlopen("libnotify.so.7", RTLD_LAZY);
39 if(handle == NULL)
40 handle= dlopen("libnotify.so.8", RTLD_LAZY);
42 if(handle == NULL)
44 printf("Failed to open libnotify.\n\n" );
46 notify_init_t init = (notify_init_t)dlsym(handle, "notify_init");
47 if ( init == NULL )
49 dlclose( handle );
50 return 1;
52 init("AppImage");
54 notify_notification_new_t nnn = (notify_notification_new_t)dlsym(handle, "notify_notification_new");
55 if ( nnn == NULL )
57 dlclose( handle );
58 return 1;
60 n = nnn(title, body, NULL, NULL);
61 notify_notification_set_timeout_t nnst = (notify_notification_set_timeout_t)dlsym(handle, "notify_notification_set_timeout");
62 if ( nnst == NULL )
64 dlclose( handle );
65 return 1;
67 nnst(n, timeout);
68 notify_notification_show_t show = (notify_notification_show_t)dlsym(handle, "notify_notification_show");
69 if ( init == NULL )
71 dlclose( handle );
72 return 1;
74 show(n, NULL );
75 dlclose(handle );
77 return 0;
81 int main( int argc, char *argv[] )
83 char *title;
84 char *body;
85 printf("%s\n", "Test");
86 title = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.";
87 body = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
88 notify(title, body, 3000); // 3 seconds timeout
89 return 0;