fidget 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +112 -0
- data/bin/fidget +68 -0
- data/lib/fidget.rb +38 -0
- data/lib/fidget/platform/darwin.rb +53 -0
- data/lib/fidget/platform/linux.rb +75 -0
- data/lib/fidget/platform/windows.rb +77 -0
- data/lib/fidget/version.rb +3 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ce823012d9505a1ffb01dc781c1481df5e507532
|
4
|
+
data.tar.gz: d328d92527cbeba4a359a0a270c8cb9d0de36c09
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 63bad66d8091d5a84099ae4b64e8d8bc230da6f4ca852bc7b7f6bca9d7184cc1fdadf9216404e1d22cc7fb31c59f58bb6e99ee64c5605ef72bb89779fffe0621
|
7
|
+
data.tar.gz: 941ff8d08cffa1cc6792f0a74b4808c5ea969271213936369e486c74f5cb1d3f30bc6534f76ebffabe0b0314801434775742f5928dd427711f0b1c7a8c75c7f5
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Fidget
|
2
|
+
A cross platform tool to prevent your computer from sleeping.
|
3
|
+
|
4
|
+
## Overview
|
5
|
+
|
6
|
+
Usage : `fidget [-o <options>] [command]`
|
7
|
+
|
8
|
+
Sleep options may very between platforms. Each platform will include at least
|
9
|
+
the following core options:
|
10
|
+
|
11
|
+
* `display` will prevent the display from sleeping.
|
12
|
+
* `sleep` prevent the system from sleeping.
|
13
|
+
* `all` to declare all known options.
|
14
|
+
|
15
|
+
Additional OS X options:
|
16
|
+
|
17
|
+
* `idle` will prevent the system from idle sleeping.
|
18
|
+
* `disk` will prevent the disk from sleeping.
|
19
|
+
|
20
|
+
Additional Windows options:
|
21
|
+
|
22
|
+
* `away` enables a low-power, but not sleeping, mode on Windows.
|
23
|
+
|
24
|
+
Additional Linux options:
|
25
|
+
|
26
|
+
* `blanking` disables terminal blanking on Linux.
|
27
|
+
|
28
|
+
If you pass the a command, then Fidget will execute that command and prevent
|
29
|
+
the system from sleeping until that command terminates. If you do not provide a
|
30
|
+
command, then Fidget will prevent sleeping until you press Ctrl-C to terminate.
|
31
|
+
|
32
|
+
## Library Usage
|
33
|
+
|
34
|
+
There are three methods you may use in your programs. The `options` parameter
|
35
|
+
should be either an array or just a list of options and can be passed as strings
|
36
|
+
or symbols.
|
37
|
+
|
38
|
+
* `Fidget.prevent_sleep(options)`
|
39
|
+
* Stops the computer from sleeping.
|
40
|
+
* Can accept a block, in which case sleep will be inhibited during the execution
|
41
|
+
of the block and resumed after.
|
42
|
+
* `Fidget.allow_sleep`
|
43
|
+
* Resume any previously inhibited sleep.
|
44
|
+
* `Fidget.current_process(options)`
|
45
|
+
* Shortcut method to inhibit sleep during the execution of the current process.
|
46
|
+
|
47
|
+
### Example usages:
|
48
|
+
|
49
|
+
Default options, with a block:
|
50
|
+
|
51
|
+
#! /usr/bin/env ruby
|
52
|
+
require 'fidget'
|
53
|
+
|
54
|
+
Fidget.prevent_sleep do
|
55
|
+
100.times do
|
56
|
+
print '.'
|
57
|
+
sleep 1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
Define sleep options to inhibit, with a block:
|
62
|
+
|
63
|
+
#! /usr/bin/env ruby
|
64
|
+
require 'fidget'
|
65
|
+
|
66
|
+
Fidget.prevent_sleep(:display, :sleep) do
|
67
|
+
100.times do
|
68
|
+
print '.'
|
69
|
+
sleep 1
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
No block, just calling methods:
|
74
|
+
|
75
|
+
#! /usr/bin/env ruby
|
76
|
+
require 'fidget'
|
77
|
+
|
78
|
+
Fidget.prevent_sleep(:display, :sleep)
|
79
|
+
|
80
|
+
100.times do
|
81
|
+
print '.'
|
82
|
+
sleep 1
|
83
|
+
end
|
84
|
+
|
85
|
+
Fidget.allow_sleep
|
86
|
+
|
87
|
+
Simply prevent the computer from sleeping while the current process is running:
|
88
|
+
|
89
|
+
#! /usr/bin/env ruby
|
90
|
+
require 'fidget'
|
91
|
+
|
92
|
+
Fidget.current_process
|
93
|
+
|
94
|
+
100.times do
|
95
|
+
print '.'
|
96
|
+
sleep 1
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
## Limitations
|
101
|
+
|
102
|
+
This is super early in development and has not yet been battle tested.
|
103
|
+
|
104
|
+
## Disclaimer
|
105
|
+
|
106
|
+
I take no liability for the use of this tool.
|
107
|
+
|
108
|
+
Contact
|
109
|
+
-------
|
110
|
+
|
111
|
+
binford2k@gmail.com
|
112
|
+
|
data/bin/fidget
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'optparse'
|
5
|
+
require 'fidget'
|
6
|
+
|
7
|
+
options = []
|
8
|
+
optparse = OptionParser.new { |opts|
|
9
|
+
opts.banner = "Usage : fidget [-o <options>] [command]
|
10
|
+
|
11
|
+
Cross platform utility to prevent your system from sleeping.
|
12
|
+
|
13
|
+
Sleep options may very between platforms. Each platform will include at least
|
14
|
+
the following core options:
|
15
|
+
* 'display' will prevent the display from sleeping.
|
16
|
+
* 'sleep' prevent the system from sleeping.
|
17
|
+
* 'all' to declare all known options.
|
18
|
+
|
19
|
+
Additional OS X options:
|
20
|
+
* 'idle' will prevent the system from idle sleeping.
|
21
|
+
* 'disk' will prevent the disk from sleeping.
|
22
|
+
|
23
|
+
Additional Windows options:
|
24
|
+
* 'away' enables a low-power, but not sleeping, mode on Windows.
|
25
|
+
|
26
|
+
Additional Linux options:
|
27
|
+
* 'blanking' disables terminal blanking on Linux.
|
28
|
+
|
29
|
+
If you pass the a command, then Fidget will execute that command and prevent
|
30
|
+
the system from sleeping until that command terminates. If you do not provide a
|
31
|
+
command, then Fidget will prevent sleeping until you press Ctrl-C to terminate.
|
32
|
+
|
33
|
+
"
|
34
|
+
opts.on("-o OPTIONS", "--options OPTIONS", "Comma separated list of sleep options.") do |arg|
|
35
|
+
options = arg.split(',').map{ |e| e.to_sym }
|
36
|
+
end
|
37
|
+
|
38
|
+
opts.on("-d", "--debug", "Display or log debugging messages") do
|
39
|
+
loglevel = Logger::DEBUG
|
40
|
+
end
|
41
|
+
|
42
|
+
opts.separator('')
|
43
|
+
|
44
|
+
opts.on("-h", "--help", "Displays this help") do
|
45
|
+
puts
|
46
|
+
puts opts
|
47
|
+
puts
|
48
|
+
exit
|
49
|
+
end
|
50
|
+
}
|
51
|
+
optparse.parse!
|
52
|
+
|
53
|
+
begin
|
54
|
+
if ARGV.size > 0
|
55
|
+
command = ARGV.join ' '
|
56
|
+
|
57
|
+
Fidget.prevent_sleep(options) do
|
58
|
+
system(command)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
Fidget.current_process(options)
|
62
|
+
puts 'System has been prevented from sleeping.'
|
63
|
+
print '- Press Ctrl-C to resume normal power management.'
|
64
|
+
sleep
|
65
|
+
end
|
66
|
+
rescue Interrupt => e
|
67
|
+
puts "Normal power management resumed."
|
68
|
+
end
|
data/lib/fidget.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class Fidget
|
2
|
+
case Gem::Platform.local.os
|
3
|
+
when /darwin/
|
4
|
+
require 'fidget/platform/darwin'
|
5
|
+
|
6
|
+
when /linux/
|
7
|
+
require 'fidget/platform/linux'
|
8
|
+
|
9
|
+
when /cygwin|mswin|mingw|bccwin|wince|emx/
|
10
|
+
require 'fidget/platform/windows'
|
11
|
+
|
12
|
+
when 'java'
|
13
|
+
STDERR.puts 'When running under jRuby, we cannot reliably manage power settings.'
|
14
|
+
require 'fidget/platform/null'
|
15
|
+
|
16
|
+
else
|
17
|
+
raise "Unknown platform: #{Gem::Platform.local.os}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.current_process(options = nil)
|
21
|
+
Fidget::Platform.current_process(options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.prevent_sleep(options = nil)
|
25
|
+
if block_given?
|
26
|
+
Fidget::Platform.prevent_sleep(options) do
|
27
|
+
yield
|
28
|
+
end
|
29
|
+
else
|
30
|
+
Fidget::Platform.prevent_sleep(options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.allow_sleep(handle)
|
35
|
+
Fidget::Platform.allow_sleep(handle)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class Fidget::Platform
|
2
|
+
def self.current_process(options)
|
3
|
+
pid = spawn("caffeinate #{arguments(options)}")
|
4
|
+
|
5
|
+
at_exit do
|
6
|
+
Process.kill('TERM', pid)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.prevent_sleep(options)
|
11
|
+
pid = spawn("caffeinate #{arguments(options)}")
|
12
|
+
|
13
|
+
if block_given?
|
14
|
+
yield
|
15
|
+
Process.kill('TERM', pid)
|
16
|
+
else
|
17
|
+
@@pids ||= []
|
18
|
+
@@pids << pid
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.allow_sleep
|
23
|
+
return unless @@pids
|
24
|
+
|
25
|
+
@@pids.each do |pid|
|
26
|
+
Process.kill('TERM', pid)
|
27
|
+
end
|
28
|
+
|
29
|
+
@@pids = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.arguments(*options)
|
33
|
+
options.flatten!
|
34
|
+
options.compact!
|
35
|
+
return '-dism' if options == [:all]
|
36
|
+
|
37
|
+
terms = {
|
38
|
+
:display => 'd',
|
39
|
+
:idle => 'i',
|
40
|
+
:disk => 'm',
|
41
|
+
:sleep => 's',
|
42
|
+
}
|
43
|
+
options.each do |opt|
|
44
|
+
STDERR.puts "Fidget: option #{opt} is not supported on OS X" unless terms.include? opt
|
45
|
+
end
|
46
|
+
|
47
|
+
args = options.map { |element| terms[element] }.compact.join
|
48
|
+
|
49
|
+
return "-#{args}" unless args.empty?
|
50
|
+
end
|
51
|
+
private_class_method :arguments
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
class Fidget::Platform
|
2
|
+
def self.current_process(options)
|
3
|
+
options = munge(options)
|
4
|
+
suspend(options)
|
5
|
+
|
6
|
+
at_exit do
|
7
|
+
resume(options)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.prevent_sleep(options)
|
12
|
+
options = munge(options)
|
13
|
+
suspend(options)
|
14
|
+
|
15
|
+
if block_given?
|
16
|
+
yield
|
17
|
+
resume(options)
|
18
|
+
end
|
19
|
+
|
20
|
+
pid
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.allow_sleep(handle)
|
24
|
+
options = munge(options)
|
25
|
+
resume(options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.munge(*options)
|
29
|
+
options.flatten!
|
30
|
+
options.compact!
|
31
|
+
options = [:display] if options.empty?
|
32
|
+
options = [:display, :blanking] if options == [:all]
|
33
|
+
options.each do |opt|
|
34
|
+
STDERR.puts "Fidget: option {opt} is not supported on Linux" unless [:display, :blanking].include? opt
|
35
|
+
end
|
36
|
+
options
|
37
|
+
end
|
38
|
+
#private_class_method :munge
|
39
|
+
|
40
|
+
def self.root_win
|
41
|
+
ids = `xwininfo -root`.each_line.collect do |line|
|
42
|
+
next unless line =~ /Window id: (0x\d+)/
|
43
|
+
$1
|
44
|
+
end.compact
|
45
|
+
raise "Parsing xwininfo failed" unless ids.size == 1
|
46
|
+
ids.first
|
47
|
+
end
|
48
|
+
#private_class_method :root_win
|
49
|
+
|
50
|
+
def self.suspend(options)
|
51
|
+
if options.include? :display
|
52
|
+
system("xdg-screensaver suspend #{root_win}")
|
53
|
+
system('xset -dpms')
|
54
|
+
end
|
55
|
+
|
56
|
+
if options.include? :blanking
|
57
|
+
@@blanking ||= File.read('/sys/module/kernel/parameters/consoleblank').strip
|
58
|
+
system('setterm -blank 0 -powerdown 0')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
private_class_method :suspend
|
62
|
+
|
63
|
+
def self.resume(options)
|
64
|
+
if options.include? :display
|
65
|
+
system("xdg-screensaver resume #{root_win}")
|
66
|
+
system('xset dpms')
|
67
|
+
end
|
68
|
+
|
69
|
+
if options.include? :blanking
|
70
|
+
system("setterm -blank #{@@blanking} -powerdown #{@@blanking}")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
private_class_method :resume
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'Win32API'
|
2
|
+
|
3
|
+
class Fidget::Platform
|
4
|
+
ES_CONTINUOUS = 0x80000000
|
5
|
+
ES_SYSTEM_REQUIRED = 0x00000001
|
6
|
+
ES_DISPLAY_REQUIRED = 0x00000002
|
7
|
+
ES_AWAYMODE_REQUIRED = 0x00000040
|
8
|
+
|
9
|
+
KB_EVENT_KEYPRESS = 0
|
10
|
+
KB_EVENT_KEYUP = 2
|
11
|
+
KB_KEY_F24 = 0x87
|
12
|
+
|
13
|
+
def self.current_process(options)
|
14
|
+
set_state(options)
|
15
|
+
|
16
|
+
at_exit do
|
17
|
+
set_state(nil)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.prevent_sleep(options)
|
22
|
+
set_state(options)
|
23
|
+
|
24
|
+
if block_given?
|
25
|
+
yield
|
26
|
+
set_state(nil)
|
27
|
+
end
|
28
|
+
|
29
|
+
pid
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.allow_sleep(handle)
|
33
|
+
set_state(nil)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Set thread execution state, using information from
|
37
|
+
# https://msdn.microsoft.com/en-us/library/aa373208(VS.85).aspx
|
38
|
+
# http://stackoverflow.com/questions/4126136/stop-a-windows-7-pc-from-going-to-sleep-while-a-ruby-program-executes
|
39
|
+
def self.set_state(*options)
|
40
|
+
options.flatten!
|
41
|
+
options.compact!
|
42
|
+
options = [:display, :sleep] if options.empty?
|
43
|
+
options = [:display, :sleep, :away] if options == [:all]
|
44
|
+
|
45
|
+
terms = {
|
46
|
+
:display => ES_DISPLAY_REQUIRED,
|
47
|
+
:sleep => ES_SYSTEM_REQUIRED,
|
48
|
+
:away => ES_AWAYMODE_REQUIRED,
|
49
|
+
}
|
50
|
+
options.each do |opt|
|
51
|
+
STDERR.puts "Fidget: option #{opt} is not supported on Windows" unless terms.include? opt
|
52
|
+
end
|
53
|
+
|
54
|
+
# translate options to constants, then OR them all together
|
55
|
+
mode = options.map { |element| terms[element] }.reduce { |memo, element| memo|element } || 0
|
56
|
+
|
57
|
+
state = Win32API.new('kernel32','SetThreadExecutionState','L')
|
58
|
+
state.call(ES_CONTINUOUS|mode)
|
59
|
+
|
60
|
+
if options.include? :display
|
61
|
+
@@kb_poker = Thread.new do
|
62
|
+
kb = Win32API.new('user32.dll', 'keybd_event', 'nnnn', 'v')
|
63
|
+
loop do
|
64
|
+
kb.call(KB_KEY_F24, 0, KB_EVENT_KEYPRESS, nil)
|
65
|
+
kb.call(KB_KEY_F24, 0, KB_EVENT_KEYUP, nil)
|
66
|
+
sleep 30
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
if options.nil?
|
72
|
+
Thread.kill @@kb_poker if @@kb_poker
|
73
|
+
end
|
74
|
+
end
|
75
|
+
private_class_method :set_state
|
76
|
+
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fidget
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Ford
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |2
|
14
|
+
Fidget was inspired by the OS X commandline `caffeinate` tool, which in turn
|
15
|
+
was inspired by the Caffeine menubar tool. However, this tool is cross platform
|
16
|
+
and can be used as a Ruby library.
|
17
|
+
|
18
|
+
This will work on:
|
19
|
+
* Modern OS X
|
20
|
+
* Linux with xdg-screensaver (Xorg)
|
21
|
+
* Windows
|
22
|
+
email: binford2k@gmail.com
|
23
|
+
executables:
|
24
|
+
- fidget
|
25
|
+
extensions: []
|
26
|
+
extra_rdoc_files: []
|
27
|
+
files:
|
28
|
+
- README.md
|
29
|
+
- LICENSE
|
30
|
+
- bin/fidget
|
31
|
+
- lib/fidget/platform/darwin.rb
|
32
|
+
- lib/fidget/platform/linux.rb
|
33
|
+
- lib/fidget/platform/windows.rb
|
34
|
+
- lib/fidget/version.rb
|
35
|
+
- lib/fidget.rb
|
36
|
+
homepage: https://github.com/binford2k/fidget
|
37
|
+
licenses:
|
38
|
+
- Apache 2.0
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.0.14.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Cross platform tool to prevent your computer from sleeping.
|
60
|
+
test_files: []
|