xscreen_usb_unlocker 0.90.2 → 0.91.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,31 +10,31 @@ This will work best with a device that presents a unique serial number, and you
10
10
 
11
11
  ## Usage
12
12
 
13
- To make sure it's not any device plugged or unpluged, you have to supply either a serial or a device id to scan for.
13
+ ### Automatic
14
14
 
15
- For now, the serial can be found with:
15
+ To try to detect devices do:
16
16
 
17
- $ lsusb -v |grep Serial
17
+ $ xscreen_usb_unlocker --select-device
18
18
 
19
- Once you find the device (if it has a serial, most phone's do.)
19
+ Obviously, the device has to be plugged in for this to work. Give it a try and let me know if anything doesn't work. It's interactive and will save a config file for you.
20
20
 
21
- $ xscreen_usb_unlocker -s SERIAL
21
+ ### Manual
22
22
 
23
- If it doesn't provide a serial, you can also use a device id. Either the vendor, device or both can be supplied in a `vendor:device` format.
23
+ You can also do all this by hand via methods like this via the output of lsusb.
24
24
 
25
- To find these just run `lsusb` and look for the `1234:ABCD` piece, that is your device id.
25
+ Once you find the device (if it has a serial, most phone's do.)
26
26
 
27
- $ xscreen_usb_unlocker -d 1234:ABCD
27
+ $ xscreen_usb_unlocker -s SERIAL
28
28
 
29
29
  You can also save these so you don't have to retype them, or have them in your history:
30
30
 
31
31
  $ xscreen_usb_unlocker -s SERIAL -d 1234:ABCD --save-config
32
32
 
33
- Last, it can be daemonized, if this happens it will write a log to your homedirectory in: `~/.logs/xscreen_usb_unlocker.log`. After you've saved your config, this is the simplest way to enable/disable it.
33
+ ### Running
34
34
 
35
- $ xscreen_usb_unlocker -D
35
+ Last, it can be daemonized, if this happens it will write a log to your homedirectory in: `~/.logs/xscreen_usb_unlocker.log`.
36
36
 
37
- You can control logging and the logfile as well, likely I will disable the log for the 1.0 release.
37
+ $ xscreen_usb_unlocker -D
38
38
 
39
39
  If you unlock xscreensaver by hand, you are not disabling this, the next time you plug/unplug your usb device, it will lock again. This is useful if for some reason your usb device isn't available, you can still use your system as normal.
40
40
 
@@ -48,7 +48,6 @@ All it does is start/kill (safely) xscreensaver based on scanning for usb device
48
48
 
49
49
  ## TODO:
50
50
 
51
- * Add a --kill option to remove the other daemonizes.
52
51
  * Add a --name to trap for the device name (regex? substring?)
53
52
  * Add basic device detection to print out a pretty table to help configure it (offer options and save automatically?)
54
53
 
@@ -0,0 +1,10 @@
1
+ # Release Notes
2
+
3
+ #### 0.91.0
4
+
5
+ * Added a way to select devices interactively. Try --select-device to see.
6
+ * Added -(-k)ill-daemon to stop a background copy of xscreen_usb_unlocker
7
+ * Dropped auto daemonize when a config file is found.
8
+ * Updated the README with clearer examples.
9
+ * Added this RELEASE file.
10
+
@@ -5,79 +5,69 @@ Options.banner = "Usage: Used to lock/unlock xscreensaver based on usb device id
5
5
  Options.on("-s", "--serial SERIAL", "make sure the device matches this serial.") { |s| Options[:serial] = s}
6
6
  Options.on("-d", "--device DEVICE", "make sure it is this specific device.") { |d| Options[:device] = d}
7
7
  Options.on("-D", "--daemonize", "Daemonize this process in the background.") { |d| Options[:daemonize] = true}
8
+ Options.on("-k", "--kill-daemon", "Kill any pids available in memory.") { |d| Options[:kill_daemon] = true }
9
+ Options.on("--select-device", "Select a device to lock the screen with..") { |d| Options[:select_device] = true}
8
10
  Options.on("--save-config", "Save the device and serial to a XscreenUsbUnlocker::Config file and exit.") { |d| Options[:save_config] = true}
9
11
  Options.parse!
10
12
 
11
- if !Options[:device] && !Options[:serial]
12
- puts 'Must supply a device or serial to look for.'
13
- exit
14
- end
15
-
16
- def lock_screen
17
- Log.info 'locking'
18
- p = spawn "xscreensaver -no-splash"
19
- Process.detach p
20
- %x[xscreensaver-command -lock]
21
- end
22
-
23
- def unlock_screen
24
- Log.info 'unlocking'
25
-
26
- if xscreensaver_pids
27
- xscreensaver_pids.each do |p|
28
- Log.info "Appears to be pid: #{p.pid}"
29
- Process.kill "QUIT", p.pid
30
- end
31
- end
32
- end
33
-
34
- def xscreensaver_running?
35
- xscreensaver_pids.any?
36
- end
37
-
38
- def xscreensaver_pids
39
- Sys::ProcTable.ps.select{|x| x.cmdline.include?("xscreensaver") && !x.cmdline.include?("ruby")}
40
- end
41
13
 
42
- def plugged_in?
14
+ if Options[:select_device]
43
15
  usb = LIBUSB::Context.new
44
- options_hash = {}
16
+ devices = usb.devices.select{|x| x.bDeviceClass != 9} # this probably doesn't need to be a '9' but I haven't looked it up yet.
17
+ if devices.any?
18
+ devices.each do |d|
19
+ puts "(#{devices.index d}) #{d.manufacturer} #{d.product}"
20
+ end
21
+ puts "Which device would you like to lock with?"
45
22
 
46
- if Options[:device]
47
- v, p = Options[:device].split(":")
48
- options_hash[:idVendor] = v.hex if v && !v.empty?
49
- options_hash[:idProduct] = p.hex if p && !p.empty?
50
- end
23
+ print "-> "
24
+ c = STDIN.getc
51
25
 
52
- devices = usb.devices(options_hash)
53
- return true if devices.select { |d| d.serial_number == Options[:serial]}.any?
54
- false
55
- end
26
+ d = devices[c.to_i]
27
+ if !d
28
+ puts "No such selection #{c}"
29
+ exit
30
+ end
31
+ puts "Using #{d.product} to lock/unlock."
32
+ new_config = XscreenUsbUnlocker::ConfigBlob.new(load = false)
56
33
 
57
- def toggle_lock
58
- if plugged_in?
59
- Log.info 'unlock requested'
60
- unlock_screen
34
+ if d.serial_number == "?"
35
+ puts "Warning, this device doesn't appear to have a serial number, anybody with the same device will be able to unlock your screen."
36
+ else
37
+ new_config["serial"] = d.serial_number
38
+ end
39
+ new_config["device"] = "%04x:%04x" % [d.idVendor, d.idProduct]
40
+ new_config.save!
41
+ puts "Saved configuration to #{new_config.file}"
42
+ exit
61
43
  else
62
- Log.info 'lock request'
63
- lock_screen
44
+ puts "You don't have anything plugged in via USB, is that even possible nowadays?"
64
45
  end
46
+ exit
65
47
  end
66
48
 
67
- # did we pry?
68
- Options.on_pry
49
+ if !Options[:device] && !Options[:serial]
50
+ puts 'Must supply a device or serial to look for.'
51
+ exit
52
+ end
69
53
 
70
54
  # should we save our config and bail?
71
55
  if Options[:save_config]
72
56
  XscreenUsbUnlocker::Config["serial"] = Options[:serial] if Options[:serial]
73
57
  XscreenUsbUnlocker::Config["device"] = Options[:device] if Options[:device]
74
58
  XscreenUsbUnlocker::Config.save!
75
- puts "Saved configuration to #{Config.file}"
59
+ puts "Saved configuration to #{XscreenUsbUnlocker::Config.file}"
60
+ exit
61
+ end
62
+
63
+ if Options[:kill_daemon]
64
+ App.kill_daemon
65
+ puts "Killing daemon."
76
66
  exit
77
67
  end
78
68
 
79
69
  # kill a running copy of xscreensaver
80
- if xscreensaver_running?
70
+ if XscreenUsbUnlocker.xscreensaver_running?
81
71
  Log.info "xscreensaver appears to be running, killing so we can trap it."
82
72
  %x[killall -QUIT xscreensaver]
83
73
  end
@@ -86,15 +76,15 @@ end
86
76
  Notifier = INotify::Notifier.new
87
77
  Dir.glob("/dev/bus/usb/*").each do |d|
88
78
  Notifier.watch(d, :delete, :create) do
89
- toggle_lock
79
+ XscreenUsbUnlocker.toggle_lock
90
80
  end
91
81
  end
92
82
 
93
83
  # fire off the lock cycle once.
94
- toggle_lock
84
+ XscreenUsbUnlocker.toggle_lock
95
85
 
96
86
  # start the notifier, which will fire off callbacks as needed.
97
- if Options[:daemonize] || File.file?(XscreenUsbUnlocker::Config.file)
87
+ if Options[:daemonize]
98
88
  Log.debug "Opening logfile."
99
89
  Log.filename "/home/ebrodeur/.logs/xscreensaver_unlocker.log"
100
90
  Log.debug "Daemonizing."
@@ -102,3 +92,4 @@ if Options[:daemonize] || File.file?(XscreenUsbUnlocker::Config.file)
102
92
  else
103
93
  Notifier.run
104
94
  end
95
+
@@ -10,5 +10,54 @@ require 'xscreen_usb_unlocker/optparser'
10
10
  require "xscreen_usb_unlocker/version"
11
11
 
12
12
  module XscreenUsbUnlocker
13
- # Your code goes here...
13
+ def self.lock_screen
14
+ Log.info 'locking'
15
+ p = spawn "xscreensaver -no-splash"
16
+ Process.detach p
17
+ %x[xscreensaver-command -lock]
18
+ end
19
+
20
+ def self.unlock_screen
21
+ Log.info 'unlocking'
22
+
23
+ if xscreensaver_pids
24
+ xscreensaver_pids.each do |p|
25
+ Log.info "Appears to be pid: #{p.pid}"
26
+ Process.kill "QUIT", p.pid
27
+ end
28
+ end
29
+ end
30
+
31
+ def self.xscreensaver_running?
32
+ xscreensaver_pids.any?
33
+ end
34
+
35
+ def self.xscreensaver_pids
36
+ Sys::ProcTable.ps.select{|x| x.cmdline.include?("xscreensaver") && !x.cmdline.include?("ruby")}
37
+ end
38
+
39
+ def self.plugged_in?
40
+ usb = LIBUSB::Context.new
41
+ options_hash = {}
42
+
43
+ if Options[:device]
44
+ v, p = Options[:device].split(":")
45
+ options_hash[:idVendor] = v.hex if v && !v.empty?
46
+ options_hash[:idProduct] = p.hex if p && !p.empty?
47
+ end
48
+
49
+ devices = usb.devices(options_hash)
50
+ return true if devices.select { |d| d.serial_number == Options[:serial]}.any?
51
+ false
52
+ end
53
+
54
+ def self.toggle_lock
55
+ if plugged_in?
56
+ Log.info 'unlock requested'
57
+ unlock_screen
58
+ else
59
+ Log.info 'lock request'
60
+ lock_screen
61
+ end
62
+ end
14
63
  end
@@ -2,11 +2,11 @@ require 'fileutils'
2
2
 
3
3
  module XscreenUsbUnlocker
4
4
  class ConfigBlob < Hash
5
- BaseDir = "/home/ebrodeur/.config/erniebrodeur"
5
+ BaseDir = "/home/ebrodeur/.config"
6
6
 
7
- def initialize
7
+ def initialize(load = true)
8
8
  FileUtils.mkdir_p BaseDir if !Dir.exist? BaseDir
9
- load
9
+ load if load
10
10
  end
11
11
 
12
12
  def file
@@ -69,7 +69,10 @@ module XscreenUsbUnlocker
69
69
  end
70
70
 
71
71
  def create_logger
72
- FileUtils.mkdir_p File.dirname @options[:filename] if File.file? @options[:filename]
72
+ if @options[:filename] != STDOUT && !File.file?(@options[:filename])
73
+ FileUtils.mkdir_p File.dirname @options[:filename]
74
+ end
75
+
73
76
  @l = ::Logger.new(@options[:filename])
74
77
  @l.level = @options[:level]
75
78
 
@@ -86,11 +89,11 @@ end
86
89
 
87
90
  # better way to do this with: Kernel.module_eval def puts ...
88
91
  module Kernel
89
- def puts (s)
92
+ def puts (*args)
90
93
  if Log.override_puts?
91
94
  Log.info s
92
95
  else
93
- Kernel::puts s
96
+ Kernel::puts args
94
97
  end
95
98
  end
96
99
  end
@@ -38,13 +38,6 @@ module XscreenUsbUnlocker
38
38
  end
39
39
  end
40
40
 
41
- def on_pry
42
- if @options[:pry]
43
- require 'pry'
44
- binding.pry
45
- end
46
- end
47
-
48
41
  def [](k)
49
42
  @options[k]
50
43
  end
@@ -1,3 +1,3 @@
1
1
  module XscreenUsbUnlocker
2
- VERSION = "0.90.2"
2
+ VERSION = "0.91.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xscreen_usb_unlocker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.90.2
4
+ version: 0.91.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-14 00:00:00.000000000 Z
12
+ date: 2012-07-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rb-inotify
@@ -104,6 +104,7 @@ files:
104
104
  - Gemfile
105
105
  - LICENSE
106
106
  - README.md
107
+ - RELEASE.md
107
108
  - Rakefile
108
109
  - bin/xscreen_usb_unlocker
109
110
  - lib/xscreen_usb_unlocker.rb