Skip to content

Commit

Permalink
style(clang): fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Aug 31, 2024
1 parent a02f5fc commit 243638c
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 148 deletions.
114 changes: 60 additions & 54 deletions src/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,108 @@
#include <stdio.h>
#include <string.h>

#if defined (_WIN32) || defined (_WIN64)
#define TRAY_WINAPI 1 ///< Use WinAPI.
#elif defined (__linux__) || defined (linux) || defined (__linux)
#define TRAY_APPINDICATOR 1
#elif defined (__APPLE__) || defined (__MACH__)
#define TRAY_APPKIT 1
#if defined(_WIN32) || defined(_WIN64)
#define TRAY_WINAPI 1 ///< Use WinAPI.
#elif defined(__linux__) || defined(linux) || defined(__linux)
#define TRAY_APPINDICATOR 1
#elif defined(__APPLE__) || defined(__MACH__)
#define TRAY_APPKIT 1
#endif

#include "tray.h"

#if TRAY_APPINDICATOR
#define TRAY_ICON1 "mail-message-new"
#define TRAY_ICON2 "mail-message-new"
#define TRAY_ICON1 "mail-message-new"
#define TRAY_ICON2 "mail-message-new"
#elif TRAY_APPKIT
#define TRAY_ICON1 "icon.png"
#define TRAY_ICON2 "icon.png"
#define TRAY_ICON1 "icon.png"
#define TRAY_ICON2 "icon.png"
#elif TRAY_WINAPI
#define TRAY_ICON1 "icon.ico" ///< Path to first icon.
#define TRAY_ICON2 "icon.ico" ///< Path to second icon.
#define TRAY_ICON1 "icon.ico" ///< Path to first icon.
#define TRAY_ICON2 "icon.ico" ///< Path to second icon.
#endif

static struct tray tray;

