1 /*
2 Copyright (C) 2012 Jay Satiro <raysatiro@yahoo.com>
3 All rights reserved.
4
5 This file is part of Workrave.
6
7 Workrave is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Workrave is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Workrave. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /**
22 This is a simple win32 example that will send a menu command to Workrave from the command line.
23
24 Visual Studio:
25 cl /W4 send_workrave_command.c
26
27 MinGW or cygwin:
28 gcc -Wall -Wextra -Wno-unknown-pragmas -o send_workrave_command.exe send_workrave_command.c
29
30
31 x:\Workrave\other>send_workrave_command.exe MENU_COMMAND_MODE_NORMAL
32 -
33 Copyright (C) 2012 Jay Satiro <raysatiro@yahoo.com>
34 All rights reserved. License GPLv3+: GNU GPL version 3 or later
35 <http://www.gnu.org/licenses/gpl.html>.
36 This is free software: you are free to change and redistribute it.
37 There is NO WARRANTY, to the extent permitted by law.
38 -
39
40 Sending Workrave command MENU_COMMAND_MODE_NORMAL...
41 Command sent.
42 */
43
44 #include <windows.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #pragma comment( lib, "user32.lib" )
50
51
52 const char *command[] =
53 {
54 "MENU_COMMAND_PREFERENCES",
55 "MENU_COMMAND_EXERCISES",
56 "MENU_COMMAND_REST_BREAK",
57 "MENU_COMMAND_MODE_NORMAL",
58 "MENU_COMMAND_MODE_QUIET",
59 "MENU_COMMAND_MODE_SUSPENDED",
60 "MENU_COMMAND_NETWORK_CONNECT",
61 "MENU_COMMAND_NETWORK_DISCONNECT",
62 "MENU_COMMAND_NETWORK_LOG",
63 "MENU_COMMAND_NETWORK_RECONNECT",
64 "MENU_COMMAND_STATISTICS",
65 "MENU_COMMAND_ABOUT",
66 "MENU_COMMAND_MODE_READING", /* toggle reading mode */
67 "MENU_COMMAND_OPEN",
68 NULL
69 };
70
71
72 /* print_license()
73 Print the GPL license and copyright.
74
75 If you've made substantial modifications to this program add an additional copyright with your name.
76 */
print_license(void)77 void print_license( void )
78 {
79 printf( "-\n" );
80 /* Example copyright notice. Leave this example intact. Copy it below to use as a template.
81 printf( "Copyright (C) 2012 Your Name <your@email> \n" );
82 */
83 printf(
84 "Copyright (C) 2012 Jay Satiro <raysatiro@yahoo.com> \n"
85 "All rights reserved. License GPLv3+: GNU GPL version 3 or later \n"
86 "<http://www.gnu.org/licenses/gpl.html>. \n"
87 "This is free software: you are free to change and redistribute it. \n"
88 "There is NO WARRANTY, to the extent permitted by law. \n"
89 );
90 printf( "-\n" );
91
92 return;
93 }
94
95
print_commands(void)96 void print_commands( void )
97 {
98 int i;
99
100 for( i = 0; command[ i ]; ++i )
101 printf( "%s\n", command[ i ] );
102
103 return;
104 }
105
106
show_usage_and_exit(void)107 void show_usage_and_exit( void )
108 {
109 printf( "Specify one of the following commands:\n" );
110 print_commands();
111 exit( 1 );
112 }
113
114
find_command(const char * str)115 int find_command( const char *str )
116 {
117 int i;
118
119 if( !str )
120 return -1;
121
122 for( i = 0; command[ i ]; ++i )
123 {
124 if( !_stricmp( str, command[ i ] ) )
125 return i;
126 }
127
128 return -1;
129 }
130
131
main(int argc,char ** argv)132 int main( int argc, char **argv )
133 {
134 int cmd = -1;
135 HWND hwnd = NULL;
136
137
138 print_license();
139 printf( "\n" );
140
141 if( argc < 2 )
142 show_usage_and_exit();
143
144 cmd = find_command( argv[ 1 ] );
145 if( cmd == -1 )
146 show_usage_and_exit();
147
148 hwnd = FindWindowEx( NULL, NULL, "gdkWindowToplevel", "Workrave" );
149 if( !hwnd )
150 {
151 printf( "Error: Workrave window not found!\n" );
152 return 1;
153 }
154
155 printf( "Sending Workrave command %s...\n", command[ cmd ] );
156 fflush( stdout );
157
158 SetLastError( 0 );
159 if( SendMessageTimeout( hwnd, WM_USER, cmd, 0, SMTO_BLOCK, 15000, NULL ) )
160 {
161 printf( "Command sent.\n" );
162 return 0;
163 }
164
165 if( GetLastError() == ERROR_TIMEOUT )
166 printf( "Error: SendMessageTimeout() timed out!\n" );
167 else
168 printf( "Error: SendMessageTimeout() failed!\n" );
169
170 return 1;
171 }
172