Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dbus/dbus.h>
- #include <dbus/dbus-glib.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- bool init(DBusError *error, DBusConnection *connection) {
- error = malloc(sizeof(struct DBusError));
- //initialise the errors
- dbus_error_init(error);
- //connect to the bus
- connection = dbus_bus_get(DBUS_BUS_SESSION, error);
- if(dbus_error_is_set(error)){
- fprintf(stderr, "Connection Error (%s)\n", error->message);
- dbus_error_free(error);
- return 1;
- }
- if(connection == NULL)
- return 1;
- //request a name on the bus
- int result = dbus_bus_request_name(connection, "hackerman.notifier.app", DBUS_NAME_FLAG_REPLACE_EXISTING, error);
- if(dbus_error_is_set(error)) {
- fprintf(stderr, "Name Error (%s)\n", error->message);
- dbus_error_free(error);
- return 1;
- }
- if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != result)
- return 1;
- return 0;
- }
- void add_arg_basic(DBusMessageIter *args, int type, const void *data){
- int status_code = dbus_message_iter_append_basic(args, type, data);
- if(!status_code){
- fprintf(stderr, "Error while appending args\n");
- exit(1);
- }
- }
- void add_arg_fixed(DBusMessageIter *args, int type, const void *data, const int n_elements) {
- int status_code = dbus_message_iter_append_fixed_array(args, type, data, n_elements);
- if(!status_code){
- fprintf(stderr, "Error while appending args\n");
- //exit(1);
- }
- }
- bool send_message(DBusError *error, DBusConnection *connection) {
- DBusMessage *message;
- DBusMessageIter args;
- DBusPendingCall *pending;
- message = dbus_message_new_method_call("org.freedesktop.Notifications",
- "/org/freedesktop/Notifications/",
- "org.freedesktop.Notifications",
- "Notify");
- if(message == NULL) {
- fprintf(stderr, "Message Null\n");
- return 1;
- }
- //append arguments to iterator
- dbus_message_iter_init_append(message, &args);
- static const char *app_name = "App Name";
- static const char *app_icon = "App Icon";
- static const char *summary = "Summary";
- static const char *body = "Body";
- static const char* actions[10];
- static const char* action = "kkt";
- actions[0] = action;
- static const char *dictionary = "";
- static const unsigned short replaces_id = 0;
- static const unsigned short expire_timeout = 0;
- printf("%d", DBUS_TYPE_STRING);
- add_arg_basic(&args, DBUS_TYPE_STRING, &app_name);
- add_arg_basic(&args, DBUS_TYPE_UINT32, &replaces_id);
- add_arg_basic(&args, DBUS_TYPE_STRING, &app_icon);
- add_arg_basic(&args, DBUS_TYPE_STRING, &summary);
- add_arg_basic(&args, DBUS_TYPE_STRING, &body);
- add_arg_fixed(&args, DBUS_TYPE_STRING, &actions, 0);
- //add_arg_fixed(&args, DBUS_TYPE_DICT_ENTRY, &dictionary, 0);
- //add_arg_basic(&args, DBUS_TYPE_INT32, &expire_timeout);
- dbus_connection_send_with_reply(connection, message, &pending, -1);
- dbus_connection_flush(connection);
- return 0;
- }
- int main(int argc, char **argv) {
- DBusError* error;
- DBusConnection *connection;
- if(init(error, connection))
- return 1;
- if(send_message(error, connection))
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment