7 #include <Application.h>
8 #include <StorageDefs.h>
9 #include <MessageFilter.h>
10 #include <MessageRunner.h>
14 extern char **original_argv
;
15 extern char **argv_save
;
16 extern char **__libc_argv
;
17 extern int original_argc
;
18 extern int __libc_argc
;
23 char *gMatchViewNames
[MAX_VIEWS
];
24 int gNumViewNames
= 0;
26 char gExePath
[B_PATH_NAME_LENGTH
];
27 image_id gExeImg
= -1;
28 int (*gExeMainProc
)(int argc
, char **argv
);
30 thread_id gWaitForBAppThID
= -1;
33 //extern "C" EnqueueMessage__Q28BPrivate13BLooperTargetP8BMessage(BLooper *_this, BMessage *message);
35 //EnqueueMessage__Q28BPrivate13BLooperTargetP8BMessage(BLooper *_this, BMessage *message);
37 BMessageFilter
*gAppFilter
;
38 bool quitting
= false;
40 filter_result
bapp_filter(BMessage
*message
,
42 BMessageFilter
*filter
)
44 if (message
->what
== 'plop') /* our doesn't count */
45 return B_DISPATCH_MESSAGE
;
46 acquire_sem(gStdoutLock
);
47 fprintf(stdout
, "\033[31mMessage for BApplication:\033[0m\n");
48 message
->PrintToStream();
49 release_sem(gStdoutLock
);
50 return B_DISPATCH_MESSAGE
;
53 filter_result
bwin_filter(BMessage
*message
,
55 BMessageFilter
*filter
)
58 BHandler
*hand
= NULL
;
61 win
= dynamic_cast<BWindow
*>(filter
->Looper());
62 acquire_sem(gStdoutLock
);
64 "\033[31mMessage for View \"%s\" of Window \"%s\":\033[0m\n",
65 hand
?hand
->Name():NULL
,
66 win
?win
->Title():NULL
);
67 message
->PrintToStream();
68 release_sem(gStdoutLock
);
69 return B_DISPATCH_MESSAGE
;
72 class MyHandler
: public BHandler
{
76 void MessageReceived(BMessage
*msg
);
81 MyHandler::MyHandler()
82 :BHandler("spying handler")
84 fWindowList
.MakeEmpty();
87 MyHandler::~MyHandler()
91 void MyHandler::MessageReceived(BMessage
*msg
)
94 BMessageFilter
*afilter
;
97 i
= be_app
->CountWindows();
99 BWindow
*win
= be_app
->WindowAt(i
-1);
100 if (win
&& !fWindowList
.HasItem(win
)) {
101 fWindowList
.AddItem(win
);
102 afilter
= new BMessageFilter(B_ANY_DELIVERY
,
106 win
->AddCommonFilter(afilter
);
112 BHandler::MessageReceived(msg
);
115 int32
wait_for_loopers(void *arg
)
118 /* wait for BApplication */
121 gAppFilter
= new BMessageFilter(B_ANY_DELIVERY
, B_ANY_SOURCE
, bapp_filter
);
124 be_app
->AddCommonFilter(gAppFilter
);
125 be_app
->AddHandler(myh
);
127 new BMessageRunner(BMessenger(myh
), new BMessage('plop'), 100000);
132 static int usage(char *argv0
)
135 printf("%s app [args...]\n", argv0
);
139 int main(int argc
, char **argv
)
145 return usage(argv
[0]);
146 trapp_name
= argv
[0];
148 for (i
= 1; i
< argc
; i
++) {
149 if (strncmp(argv
[i
], "-", 1))
151 else if (!strcmp(argv
[i
], "-view")) {
152 if (gNumViewNames
>= MAX_VIEWS
) {
153 printf("too many view names\n");
158 printf("missing arg to -view\n");
161 gMatchViewNames
[gNumViewNames
] = argv
[i
];
163 } else if (!strcmp(argv
[i
], "-firstw")) {
166 printf("missing arg to -firstw\n");
169 gStartWin
= atoi(argv
[i
]);
171 return usage(argv
[0]);
175 return usage(argv
[0]);
179 for (i
= 0; i
< argc
; i
++)
180 printf("argv[%d] = %s\n", i
, argv
[i
]);
182 if (strncmp(argv
[0], "/", 1)) {
183 getcwd(gExePath
, B_PATH_NAME_LENGTH
-10);
184 strcat(gExePath
, "/");
186 strncat(gExePath
, argv
[0], B_PATH_NAME_LENGTH
-1-strlen(gExePath
));
187 printf("cmd = %s\n", gExePath
);
188 gExeImg
= load_add_on(gExePath
);
189 if (gExeImg
< B_OK
) {
190 fprintf(stderr
, "load_add_on: %s\n", strerror(gExeImg
));
194 // original are static...
195 //printf("original: %d; %s\n", original_argc, *original_argv);
196 fprintf(stderr
, "libc: %d; %s\n", __libc_argc
, *__libc_argv
);
197 fprintf(stderr
, "save: %s\n", *argv_save
);
199 //argv[0] = trapp_name;
204 gStdoutLock
= create_sem(1, "spybmsg_stdout_lock");
206 err
= get_image_symbol(gExeImg
, "main", B_SYMBOL_TYPE_TEXT
, (void **)&gExeMainProc
);
208 fprintf(stderr
, "get_image_symbol(main): %s\n", strerror(gExeImg
));
211 printf("main @ %p\n", gExeMainProc
);
213 resume_thread(spawn_thread(wait_for_loopers
,
214 "waiting for BLoopers",
215 B_NORMAL_PRIORITY
, NULL
));
217 i
= gExeMainProc(argc
, argv
);