1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation; either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * gtkD is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with gtkD; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 17 */ 18 19 module gtk.SpawnTests; 20 21 import glib.Spawn; 22 23 import gio.Application : GioApplication = Application; 24 import gtk.Application; 25 import gtk.ApplicationWindow; 26 import gtk.TextView; 27 import gtk.TextBuffer; 28 import gtk.TextIter; 29 import gtk.Box; 30 import gtk.VBox; 31 import gtk.ScrolledWindow; 32 import gtk.Button; 33 import gtk.Image; 34 35 import std.string; 36 import std.stdio; 37 38 class SpawnWindow : ApplicationWindow 39 { 40 TextView viewInput; 41 TextView viewOutput; 42 TextView viewError; 43 TextIter iterOut; 44 TextIter iterError; 45 46 this(Application application) 47 { 48 super(application); 49 setTitle("Spawn testing"); 50 setupWindow(); 51 setSizeRequest(400,400); 52 showAll(); 53 } 54 55 private void setupWindow() 56 { 57 Box main = new VBox(false, 2); 58 59 viewInput = new TextView(); 60 viewOutput = new TextView(); 61 viewError = new TextView(); 62 63 main.packStart(new ScrolledWindow(viewInput), false, false, 2); 64 Button button = new Button("exec", &execInput); 65 main.packStart(button, false, false, 4); 66 main.packStart(new ScrolledWindow(viewOutput), true, true, 2); 67 main.packStart(new ScrolledWindow(viewError), false, false, 2); 68 69 setBorderWidth(7); 70 add(main); 71 } 72 73 private void execInput(Button button) 74 { 75 string[] args = std..string.split(viewInput.getBuffer().getText()); 76 exec(args); 77 } 78 79 private bool exec(string[] args) 80 { 81 foreach ( int i, string arg ; args) 82 { 83 writefln("[%s] >%s<", i, arg); 84 } 85 Spawn spawn = new Spawn(args[0]); 86 87 if (args.length > 1 ) 88 { 89 for( int i=1 ; i<args.length ; i++ ) 90 { 91 writefln("SpawnTests.exec adding parameter [%s] %s",i,args[i]); 92 spawn.addParm(args[i]); 93 } 94 } 95 return exec(spawn); 96 } 97 98 bool childEnded(Spawn spawn) 99 { 100 writefln("process %s ended with status %s", viewInput.getBuffer().getText(),spawn.getExitStatus()); 101 return true; //gotta check this. 102 } 103 104 private bool exec(Spawn spawn) 105 { 106 //TODO: These functions should accept a string. 107 viewOutput.getBuffer().setText(""); 108 viewError.getBuffer().setText(""); 109 110 TextBuffer bufferError = viewError.getBuffer(); 111 TextIter iterError = new TextIter(); 112 113 int result = spawn.commandLineSync(&childEnded, &syncOutput, &syncError); 114 115 bufferError.getEndIter(iterError); 116 viewError.getBuffer().insert(iterError, spawn.getLastError()~"\n"); 117 118 writefln("exit loop"); 119 120 spawn.close(); 121 return true; 122 } 123 124 public bool syncOutput(string line) 125 { 126 TextIter iter = new TextIter(); 127 viewOutput.getBuffer().getEndIter(iter); 128 viewOutput.getBuffer().insert(iter, line~"\n"); 129 return true; 130 } 131 132 public bool syncError(string line) 133 { 134 TextIter iter = new TextIter(); 135 viewError.getBuffer().getEndIter(iter); 136 viewError.getBuffer().insert(iter, line~"\n"); 137 return true; 138 } 139 140 public void setInput(string[] args) 141 { 142 TextBuffer inBuffer = viewInput.getBuffer(); 143 string t; 144 foreach ( int count, string arg; args) 145 { 146 if ( count > 0 ) t ~= " "; 147 t ~= arg; 148 } 149 inBuffer.setText(t); 150 } 151 152 public void setInput(string arg) 153 { 154 viewInput.getBuffer().setText(arg); 155 } 156 } 157 158 int main(string[] args) 159 { 160 auto application = new Application("org.gtkd.demo.spawntest", GApplicationFlags.FLAGS_NONE); 161 162 application.addOnActivate(delegate void(GioApplication app) { 163 SpawnWindow sw = new SpawnWindow(application); 164 165 if ( args.length > 1 ) 166 { 167 sw.setInput(args[1..args.length]); 168 } 169 else 170 { 171 sw.setInput("/bin/ls"); 172 } 173 }); 174 175 return application.run(args); 176 }