static void sn_activate(char *name, char *exec_str) { SnLauncherContext *context; pid_t child_pid = 0; context = sn_launcher_context_new (sn_dpy, screen); sn_launcher_context_set_name (context, name); sn_launcher_context_set_binary_name (context, exec_str); sn_launcher_context_initiate (context, "monoluanch launch", exec_str, CurrentTime); switch ((child_pid = fork ())) { case -1: fprintf (stderr, "Fork failed\n" ); break; case 0: sn_launcher_context_setup_child_process (context); mb_exec(exec_str); fprintf (stderr, "Failed to exec %s \n", exec_str); _exit (1); break; } }
void launcher_action(LauncherIcon *icon, XEvent* evt) { char *cmd = calloc(strlen(icon->cmd) + 10, 1); sprintf(cmd, "(%s&)", icon->cmd); #if HAVE_SN SnLauncherContext* ctx; Time time; if (startup_notifications) { ctx = sn_launcher_context_new(server.sn_dsp, server.screen); sn_launcher_context_set_name(ctx, icon->icon_tooltip); sn_launcher_context_set_description(ctx, "Application launched from tint2"); sn_launcher_context_set_binary_name (ctx, icon->cmd); // Get a timestamp from the X event if (evt->type == ButtonPress || evt->type == ButtonRelease) { time = evt->xbutton.time; } else { fprintf(stderr, "Unknown X event: %d\n", evt->type); free(cmd); return; } sn_launcher_context_initiate(ctx, "tint2", icon->cmd, time); } #endif /* HAVE_SN */ pid_t pid; pid = fork(); if (pid < 0) { fprintf(stderr, "Could not fork\n"); } else if (pid == 0) { // Child process #if HAVE_SN if (startup_notifications) { sn_launcher_context_setup_child_process(ctx); } #endif // HAVE_SN // Allow children to exist after parent destruction setsid(); // Run the command execl("/bin/sh", "/bin/sh", "-c", icon->cmd, NULL); fprintf(stderr, "Failed to execlp %s\n", icon->cmd); #if HAVE_SN if (startup_notifications) { sn_launcher_context_unref(ctx); } #endif // HAVE_SN exit(1); } else { // Parent process #if HAVE_SN if (startup_notifications) { g_tree_insert(server.pids, GINT_TO_POINTER (pid), ctx); } #endif // HAVE_SN } free(cmd); }
void XDesktopContainer::runCommand(const string & command) { pid_t pid; //fork and execute program if (pid=fork() != 0) { //Primer proceso hijo #ifdef HAVE_STARTUP_NOTIFICATION if (sn_context != NULL) sn_launcher_context_setup_child_process (sn_context); #endif /* HAVE_STARTUP_NOTIFICATION */ setsid(); if(execl("/bin/sh", "/bin/sh", "-c", command.c_str(), 0) == -1){ printf("Error to execute command %s\n", command.c_str()); exit(1); } exit(0); //exit fork } waitpid(pid, NULL, 0); }
// Launch a command and initiate a startup notification int runcmd(FXString cmd, FXString cmdname, FXString dir, FXString startdir, FXbool usesn = true, FXString snexcepts = "") { int ret; // Change to current directory ret = chdir(dir.text()); if (ret < 0) { int errcode = errno; if (errcode) { fprintf(stderr, _("Error: Can't enter folder %s: %s"), dir.text(), strerror(errcode)); } else { fprintf(stderr, _("Error: Can't enter folder %s"), dir.text()); } return(-1); } // Get rid of possible command options cmdname = cmdname.before(' '); // Check if command is in the startup notification exception list FXbool startup_notify = true; if (snexcepts != "") { FXString entry; for (int i = 0; ; i++) { entry = snexcepts.section(':', i); if (streq(entry.text(), "")) { break; } if (streq(entry.text(), cmdname.text())) { startup_notify = false; break; } } } // Run command with startup notification if (usesn && startup_notify) { Display* xdisplay; SnDisplay* display; SnLauncherContext* context; Time timestamp; // Open display xdisplay = XOpenDisplay(NULL); if (xdisplay == NULL) { fprintf(stderr, _("Error: Can't open display\n")); ret = chdir(startdir.text()); if (ret < 0) { int errcode = errno; if (errcode) { fprintf(stderr, _("Error: Can't enter folder %s: %s"), startdir.text(), strerror(errcode)); } else { fprintf(stderr, _("Error: Can't enter folder %s"), startdir.text()); } } return(-1); } // Message displayed in the task bar (if any) FXString message; message.format(_("Start of %s"), cmdname.text()); // Initiate launcher context display = sn_display_new(xdisplay, NULL, NULL); context = sn_launcher_context_new(display, DefaultScreen(xdisplay)); sn_launcher_context_set_name(context, message.text()); sn_launcher_context_set_binary_name(context, cmdname.text()); sn_launcher_context_set_description(context, message.text()); sn_launcher_context_set_icon_name(context, cmdname.text()); timestamp = gettimestamp(); sn_launcher_context_initiate(context, "Xfe", cmd.text(), timestamp); // Run command in background cmd += " &"; static pid_t child_pid = 0; switch ((child_pid = fork())) { case -1: fprintf(stderr, _("Error: Fork failed: %s\n"), strerror(errno)); break; case 0: // Child sn_launcher_context_setup_child_process(context); execl("/bin/sh", "sh", "-c", cmd.text(), (char*)NULL); _exit(EXIT_SUCCESS); break; } sn_launcher_context_unref(context); } // Run command without startup notification else { // Run command in background cmd += " &"; ret = system(cmd.text()); if (ret < 0) { fprintf(stderr, _("Error: Can't execute command %s"), cmd.text()); return(-1); } // Just display the wait cursor during a second sleep(1); } // Go back to startup directory ret = chdir(startdir.text()); if (ret < 0) { int errcode = errno; if (errcode) { fprintf(stderr, _("Error: Can't enter folder %s: %s"), startdir.text(), strerror(errcode)); } else { fprintf(stderr, _("Error: Can't enter folder %s"), startdir.text()); } return(-1); } return(0); }