-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.vala
657 lines (582 loc) · 26.3 KB
/
main.vala
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
* 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 3 of the License, or
* (at your option) 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/*
* Yet another terminal =)
*
* valac --pkg gtk+-3.0 ./main.vala ./hvbox.vala ...
*
* http://www.valadoc.org/gtk+-3.0/Gtk.Container.html
* http://developer.gnome.org/gtk3/3.0/GtkContainer.html#gtk-container-get-resize-mode
* http://developer.gnome.org/gtkmm-tutorial/3.0/sec-custom-containers.html.en
* https://github.com/mdamt/blankon-panel
* https://live.gnome.org/Vala/CustomWidgetSamples
* http://git.xmms2.org/xmms2/abraca/tree/src/widgets/rating_entry.vala?id=ed5e182c4074f1bff56010658a36a75d95807921
* http://git.freesmartphone.org/?p=vala-terminal.git;a=tree;f=src;h=fca9afcd911ef55db74734080894abfc84576f3c;hb=HEAD
* http://live.gnome.org/Vala/GStreamerSample
* http://zetcode.com/tutorials/gtktutorial/gtkevents/
*
* pool replace, In C, you can put your buttons and widgets in a GtkOffscreenWindow using gtk_widget_reparent() and then use gtk_offscreen_window_get_pixbuf() to render it onto a GdkPixbuf, which you can then save to a file. Sorry I don't have any Python code, but I don't think the offscreen window is available in PyGTK yet.
* http://developer.gnome.org/gtk3/3.0/GtkStyleContext.html#gtk-render-frame
* http://developer.gnome.org/gtk3/3.0/gtk-migrating-GtkApplication.html
* https://gitorious.org/gnome-boxes/gnome-boxes/blobs/master/src/app.vala
* http://live.gnome.org/Vala/GSettingsSample
* http://code.valaide.org/content/example-program-using-keyfile-glib-class-readwrite-ini-files
* http://developer.gnome.org/pango/stable/PangoMarkupFormat.html
* http://developer.gnome.org/gcr/3.2/
*
* http://www.mono-project.com/GtkSharp_TreeView_Tutorial
* http://www.kksou.com/php-gtk2/articles/finetune-interactive-search-in-GtkTreeView---Part-4---set-custom-compare-function.php
* https://mail.gnome.org/archives/commits-list/2012-February/msg03582.html
* about reparent http://developer.gnome.org/gtk-faq/stable/x635.html
*/
using Gtk;
using Posix;
static const string DEFAULT_APP_ID = "org.gtk.altyo";
bool ParseGlobalsPath(string option_name,
string? val,
void *data,
ref Error error){
if(val != null)
Globals.path=val;
else
Globals.path=Globals.remote_cwd;
return true;
}
[CCode ( cheader_filename = "execinfo.h",cname = "backtrace")]
extern int linux_backtrace (void*[] buffer, int size);
[CCode ( cheader_filename = "execinfo.h",cname = "backtrace_symbols")]
extern string[]? linux_backtrace_symbols (void **buffer, int size);
struct Globals{
static bool reload = false;
static bool opt_help = false;
static bool opt_version = false;
static string? cmd_conf_file = null;
static bool toggle = false;
static string? app_id = null;
static bool disable_hotkey = false;
static bool standalone_mode = false;
static string? path = null;
static bool config_readonly = false;
static bool force_debug = false;
static bool cmd_fullscreen = false;
static string? cmd_title_tab = null;
static string? cmd_select_tab = null;
static string? cmd_close_tab = null;
static bool list_id = false;
static bool force_remote = false;
static string? remote_cwd = null;
static bool xterm_mode = false;
[CCode (array_length = false, array_null_terminated = true)]
public static string[]? exec_file_with_args = null;
public static const OptionEntry[] options = {
/*allow show help from remote call*/
{ "help", 'h', OptionFlags.HIDDEN, OptionArg.NONE, ref Globals.opt_help, null, null },
{ "reload", 'r', 0, OptionArg.NONE, ref Globals.reload,N_("Reload configuration"), null },
{ "cfg", 'c', 0, OptionArg.FILENAME, ref Globals.cmd_conf_file,N_("Read configuration from file"), N_("/path/to/config.ini") },
/*The option takes a string argument, multiple uses of the option are collected into an array of strings. */
{ "exec", 'e', 0, OptionArg.STRING_ARRAY, ref Globals.exec_file_with_args,N_("Run command in new tab"), N_("\"command arg1 argN...\"") },
{ "command",0, OptionFlags.HIDDEN, OptionArg.STRING_ARRAY, ref Globals.exec_file_with_args,N_("Run command in new tab"), N_("\"command arg1 argN...\"") },
{ "toggle", 0, 0, OptionArg.NONE, ref Globals.toggle,N_("Show/hide window"), null },
{ "id", 0, 0, OptionArg.STRING, ref Globals.app_id,N_("Set application id, none means disable application id"),"org.gtk.altyo_my,none" },
{ "listid", 0, 0, OptionArg.NONE, ref Globals.list_id,N_("Show ids of running AltYo instances"), null },
{ "disable-hotkey", 0, 0, OptionArg.NONE, ref Globals.disable_hotkey,N_("Disable main hotkey"),null},
{ "standalone", 0, 0, OptionArg.NONE, ref Globals.standalone_mode,N_("Disable control of window dimension"),null},
{ "default-path", 0, OptionFlags.OPTIONAL_ARG, OptionArg.CALLBACK, (void *)ParseGlobalsPath,N_("Set/update default path. Without arguments CWD will be used."),"/home/user/special" },
{ "config-readonly", 0, 0, OptionArg.NONE, ref Globals.config_readonly, N_("Lock any configuration changes"), null },
{ "debug", 'd', 0, OptionArg.NONE, ref Globals.force_debug,N_("Force debug"), null },
{ "fullscreen", 'f', 0, OptionArg.NONE, ref Globals.cmd_fullscreen,N_("Toggle AltYo in fullscreen mode"), null },
#if VALA_0_22
{ "tab-title", 't', 0, OptionArg.STRING, ref Globals.cmd_title_tab,N_("Get/Set tab title"), null },
#endif
{ "select-tab", 0, 0, OptionArg.STRING, ref Globals.cmd_select_tab,N_("Select tab by index"), null },
{ "close-tab", 0, 0, OptionArg.STRING, ref Globals.cmd_close_tab,N_("Close tab by index"), null },
{ "remote", 0, 0, OptionArg.NONE, ref Globals.force_remote,N_("Connect to remote instance or exit."), null },
{ "version", 0, 0, OptionArg.NONE, ref Globals.opt_version, N_("Display version number"), null },
{ "xterm", 0, 0, OptionArg.NONE, ref Globals.xterm_mode, N_("Run in xterm compatibility mode"), null },
{ null }
};
/* https://www.debian.org/doc/debian-policy/ch-customized-programs.html#s11.8.3
* To be an x-terminal-emulator, a program must:
* Be able to emulate a DEC VT100 terminal, or a compatible terminal.
* Support the command-line option -e command, which creates a new terminal window[106] and runs the specified command,
* interpreting the entirety of the rest of the command line as a command to pass straight to exec, in the manner that xterm does.
* Support the command-line option -T title, which creates a new terminal window with the window title title.
* */
public static bool x_terminal_emulator_exec (ref unowned string[] args)
{
bool found=false;
try {
if(args != null){
string s = "";
string [] arr = {};
foreach (unowned string arg in args) {
if(found){
/* for compatibility with xterm, don't allow miltiply exec commands
* if(arg == "-e" || arg == "--command"){
arr += s;
s = "";
continue;
}*/
s += " "+arg; //combine everything after '-e' as single string
}else{
if(arg == "-e" || arg == "--command" || arg == "--exec")
found = true;
if(arg.has_prefix("--command=") ){
s +=arg.substring(10);
found = true;
}
if(arg.has_prefix("--exec=")){
s +=arg.substring(7);
found = true;
}
}
}
if(found){
arr += s;
Globals.exec_file_with_args = null;
Globals.exec_file_with_args = arr;
}
}
} catch (ShellError e) {
GLib.stdout.printf ("Error: %s\n", e.message);
}
return found;
}
public static void print_backtrace () {
void*[] array = new void*[20];
int size = linux_backtrace (array, 20);
string[] strings = linux_backtrace_symbols (array, size);
for(int i=0; i< size; i++) {
print ("(%d): %s\n", i, strings[i]);
}
/* this can't work because the array length in unknown by vala
foreach (string trace in strings) {
stdout.printf("%s\n", trace);
}*/
}
}//Globals
unowned Gtk.Window main_win;
static void signal_handler (int signum) {
main_win.destroy();
}
static void null_handler(string? domain, LogLevelFlags flags, string message) {
}
static void print_handler(string? domain, LogLevelFlags flags, string message) {
printf("domain:%s message:%s\n",domain,message);
GLib.stdout.flush();
}
/* sync file_workaround_if_focuslost with workaround_if_focuslost option
* */
static void sync_workaround(MySettings conf, string file_workaround_if_focuslost){
bool workaround = conf.get_boolean("workaround_if_focuslost",false);
bool w_file = GLib.FileUtils.test(file_workaround_if_focuslost,GLib.FileTest.EXISTS);
debug("workaround != w_file %d != %d",(int)workaround,(int)w_file);
if(workaround && !w_file){
GLib.FileUtils.set_data(file_workaround_if_focuslost,null);
}else if(!workaround && w_file){
GLib.FileUtils.remove(file_workaround_if_focuslost);
}
}
static void configure_debug(MySettings conf){
if(conf.force_debug) {
Log.set_handler(null,
LogLevelFlags.LEVEL_MASK &
(LogLevelFlags.LEVEL_DEBUG |
LogLevelFlags.LEVEL_MESSAGE |
LogLevelFlags.LEVEL_WARNING |
LogLevelFlags.LEVEL_INFO |
LogLevelFlags.LEVEL_CRITICAL), print_handler);
}else if(!conf.get_boolean("debug",false))
Log.set_handler(null, LogLevelFlags.LEVEL_MASK & ~LogLevelFlags.LEVEL_ERROR, null_handler);
else{
var mask = conf.get_string_list("debug_level",{"debug","message","warning","info","critical"});
LogLevelFlags log_mask = LogLevelFlags.LEVEL_ERROR;//for accerts
if(mask!=null)
foreach(var level in mask){
log_mask = ((level == "debug") ?
log_mask | LogLevelFlags.LEVEL_DEBUG :
log_mask);
log_mask = ((level == "message") ?
log_mask | LogLevelFlags.LEVEL_MESSAGE :
log_mask);
log_mask = ((level == "warning") ?
log_mask | LogLevelFlags.LEVEL_WARNING :
log_mask);
log_mask = ((level == "info") ?
log_mask | LogLevelFlags.LEVEL_INFO :
log_mask);
log_mask = ((level == "critical") ?
log_mask | LogLevelFlags.LEVEL_CRITICAL :
log_mask);
}
//disable all except log_mask
//Log.set_handler(null, LogLevelFlags.LEVEL_MASK & ~log_mask, null_handler);
Log.set_handler(null, LogLevelFlags.LEVEL_MASK & log_mask, print_handler);
}
}
[DBus (name = "org.gtk.altyo")]
public class AltYoDbusServer : Object {
weak Gtk.Application app;
public AltYoDbusServer(Gtk.Application app){
this.app = app;
}
public string get_window_title(){
unowned List<weak Window> list = app.get_windows();
if(list!=null){
var win=((VTMainWindow)list.data);
return win.title;
}
return "window not found :(";
}
}
[DBus (name = "org.gtk.altyo")]
interface AltYoDbusClient : Object {
public abstract string get_window_title () throws IOError;
}
public class AppAltYo: Gtk.Application {
public AppAltYo(string? application_id, ApplicationFlags flags){
Object (application_id:application_id, flags:flags);
}
//CCode for vala 0.18 (backport from vala 0.22)
//prevent segmentation fault if compiled with valac 0.18
public override bool local_command_line ([CCode (array_length = false, array_null_terminated = true)]ref unowned string[] arguments, out int exit_status){
exit_status=-1;
return false;
}
/* glib compatability hack
* http://www.trevorpounds.com/blog/?p=103
* >Building and linking to an older version of libc or using a chroot is a much better, less error prone approach.
* */
//~[CCode (cname = "__asm__(\".symver memcpy,memcpy@GLIBC_2.2.5\");//",type="")]
//~ public extern void* fake_function();
#if VALA_0_22
//if > GLib.Version.@2_34
public override bool dbus_register (DBusConnection connection, string object_path){
try {
connection.register_object (object_path, new AltYoDbusServer (this));
} catch (IOError e) {
GLib.stderr.printf ("Could not register AltYoServer\n");
}
return true;//continue
}
#endif
}
public delegate void myprintcb (string print_string);
int apply_flags(VTMainWindow remote_window,myprintcb myprint){
int return_code=0;
if(Globals.force_debug){
configure_debug(remote_window.conf);
}
if(Globals.reload){
remote_window.conf.load_config();
}
if(Globals.path!=null){
remote_window.conf.default_path=Globals.path;
}
if(Globals.exec_file_with_args!=null){
foreach(var s in Globals.exec_file_with_args){
debug("exec %s",s);
remote_window.ayobject.add_tab_with_title(s,s);
}
if(remote_window.current_state == WStates.HIDDEN)
remote_window.pull_down();
}
if(Globals.toggle){
remote_window.toggle_window();
}
if(Globals.cmd_fullscreen){
if(remote_window.maximized)
remote_window.maximized=false;
else
remote_window.maximized=true;
}
if(remote_window.conf.get_boolean("window_allow_remote_control",false)){
if(Globals.cmd_title_tab!=null){
var current_index=remote_window.ayobject.cmd_get_tab_index();
var count=remote_window.ayobject.cmd_get_tabs_count();
if(Globals.cmd_title_tab!=""){
var s_arr = Globals.cmd_title_tab.split (":",2);
uint64 index=0;/*index from user starting from 1*/
if(s_arr.length==2 && uint64.try_parse(s_arr[0],out index) && index<=count && index>0){
if(s_arr[1]=="") s_arr[1]=null;
myprint("altyo: new title=%d:%s\n".printf((int)index,s_arr[1]));
remote_window.ayobject.cmd_set_tab_title((uint)index-1,s_arr[1]);
}else{
if(s_arr.length!=2)
myprint("altyo usage:\n\t-t \"3:new title\"\n\t-t \"\"\n");
if(index>count)
myprint("altyo: Err index>count (%d > %d)\n".printf((int)index,(int)count) );
if(index==0)
myprint("altyo: Err index must be > 1\n");
return_code=1;
}
}else{
myprint("%d/%d \n".printf(current_index+1,(int)count) );
for(var i=0; i<count; i++){
myprint("%d:%s\n".printf(i+1,remote_window.ayobject.cmd_get_tab_title(i)) );
}
}
}
if(Globals.cmd_select_tab!=null){
var count=remote_window.ayobject.cmd_get_tabs_count();
uint64 index=0;/*index from user starting from 1*/
if(uint64.try_parse(Globals.cmd_select_tab,out index) && index<=count && index>0 ){
remote_window.ayobject.cmd_activate_tab((uint)(index-1));
debug("altyo: selected %d\n".printf((int)index) );
}else{
if(index>count)
myprint("altyo: Err index>count (%d > %d)\n".printf((int)index,(int)count) );
if(index==0)
myprint("altyo: Err index must be > 1\n");
return_code=1;
}
}
if(Globals.cmd_close_tab!=null){
var count=remote_window.ayobject.cmd_get_tabs_count();
uint64 index=0;/*index from user starting from 1*/
if(uint64.try_parse(Globals.cmd_close_tab,out index) && index<=count && index>0 ){
remote_window.ayobject.close_tab((int)(index-1));
debug("altyo: tab %d closed\n".printf((int)index) );
}else{
if(index>count)
myprint("altyo: Err index>count (%d > %d)\n".printf((int)index,(int)count) );
if(index==0)
myprint("altyo: Err index must be > 1\n");
return_code=1;
}
}
}//if window_allow_remote_control
else{
if(Globals.cmd_title_tab!=null || Globals.cmd_select_tab!=null || Globals.cmd_close_tab!=null)
myprint("altyo: Err remote commands is disabled in configuration file!\n");
return_code=1;
}
return return_code;
}
int main (string[] args) {
GLib.ApplicationFlags appflags=ApplicationFlags.HANDLES_COMMAND_LINE;
Intl.setlocale (LocaleCategory.ALL, "");
Intl.bindtextdomain (GETTEXT_PACKAGE, null);
Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Intl.textdomain (GETTEXT_PACKAGE);
//Gtk.init (ref args);
Globals.app_id=DEFAULT_APP_ID;//default app id
Globals.remote_cwd = GLib.Environment.get_current_dir();
/* Parse only --id and --standalone options
* others will be parsed in application app.startup.connect event
* */
OptionContext local_ctx = new OptionContext("AltYo");//global var, used in app.startup
try {
local_ctx.add_main_entries(Globals.options, null);
string[] args2 = args; //copy args for local use, original args will be used on remote side
unowned string[] args3 = args2;//tmp pointer
local_ctx.parse(ref args3);
args2=null;
var self_basename = GLib.Path.get_basename(args[0]);
if( self_basename != "altyo" || Globals.xterm_mode){
Globals.xterm_mode = true;
Globals.x_terminal_emulator_exec(ref args);
GLib.stderr.printf("AltYo runnied as %s in xterm mode\n",self_basename);
}
} catch (Error e) {
GLib.stderr.printf("Error initializing: %s\n", e.message);
return 1;
}
if(Globals.opt_version){
printf("altyo %s \nLicense GPLv3+\n",(AY_CHANGELOG_TAG!="" ? AY_CHANGELOG_TAG : "0.4") +" "+AY_GIT_HASH );
Posix.exit(0);
}
#if VALA_0_22
/*searching for remote instances*/
if(Globals.list_id){
Variant interfaces;
DBusConnection session_bus = Bus.get_sync (BusType.SESSION);
try {
interfaces = session_bus.call_sync ("org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"ListNames",
null,
new VariantType ("(as)"),
DBusCallFlags.NONE,
-1);
}catch (GLib.Error e) {
GLib.stderr.printf ("unable to search for existing altyo instances: %s \n", e.message);
return 1;
}
foreach (var val in interfaces.get_child_value (0)) {
var address = (string) val;
if (address.has_prefix (DEFAULT_APP_ID)){//search only inside org.gtk.altyo
printf("%s",address);
try{
string path = "/"+address.replace(".","/");
AltYoDbusClient client = session_bus.get_proxy_sync (address,path,
GLib.DBusProxyFlags.DO_NOT_CONNECT_SIGNALS|GLib.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES);
printf(" %s",client.get_window_title());
}catch (GLib.Error e) {
//GLib.stderr.printf ("unable to search for existing altyo instances: %d %s \n",e.code, e.message);
}
printf("\n");
}
}
return 0;//list and exit
}
#endif
if(Globals.standalone_mode && Globals.app_id==DEFAULT_APP_ID){
Globals.app_id+="._%d".printf(Posix.getpid());//generate unique id for standalone_mode
}
if(!Globals.app_id.has_prefix(DEFAULT_APP_ID)){
/*this name restriction occur because we need limit searching in --listid */
printf(_("Application id must begin with %s \n for example %s.my_instance\n"),DEFAULT_APP_ID,DEFAULT_APP_ID);
return 1;
}
if(Globals.app_id == "none")
Globals.app_id=null;
else if(!GLib.Application.id_is_valid(Globals.app_id)){
printf(_("Wrong application id \"%s\""),Globals.app_id);
printf(_("""
Application identifiers must contain only the ASCII characters "A-Z[0-9]_-." and must not begin with a digit.
Application identifiers must contain at least one '.' (period) character (and thus at least three elements).
Application identifiers must not begin or end with a '.' (period) character.
Application identifiers must not contain consecutive '.' (period) characters.
Application identifiers must not exceed 255 characters."""));
return 1;//stop on error
}
if(Globals.standalone_mode){
Globals.disable_hotkey=true;
}
/******************************************************************/
/* use file as special configuration marker
* because we should not parse config.ini file yet
* later we will sync it in sync_workaround()
* simple and fast
* */
string file_workaround_if_focuslost = GLib.Environment.get_user_config_dir()+"/altyo/workaround_if_focuslost";
if(GLib.FileUtils.test(file_workaround_if_focuslost,GLib.FileTest.EXISTS) ){
/* more info in README.md in FAQ.
* */
if(!GLib.Environment.set_variable("GDK_CORE_DEVICE_EVENTS","1",true)) //must be set before new AppAltYo()
printf("altyo: Unable to set GDK_CORE_DEVICE_EVENTS=1\n");
else if(Globals.force_debug)
printf("altyo: set GDK_CORE_DEVICE_EVENTS=1\n");
}
/******************************************************************/
var app = new AppAltYo(Globals.app_id, appflags);
//remote args usage
app.command_line.connect((command_line)=>{//ApplicationCommandLine
if(!command_line.get_is_remote() )//local command line was handled in app.startup
return 0;//just ignore it
string[] argv = command_line.get_arguments();
debug("app.command_line.connect argv.length=%d %s",argv.length,argv[0]);
OptionContext ctx = new OptionContext("AltYo");
ctx.add_main_entries(Globals.options, null);
if(argv.length==1 ){//no parameters
unowned List<weak Window> list = app.get_windows();
if(list!=null)
((VTMainWindow)list.data).pull_down(); //another altyo already running, show it
return 0;//ok
}else{
ctx.set_help_enabled (false);//disable exit from application if wrong parameters
unowned string[] pargv=argv;
Globals.exec_file_with_args=null;//clear array
Globals.cmd_conf_file=null;
Globals.reload=false;
Globals.opt_help=false;
Globals.toggle=false;
Globals.path=null;
Globals.force_debug=false;
Globals.cmd_fullscreen = false;
Globals.cmd_title_tab = null;
Globals.cmd_select_tab = null;
Globals.cmd_close_tab = null;
Globals.remote_cwd = command_line.get_cwd();
Globals.xterm_mode = false;
var old_standalone_mode=Globals.standalone_mode;
try {
if(!ctx.parse(ref pargv))return 3;
} catch (Error e) {
command_line.print("altyo: Error initializing: %s\n", e.message);
}
Globals.standalone_mode=old_standalone_mode;//restore standalone_mode state
var self_basename = GLib.Path.get_basename(argv[0]);
if( self_basename != "altyo" || Globals.xterm_mode){
Globals.xterm_mode = true;
string[] argv2 = command_line.get_arguments();
unowned string[] argv3 = argv2;
Globals.x_terminal_emulator_exec (ref argv3);
debug("AltYo runnied as %s in xterm mode\n",self_basename);
argv2=null;
}else
Globals.xterm_mode = false;
debug("app.command_line.connect reload=%d",(int)Globals.reload);
VTMainWindow remote_window=null;
unowned List<weak Window> list = app.get_windows();
if(list!=null)
remote_window=((VTMainWindow)list.data);
else{
command_line.print("altyo: Err remote window not found\n");
return 2;
}
var return_code = apply_flags(remote_window,(s)=>{
command_line.print(s);
});
Globals.reload=false;
return return_code;//exit status
}
});//app.command_line.connect
app.startup.connect(()=>{//first run
if(Globals.force_remote){
printf("altyo: remote instance %s not found!\n",Globals.app_id);
Posix.exit(1);
}
debug("app.startup.connect");
var conf = new MySettings(Globals.cmd_conf_file,Globals.standalone_mode);
conf.readonly=Globals.config_readonly;
conf.disable_hotkey=Globals.disable_hotkey;
conf.default_path=Globals.path;
conf.force_debug=Globals.force_debug;
if(!conf.opened){
printf("altyo: Unable to open configuration file!\n");
Posix.exit(1);
}
conf.get_boolean("window_allow_remote_control",false);//remember option type
configure_debug(conf);
sync_workaround(conf,file_workaround_if_focuslost);
debug("git_hash=%s",AY_GIT_HASH);
debug("changelog_tag=%s",AY_CHANGELOG_TAG);
conf.on_load.connect(()=>{
configure_debug(conf);
sync_workaround(conf,file_workaround_if_focuslost);
});
var win = new VTMainWindow (WindowType.TOPLEVEL);
win.set_application(app);
win.CreateVTWindow(conf);
main_win=win;
apply_flags(win,(s)=>{print(s);});
sigaction_t action = sigaction_t ();
action.sa_handler = signal_handler;
/* Hook up signal handlers */
sigaction (SIGINT, action, null);
sigaction (SIGQUIT, action, null);
//sigaction (SIGABRT, action, null);//something wrong! don't save file
sigaction (SIGTERM, action, null);
sigaction (SIGKILL, action, null);
Gtk.main ();
});//app.startup.connect
var status = app.run(args);
return status;
}