-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cpp
More file actions
143 lines (128 loc) · 4.36 KB
/
plugin.cpp
File metadata and controls
143 lines (128 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Example Plugin Template for whatsmycli
*
* This is a simple example plugin that demonstrates:
* - Plugin API v2 with argument support
* - Basic plugin_run() implementation
* - Platform detection
* - Error handling
* - Return codes
*
* Copyright (C) 2025 enXov
* Licensed under GPLv3 - see LICENSE file
*/
#include <iostream>
#include <string>
// Platform detection
#ifdef _WIN32
#define PLATFORM_NAME "Windows"
#include <windows.h>
#elif __APPLE__
#define PLATFORM_NAME "macOS"
#include <unistd.h>
#include <sys/utsname.h>
#else
#define PLATFORM_NAME "Linux"
#include <unistd.h>
#include <sys/utsname.h>
#endif
/**
* Get system information (platform-specific)
*/
std::string get_system_info() {
std::string info;
#ifdef _WIN32
// Windows implementation
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
// Note: GetVersionEx is deprecated but used here for example simplicity
// Real plugins should use modern APIs
#pragma warning(disable: 4996)
if (GetVersionEx((OSVERSIONINFO*)&osvi)) {
info = "Windows " + std::to_string(osvi.dwMajorVersion) +
"." + std::to_string(osvi.dwMinorVersion);
} else {
info = "Windows (version unknown)";
}
#pragma warning(default: 4996)
#else
// Unix-like systems (Linux, macOS)
struct utsname buffer;
if (uname(&buffer) == 0) {
info = std::string(buffer.sysname) + " " +
std::string(buffer.release);
} else {
info = PLATFORM_NAME;
}
#endif
return info;
}
/**
* Plugin entry point with argument support (API v2)
*
* This function is called by whatsmy when the plugin is executed.
*
* @param argc Number of arguments passed to the plugin
* @param argv Array of argument strings
* argv[0] is the plugin name
* argv[1..argc-1] are additional arguments
*
* Return values:
* 0 = Success
* 1 = General error
* Other non-zero values = Custom error codes
*
* Example usage:
* whatsmy example -> argc=1, argv[0]="example"
* whatsmy example John -> argc=2, argv[0]="example", argv[1]="John"
* whatsmy example foo bar -> argc=3, argv[0]="example", argv[1]="foo", argv[2]="bar"
*/
// Windows DLL export macro
#ifdef _WIN32
#define WHATSMY_PLUGIN_EXPORT __declspec(dllexport)
#else
#define WHATSMY_PLUGIN_EXPORT
#endif
extern "C" {
WHATSMY_PLUGIN_EXPORT int plugin_run(int argc, char* argv[]) {
try {
// Print plugin information
std::cout << "==================================" << std::endl;
std::cout << " Example Plugin for whatsmycli " << std::endl;
std::cout << " API Version: 2.0 (with args) " << std::endl;
std::cout << "==================================" << std::endl;
std::cout << std::endl;
// Display arguments received
std::cout << "Arguments received: " << argc << std::endl;
for (int i = 0; i < argc; i++) {
std::cout << " argv[" << i << "] = " << argv[i] << std::endl;
}
std::cout << std::endl;
// Show platform information
std::cout << "Platform: " << PLATFORM_NAME << std::endl;
std::cout << "System: " << get_system_info() << std::endl;
std::cout << std::endl;
// Example: Greet user if name is provided as argument
if (argc >= 2) {
std::cout << "Hello to you too, " << argv[1] << "!" << std::endl;
} else {
std::cout << "Hello, World!" << std::endl;
std::cout << "Try: whatsmy example YourName" << std::endl;
}
std::cout << std::endl;
// Example: Demonstrate error handling
std::cout << "Tip: Return 0 for success, non-zero for errors." << std::endl;
// Success!
return 0;
} catch (const std::exception& e) {
// Handle exceptions gracefully
std::cerr << "Error: " << e.what() << std::endl;
return 1;
} catch (...) {
// Catch any other exceptions
std::cerr << "Unknown error occurred" << std::endl;
return 1;
}
}
}