123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define NUM_LAYOUTS 2
- char* layoutholder[NUM_LAYOUTS];
- char *
- getlayout()
- {
- char* layout = malloc(3);
- FILE *p;
- p = popen("setxkbmap -query | grep layout | cut -d ' ' -f 6", "r");
- if (p == NULL) {
- printf("Error: setxkbmap not found. Please install it in this system.\n");
- return "er";
- }
- else {
- int ch;
- int i = 0;
- while ((ch = fgetc(p)) != EOF) {
- layout[i] = ch;
- i++;
- }
- pclose(p);
-
- layout[2] = '\0';
- return layout;
- }
- }
- void
- switchlayout(char* curlayout)
- {
- FILE* p;
- char stmt[15] = "setxkbmap ";
- for (int i = 0; i < NUM_LAYOUTS; i++) {
-
- if (strncmp(curlayout, layoutholder[i], 2) == 0) {
-
- if ((i + 1) == NUM_LAYOUTS) {
- printf("Switching to %s layout.\n", layoutholder[0]);
- strcat(stmt, layoutholder[0]);
- p = popen(stmt, "r");
- break;
- }
- else {
- printf("Switching to %s layout.\n", layoutholder[i+1]);
- strcat(stmt, layoutholder[i+1]);
- p = popen(stmt, "r");
- break;
- }
- }
- }
- pclose(p);
- }
- int
- main(int argc, char* argv[])
- {
- layoutholder[0] = "us";
- layoutholder[1] = "br";
-
-
-
- char* layout = getlayout();
- if (strncmp(layout, "er", 2) == 0) {
-
- printf("Unable to query keyboard value.\n");
- free(layout);
- return 1;
- }
- switchlayout(layout);
- free(layout);
- return 0;
- }
|