static void toggle_cb(struct tray_menu *item) {
static void
toggle_cb(struct tray_menu *item) {
printf("toggle cb\n");
item->checked = !item->checked;
tray_update(&tray);
}

static void hello_cb(struct tray_menu *item) {
(void)item;
static void
hello_cb(struct tray_menu *item) {
(void) item;
printf("hello cb\n");
if (strcmp(tray.icon, TRAY_ICON1) == 0) {
tray.icon = TRAY_ICON2;
} else {
}
else {
tray.icon = TRAY_ICON1;
}
tray_update(&tray);
}

static void quit_cb(struct tray_menu *item) {
(void)item;
static void
quit_cb(struct tray_menu *item) {
(void) item;
printf("quit cb\n");
tray_exit();
}

static void submenu_cb(struct tray_menu *item) {
(void)item;
static void
submenu_cb(struct tray_menu *item) {
(void) item;
printf("submenu: clicked on %s\n", item->text);
tray_update(&tray);
}

// Test tray init
static struct tray tray = {
.icon = TRAY_ICON1,
.icon = TRAY_ICON1,
#if TRAY_WINAPI
.tooltip = "Tray",
.tooltip = "Tray",
#endif
.menu =
(struct tray_menu[]) {
{.text = "Hello", .cb = hello_cb},
{.text = "Checked", .checked = 1, .checkbox = 1, .cb = toggle_cb},
{.text = "Disabled", .disabled = 1},
{.text = "-"},
{.text = "SubMenu",
.submenu =
(struct tray_menu[]) {
{.text = "FIRST", .checked = 1, .checkbox = 1, .cb = submenu_cb},
{.text = "SECOND",
.submenu =
(struct tray_menu[]) {
{.text = "THIRD",
.submenu =
(struct tray_menu[]) {
{.text = "7", .cb = submenu_cb},
{.text = "-"},
{.text = "8", .cb = submenu_cb},
{.text = NULL}}},
{.text = "FOUR",
.submenu =
(struct tray_menu[]) {
{.text = "5", .cb = submenu_cb},
{.text = "6", .cb = submenu_cb},
{.text = NULL}}},
{.text = NULL}}},
{.text = NULL}}},
{.text = "-"},
{.text = "Quit", .cb = quit_cb},
{.text = NULL}},
.menu =
(struct tray_menu[]) {
{ .text = "Hello", .cb = hello_cb },
{ .text = "Checked", .checked = 1, .checkbox = 1, .cb = toggle_cb },
{ .text = "Disabled", .disabled = 1 },
{ .text = "-" },
{ .text = "SubMenu",
.submenu =
(struct tray_menu[]) {
{ .text = "FIRST", .checked = 1, .checkbox = 1, .cb = submenu_cb },
{ .text = "SECOND",
.submenu =
(struct tray_menu[]) {
{ .text = "THIRD",
.submenu =
(struct tray_menu[]) {
{ .text = "7", .cb = submenu_cb },
{ .text = "-" },
{ .text = "8", .cb = submenu_cb },
{ .text = NULL } } },
{ .text = "FOUR",
.submenu =
(struct tray_menu[]) {
{ .text = "5", .cb = submenu_cb },
{ .text = "6", .cb = submenu_cb },
{ .text = NULL } } },
{ .text = NULL } } },
{ .text = NULL } } },
{ .text = "-" },
{ .text = "Quit", .cb = quit_cb },
{ .text = NULL } },
};

/**
* @brief Main entry point.
* @return 0 on success, 1 on error.
*/
int main() {
int
main() {
if (tray_init(&tray) < 0) {
printf("failed to create tray\n");
return 1;
Expand Down
76 changes: 42 additions & 34 deletions src/tray_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
#include "tray.h"

// system includes
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

// lib includes
#ifdef TRAY_AYATANA_APPINDICATOR
#include <libayatana-appindicator/app-indicator.h>
#include <libayatana-appindicator/app-indicator.h>
#elif TRAY_LEGACY_APPINDICATOR
#include <libappindicator/app-indicator.h>
#include <libappindicator/app-indicator.h>
#endif
#ifndef IS_APP_INDICATOR
#define IS_APP_INDICATOR APP_IS_INDICATOR ///< Define IS_APP_INDICATOR for app-indicator compatibility.
#define IS_APP_INDICATOR APP_IS_INDICATOR ///< Define IS_APP_INDICATOR for app-indicator compatibility.
#endif
#include <libnotify/notify.h>
#define TRAY_APPINDICATOR_ID "tray-id" ///< Tray appindicator ID.
Expand All @@ -27,36 +27,38 @@ static pthread_cond_t async_update_cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t async_update_mutex = PTHREAD_MUTEX_INITIALIZER;

static AppIndicator *indicator = NULL;
static int loop_result = 0;
static int loop_result = 0;
static NotifyNotification *currentNotification = NULL;
static void _tray_menu_cb(GtkMenuItem *item, gpointer data) {
(void)item;
struct tray_menu *m = (struct tray_menu *)data;
static void
_tray_menu_cb(GtkMenuItem *item, gpointer data) {
(void) item;
struct tray_menu *m = (struct tray_menu *) data;
m->cb(m);
}

static GtkMenuShell *_tray_menu(struct tray_menu *m) {
GtkMenuShell *menu = (GtkMenuShell *)gtk_menu_new();
for(; m != NULL && m->text != NULL; m++) {
static GtkMenuShell *
_tray_menu(struct tray_menu *m) {
GtkMenuShell *menu = (GtkMenuShell *) gtk_menu_new();
for (; m != NULL && m->text != NULL; m++) {
GtkWidget *item;
if(strcmp(m->text, "-") == 0) {
if (strcmp(m->text, "-") == 0) {
item = gtk_separator_menu_item_new();
}
else {
if(m->submenu != NULL) {
if (m->submenu != NULL) {
item = gtk_menu_item_new_with_label(m->text);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
GTK_WIDGET(_tray_menu(m->submenu)));
}
else if(m->checkbox) {
else if (m->checkbox) {
item = gtk_check_menu_item_new_with_label(m->text);
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), !!m->checked);
}
else {
item = gtk_menu_item_new_with_label(m->text);
}
gtk_widget_set_sensitive(item, !m->disabled);
if(m->cb != NULL) {
if (m->cb != NULL) {
g_signal_connect(item, "activate", G_CALLBACK(_tray_menu_cb), m);
}
}
Expand All @@ -66,43 +68,46 @@ static GtkMenuShell *_tray_menu(struct tray_menu *m) {
return menu;
}

int tray_init(struct tray *tray) {
if(gtk_init_check(0, NULL) == FALSE) {
int
tray_init(struct tray *tray) {
if (gtk_init_check(0, NULL) == FALSE) {
return -1;
}
notify_init("tray-icon");
indicator = app_indicator_new(TRAY_APPINDICATOR_ID, tray->icon,
APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
if(indicator == NULL || !IS_APP_INDICATOR(indicator))return -1;
if (indicator == NULL || !IS_APP_INDICATOR(indicator)) return -1;
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
tray_update(tray);
return 0;
}

int tray_loop(int blocking) {
int
tray_loop(int blocking) {
gtk_main_iteration_do(blocking);
return loop_result;
}

static gboolean tray_update_internal(gpointer user_data) {
static gboolean
tray_update_internal(gpointer user_data) {
struct tray *tray = user_data;

if(indicator != NULL && IS_APP_INDICATOR(indicator)){
if (indicator != NULL && IS_APP_INDICATOR(indicator)) {
app_indicator_set_icon_full(indicator, tray->icon, tray->icon);
// GTK is all about reference counting, so previous menu should be destroyed
// here
app_indicator_set_menu(indicator, GTK_MENU(_tray_menu(tray->menu)));
}
if(tray->notification_text != 0 && strlen(tray->notification_text) > 0 && notify_is_initted()) {
if(currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)){
notify_notification_close(currentNotification,NULL);
if (tray->notification_text != 0 && strlen(tray->notification_text) > 0 && notify_is_initted()) {
if (currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)) {
notify_notification_close(currentNotification, NULL);
g_object_unref(G_OBJECT(currentNotification));
}
const char *notification_icon = tray->notification_icon != NULL ? tray->notification_icon : tray->icon;
currentNotification = notify_notification_new(tray->notification_title, tray->notification_text, notification_icon);
if(currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)){
if(tray->notification_cb != NULL){
notify_notification_add_action(currentNotification,"default","Default",tray->notification_cb,NULL,NULL);
if (currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)) {
if (tray->notification_cb != NULL) {
notify_notification_add_action(currentNotification, "default", "Default", tray->notification_cb, NULL, NULL);
}
notify_notification_show(currentNotification, NULL);
}
Expand All @@ -116,7 +121,8 @@ static gboolean tray_update_internal(gpointer user_data) {
return G_SOURCE_REMOVE;
}

void tray_update(struct tray *tray) {
void
tray_update(struct tray *tray) {
// Perform the tray update on the tray loop thread, but block
// in this thread to ensure none of the strings stored in the
// tray icon struct go out of scope before the callback runs.
Expand Down Expand Up @@ -147,16 +153,18 @@ void tray_update(struct tray *tray) {
}
}

static gboolean tray_exit_internal(gpointer user_data) {
if(currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)){
int v = notify_notification_close(currentNotification,NULL);
if(v == TRUE)g_object_unref(G_OBJECT(currentNotification));
static gboolean
tray_exit_internal(gpointer user_data) {
if (currentNotification != NULL && NOTIFY_IS_NOTIFICATION(currentNotification)) {
int v = notify_notification_close(currentNotification, NULL);
if (v == TRUE) g_object_unref(G_OBJECT(currentNotification));
}
notify_uninit();
return G_SOURCE_REMOVE;
}

void tray_exit(void) {
void
tray_exit(void) {
// Wait for any pending callbacks to complete
pthread_mutex_lock(&async_update_mutex);
while (async_update_pending) {
Expand Down
Loading

0 comments on commit 243638c

Please sign in to comment.