BiggieJozin

Untitled

Jan 26th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include <dbus/dbus.h>
  2. #include <dbus/dbus-glib.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. bool init(DBusError *error, DBusConnection *connection) {
  8. error = malloc(sizeof(struct DBusError));
  9. //initialise the errors
  10. dbus_error_init(error);
  11.  
  12. //connect to the bus
  13. connection = dbus_bus_get(DBUS_BUS_SESSION, error);
  14. if(dbus_error_is_set(error)){
  15. fprintf(stderr, "Connection Error (%s)\n", error->message);
  16. dbus_error_free(error);
  17. return 1;
  18. }
  19.  
  20. if(connection == NULL)
  21. return 1;
  22.  
  23. //request a name on the bus
  24. int result = dbus_bus_request_name(connection, "hackerman.notifier.app", DBUS_NAME_FLAG_REPLACE_EXISTING, error);
  25.  
  26. if(dbus_error_is_set(error)) {
  27. fprintf(stderr, "Name Error (%s)\n", error->message);
  28. dbus_error_free(error);
  29. return 1;
  30. }
  31.  
  32. if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != result)
  33. return 1;
  34.  
  35. return 0;
  36. }
  37.  
  38. void add_arg_basic(DBusMessageIter *args, int type, const void *data){
  39. int status_code = dbus_message_iter_append_basic(args, type, data);
  40. if(!status_code){
  41. fprintf(stderr, "Error while appending args\n");
  42. exit(1);
  43. }
  44. }
  45.  
  46. void add_arg_fixed(DBusMessageIter *args, int type, const void *data, const int n_elements) {
  47. int status_code = dbus_message_iter_append_fixed_array(args, type, data, n_elements);
  48. if(!status_code){
  49. fprintf(stderr, "Error while appending args\n");
  50. //exit(1);
  51. }
  52. }
  53.  
  54. bool send_message(DBusError *error, DBusConnection *connection) {
  55. DBusMessage *message;
  56. DBusMessageIter args;
  57. DBusPendingCall *pending;
  58.  
  59. message = dbus_message_new_method_call("org.freedesktop.Notifications",
  60. "/org/freedesktop/Notifications/",
  61. "org.freedesktop.Notifications",
  62. "Notify");
  63.  
  64. if(message == NULL) {
  65. fprintf(stderr, "Message Null\n");
  66. return 1;
  67. }
  68.  
  69. //append arguments to iterator
  70.  
  71. dbus_message_iter_init_append(message, &args);
  72.  
  73. static const char *app_name = "App Name";
  74. static const char *app_icon = "App Icon";
  75. static const char *summary = "Summary";
  76. static const char *body = "Body";
  77. static const char* actions[10];
  78. static const char* action = "kkt";
  79. actions[0] = action;
  80.  
  81. static const char *dictionary = "";
  82. static const unsigned short replaces_id = 0;
  83. static const unsigned short expire_timeout = 0;
  84.  
  85. printf("%d", DBUS_TYPE_STRING);
  86. add_arg_basic(&args, DBUS_TYPE_STRING, &app_name);
  87. add_arg_basic(&args, DBUS_TYPE_UINT32, &replaces_id);
  88. add_arg_basic(&args, DBUS_TYPE_STRING, &app_icon);
  89. add_arg_basic(&args, DBUS_TYPE_STRING, &summary);
  90. add_arg_basic(&args, DBUS_TYPE_STRING, &body);
  91. add_arg_fixed(&args, DBUS_TYPE_STRING, &actions, 0);
  92. //add_arg_fixed(&args, DBUS_TYPE_DICT_ENTRY, &dictionary, 0);
  93. //add_arg_basic(&args, DBUS_TYPE_INT32, &expire_timeout);
  94.  
  95. dbus_connection_send_with_reply(connection, message, &pending, -1);
  96.  
  97. dbus_connection_flush(connection);
  98. return 0;
  99. }
  100.  
  101.  
  102. int main(int argc, char **argv) {
  103. DBusError* error;
  104. DBusConnection *connection;
  105.  
  106. if(init(error, connection))
  107. return 1;
  108.  
  109. if(send_message(error, connection))
  110. return 1;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment