Page 1 sur 1

Writing a Daemon/Task

Message non luPosté: 13 Mai 2014, 17:51
de jsrk
Hello everyboby,
I am new to this community and I apologize in advance for not posting this french but unfortunately I don't speak the language :(.

So I would like to write a daemon or task that's active in the background and waits for certain key combinations to perform certain actions. The usage of the calculator should continue to work unimpaired. I was wondering if something like:

Code: Tout sélectionner
while(true)
{
      if(isKeyPressed(KEY_NSPIRE_R) && isKeyPressed(KEY_NSPIRE_E))
      {
            resetHandheld();
            break;
      }
      sleep(500);
}


is possible at all. If it is not, is there anyway to register a program to be launched on certain hotkey combos/shortcuts?

Re: Writing a Daemon/Task

Message non luPosté: 13 Mai 2014, 17:57
de Excale
jsrk a écrit:Hello everyboby,
I am new to this community and I apologize in advance for not posting this french but unfortunately I don't speak the language :(.

English is not an issue (it is in fact way better to write in English than to attempt to use an automatic translator). :)

jsrk a écrit:So I would like to write a daemon or task that's active in the background and waits for certain key combinations to perform certain actions. The usage of the calculator should continue to work unimpaired. I was wondering if something like:

Code: Tout sélectionner
while(true)
{
      if(isKeyPressed(KEY_NSPIRE_R) && isKeyPressed(KEY_NSPIRE_E))
      {
            resetHandheld();
            break;
      }
      sleep(500);
}


is possible at all. If it is not, is there anyway to register a program to be launched on certain hotkey combos/shortcuts?

There are some ways to make a daemon, but this will clearly not work that way. A probably not too-hard way to do this would be to hook your program in the event loop or timer interrupt handler. See the ndless-hook "documentation" (search for HOOK_INSTALL in the source code).

Re: Writing a Daemon/Task

Message non luPosté: 13 Mai 2014, 18:01
de Lepzulnag
I've never done that by myself, but Levak's nCapture installs a hook which waits the user to press CTRL+'.'
Looking at his code may be helpful.

Re: Writing a Daemon/Task

Message non luPosté: 13 Mai 2014, 18:15
de jsrk
Thank you both for your help, I really appreciate it.

Re: Writing a Daemon/Task

Message non luPosté: 13 Mai 2014, 18:27
de Levak
The most elegant way is to use Nucleus tasks/timers (which is on what the Nspire is based on) which are made for that.
Obviously, those syscalls have to be found by yourself which is not an easy solution. I have the addresses for CX CAS 3.1 but not for others such as 3.6.
I was wondering Ndless integrating those syscalls but it appears it only includes file management one (or hackspire is not up to date).

Below is an old test I found on my HDD :
Code: Tout sélectionner
#include <os.h>
#include <libndls.h>

typedef void (*Timer_Callback)(void);

static unsigned TMS_Reset_Timer_addrs[] = {0x0, 0x0, 0x0, 0x10277740};
#define TMS_Reset_Timer SYSCALL_CUSTOM(TMS_Reset_Timer_addrs, void, void * /* timer */, Timer_Callback, int /* initial_time */, int /* reschedule_time */, int /* 4 = disable, 5 = enable */)
                                                                                                                                                                                                     
static unsigned TMS_Control_Timer_addrs[] = {0x0, 0x0, 0x0, 0x102776AC};
#define TMS_Control_Timer SYSCALL_CUSTOM(TMS_Control_Timer_addrs, void, void * /* timer */, int /* 4 = disable, 5 = enable */)
static unsigned TMS_Create_Timer_addrs[] = {0x0, 0x0, 0x0, 0x10277830};
#define TMS_Create_Timer SYSCALL_CUSTOM(TMS_Create_Timer_addrs, void, void * /* timer */, char * /* name */, Timer_Callback, int /* id */, int /* initial_timer */, int /* reschedule_time */, int /* 4 = disable, 5 = enable */)
static unsigned TMS_Delete_Timer_addrs[] = {0x0, 0x0, 0x0, 0x102777BC};
#define TMS_Delete_Timer SYSCALL_CUSTOM(TMS_Delete_Timer_addrs, void, void * /* timer */)

static unsigned NU_printf_addrs[] = {0x0, 0x0, 0x0, 0x10101FF8};
#define NU_printf SYSCALL_CUSTOM(NU_printf_addrs, int, char * /* format */, ...)

static int mtimer[0x64];

void timer_handler()
{
    NU_printf("Hello World\n");
    TMS_Reset_Timer(&mtimer, timer_handler, 100, 0, 5);
//    TMS_Control_Timer(&mtimer, 5);
//    TMS_Delete_Timer(&mtimer);
}

int main(int argc, char* argv[]) {
    if(argc < 1) return 0;

    TMS_Create_Timer(&mtimer, "1s", timer_handler, 0, 100, 0, 5);
    nl_set_resident();
    return;
}