|
| 1 | +#ifndef __COMFORTABLE_SWIPE__service_status__ |
| 2 | +#define __COMFORTABLE_SWIPE__service_status__ |
| 3 | + |
| 4 | +/* |
| 5 | +Comfortable Swipe |
| 6 | +by Rico Tiongson |
| 7 | +
|
| 8 | +This program is free software: you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation, either version 3 of the License, or |
| 11 | +(at your option) any later version. |
| 12 | +
|
| 13 | +This program is distributed in the hope that it will be useful, |
| 14 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +GNU General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU General Public License |
| 19 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | +*/ |
| 21 | + |
| 22 | +#include "../index.hpp" |
| 23 | +#include <stdexcept> // std::runtime_error |
| 24 | +#include <unistd.h> // popen, pclose, getpid, access, F_OK |
| 25 | +#include <memory> // std::unique_ptr |
| 26 | +#include <array> // std::array |
| 27 | +#include <cstdlib> // std::atoi |
| 28 | +#include <cstdio> // FILE, std::feof, std::fgets, std::printf |
| 29 | + |
| 30 | +namespace comfortable_swipe::service |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Restarts the comfortable-swipe service. |
| 34 | + */ |
| 35 | + void status() |
| 36 | + { |
| 37 | + // check if comfortable-swipe is running |
| 38 | + bool running = false; |
| 39 | + std::array<char, 128> buffer; |
| 40 | + std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("pgrep -f comfortable-swipe", "r"), pclose); |
| 41 | + if (pipe && !std::feof(pipe.get()) && std::fgets(buffer.data(), buffer.size(), pipe.get()) != NULL) |
| 42 | + { |
| 43 | + int pid = std::atoi(buffer.data()); |
| 44 | + if (pid != getpid()) |
| 45 | + running = true; |
| 46 | + } |
| 47 | + |
| 48 | + // check if autostart is on |
| 49 | + auto autostart_f = comfortable_swipe::util::autostart_filename(); |
| 50 | + bool autostart_on = access(autostart_f, F_OK) != -1; |
| 51 | + |
| 52 | + // print status |
| 53 | + std::printf("program is %s\n", running ? "ON" : "OFF"); |
| 54 | + std::printf("autostart is %s\n", autostart_on ? "ON" : "OFF"); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#endif /* __COMFORTABLE_SWIPE__service_restart__ */ |
0 commit comments