Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed some problems creating that would make the program crash

if the mouse was over the icon too fast
  • Loading branch information...
commit 13b8cd470f81a8493a6b9a588974ba092b47dd74 1 parent ef7b681
Gabriel Takeuchi authored
2  src-native/linux/Makefile
@@ -59,7 +59,7 @@ tray_linux_NativeLinuxTray.h: ../../src/main/java/tray/linux/NativeLinuxTray.jav
59 59 .c.o:
60 60 $(CC) -c $(CFLAGS) $(INCPATH) -m32 -o $@ $<
61 61
62   -nativeTrayTest:
  62 +nativeTrayTest: nativeTrayMainTest.c
63 63 gcc -o nativeTrayTest nativeTrayMainTest.c -g -lpthread -L$(TRAY_PATH) -llinuxtray_x64
64 64
65 65 clean:
4 src-native/linux/commonFunctions.c
@@ -10,7 +10,9 @@ int nextInstanceId = 0;
10 10 JavaVM *javaVM = NULL;
11 11
12 12 jobject getLinuxTrayIconAdapter(JNIEnv* env, int nativeId) {
13   -
  13 + if (gtkMainThreadJniEnv == NULL) {
  14 + return NULL;
  15 + }
14 16 jclass classRef = (*gtkMainThreadJniEnv)->FindClass(gtkMainThreadJniEnv, LINUX_NATIVE_TRAY_ADAPTER_CLASS);
15 17 jmethodID mid = (*env)->GetStaticMethodID(env, classRef, "getLinuxTrayIconAdapter", "(I)L" JAVA_TRAY_ICON_ADAPTER_CLASS ";");
16 18 if (mid == 0) {
38 src-native/linux/nativeCreateTrayIcon.c
@@ -43,7 +43,12 @@ int createTrayIconForIconAndWithTooltip(char * iconFileName, char * tooltipText)
43 43 waitUntilTheTrayIconIsCreatedWhenGtkMainIsIdle(trayIconData);
44 44
45 45 int newInstanceId = trayIconData->nativeId;
  46 +
  47 + pthread_cond_destroy(&trayIconData->condToWaitInit);
  48 + pthread_mutex_destroy(&trayIconData->lockToWaitInit);
46 49 free(trayIconData);
  50 +
  51 + printf(" Tray icon creation finished. **\n");
47 52 return newInstanceId;
48 53 }
49 54
@@ -51,10 +56,14 @@ int createTrayIconForIconAndWithTooltip(char * iconFileName, char * tooltipText)
51 56 gboolean createTrayIconWhenGtkMainIsIdle(TrayIconData * trayIconData)
52 57 {
53 58 GtkStatusIcon *trayIcon = gtk_status_icon_new_from_file (trayIconData->iconFileName);
  59 + gtk_status_icon_set_visible(trayIcon, FALSE);
  60 +
54 61 TrayIconInstance * trayIconInstance = &nativeInstance[trayIconData->nativeId];
55 62 trayIconInstance->trayIcon = trayIcon;
56 63
57   - gtk_status_icon_set_tooltip (trayIcon, trayIconData->tooltipText);
  64 + const char * tooltipText = strdup(trayIconData->tooltipText);
  65 +
  66 + gtk_status_icon_set_tooltip (trayIcon, tooltipText);
58 67
59 68 trayIconInstance->trayIconMenu = createTrayIconMenu(trayIcon);
60 69 gtk_widget_show_all (trayIconInstance->trayIconMenu);
@@ -63,19 +72,11 @@ gboolean createTrayIconWhenGtkMainIsIdle(TrayIconData * trayIconData)
63 72 *nativeIdParam = trayIconData->nativeId;
64 73 g_signal_connect(GTK_STATUS_ICON (trayIcon), "activate", GTK_SIGNAL_FUNC (trayIconActivatedHandler), nativeIdParam);
65 74
66   - gtk_status_icon_set_visible(trayIcon, TRUE);
67   -
68   -
69   - printf("Will lock before sending signal\n");
70 75 pthread_mutex_lock(&trayIconData->lockToWaitInit);
71   -
72   -
73   - printf("Will send signal\n");
74 76 pthread_cond_signal(&trayIconData->condToWaitInit);
75   -
76   - printf("Signal sent, releasing lock\n");
77 77 pthread_mutex_unlock(&trayIconData->lockToWaitInit);
78   - printf("Lock released\n");
  78 +
  79 + gtk_status_icon_set_visible(trayIcon, TRUE);
79 80
80 81 return 0;
81 82 }
@@ -100,23 +101,30 @@ void initializeMutexToWaitUntilActualTrayIconCreation(TrayIconData * trayIconDat
100 101
101 102 void waitUntilTheTrayIconIsCreatedWhenGtkMainIsIdle(TrayIconData * trayIconData)
102 103 {
103   - printf(" -- will wait condition -- \n");
104 104 // wait until the tray icon is created
105 105 pthread_cond_wait(&trayIconData->condToWaitInit, &trayIconData->lockToWaitInit);
106 106 pthread_mutex_unlock(&trayIconData->lockToWaitInit);
107   -
108   - printf(" -- condition met -- \n");
  107 + sleep(1);
109 108 }
110 109
111 110 void trayIconActivatedHandler(GObject *trayIcon, gpointer nativeIdPointer)
112 111 {
113 112 (void) trayIcon; // avoid warning
  113 + if (gtkMainThreadJniEnv == NULL) {
  114 + print_debugger("gtkMainThreadJniEnv is NULL. This situation should be impossible.");
  115 + return;
  116 + }
114 117 jobject trayAdapter = getLinuxTrayIconAdapter(gtkMainThreadJniEnv, *((int*)nativeIdPointer));
  118 + if (trayAdapter == NULL){
  119 + print_debugger("Tray adapter is NULL.");
  120 + return;
  121 + }
115 122 jclass classRef = (*gtkMainThreadJniEnv)->GetObjectClass(gtkMainThreadJniEnv, trayAdapter);
116 123
117 124 jmethodID mid = (*gtkMainThreadJniEnv)->GetMethodID(gtkMainThreadJniEnv, classRef, ACTION_CALLBACK_METHOD_NAME, "()V");
118 125
119 126 if (mid == 0) {
  127 + print_debugger(ACTION_CALLBACK_METHOD_NAME " was not found.");
120 128 return;
121 129 }
122 130
@@ -147,9 +155,7 @@ int getNextInstanceId() {
147 155 GtkWidget *createTrayIconMenu(GtkStatusIcon *trayIcon)
148 156 {
149 157 GtkWidget *trayIconMenu = gtk_menu_new();
150   -
151 158 g_signal_connect(GTK_STATUS_ICON (trayIcon), "popup-menu", GTK_SIGNAL_FUNC (trayIconPopupHandler), trayIconMenu);
152   -
153 159 return trayIconMenu;
154 160 }
155 161
8 src-native/linux/nativeInit.c
... ... @@ -1,10 +1,12 @@
1 1 #include <stdlib.h>
  2 +#include <pthread.h>
2 3 #include "commonFunctions.h"
3 4
4 5 void * gtkMainThread(void * nothingUseful);
5 6 JNIEXPORT void JNICALL Java_tray_linux_NativeLinuxTray_nativeInit0
6 7 (JNIEnv *env, jobject invokingObject)
7 8 {
  9 + (void)invokingObject;//avoid warning
8 10 (*env)->GetJavaVM(env, &javaVM);
9 11
10 12 gtk_init_with_empty_args();
@@ -15,9 +17,11 @@ JNIEXPORT void JNICALL Java_tray_linux_NativeLinuxTray_nativeInit0
15 17
16 18 void * gtkMainThread(void * nothingUseful)
17 19 {
  20 + (void)nothingUseful;
18 21 (*javaVM)->AttachCurrentThread(javaVM, (void**) &gtkMainThreadJniEnv, NULL );
19 22 gtk_main();
20 23 (*javaVM)->DetachCurrentThread(javaVM);
  24 + return NULL;
21 25 }
22 26
23 27 void gtk_init_with_empty_args()
@@ -25,5 +29,7 @@ void gtk_init_with_empty_args()
25 29 int argc = 0;
26 30 char ** argv = malloc(sizeof(char*));
27 31 argv[0] = "";
28   - gtk_init (&argc, &argv);
  32 + if (gtk_init_check (&argc, &argv) == FALSE) {
  33 + fprintf(stderr, "Failed to start GTK\n");
  34 + }
29 35 }
1  src-native/linux/nativeTrayMainTest.c
@@ -13,6 +13,7 @@ int main()
13 13 pthread_create(&mainThreadId, NULL, mainThread , NULL);
14 14
15 15 createTrayIconForIconAndWithTooltip("/home/takeuchi/projects/trayicon-tests/skype2gmail.svg", "some tip");
  16 + createTrayIconForIconAndWithTooltip("/home/takeuchi/projects/trayicon-tests/skype2gmail.svg", "some tip");
16 17
17 18 pthread_join(mainThreadId, NULL);
18 19 }
BIN  src/main/resources/liblinuxtray_x64.so
Binary file not shown
BIN  src/main/resources/liblinuxtray_x86.so
Binary file not shown

0 comments on commit 13b8cd4

Please sign in to comment.
Something went wrong with that request. Please try again.