I currently use addLocalMonitorForEventsMatchingMask to monitor mouse events and I call it from main thread. It works great. However I now want to move it to off the main thread. Is there an alternative like something like creating a hidden window from a thread and doing NSRunLoop?
I read on the docs: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/MonitoringEvents/MonitoringEvents.html
The handlers are always called on the main thread. Both class methods return the monitor object, which the calling object does not own (and thus has no need to retain or release).
Is there any alternative that I can do from off main thread?
I can't setup callbacks on the mainthread from another thread. I'm using an FFI and its not capable of that right now.
Here is my code in case it helps, but I'm hoping for alternative to this for off main thread please:
myHandler_js = function(c_arg1__self, objc_arg1__aNSEventPtr) {
var cType = ostypes.API('objc_msgSend')(objc_arg1__aNSEventPtr, ostypes.HELPER.sel('type'));
cType = ctypes.cast(cType, ostypes.TYPE.NSEventType);
return objc_arg1__aNSEventPtr; // return null to block
};
myHandler_c = ostypes.TYPE.IMP_for_EventMonitorCallback.ptr(myHandler_js);
myBlock_c = ostypes.HELPER.createBlock(myHandler_c);
var rez_add = ostypes.API('objc_msgSend')(ostypes.HELPER.class('NSEvent'), ostypes.HELPER.sel('addLocalMonitorForEventsMatchingMask:handler:'), ostypes.TYPE.NSEventMask(ostypes.CONST.NSKeyDownMask), myBlock_c.address());
Solution is to use CGEventTap.
This one is having issues but Ill update it as soon as its ready:
GetCurrentProcess(psn);
var mask = 1 << kCGEventLeftMouseDown | // CGEventMaskBit(kCGEventLeftMouseDown)
1 << kCGEventLeftMouseUp |
1 << kCGEventRightMouseDown |
1 << kCGEventRightMouseUp |
1 << kCGEventOtherMouseDown |
1 << kCGEventOtherMouseUp |
1 << kCGEventScrollWheel;
mouseEventTap = CGEventTapCreateForPSN(&psn, kCGHeadInsertEventTap, kCGEventTapOptionDefault, mask, null);
if (!mouseEventTap.isNull()) {
aRLS = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, mouseEventTap, 0);
CFRelease(mouseEventTap);
if (!aRLS.isNull()) {
aLoop = CFRunLoopGetCurrent();
CFRunLoopAddSource(aLoop, aRLS, kCFRunLoopCommonModes);
CFRelease(aRLS);
CFRelease(aLoop);
rez = CFRunLoopRunInMode(ostypes.CONST.kCFRunLoopCommonModes, 10, false); // figure out how to make this run indefinitely
// rez is 1 :(
}
}
Related
Lets say you have a coroutine returning a task (using winrt to illustrate)
winrt::Windows::foundation::IAsyncOperation<int> my_coroutine(int i)
{
co_await winrt::resume_background();
Sleep(std::max(4 - i, 0));
co_return i;
}
and you want to in RxCpp like this:
range(0, 4)
| map(&my_coroutine)
| observe_on(observe_on_event_loop())
| subscribe<string>(println(cout))
;
// Output: 3 2 1 0
So the obvious problem is that we don't want map to pass an IAsyncOperation<int> to observe_on::on_next but rather we would have the coroutine call observe_on::on_next passing an int, when it returns its value.
Perhaps we could convert IAsyncOperation<int> to rxcpp::Observable<int> and use flat_map although it seams a bit wasteful to use a range-like monad when we only have a future monad.
Is there a easy way to do this with RxCpp? If it's rather complicated, I'm happy to know that so I can move on.
P.S. I'm actually co_awaiting winrt::resume_on_signal, so maybe RxCpp provides some similar functionality with event HANDLEs.
Edit: I suppose the obvious thing to do is just poll the futures on the event loop, pushing them (i.e. calling on_next) only when they have completed.
I have decided to give up on RxCpp, and am instead pursuing writing my pipeline with templates to take advantage of type deduction from lambda arguments (static polymorphism for refactoring rather than reuse).
I think the correct thing to do is just to not have the future at all
int my_function(int i)
{
Sleep(std::max(4 - i, 0));
return i;
}
and run this function on a thread pool
range(0, 4)
| observe_on(observe_on_thread_pool()) // doesn't actually exist
| map(&my_function)
| observe_on(observe_on_event_loop())
| subscribe<string>(println(cout))
;
// Output: 3 2 1 0
making sure that the observer it calls next on is thread safe.
And in rxcppv3 it shouldn't be too hard to write a custom operator in place of observe_on(observe_on_thread_pool()) | map:
const auto map_on_thread_pool = [](auto func){
return make_lifter([=](auto r){
return make_observer(r, [=](auto& r, auto v){
auto f = [=](){ r.next(func(v)); };
//On the windows thread pool
TrySubmitThreadpoolCallback(..); // what ever hacks are needed to run f
});
});
};
I have been reading this upvoted answer on Stack Overflow: https://stackoverflow.com/a/26129960/12311164
It says that replacing wait(delay, units); in SC_THREAD to next_trigger(delay, units) in SC_METHOD works.
But when I tried, it does not work. I am trying to build adder module with 2 ns output delay. Instead of having a 2 ns output delay, the adder output is getting updated every 2 ns.
Design:
#include "systemc.h"
#define WIDTH 4
SC_MODULE(adder) {
sc_in<sc_uint<WIDTH> > A, B;
sc_out<sc_uint<WIDTH> > OUT;
void add(){
sc_time t1 = sc_time_stamp();
int current_time = t1.value();
int intermediate = A.read() + B.read();
next_trigger(2, SC_NS);
OUT.write(intermediate);
cout << " SC_METHOD add triggered at "<<sc_time_stamp() <<endl;
}
SC_CTOR(adder){
SC_METHOD(add);
sensitive << A << B;
}
};
I know how to simulate delay using 2 techniques: sc_event and SC_METHOD and the wait statement in SC_THREAD, but I would like to simulate the delay using next_trigger(). I have read the Language Reference Manual, but could not figure how to do it.
Simulated on EDA Playground here: https://edaplayground.com/x/dFzc
I think I need to trigger 2 NS after the inputs change, how to do that?
You will have to track state manually:
sc_uint<WIDTH> intermediate;
void add(){
if (A->event() || B->event() || sc_delta_count() == 0) {
intermediate = A.read() + B.read();
next_trigger(2, SC_NS);
} else {
OUT->write(intermediate);
}
}
The problem is that using next_trigger doesn't magically transform your SC_METHOD into SC_THREAD. In general, I find any usage of next_trigger inconvenient and there are better ways of doing this using sc_event.
I have created a CGEventTap like this:
GetCurrentProcess(psn);
var mask = 1 << kCGEventLeftMouseDown | // CGEventMaskBit(kCGEventLeftMouseDown)
1 << kCGEventLeftMouseUp |
1 << kCGEventRightMouseDown |
1 << kCGEventRightMouseUp |
1 << kCGEventOtherMouseDown |
1 << kCGEventOtherMouseUp |
1 << kCGEventScrollWheel;
mouseEventTap = CGEventTapCreateForPSN(&psn, kCGHeadInsertEventTap, kCGEventTapOptionDefault, mask, null);
if (!mouseEventTap.isNull()) {
aRLS = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, mouseEventTap, 0);
CFRelease(mouseEventTap);
if (!aRLS.isNull()) {
aLoop = CFRunLoopGetCurrent();
CFRunLoopAddSource(aLoop, aRLS, kCFRunLoopCommonModes);
CFRelease(aRLS);
CFRelease(aLoop);
rez = CFRunLoopRunInMode(ostypes.CONST.kCFRunLoopCommonModes, 10, false); // figure out how to make this run indefinitely
// rez is 1 :(
}
}
My CFRunLoopRun is exiting immediately, instead of running for 10seconds. And it says code is 1 which means no sources are in that mode. But I clearly did a CFRunLoopAddSource to the common modes option kCFRunLoopRunFinished. The run loop mode mode has no sources or timers.. Anyone know whats up? This is on not-main thread.
You can't run a run loop in kCFRunLoopCommonModes. This is clearly stated in the documentation for CFRunLoopRunInMode().
kCFRunLoopCommonModes is a virtual mode. It's basically a set of other modes. It can only be used when adding (or removing) a source to a run loop to say "monitor this source when the run loop is run in any of the modes in the set". But when you run a run loop, you have to run it in a specific, real mode, not this virtual mode which represents a set of other modes.
I recommend that, when you're working on a private thread and only want to monitor private sources, that you add the source to a custom mode and run the run loop in that mode. A custom mode is just a string with a unique value. For example, something like "com.yourcompany.yourproject.yourmodespurpose". Using a custom mode makes sure that the run loop never does anything unexpected, like firing a source added by the frameworks.
You must not release aLoop. Functions that don't have "Create" or "Copy" in their name do not give you ownership.
You will need a loop around your call to CFRunLoopRunInMode() because it will return every time it handles an event from your source (kCFRunLoopRunHandledSource == 4) or hits the timeout (kCFRunLoopRunTimedOut == 3). You should break out of the loop if it ever returns anything else.
Hello i am using AM1808 ARM9 microprocessor.
I have an interfacing of GSM dongle.I want to make the GSM dongle connection as well all data transmission as well reception in the background using pthreads.
When i am trying to connect the dongle in background it is continuously blinking green light and i could not get connect to the server.
I can not find the problem.
Here is my code for the datacard initialisation as well as communication routine.
I am initialising the Thread in the Main thread.
int InitializeDataCard(void)
{
static int connect_ret = 0;
pthread_t processing_th;
pthread_create(&processing_th, NULL, Datacard_Connection_Thread, &db_mc_object);
pthread_detach(processing_th);
ShowMessageBox(msgbox_text[136], MB_TASKMODAL);
if(connect_ret)
{
ShowMessageBox(msgbox_text[163], MB_ICONERROR);
return -1;`enter code here`
}
else
{
return 0;
}
}
int ConnectToServer(void)
{
int connect_ret = 0;
Dprintf(__func__,"Trying to connect ....");
DPUUtilsLib_RetrieveParameter(PID_DATACARD_INFO,(UCHAR *)&datacard_info,sizeof(datacard_info));
connect_ret = DataCardConnect(datacard_info);
sleep(3);
//g_do_process = 0;
Dprintf(__func__,"DataCardConnect ret=%d",connect_ret);
return connect_ret;
}
void * Datacard_Connection_Thread(void *tempdata)
{
int ret=0,response = -1,enc_ret=0;
static int g_gsm_response = 0;
dpu_csv_file_param_t fileparam;
dpu_db_export_search_params_t tempparams;
dpu_db_milk_collection_t *livedata,*updatedata;
dpu_db_user_master_t CreatedBy,UpdatedBy;
dpu_db_society_master_t soc_info;
char filename[50]={0};
livedata = (dpu_db_milk_collection_t *)tempdata;
//Connect to the Network
create_connection :
ret = ConnectToServer();
//connected successfully
if(!ret)
{
//Get the SocietyCode from the Database
if(g_data_available)
{
g_data_available=0;
soc_info.SocietyId = g_society_list[g_selected_society].SocietyId;
DPUDBLib_search_society_master(&soc_info);
strncpy(livedata->SocietyCode,soc_info.SocietyCode,5);
Dprintf(__func__,"%04d\n %5.2f\n %5.2f\n %5.2f\n %5.2f\n %5.2f\n %5.2f\n %7.2f\n %7.2f\n %03d\n %c\n %d\n %5.2d\n %s\n %d",
livedata->MemberCode,livedata->Fat,livedata->LRCLR,livedata->SNF,livedata->Solid,
livedata->FatKG,livedata->SNFKG,livedata->Rate,livedata->Amount,
livedata->CanNumber,livedata->EntryMode,livedata->MC_RateChartId,livedata->Water,livedata->SocietyCode,__LINE__);
sprintf(tempparams.FileName,"%s/%s",DATA_TEMP,MT_MILKLIVEFILE);
memcpy(fileparam.FilePath,tempparams.FileName,sizeof(tempparams.FileName));
fileparam.Type = DPU_CSV_EXPORT;
fileparam.FileType = DPU_CSV_MILK_LIVE_DATA_FILE;
//open a csv file
DPUCSVLib_open_csv_file(&fileparam);
memset(&CreatedBy,0,sizeof(dpu_db_user_master_t));
memset(&UpdatedBy,0,sizeof(dpu_db_user_master_t));
strncpy(CreatedBy.Username,TempUser,5);
//write the live data into the file
DPUCSVLib_write_csv_file(&fileparam,livedata,&CreatedBy,&UpdatedBy);
//encrypt the file
enc_ret = DPUEncryptFile(tempparams.FileName,filename);
if(!enc_ret)
{
//send file request to server for the accepting the data
response = SendFileRequest(g_gsm_response,filename,9);
if(!response)
{
//receive the response of the server
response = GetResponceFromServer(g_gsm_response,&response,9);
if(response || g_gsm_response) response = -1;
else
{
//If record is successfully sent to the server then update the FlagGSM flag of the record as well as
//Update the database
g_update_record = 1;
livedata->FlagGSM = 1;
updatedata->MilkCollectionId = livedata->MilkCollectionId;
DPUDBLib_search_milk_collection_entry_by_record(&updatedata);
DPUDBLib_edit_milk_collection_entry(&updatedata,&livedata);
g_update_record = 0;
}
}
//remove the temp file generated
remove(filename);
}
}
}
else
{
//if connection is not successfully done then try to reconnect the server
ShowMessageBox(msgbox_text[156], MB_ICONERROR);
goto create_connection;
}
}
I think there is basic mistake here. By declaring the static variable connect_ret in InitializeDataCard, it does not mean in any way that it is going to be the same variable declared in ConnectToServer. Therefore, the first function will always have the same behaviour...
I think you'll need a global variable (i.e. not defined in a function) and possibily some kind of synchronization, because when you create the thread, then probably it won't be executed immediately, so even if you have a global variable, you can't check against it until you know that it has been correctly set.
I'm messing around with the PushSource sample filter shipped with the DirectShow SDK and I'm having the following problem:
When I call IMediaControl::Run(), it returns S_FALSE which means "the graph is preparing to run, but some filters have not completed the transition to a running state". MSDN suggests to then call IMediaControl::GetState() and wait for the transition to finish.
And so, I call IMediaControl::GetState(INFINITE, ...) which is supposed to solve the problem.
However, to the contrary, it returns VFW_S_STATE_INTERMEDIATE even though I've specified an infinite waiting time.
I've tried all three variations (Bitmap, Bitmap Set and Desktop) and they all behave the same way, which initially lead me to believe there is a bug in there somewhere.
However, then, I tried using IFilterGraph::AddSourceFilter to do the same and it did the same thing, which must mean it's my rendering code that is the problem:
CoInitialize(0);
IGraphBuilder *graph = 0;
assert(S_OK == CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&graph));
IBaseFilter *pushSource = 0;
graph->AddSourceFilter(L"sample.bmp", L"Source", &pushSource);
IPin *srcOut = 0;
assert(S_OK == GetPin(pushSource, PINDIR_OUTPUT, &srcOut));
graph->Render(srcOut);
IMediaControl *c = 0;
IMediaEvent *pEvent;
assert(S_OK == graph->QueryInterface(IID_IMediaControl, (void**)&c));
assert(S_OK == graph->QueryInterface(IID_IMediaEvent, (void**)&pEvent));
HRESULT hr = c->Run();
if(hr != S_OK)
{
if(hr == S_FALSE)
{
OAFilterState state;
hr = c->GetState(INFINITE, &state);
assert(hr == S_OK );
}
}
long code;
assert(S_OK == pEvent->WaitForCompletion(INFINITE, &code));
Anyone knows how to fix this?
IBaseFilter *pushSource = 0;
graph->AddSourceFilter(L"sample.bmp", L"Source", &pushSource);
AddSourceFilter adds a default source filter, I don't think it will add your pushsource samplefilter.
I would recommend to add the graph to the ROT, so you can inspect it with graphedit.
And what happens if you don't call GetState()?
hr = pMediaControl->Run();
if(FAILED(hr)) {
/// handle error
}
long evCode=0;
while (evCode == 0)
{
pEvent->WaitForCompletion(1000, &evCode);
/// other code
}
Open GraphEditPlus, add your filter, render its pin and press Run. Then you'll see states of each filter separately, so you'll see what filter didn't run and why.