Improve the kyboard lock and shortcut logic

This commit is contained in:
Gobinath 2017-07-06 16:49:44 -04:00
parent eedb2c53c7
commit ca65add8e8
2 changed files with 25 additions and 20 deletions

View File

@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import gi, threading, logging import gi, threading, logging, time
from Xlib.display import Display, X from Xlib.display import Display, X
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib from gi.repository import Gtk, Gdk, GLib
@ -41,6 +41,7 @@ class BreakScreen:
self.count_labels = [] self.count_labels = []
self.glade_file = glade_file self.glade_file = glade_file
self.enable_shortcut = False self.enable_shortcut = False
self.display = Display()
# Initialize the theme # Initialize the theme
css_provider = Gtk.CssProvider() css_provider = Gtk.CssProvider()
@ -204,32 +205,36 @@ class BreakScreen:
""" """
logging.info("Lock the keyboard") logging.info("Lock the keyboard")
self.lock_keyboard = True self.lock_keyboard = True
display = Display()
root = display.screen().root # Grab the keyboard
# Grap the keyboard root = self.display.screen().root
root.grab_keyboard(owner_events=False, pointer_mode=X.GrabModeAsync, keyboard_mode=X.GrabModeAsync, time=X.CurrentTime) root.change_attributes(event_mask = X.KeyPressMask|X.KeyReleaseMask)
root.grab_keyboard(True, X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)
# Consume keyboard events # Consume keyboard events
while self.lock_keyboard: while self.lock_keyboard:
event = display.next_event() if self.display.pending_events() > 0:
display.allow_events(mode=X.AsyncBoth, time=X.CurrentTime) # Avoid waiting for next event by checking pending events
if self.enable_shortcut and event.type == X.KeyPress: event = self.display.next_event()
if event.detail == self.keycode_shortcut_skip: if self.enable_shortcut and event.type == X.KeyPress:
self.skip_break() if event.detail == self.keycode_shortcut_skip:
break self.skip_break()
elif self.enable_postpone and event.detail == self.keycode_shortcut_postpone: break
self.postpone_break() elif self.enable_postpone and event.detail == self.keycode_shortcut_postpone:
break self.postpone_break()
break
# Ungrap the keyboard else:
logging.info("Unlock the keyboard") # Reduce the CPU usage by sleeping for a second
display.ungrab_keyboard(X.CurrentTime) time.sleep(1)
display.flush()
def __release_keyboard(self): def __release_keyboard(self):
""" """
Release the locked keyboard. Release the locked keyboard.
""" """
logging.info("Unlock the keyboard")
self.lock_keyboard = False self.lock_keyboard = False
self.display.ungrab_keyboard(X.CurrentTime)
self.display.flush()
def __destroy_all_screens(self): def __destroy_all_screens(self):
""" """

View File

@ -53,7 +53,7 @@
<property name="page_increment">5</property> <property name="page_increment">5</property>
</object> </object>
<object class="GtkAdjustment" id="adjust_disable_keyboard_shortcut_duration"> <object class="GtkAdjustment" id="adjust_disable_keyboard_shortcut_duration">
<property name="lower">1</property> <property name="lower">0</property>
<property name="upper">15</property> <property name="upper">15</property>
<property name="step_increment">1</property> <property name="step_increment">1</property>
<property name="page_increment">5</property> <property name="page_increment">5</property>