-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
176 lines (147 loc) · 6.21 KB
/
Copy pathmain.cpp
File metadata and controls
176 lines (147 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// This file is the part of wkterm, webkit-based terminal emulator
// (C) 2011 Ilya Shapovalov
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <iostream>
#include <gio/gio.h>
#include <gio/gunixinputstream.h>
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSContextRef.h>
#include <JavaScriptCore/JSStringRef.h>
WebKitWebView* webView = NULL;
int fdMaster;
static JSValueRef sendToInput(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
assert(argumentCount==1);
// std::cout << "Bar Called\n";
JSValueRef ex;
double num = JSValueToNumber(ctx,arguments[0],&ex);
char c = (char)num;
if (c=='\r') c = '\n';
write(fdMaster, &c, 1);
std::cout << "writing.." << c << std::endl;
/*
JSStringRef str = JSValueToStringCopy(ctx, arguments[0], &ex);
char buf[4096];
JSStringGetUTF8CString(str, buf,4096);
std::cout << "writing.." << buf << std::endl;
write(fdMaster, buf, strlen(buf));
*/
return JSValueMakeUndefined(ctx);
}
static JSValueRef setTerminalSize(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
assert(argumentCount==2);
JSValueRef ex;
struct winsize sz;
sz.ws_row = (int)JSValueToNumber(ctx,arguments[0],&ex);
sz.ws_col = (int)JSValueToNumber(ctx,arguments[1],&ex);
int ret = ioctl(fdMaster, TIOCSWINSZ, &sz);
return JSValueMakeUndefined(ctx);
}
static void window_object_cleared_cb(WebKitWebView *web_view, WebKitWebFrame *web_frame, gpointer context, gpointer arg3, gpointer user_data) {
JSGlobalContextRef jsContext = webkit_web_frame_get_global_context(web_frame);
JSObjectRef globalObj = JSContextGetGlobalObject(jsContext);
JSValueRef exception=0;
JSValueRef jsfunc;
jsfunc=JSObjectMakeFunctionWithCallback(jsContext,JSStringCreateWithUTF8CString("sendToInput"),sendToInput);
JSObjectSetProperty(jsContext, globalObj,JSStringCreateWithUTF8CString("sendToInput"), jsfunc,kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, &exception);
jsfunc=JSObjectMakeFunctionWithCallback(jsContext,JSStringCreateWithUTF8CString("setTerminalSize"),setTerminalSize);
JSObjectSetProperty(jsContext, globalObj,JSStringCreateWithUTF8CString("setTerminalSize"), jsfunc,kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, &exception);
}
#include "cJSON.h"
char bufInput[4096];
static void onPipeRead(GObject *source, GAsyncResult *res, gpointer user_data) {
GInputStream *inputStream = G_INPUT_STREAM (source);
GError *err = NULL;
int main_len = g_input_stream_read_finish (inputStream, res, &err);
bufInput[main_len] = 0;
// size_t size = (size_t)g_async_result_get_user_data(res);
// std::cout << size << std::endl;
if (main_len>0) {
//for (int i=0;i<main_len;i++) std::cout << (bufInput[i]<32?'#':(char)bufInput[i]) << (int)bufInput[i] << ", ";
//std::cout << std::endl;
cJSON *json = cJSON_CreateString(bufInput);
char *bufJson = cJSON_Print(json);
char buf[strlen(bufJson)+100];
// std::cout << (int)bufJson[0] << ',' << (int)bufJson[1] << std::endl;
sprintf(buf,"print(%s);",bufJson);
// std::cout << buf << std::endl;
free(bufJson);
webkit_web_view_execute_script(webView, buf);
}
g_input_stream_read_async(inputStream, bufInput, 4096, G_PRIORITY_DEFAULT, NULL, onPipeRead,NULL);
}
#include <pty.h> /* for openpty and forkpty */
#include <utmp.h> /* for login_tty */
void connect_shell() {
GInputStream * inputStream = g_unix_input_stream_new(fdMaster,false);
g_input_stream_read_async(inputStream, bufInput, 4096, G_PRIORITY_DEFAULT, NULL, onPipeRead,NULL);
}
void run_shell() {
termios tio ;
winsize winp ;
char buf[1024];
int pid = forkpty(&fdMaster, buf, NULL,NULL);//&tio, &winp);
if (pid==0) {
execlp("/bin/bash","/bin/bash","-i","-l",NULL);
exit(EXIT_FAILURE);
}
}
static void load_finished_cb(WebKitWebView *web_view, WebKitWebFrame *web_frame, gpointer data) {
printf("Finished downloading %s\n", webkit_web_view_get_uri(web_view));
webView = web_view;
connect_shell();
}
static void destroy_cb(GtkWidget* widget, gpointer data) {
gtk_main_quit();
}
int main(int argc, char* argv[]) {
GtkWidget* window;
WebKitWebView* web_view = NULL;
run_shell();
gtk_init(&argc, &argv);
char *dir = get_current_dir_name();
char uri[4096];
sprintf(uri,"file://%s/terminal.html",dir);
free(dir);
if(!g_thread_supported())
g_thread_init(NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
GdkGeometry gdkGeom;
gdkGeom.min_width = 100;
gdkGeom.min_height = 100;
gtk_window_set_geometry_hints(GTK_WINDOW(window),NULL ,&gdkGeom, GDK_HINT_MIN_SIZE);
g_signal_connect(window, "destroy", G_CALLBACK(destroy_cb), NULL);
web_view = web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
webkit_web_view_set_transparent(web_view, TRUE);
g_signal_connect(web_view, "load-finished", G_CALLBACK(load_finished_cb), NULL);
g_signal_connect (web_view, "window-object-cleared", G_CALLBACK(window_object_cleared_cb), web_view);
webkit_web_view_load_uri(web_view, uri);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(web_view));
gtk_widget_grab_focus(GTK_WIDGET(web_view));
gtk_widget_show_all(window);
gtk_main();
//todo: shell cleanup?
return 0;
}