1#include <fcntl.h>
2
3#include <kapplication.h>
4#include <kaboutdata.h>
5#include <kcmdlineargs.h>
6#include <kdebug.h>
7#include <klocale.h>
8#include <kshell.h>
9
10#include "ksystraycmd.h"
11
12#include <X11/Xlib.h>
13#include <QX11Info>
14#ifndef KDE_USE_FINAL
15const int XFocusOut = FocusOut;
16const int XFocusIn = FocusIn;
17#endif
18#undef FocusOut
19#undef FocusIn
20#undef KeyPress
21#undef KeyRelease
22
23
24int main( int argc, char *argv[] )
25{
26 KAboutData aboutData( "ksystraycmd", 0, ki18n( "KSysTrayCmd" ),
27 "0.1",
28 ki18n( "Allows any application to be kept in the system tray" ),
29 KAboutData::License_GPL,
30 ki18n("(C) 2001-2002 Richard Moore (rich@kde.org)") );
31 aboutData.addAuthor( ki18n("Richard Moore"), KLocalizedString(), "rich@kde.org" );
32
33 KCmdLineArgs::init( argc, argv, &aboutData );
34
35 KCmdLineOptions options;
36 options.add("!+command", ki18n("Command to execute"));
37 // "!" means: all options after command are treated as arguments to the command
38 options.add("window <regexp>", ki18n("A regular expression matching the window title\n"
39 "If you do not specify one, then the very first window\n"
40 "to appear will be taken - not recommended."));
41 options.add("wid <int>", ki18n("The window id of the target window\n"
42 "Specifies the id of the window to use. If the id starts with 0x\n"
43 "it is assumed to be in hex."));
44 options.add("hidden", ki18n( "Hide the window to the tray on startup" ));
45 options.add("startonshow", ki18n( "Wait until we are told to show the window before\n"
46 "executing the command" ));
47 options.add("tooltip <text>", ki18n( "Sets the initial tooltip for the tray icon" ));
48 options.add("keeprunning", ki18n( "Keep the tray icon even if the client exits. This option\n"
49 "has no effect unless startonshow is specified." ));
50 options.add("ownicon", ki18n( "Use ksystraycmd's icon instead of the window's icon in the systray\n"
51 "(should be used with --icon to specify ksystraycmd icon)" ));
52 options.add("ontop", ki18n( "Try to keep the window above other windows"));
53 options.add("quitonhide", ki18n( "Quit the client when we are told to hide the window.\n"
54 "This has no effect unless startonshow is specified and implies keeprunning." ));
55 /*options.add("menuitem <item>", ki18n( "Adds a custom entry to the tray icon menu\n"
56 "The item should have the form text:command." ));*/
57 KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
58
59 KApplication app;
60
61 //
62 // Setup the tray icon from the arguments.
63 //
64 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
65 KSysTrayCmd cmd;
66
67 // Read the window id
68 QString wid = args->getOption( "wid" );
69 if ( !wid.isEmpty() ) {
70 int base = 10;
71 if ( wid.startsWith( "0x" ) ) {
72 base = 16;
73 wid = wid.right( wid.length() - 2 );
74 }
75
76 bool ok=true;
77 ulong w = wid.toULong( &ok, base );
78 if ( ok )
79 cmd.setTargetWindow( w );
80 else {
81 kWarning() << "KSysTrayCmd: Got bad win id" ;
82 }
83 }
84
85 // Read window title regexp
86 QString title = args->getOption( "window" );
87 if ( !title.isEmpty() )
88 cmd.setPattern( title );
89
90 if ( title.isEmpty() && wid.isEmpty() && (args->count() == 0) )
91 KCmdLineArgs::usageError(i18n("No command or window specified"));
92
93 // Read the command
94 QString command;
95 for ( int i = 0; i < args->count(); i++ )
96 command += KShell::quoteArg(args->arg(i)) + ' ';
97 if ( !command.isEmpty() )
98 cmd.setCommand( command );
99
100 // Tooltip
101 QString tip = args->getOption( "tooltip" );
102 if ( !tip.isEmpty() )
103 cmd.setDefaultTip( tip );
104
105 // Apply icon and tooltip
106 cmd.refresh();
107
108 // Keep running flag
109 if ( args->isSet( "keeprunning" ) )
110 cmd.setNoQuit( true );
111
112 if ( args->isSet( "quitonhide" ) ) {
113 cmd.setNoQuit( true );
114 cmd.setQuitOnHide( true );
115 }
116
117 // Start hidden
118 if ( args->isSet( "hidden" ) )
119 cmd.hideWindow();
120
121 // On top
122 if ( args->isSet( "ontop" ) )
123 cmd.setOnTop(true);
124
125 // Use ksystraycmd icon
126 if ( args->isSet( "ownicon" ) )
127 cmd.setOwnIcon(true);
128
129 // Lazy invocation flag
130 if ( args->isSet( "startonshow" ) ) {
131 cmd.setStartOnShow( true );
132 cmd.show();
133 }
134 else {
135 if ( !cmd.start() )
136 return 1;
137 }
138
139 fcntl(ConnectionNumber(QX11Info::display()), F_SETFD, 1);
140 args->clear();
141
142 return app.exec();
143}
144
145