diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e4d4b9a --- /dev/null +++ b/.clang-format @@ -0,0 +1,177 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignArrayOfStructures: None +AlignConsecutiveMacros: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: AcrossComments +AlignEscapedNewlines: DontAlign +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: MultiLine + AfterEnum: true + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeConceptDeclarations: true +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: false +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseLabels: true +IndentCaseBlocks: false +IndentGotoLabels: false +IndentPPDirectives: BeforeHash +IndentExternBlock: Indent +IndentRequires: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: Wrapped +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +LambdaBodyIndentation: Signature +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PenaltyIndentedWhitespace: 0 +PointerAlignment: Right +PPIndentWidth: -1 +ReferenceAlignment: Right +ReflowComments: true +ShortNamespaceLines: 1 +SortIncludes: CaseInsensitive +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceAroundPointerQualifiers: Default +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +BitFieldColonSpacing: Both +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME +... + diff --git a/src/activate_linux.c b/src/activate_linux.c index a0d1091..8906bb2 100644 --- a/src/activate_linux.c +++ b/src/activate_linux.c @@ -1,28 +1,35 @@ +#include "color.h" +#include "i18n.h" + +#include +#include #include #include #include #include - -#include -#include -#include #include #include #include - -#include -#include - -#include "color.h" -#include "i18n.h" +#include +#include +#include // draw text -void draw(cairo_t *cr, char *title, char *subtitle, float scale, struct rgba_color_t color, char* customfont, int boldmode, int slantmode) { +void draw( + cairo_t *cr, + char *title, + char *subtitle, + float scale, + struct rgba_color_t color, + char *customfont, + int boldmode, + int slantmode) +{ // set color cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); // no subpixel anti-aliasing because we are on transparent BG - cairo_font_options_t* font_options = cairo_font_options_create(); + cairo_font_options_t *font_options = cairo_font_options_create(); cairo_font_options_set_antialias(font_options, CAIRO_ANTIALIAS_GRAY); cairo_set_font_options(cr, font_options); @@ -52,10 +59,11 @@ void draw(cairo_t *cr, char *title, char *subtitle, float scale, struct rgba_col cairo_font_options_destroy(font_options); } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ Display *d = XOpenDisplay(NULL); - Window root = DefaultRootWindow(d); - int default_screen = XDefaultScreen(d); + Window root = DefaultRootWindow(d); + int default_screen = XDefaultScreen(d); int num_entries = 0; @@ -71,17 +79,16 @@ int main(int argc, char *argv[]) { // title, subtitle text; char *system_name; - #ifdef __APPLE__ - system_name = "macOS"; - #elif __FreeBSD__ - system_name = "BSD"; - #else - system_name = "Linux"; - #endif +#ifdef __APPLE__ + system_name = "macOS"; +#elif __FreeBSD__ + system_name = "BSD"; +#else + system_name = "Linux"; +#endif i18n_info i18n = i18n_get_info(system_name); - char *title = i18n.title, - *subtitle = i18n.subtitle; - char *customfont = ""; + char *title = i18n.title, *subtitle = i18n.subtitle; + char *customfont = ""; int overlay_width = 340; int overlay_height = 120; @@ -126,42 +133,47 @@ int main(int argc, char *argv[]) { break; case 's': scale = atof(optarg); - if(scale < 0.0f) { - fprintf(stderr, "Error occurred during parsing custom scale.\n"); + if (scale < 0.0f) { + fprintf( + stderr, + "Error occurred during parsing custom scale.\n"); return 1; } break; case 'c': text_color = rgba_color_string(optarg); if (text_color.a < 0.0) { - fprintf(stderr, "Error occurred during parsing custom color.\n"); + fprintf( + stderr, + "Error occurred during parsing custom color.\n"); return 1; } break; case '?': case 'h': - #define HELP(X) fprintf(stderr, " " X "\n") - #define STYLE(x) "\033[" # x "m" - #define COLOR(x, y) "\033[" # x ";" # y "m" - fprintf(stderr, "Usage: %s [-b] [-i] [-c color] [-f font] [-m message] [-s scale] [-t title]\n", argv[0]); +#define HELP(X) fprintf(stderr, " " X "\n") +#define STYLE(x) "\033[" #x "m" +#define COLOR(x, y) "\033[" #x ";" #y "m" + fprintf( + stderr, + "Usage: %s [-b] [-i] [-c color] [-f font] [-m message] [-s scale] [-t title]\n", + argv[0]); HELP("-b\t\tShow " STYLE(1) "bold" STYLE(0) " text"); HELP("-w\t\tSet EWMH bypass_compositor hint"); HELP("-d\t\tFork to background on startup"); HELP("-i\t\tShow " STYLE(3) "italic/slanted" STYLE(0) " text"); - HELP("-c color\tSpecify color in " COLOR(1, 31) "r" STYLE(0) - "-" COLOR(1, 32) "g" STYLE(0) "-" COLOR(1,34) "b" STYLE(0) - "-" COLOR(1, 33) "a" STYLE(0) " notation"); - HELP("\t\twhere " COLOR(1, 31) "r" STYLE(0) "/" COLOR(1,32) - "g" STYLE(0) "/" COLOR(1, 34) "b" STYLE(0) "/" COLOR(1, 33) - "a" STYLE(0) " is between " COLOR(1, 32) "0.0" STYLE(0) - "-" COLOR(1, 34) "1.0" STYLE(0)); + HELP("-c color\tSpecify color in " COLOR(1, 31) "r" STYLE(0) "-" COLOR(1, 32) "g" STYLE( + 0) "-" COLOR(1, 34) "b" STYLE(0) "-" COLOR(1, 33) "a" STYLE(0) " notation"); + HELP("\t\twhere " COLOR(1, 31) "r" STYLE(0) "/" COLOR(1, 32) "g" STYLE(0) "/" COLOR(1, 34) "b" STYLE(0) "/" COLOR( + 1, + 33) "a" STYLE(0) " is between " COLOR(1, 32) "0.0" STYLE(0) "-" COLOR(1, 34) "1.0" STYLE(0)); HELP("-f font\tSet the text font (string)"); HELP("-t title\tSet title text (string)"); HELP("-m message\tSet message text (string)"); HELP("-s scale\tScale ratio (float)"); - #undef HELP - #undef STYLE - #undef COLOR +#undef HELP +#undef STYLE +#undef COLOR exit(EXIT_SUCCESS); } } @@ -170,8 +182,12 @@ int main(int argc, char *argv[]) { if (daemonize == 1) { int pid = -1; pid = fork(); - if (pid > 0) exit(EXIT_SUCCESS); - else if(pid == 0) setsid(); + + if (pid > 0) { + exit(EXIT_SUCCESS); + } else if (pid == 0) { + setsid(); + } } XSetWindowAttributes attrs; @@ -179,15 +195,17 @@ int main(int argc, char *argv[]) { XVisualInfo vinfo; - // MacOS doesnt support 32 bit color through XQuartz, massive hack - #ifdef __APPLE__ - int colorDepth = 24; - #else - int colorDepth = 32; - #endif +// MacOS doesnt support 32 bit color through XQuartz, massive hack +#ifdef __APPLE__ + int colorDepth = 24; +#else + int colorDepth = 32; +#endif if (!XMatchVisualInfo(d, default_screen, colorDepth, TrueColor, &vinfo)) { - printf("No visuals found supporting %i bit color, terminating \n", colorDepth); + printf( + "No visuals found supporting %i bit color, terminating \n", + colorDepth); exit(EXIT_FAILURE); } @@ -196,9 +214,9 @@ int main(int argc, char *argv[]) { attrs.background_pixel = 0; attrs.border_pixel = 0; - Window overlay[num_entries]; + Window overlay[num_entries]; cairo_surface_t *surface[num_entries]; - cairo_t *cairo_ctx[num_entries]; + cairo_t *cairo_ctx[num_entries]; overlay_height *= scale; overlay_width *= scale; @@ -206,54 +224,70 @@ int main(int argc, char *argv[]) { // create overlay on each screen for (int i = 0; i < num_entries; i++) { overlay[i] = XCreateWindow( - d, // display - root, // parent - si[i].x_org + si[i].width - overlay_width, // x position - si[i].y_org + si[i].height - overlay_height, // y position - overlay_width, // width - overlay_height, // height - 0, // border width - vinfo.depth, // depth - InputOutput, // class - vinfo.visual, // visual - CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel, // value mask - &attrs // attributes + d, // display + root, // parent + si[i].x_org + si[i].width - overlay_width, // x position + si[i].y_org + si[i].height - overlay_height, // y position + overlay_width, // width + overlay_height, // height + 0, // border width + vinfo.depth, // depth + InputOutput, // class + vinfo.visual, // visual + CWOverrideRedirect | CWColormap | CWBackPixel + | CWBorderPixel, // value mask + &attrs // attributes ); XMapWindow(d, overlay[i]); // allows the mouse to click through the overlay - XRectangle rect; + XRectangle rect; XserverRegion region = XFixesCreateRegion(d, &rect, 1); XFixesSetWindowShapeRegion(d, overlay[i], ShapeInput, 0, 0, region); XFixesDestroyRegion(d, region); - // sets a WM_CLASS to allow the user to blacklist some effect from compositor + // sets a WM_CLASS to allow the user to blacklist some effect from + // compositor XClassHint *xch = XAllocClassHint(); xch->res_name = "activate-linux"; xch->res_class = "activate-linux"; XSetClassHint(d, overlay[i], xch); // Set _NET_WM_BYPASS_COMPOSITOR - if(bypass_compositor == 1) { + if (bypass_compositor == 1) { XChangeProperty( - d, overlay[i], + d, + overlay[i], XInternAtom(d, "_NET_WM_BYPASS_COMPOSITOR", False), - XA_CARDINAL, 32, PropModeReplace, &bypass_compositor, 1 - ); + XA_CARDINAL, + 32, + PropModeReplace, + &bypass_compositor, + 1); } // cairo context - surface[i] = cairo_xlib_surface_create(d, overlay[i], vinfo.visual, overlay_width, overlay_height); + surface[i] = cairo_xlib_surface_create( + d, overlay[i], vinfo.visual, overlay_width, overlay_height); cairo_ctx[i] = cairo_create(surface[i]); - draw(cairo_ctx[i], title, subtitle, scale, text_color, customfont, boldmode, slantmode); + draw( + cairo_ctx[i], + title, + subtitle, + scale, + text_color, + customfont, + boldmode, + slantmode); } // wait for X events forever XEvent event; - while(1) { + while (1) { XNextEvent(d, &event); } + // free used resources for (int i = 0; i < num_entries; i++) { XUnmapWindow(d, overlay[i]); diff --git a/src/color.c b/src/color.c index 76830f1..fc6d8a0 100644 --- a/src/color.c +++ b/src/color.c @@ -4,7 +4,8 @@ #include #include -struct rgba_color_t rgba_color_new(float r, float g, float b, float a) { +struct rgba_color_t rgba_color_new(float r, float g, float b, float a) +{ struct rgba_color_t color = { .r = r, .g = g, @@ -15,13 +16,15 @@ struct rgba_color_t rgba_color_new(float r, float g, float b, float a) { return color; } -struct rgba_color_t rgba_color_default() { +struct rgba_color_t rgba_color_default() +{ const struct rgba_color_t color = rgba_color_new(1.0, 1.0, 1.0, 0.35); return color; } -struct rgba_color_t rgba_color_string(char *src) { +struct rgba_color_t rgba_color_string(char *src) +{ // Implementation note: // // Due to the way that the parsing is done, it automatically takes care of diff --git a/src/color.h b/src/color.h index cd992f0..d45dbd2 100644 --- a/src/color.h +++ b/src/color.h @@ -9,7 +9,8 @@ * All values range from 0 to 1, unless an error occured, in which case the * error code will be stored in the alpha field as a negative number. */ -struct rgba_color_t { +struct rgba_color_t +{ float r, g, b, a; }; @@ -28,7 +29,8 @@ struct rgba_color_t { struct rgba_color_t rgba_color_new(float r, float g, float b, float a); /** - * Constructs a new rgba_color_t with the default values (r=1.0, g=1.0, b=1.0, a=0.35). + * Constructs a new rgba_color_t with the default values (r=1.0, g=1.0, b=1.0, + * a=0.35). * * @returns A new rgba_color_t with the default values. */ diff --git a/src/i18n.c b/src/i18n.c index 9fd1fad..cdda96b 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -1,37 +1,55 @@ #include "i18n.h" -#include + #include +#include #define i18n_map_size 3 + char *i18n_map[i18n_map_size][4] = { - {"zh_CN", "激活 ", "转到“设置”以激活 ", "。"}, - {"zh_TW", "啟用 ", "移至[設定]以啟用 ", "。"}, - {"zh_HK", "啟用 ", "移至[設定]以啟用 ", "。"}, + { "zh_CN", "激活 ", "转到“设置”以激活 ", "。" }, + { "zh_TW", "啟用 ", "移至[設定]以啟用 ", "。" }, + { "zh_HK", "啟用 ", "移至[設定]以啟用 ", "。" }, }; -char *another_strcat(size_t len, char *strings[]){ + +char *another_strcat(size_t len, char *strings[]) +{ unsigned long t = 0; - for (size_t i = 0; i < len; ++i) + + for (size_t i = 0; i < len; ++i) { t += strlen(strings[i]); - char *tmp = (char*)malloc((t+1) * sizeof(char)); + } + + char *tmp = (char *) malloc((t + 1) * sizeof(char)); tmp[0] = '\0'; - for (size_t i = 0; i < len; ++i) + + for (size_t i = 0; i < len; ++i) { strcat(tmp, strings[i]); + } + return tmp; } -i18n_info i18n_get_info(char *system_name){ - char *title = "Activate ", - *subtitle_pre = "Go to Settings to activate ", + +i18n_info i18n_get_info(char *system_name) +{ + char *title = "Activate ", *subtitle_pre = "Go to Settings to activate ", *subtitle_suf = "."; - char *lang = getenv("LANG"); - for (size_t i = 0; i < i18n_map_size; ++i){ - if (!strncmp(lang, i18n_map[i][0], 5)){ + char *lang = getenv("LANG"); + + for (size_t i = 0; i < i18n_map_size; ++i) { + if (!strncmp(lang, i18n_map[i][0], 5)) { title = i18n_map[i][1]; - subtitle_pre = i18n_map[i][2], - subtitle_suf = i18n_map[i][3]; + subtitle_pre = i18n_map[i][2], subtitle_suf = i18n_map[i][3]; break; } } - char *argv_title[] = {title, system_name}; - char *argv_subtitle[] = {subtitle_pre, system_name, subtitle_suf}; - return (i18n_info){another_strcat(2, argv_title), another_strcat(3, argv_subtitle)}; -} \ No newline at end of file + + char *argv_title[] = { title, system_name }; + char *argv_subtitle[] = { subtitle_pre, system_name, subtitle_suf }; + + // clang-format off + return (i18n_info) { + .title = another_strcat(2, argv_title), + .subtitle = another_strcat(3, argv_subtitle), + }; + // clang-format on +} diff --git a/src/i18n.h b/src/i18n.h index 8b161fd..4e9fdbc 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -1,7 +1,9 @@ #ifndef I18N #define I18N -typedef struct{ +typedef struct +{ char *title, *subtitle; -}i18n_info; +} i18n_info; + i18n_info i18n_get_info(char *system_name); -#endif \ No newline at end of file +#endif