6

I want to write code or a script to lock the PC when my C++ program receives a specific command from Ethernet.

Is there any way to show the Login screen from the command line? I want to lock the screen from a c++ code. Actually I want to do something like Ctrl + Alt + L programmatically (from a c++ code). Thanks

Edit: I found the way. For those who want to know, as Lubuntu uses LXDE desktop, it has some methods to do thing like it. The command for locking the screen is lxlock.

fossfreedom
  • 172,746
oMatrix
  • 65
  • 4
  • Not really a complete answer, but I would look for a way to do this with DBus bindings. This is very elegant as it's almost universal for every desktop environment. And LXDE supports DBus very well, looking at the source code. Try to find the Lock command using a dbus exlorer like qdbusviewer and a way to call the functions programmatically in C++. – gertvdijk Jan 19 '13 at 12:48

3 Answers3

1

In accordance with what @gertvdijk said, you can use DBus:

qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock

I have used that in my lock and off project.

0

Your question as it is written now is a bit unclear. I'll try to guess what you want and give you a couple answers to try.

  1. Ctrl + Alt + L will lock the screen. Everything is still running, you are logged in, but the screen is locked.
  2. Ctrl + Alt + T opens the Terminal, then type gnome-session-quit

Note that if #2 is what you want, this is an exact duplicate of this question 'How can you log out via the terminal', which is where I copied the second answer from.

Please edit your question so it is clearer, or so we can close it as a duplicate.

Tom Brossman
  • 13,111
  • 2
    I want to do something like Ctrl + Alt + L via a c++ code. I want to write a code to lock the PC when my program receives a specific command from ethernet. – oMatrix Jan 19 '13 at 11:54
  • @oMatrix I've tried editing your question for you to make it clearer. Please take a look and re-edit it if I missed anything or got it wrong. Also, this may be off-topic here, or at least a better fit on one of the programming-specific sites like stackoverflow. – Tom Brossman Jan 19 '13 at 12:03
  • Thanks for your help. I thought may be there's something like gnome-session-quit for locking the screen and as I'm using Lubuntu, I thought it's the best place to ask this question. And actually my question is not a programming related question in my opinion. Thanks again. – oMatrix Jan 19 '13 at 12:18
  • 3
    This question is on-topic. Questions for Users and Developers of Ubuntu and official derivatives. – Marco Ceppi Jan 19 '13 at 12:34
-3

I found the way. For those who want to know. As Lubuntu uses LXDE desktop, it has some methods to do thing like it. The command for locking the screen is lxlock.

oMatrix
  • 65
  • 4
  • 5
    Pardon me? You clearly stated in your question you were looking for a way to do this programmatically in C++ and now you answer your own question with a Shell script? (source of lxlock is here which lists the Dbus commands to send) And here is a tutorial on how to program with Dbus in C++. – gertvdijk Jan 19 '13 at 17:29
  • sorry? I think you should read the question with more attention. I wrote "I want to write code <<<>>> to lock the PC .....". So I think you shouldn't give me -1. I just wanted to lock the desktop from my C++ code. so I can use script, Dbus or any other way that works. – oMatrix Jan 20 '13 at 07:16
  • 2
    @oMatrix see your earlier comment on the answer of Tom Brossman. You said you needed C++ code. Clearly. You can't expect other users to answer your question when you change the question on such an important part by yourself. This deserves really -1's, sorry. You could have deleted your question and asked another one. – gertvdijk Jan 20 '13 at 19:26