File: send_workrave_command.c

package info (click to toggle)
workrave 1.10.54-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,992 kB
  • sloc: cpp: 46,596; ansic: 9,760; sh: 2,754; javascript: 1,935; makefile: 1,452; python: 973; xml: 672; objc: 455; perl: 113; cs: 70; sed: 16
file content (171 lines) | stat: -rw-r--r-- 3,933 bytes parent folder | download | duplicates (7)
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
/*
Copyright (C) 2012 Jay Satiro <raysatiro@yahoo.com>
All rights reserved.

This file is part of Workrave.

Workrave 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.

Workrave 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 Workrave.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
This is a simple win32 example that will send a menu command to Workrave from the command line.

Visual Studio:
cl /W4 send_workrave_command.c

MinGW or cygwin:
gcc -Wall -Wextra -Wno-unknown-pragmas -o send_workrave_command.exe send_workrave_command.c


x:\Workrave\other>send_workrave_command.exe MENU_COMMAND_MODE_NORMAL
-
Copyright (C) 2012 Jay Satiro <raysatiro@yahoo.com>
All rights reserved. License GPLv3+: GNU GPL version 3 or later
<http://www.gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
-

Sending Workrave command MENU_COMMAND_MODE_NORMAL...
Command sent.
*/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma comment( lib, "user32.lib" )


const char *command[] = 
{
	"MENU_COMMAND_PREFERENCES",
	"MENU_COMMAND_EXERCISES",
	"MENU_COMMAND_REST_BREAK",
	"MENU_COMMAND_MODE_NORMAL",
	"MENU_COMMAND_MODE_QUIET",
	"MENU_COMMAND_MODE_SUSPENDED",
	"MENU_COMMAND_NETWORK_CONNECT",
	"MENU_COMMAND_NETWORK_DISCONNECT",
	"MENU_COMMAND_NETWORK_LOG",
	"MENU_COMMAND_NETWORK_RECONNECT",
	"MENU_COMMAND_STATISTICS",
	"MENU_COMMAND_ABOUT",
	"MENU_COMMAND_MODE_READING", /* toggle reading mode */
	"MENU_COMMAND_OPEN",
	NULL
};


/* print_license()
Print the GPL license and copyright.

If you've made substantial modifications to this program add an additional copyright with your name.
*/
void print_license( void )
{
	printf( "-\n" );
	/* Example copyright notice. Leave this example intact. Copy it below to use as a template.
	printf( "Copyright (C) 2012 Your Name <your@email> \n" );
	*/
	printf( 
		"Copyright (C) 2012 Jay Satiro <raysatiro@yahoo.com> \n"
		"All rights reserved. License GPLv3+: GNU GPL version 3 or later \n"
		"<http://www.gnu.org/licenses/gpl.html>. \n"
		"This is free software: you are free to change and redistribute it. \n"
		"There is NO WARRANTY, to the extent permitted by law. \n"
	);
	printf( "-\n" );
	
	return;
}


void print_commands( void )
{
	int i;
	
	for( i = 0; command[ i ]; ++i )
		printf( "%s\n", command[ i ] );
	
	return;
}


void show_usage_and_exit( void )
{
	printf( "Specify one of the following commands:\n" );
	print_commands();
	exit( 1 );
}


int find_command( const char *str )
{
	int i;
	
	if( !str )
		return -1;
	
	for( i = 0; command[ i ]; ++i )
	{
		if( !_stricmp( str, command[ i ] ) )
			return i;
	}
	
	return -1;
}


int main( int argc, char **argv )
{
	int cmd = -1;
	HWND hwnd = NULL;
	
	
	print_license();
	printf( "\n" );
	
	if( argc < 2 )
		show_usage_and_exit();
	
	cmd = find_command( argv[ 1 ] );
	if( cmd == -1 )
		show_usage_and_exit();
	
	hwnd = FindWindowEx( NULL, NULL, "gdkWindowToplevel", "Workrave" );
	if( !hwnd )
	{
		printf( "Error: Workrave window not found!\n" );
		return 1;
	}
	
	printf( "Sending Workrave command %s...\n", command[ cmd ] );
	fflush( stdout );
	
	SetLastError( 0 );
	if( SendMessageTimeout( hwnd, WM_USER, cmd, 0, SMTO_BLOCK, 15000, NULL ) )
	{
		printf( "Command sent.\n" );
		return 0;
	}
	
	if( GetLastError() == ERROR_TIMEOUT )
		printf( "Error: SendMessageTimeout() timed out!\n" );
	else
		printf( "Error: SendMessageTimeout() failed!\n" );
	
	return 1;
}