FLTK logo

[master] 85ac3d3 - FLUID: defer calling Fl::add_fd.

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] 85ac3d3 - FLUID: defer calling Fl::add_fd. "Matthias Melcher" Jan 29, 2023  
 
commit 85ac3d31474e48d0e84d2dd03b369e0e559e007e
Author:     Matthias Melcher <github@matthiasm.com>
AuthorDate: Sun Jan 29 11:51:44 2023 +0100
Commit:     Matthias Melcher <github@matthiasm.com>
CommitDate: Sun Jan 29 11:51:55 2023 +0100

    FLUID: defer calling Fl::add_fd.

 fluid/ExternalCodeEditor_UNIX.cxx | 40 ++++++++++++++++++++++++++++-----------
 fluid/ExternalCodeEditor_UNIX.h   |  2 ++
 2 files changed, 31 insertions(+), 11 deletions(-)

diff --git fluid/ExternalCodeEditor_UNIX.cxx fluid/ExternalCodeEditor_UNIX.cxx
index 911fa3f..2a8f8a5 100644
--- fluid/ExternalCodeEditor_UNIX.cxx
+++ fluid/ExternalCodeEditor_UNIX.cxx
@@ -59,11 +59,8 @@ ExternalCodeEditor::ExternalCodeEditor() {
   filename_   = 0;
   file_mtime_ = 0;
   file_size_  = 0;
-  if (::pipe(alert_pipe_) == 0) {
-    Fl::add_fd(alert_pipe_[0], FL_READ, alert_pipe_cb, this);
-  } else {
-    alert_pipe_[0] = alert_pipe_[1] = -1;
-  }
+  alert_pipe_[0] = alert_pipe_[1] = -1;
+  alert_pipe_open_ = false;
 }
 
 /**
@@ -76,11 +73,12 @@ ExternalCodeEditor::~ExternalCodeEditor() {
            (void*)this, (long)pid_);
   close_editor();   // close editor, delete tmp file
   set_filename(0);  // free()s filename
-  if (alert_pipe_[0] != -1) {
+
+  if (alert_pipe_open_) {
     Fl::remove_fd(alert_pipe_[0]);
-    ::close(alert_pipe_[0]);
+    if (alert_pipe_[0] != -1) ::close(alert_pipe_[0]);
+    if (alert_pipe_[1] != -1) ::close(alert_pipe_[1]);
   }
-  if (alert_pipe_[1] != -1) ::close(alert_pipe_[1]);
 }
 
 /**
@@ -360,6 +358,23 @@ static int make_args(char *s,         // string containing words (gets trashed!)
 }
 
 /**
+ If no alert pipe is open yet, try to create the pipe and hook it up the the fd callback.
+
+ The alert pipe is used to communicate from the forked process to the main
+ FLTK app in case launching the editor failed.
+ */
+void ExternalCodeEditor::open_alert_pipe() {
+  if (!alert_pipe_open_) {
+    if (::pipe(alert_pipe_) == 0) {
+      Fl::add_fd(alert_pipe_[0], FL_READ, alert_pipe_cb, this);
+      alert_pipe_open_ = true;
+    } else {
+      alert_pipe_[0] = alert_pipe_[1] = -1;
+    }
+  }
+}
+
+/**
  Start editor in background (fork/exec)
  \return 0 on success, leaves editor child process running as 'pid_'
  \return -1 on error, posts dialog with reason (child exits)
@@ -371,6 +386,7 @@ int ExternalCodeEditor::start_editor(const char *editor_cmd,
   char cmd[1024];
   snprintf(cmd, sizeof(cmd), "%s %s", editor_cmd, filename);
   command_line_.value(editor_cmd);
+  open_alert_pipe();
   // Fork editor to background..
   switch ( pid_ = fork() ) {
     case -1:    // error
@@ -384,8 +400,10 @@ int ExternalCodeEditor::start_editor(const char *editor_cmd,
       char **args = 0;
       if (make_args(cmd, &nargs, &args) > 0) {
         execvp(args[0], args);  // run command - doesn't return if succeeds
-        int err = errno;
-        ::write(alert_pipe_[1], &err, sizeof(int));
+        if (alert_pipe_open_) {
+          int err = errno;
+          ::write(alert_pipe_[1], &err, sizeof(int));
+        }
         exit(1);
       }
       exit(1);
@@ -541,7 +559,7 @@ void ExternalCodeEditor::alert_pipe_cb(FL_SOCKET s, void* d) {
   const char* cmd = self->command_line_.value();
   if (cmd && *cmd) {
     if (cmd[0] == '/') { // is this an absoluet filename?
-      fl_alert("Can't launch external editor '%s':\n%s\n\ncmd: \"%s\")",
+      fl_alert("Can't launch external editor '%s':\n%s\n\ncmd: \"%s\"",
                fl_filename_name(cmd), strerror(self->last_error_), cmd);
     } else {
       char pwd[FL_PATH_MAX+1];
diff --git fluid/ExternalCodeEditor_UNIX.h fluid/ExternalCodeEditor_UNIX.h
index 87feba8..fd3a93b 100644
--- fluid/ExternalCodeEditor_UNIX.h
+++ fluid/ExternalCodeEditor_UNIX.h
@@ -27,6 +27,7 @@ class ExternalCodeEditor {
   Fd_String command_line_;
   int last_error_;
   int alert_pipe_[2];
+  bool alert_pipe_open_;
   static void alert_pipe_cb(FL_SOCKET, void*);
 
 protected:
@@ -35,6 +36,7 @@ protected:
   const char *tmp_filename();
   int start_editor(const char *cmd, const char *filename);
   void set_filename(const char *val);
+  void open_alert_pipe();
 
 public:
   ExternalCodeEditor();
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.